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 | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 1014 | printf("%s: len=%zu, ",unicode_kind_name(op), ascii->length); |
| 1015 | |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 1016 | if (ascii->wstr == data) |
| 1017 | printf("shared "); |
| 1018 | printf("wstr=%p", ascii->wstr); |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 1019 | |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1020 | if (!(ascii->state.ascii == 1 && ascii->state.compact == 1)) { |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 1021 | printf(" (%zu), ", compact->wstr_length); |
| 1022 | if (!ascii->state.compact && compact->utf8 == unicode->data.any) |
| 1023 | printf("shared "); |
| 1024 | printf("utf8=%p (%zu)", compact->utf8, compact->utf8_length); |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 1025 | } |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 1026 | printf(", data=%p\n", data); |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 1027 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1028 | #endif |
| 1029 | |
| 1030 | PyObject * |
| 1031 | PyUnicode_New(Py_ssize_t size, Py_UCS4 maxchar) |
| 1032 | { |
| 1033 | PyObject *obj; |
| 1034 | PyCompactUnicodeObject *unicode; |
| 1035 | void *data; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1036 | enum PyUnicode_Kind kind; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 1037 | int is_sharing, is_ascii; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1038 | Py_ssize_t char_size; |
| 1039 | Py_ssize_t struct_size; |
| 1040 | |
| 1041 | /* Optimization for empty strings */ |
| 1042 | if (size == 0 && unicode_empty != NULL) { |
| 1043 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1044 | return unicode_empty; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1045 | } |
| 1046 | |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 1047 | is_ascii = 0; |
| 1048 | is_sharing = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1049 | struct_size = sizeof(PyCompactUnicodeObject); |
| 1050 | if (maxchar < 128) { |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1051 | kind = PyUnicode_1BYTE_KIND; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1052 | char_size = 1; |
| 1053 | is_ascii = 1; |
| 1054 | struct_size = sizeof(PyASCIIObject); |
| 1055 | } |
| 1056 | else if (maxchar < 256) { |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1057 | kind = PyUnicode_1BYTE_KIND; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1058 | char_size = 1; |
| 1059 | } |
| 1060 | else if (maxchar < 65536) { |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1061 | kind = PyUnicode_2BYTE_KIND; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1062 | char_size = 2; |
| 1063 | if (sizeof(wchar_t) == 2) |
| 1064 | is_sharing = 1; |
| 1065 | } |
| 1066 | else { |
Victor Stinner | c9590ad | 2012-03-04 01:34:37 +0100 | [diff] [blame] | 1067 | if (maxchar > MAX_UNICODE) { |
| 1068 | PyErr_SetString(PyExc_SystemError, |
| 1069 | "invalid maximum character passed to PyUnicode_New"); |
| 1070 | return NULL; |
| 1071 | } |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1072 | kind = PyUnicode_4BYTE_KIND; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1073 | char_size = 4; |
| 1074 | if (sizeof(wchar_t) == 4) |
| 1075 | is_sharing = 1; |
| 1076 | } |
| 1077 | |
| 1078 | /* Ensure we won't overflow the size. */ |
| 1079 | if (size < 0) { |
| 1080 | PyErr_SetString(PyExc_SystemError, |
| 1081 | "Negative size passed to PyUnicode_New"); |
| 1082 | return NULL; |
| 1083 | } |
| 1084 | if (size > ((PY_SSIZE_T_MAX - struct_size) / char_size - 1)) |
| 1085 | return PyErr_NoMemory(); |
| 1086 | |
| 1087 | /* Duplicated allocation code from _PyObject_New() instead of a call to |
| 1088 | * PyObject_New() so we are able to allocate space for the object and |
| 1089 | * it's data buffer. |
| 1090 | */ |
| 1091 | obj = (PyObject *) PyObject_MALLOC(struct_size + (size + 1) * char_size); |
| 1092 | if (obj == NULL) |
| 1093 | return PyErr_NoMemory(); |
| 1094 | obj = PyObject_INIT(obj, &PyUnicode_Type); |
| 1095 | if (obj == NULL) |
| 1096 | return NULL; |
| 1097 | |
| 1098 | unicode = (PyCompactUnicodeObject *)obj; |
| 1099 | if (is_ascii) |
| 1100 | data = ((PyASCIIObject*)obj) + 1; |
| 1101 | else |
| 1102 | data = unicode + 1; |
| 1103 | _PyUnicode_LENGTH(unicode) = size; |
| 1104 | _PyUnicode_HASH(unicode) = -1; |
| 1105 | _PyUnicode_STATE(unicode).interned = 0; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1106 | _PyUnicode_STATE(unicode).kind = kind; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1107 | _PyUnicode_STATE(unicode).compact = 1; |
| 1108 | _PyUnicode_STATE(unicode).ready = 1; |
| 1109 | _PyUnicode_STATE(unicode).ascii = is_ascii; |
| 1110 | if (is_ascii) { |
| 1111 | ((char*)data)[size] = 0; |
| 1112 | _PyUnicode_WSTR(unicode) = NULL; |
| 1113 | } |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1114 | else if (kind == PyUnicode_1BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1115 | ((char*)data)[size] = 0; |
| 1116 | _PyUnicode_WSTR(unicode) = NULL; |
| 1117 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1118 | unicode->utf8 = NULL; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 1119 | unicode->utf8_length = 0; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1120 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1121 | else { |
| 1122 | unicode->utf8 = NULL; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 1123 | unicode->utf8_length = 0; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1124 | if (kind == PyUnicode_2BYTE_KIND) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1125 | ((Py_UCS2*)data)[size] = 0; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1126 | else /* kind == PyUnicode_4BYTE_KIND */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1127 | ((Py_UCS4*)data)[size] = 0; |
| 1128 | if (is_sharing) { |
| 1129 | _PyUnicode_WSTR_LENGTH(unicode) = size; |
| 1130 | _PyUnicode_WSTR(unicode) = (wchar_t *)data; |
| 1131 | } |
| 1132 | else { |
| 1133 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1134 | _PyUnicode_WSTR(unicode) = NULL; |
| 1135 | } |
| 1136 | } |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1137 | #ifdef Py_DEBUG |
Victor Stinner | afffce4 | 2012-10-03 23:03:17 +0200 | [diff] [blame] | 1138 | unicode_fill_invalid((PyObject*)unicode, 0); |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1139 | #endif |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 1140 | assert(_PyUnicode_CheckConsistency((PyObject*)unicode, 0)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1141 | return obj; |
| 1142 | } |
| 1143 | |
| 1144 | #if SIZEOF_WCHAR_T == 2 |
| 1145 | /* Helper function to convert a 16-bits wchar_t representation to UCS4, this |
| 1146 | will decode surrogate pairs, the other conversions are implemented as macros |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 1147 | for efficiency. |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1148 | |
| 1149 | This function assumes that unicode can hold one more code point than wstr |
| 1150 | characters for a terminating null character. */ |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1151 | static void |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1152 | 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] | 1153 | PyObject *unicode) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1154 | { |
| 1155 | const wchar_t *iter; |
| 1156 | Py_UCS4 *ucs4_out; |
| 1157 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1158 | assert(unicode != NULL); |
| 1159 | assert(_PyUnicode_CHECK(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1160 | assert(_PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND); |
| 1161 | ucs4_out = PyUnicode_4BYTE_DATA(unicode); |
| 1162 | |
| 1163 | for (iter = begin; iter < end; ) { |
| 1164 | assert(ucs4_out < (PyUnicode_4BYTE_DATA(unicode) + |
| 1165 | _PyUnicode_GET_LENGTH(unicode))); |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 1166 | if (Py_UNICODE_IS_HIGH_SURROGATE(iter[0]) |
| 1167 | && (iter+1) < end |
| 1168 | && Py_UNICODE_IS_LOW_SURROGATE(iter[1])) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1169 | { |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 1170 | *ucs4_out++ = Py_UNICODE_JOIN_SURROGATES(iter[0], iter[1]); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1171 | iter += 2; |
| 1172 | } |
| 1173 | else { |
| 1174 | *ucs4_out++ = *iter; |
| 1175 | iter++; |
| 1176 | } |
| 1177 | } |
| 1178 | assert(ucs4_out == (PyUnicode_4BYTE_DATA(unicode) + |
| 1179 | _PyUnicode_GET_LENGTH(unicode))); |
| 1180 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1181 | } |
| 1182 | #endif |
| 1183 | |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1184 | static int |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1185 | unicode_check_modifiable(PyObject *unicode) |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1186 | { |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1187 | if (!unicode_modifiable(unicode)) { |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 1188 | PyErr_SetString(PyExc_SystemError, |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1189 | "Cannot modify a string currently used"); |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1190 | return -1; |
| 1191 | } |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1192 | return 0; |
| 1193 | } |
| 1194 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1195 | static int |
| 1196 | _copy_characters(PyObject *to, Py_ssize_t to_start, |
| 1197 | PyObject *from, Py_ssize_t from_start, |
| 1198 | Py_ssize_t how_many, int check_maxchar) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1199 | { |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1200 | unsigned int from_kind, to_kind; |
| 1201 | void *from_data, *to_data; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1202 | |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 1203 | assert(0 <= how_many); |
| 1204 | assert(0 <= from_start); |
| 1205 | assert(0 <= to_start); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1206 | assert(PyUnicode_Check(from)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1207 | assert(PyUnicode_IS_READY(from)); |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 1208 | assert(from_start + how_many <= PyUnicode_GET_LENGTH(from)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1209 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1210 | assert(PyUnicode_Check(to)); |
| 1211 | assert(PyUnicode_IS_READY(to)); |
| 1212 | assert(to_start + how_many <= PyUnicode_GET_LENGTH(to)); |
| 1213 | |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1214 | if (how_many == 0) |
| 1215 | return 0; |
| 1216 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1217 | from_kind = PyUnicode_KIND(from); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1218 | from_data = PyUnicode_DATA(from); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1219 | to_kind = PyUnicode_KIND(to); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1220 | to_data = PyUnicode_DATA(to); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1221 | |
Victor Stinner | f185226 | 2012-06-16 16:38:26 +0200 | [diff] [blame] | 1222 | #ifdef Py_DEBUG |
| 1223 | if (!check_maxchar |
| 1224 | && PyUnicode_MAX_CHAR_VALUE(from) > PyUnicode_MAX_CHAR_VALUE(to)) |
| 1225 | { |
| 1226 | const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to); |
| 1227 | Py_UCS4 ch; |
| 1228 | Py_ssize_t i; |
| 1229 | for (i=0; i < how_many; i++) { |
| 1230 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
| 1231 | assert(ch <= to_maxchar); |
| 1232 | } |
| 1233 | } |
| 1234 | #endif |
| 1235 | |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1236 | if (from_kind == to_kind) { |
Victor Stinner | f185226 | 2012-06-16 16:38:26 +0200 | [diff] [blame] | 1237 | if (check_maxchar |
| 1238 | && !PyUnicode_IS_ASCII(from) && PyUnicode_IS_ASCII(to)) |
| 1239 | { |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1240 | /* Writing Latin-1 characters into an ASCII string requires to |
| 1241 | check that all written characters are pure ASCII */ |
Victor Stinner | f185226 | 2012-06-16 16:38:26 +0200 | [diff] [blame] | 1242 | Py_UCS4 max_char; |
| 1243 | max_char = ucs1lib_find_max_char(from_data, |
| 1244 | (Py_UCS1*)from_data + how_many); |
| 1245 | if (max_char >= 128) |
| 1246 | return -1; |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1247 | } |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 1248 | Py_MEMCPY((char*)to_data + to_kind * to_start, |
| 1249 | (char*)from_data + from_kind * from_start, |
| 1250 | to_kind * how_many); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1251 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1252 | else if (from_kind == PyUnicode_1BYTE_KIND |
| 1253 | && to_kind == PyUnicode_2BYTE_KIND) |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1254 | { |
| 1255 | _PyUnicode_CONVERT_BYTES( |
| 1256 | Py_UCS1, Py_UCS2, |
| 1257 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 1258 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 1259 | PyUnicode_2BYTE_DATA(to) + to_start |
| 1260 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1261 | } |
Victor Stinner | 157f83f | 2011-09-28 21:41:31 +0200 | [diff] [blame] | 1262 | else if (from_kind == PyUnicode_1BYTE_KIND |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1263 | && to_kind == PyUnicode_4BYTE_KIND) |
| 1264 | { |
| 1265 | _PyUnicode_CONVERT_BYTES( |
| 1266 | Py_UCS1, Py_UCS4, |
| 1267 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 1268 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 1269 | PyUnicode_4BYTE_DATA(to) + to_start |
| 1270 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1271 | } |
| 1272 | else if (from_kind == PyUnicode_2BYTE_KIND |
| 1273 | && to_kind == PyUnicode_4BYTE_KIND) |
| 1274 | { |
| 1275 | _PyUnicode_CONVERT_BYTES( |
| 1276 | Py_UCS2, Py_UCS4, |
| 1277 | PyUnicode_2BYTE_DATA(from) + from_start, |
| 1278 | PyUnicode_2BYTE_DATA(from) + from_start + how_many, |
| 1279 | PyUnicode_4BYTE_DATA(to) + to_start |
| 1280 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1281 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1282 | else { |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1283 | assert (PyUnicode_MAX_CHAR_VALUE(from) > PyUnicode_MAX_CHAR_VALUE(to)); |
| 1284 | |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1285 | if (!check_maxchar) { |
| 1286 | if (from_kind == PyUnicode_2BYTE_KIND |
| 1287 | && to_kind == PyUnicode_1BYTE_KIND) |
| 1288 | { |
| 1289 | _PyUnicode_CONVERT_BYTES( |
| 1290 | Py_UCS2, Py_UCS1, |
| 1291 | PyUnicode_2BYTE_DATA(from) + from_start, |
| 1292 | PyUnicode_2BYTE_DATA(from) + from_start + how_many, |
| 1293 | PyUnicode_1BYTE_DATA(to) + to_start |
| 1294 | ); |
| 1295 | } |
| 1296 | else if (from_kind == PyUnicode_4BYTE_KIND |
| 1297 | && to_kind == PyUnicode_1BYTE_KIND) |
| 1298 | { |
| 1299 | _PyUnicode_CONVERT_BYTES( |
| 1300 | Py_UCS4, Py_UCS1, |
| 1301 | PyUnicode_4BYTE_DATA(from) + from_start, |
| 1302 | PyUnicode_4BYTE_DATA(from) + from_start + how_many, |
| 1303 | PyUnicode_1BYTE_DATA(to) + to_start |
| 1304 | ); |
| 1305 | } |
| 1306 | else if (from_kind == PyUnicode_4BYTE_KIND |
| 1307 | && to_kind == PyUnicode_2BYTE_KIND) |
| 1308 | { |
| 1309 | _PyUnicode_CONVERT_BYTES( |
| 1310 | Py_UCS4, Py_UCS2, |
| 1311 | PyUnicode_4BYTE_DATA(from) + from_start, |
| 1312 | PyUnicode_4BYTE_DATA(from) + from_start + how_many, |
| 1313 | PyUnicode_2BYTE_DATA(to) + to_start |
| 1314 | ); |
| 1315 | } |
| 1316 | else { |
| 1317 | assert(0); |
| 1318 | return -1; |
| 1319 | } |
| 1320 | } |
Victor Stinner | f185226 | 2012-06-16 16:38:26 +0200 | [diff] [blame] | 1321 | else { |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1322 | const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1323 | Py_UCS4 ch; |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1324 | Py_ssize_t i; |
| 1325 | |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1326 | for (i=0; i < how_many; i++) { |
| 1327 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1328 | if (ch > to_maxchar) |
| 1329 | return -1; |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1330 | PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); |
| 1331 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1332 | } |
| 1333 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1334 | return 0; |
| 1335 | } |
| 1336 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1337 | void |
| 1338 | _PyUnicode_FastCopyCharacters( |
| 1339 | PyObject *to, Py_ssize_t to_start, |
| 1340 | PyObject *from, Py_ssize_t from_start, Py_ssize_t how_many) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1341 | { |
| 1342 | (void)_copy_characters(to, to_start, from, from_start, how_many, 0); |
| 1343 | } |
| 1344 | |
| 1345 | Py_ssize_t |
| 1346 | PyUnicode_CopyCharacters(PyObject *to, Py_ssize_t to_start, |
| 1347 | PyObject *from, Py_ssize_t from_start, |
| 1348 | Py_ssize_t how_many) |
| 1349 | { |
| 1350 | int err; |
| 1351 | |
| 1352 | if (!PyUnicode_Check(from) || !PyUnicode_Check(to)) { |
| 1353 | PyErr_BadInternalCall(); |
| 1354 | return -1; |
| 1355 | } |
| 1356 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 1357 | if (PyUnicode_READY(from) == -1) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1358 | return -1; |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 1359 | if (PyUnicode_READY(to) == -1) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1360 | return -1; |
| 1361 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1362 | if (from_start < 0) { |
| 1363 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 1364 | return -1; |
| 1365 | } |
| 1366 | if (to_start < 0) { |
| 1367 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 1368 | return -1; |
| 1369 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1370 | how_many = Py_MIN(PyUnicode_GET_LENGTH(from), how_many); |
| 1371 | if (to_start + how_many > PyUnicode_GET_LENGTH(to)) { |
| 1372 | PyErr_Format(PyExc_SystemError, |
| 1373 | "Cannot write %zi characters at %zi " |
| 1374 | "in a string of %zi characters", |
| 1375 | how_many, to_start, PyUnicode_GET_LENGTH(to)); |
| 1376 | return -1; |
| 1377 | } |
| 1378 | |
| 1379 | if (how_many == 0) |
| 1380 | return 0; |
| 1381 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1382 | if (unicode_check_modifiable(to)) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1383 | return -1; |
| 1384 | |
| 1385 | err = _copy_characters(to, to_start, from, from_start, how_many, 1); |
| 1386 | if (err) { |
| 1387 | PyErr_Format(PyExc_SystemError, |
| 1388 | "Cannot copy %s characters " |
| 1389 | "into a string of %s characters", |
| 1390 | unicode_kind_name(from), |
| 1391 | unicode_kind_name(to)); |
| 1392 | return -1; |
| 1393 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1394 | return how_many; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1395 | } |
| 1396 | |
Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 1397 | /* Find the maximum code point and count the number of surrogate pairs so a |
| 1398 | correct string length can be computed before converting a string to UCS4. |
| 1399 | This function counts single surrogates as a character and not as a pair. |
| 1400 | |
| 1401 | Return 0 on success, or -1 on error. */ |
| 1402 | static int |
| 1403 | find_maxchar_surrogates(const wchar_t *begin, const wchar_t *end, |
| 1404 | Py_UCS4 *maxchar, Py_ssize_t *num_surrogates) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1405 | { |
| 1406 | const wchar_t *iter; |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1407 | Py_UCS4 ch; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1408 | |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1409 | assert(num_surrogates != NULL && maxchar != NULL); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1410 | *num_surrogates = 0; |
| 1411 | *maxchar = 0; |
| 1412 | |
| 1413 | for (iter = begin; iter < end; ) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1414 | #if SIZEOF_WCHAR_T == 2 |
Victor Stinner | cf77da9 | 2013-03-06 01:09:24 +0100 | [diff] [blame] | 1415 | if (Py_UNICODE_IS_HIGH_SURROGATE(iter[0]) |
| 1416 | && (iter+1) < end |
| 1417 | && Py_UNICODE_IS_LOW_SURROGATE(iter[1])) |
| 1418 | { |
| 1419 | ch = Py_UNICODE_JOIN_SURROGATES(iter[0], iter[1]); |
| 1420 | ++(*num_surrogates); |
| 1421 | iter += 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1422 | } |
| 1423 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1424 | #endif |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1425 | { |
| 1426 | ch = *iter; |
| 1427 | iter++; |
| 1428 | } |
| 1429 | if (ch > *maxchar) { |
| 1430 | *maxchar = ch; |
| 1431 | if (*maxchar > MAX_UNICODE) { |
| 1432 | PyErr_Format(PyExc_ValueError, |
| 1433 | "character U+%x is not in range [U+0000; U+10ffff]", |
| 1434 | ch); |
| 1435 | return -1; |
| 1436 | } |
| 1437 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1438 | } |
| 1439 | return 0; |
| 1440 | } |
| 1441 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1442 | int |
| 1443 | _PyUnicode_Ready(PyObject *unicode) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1444 | { |
| 1445 | wchar_t *end; |
| 1446 | Py_UCS4 maxchar = 0; |
| 1447 | Py_ssize_t num_surrogates; |
| 1448 | #if SIZEOF_WCHAR_T == 2 |
| 1449 | Py_ssize_t length_wo_surrogates; |
| 1450 | #endif |
| 1451 | |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 1452 | /* _PyUnicode_Ready() is only intended for old-style API usage where |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1453 | strings were created using _PyObject_New() and where no canonical |
| 1454 | representation (the str field) has been set yet aka strings |
| 1455 | which are not yet ready. */ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1456 | assert(_PyUnicode_CHECK(unicode)); |
| 1457 | assert(_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1458 | assert(_PyUnicode_WSTR(unicode) != NULL); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1459 | assert(_PyUnicode_DATA_ANY(unicode) == NULL); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1460 | assert(_PyUnicode_UTF8(unicode) == NULL); |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1461 | /* Actually, it should neither be interned nor be anything else: */ |
| 1462 | assert(_PyUnicode_STATE(unicode).interned == SSTATE_NOT_INTERNED); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1463 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1464 | end = _PyUnicode_WSTR(unicode) + _PyUnicode_WSTR_LENGTH(unicode); |
Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 1465 | if (find_maxchar_surrogates(_PyUnicode_WSTR(unicode), end, |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1466 | &maxchar, &num_surrogates) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1467 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1468 | |
| 1469 | if (maxchar < 256) { |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1470 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(_PyUnicode_WSTR_LENGTH(unicode) + 1); |
| 1471 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1472 | PyErr_NoMemory(); |
| 1473 | return -1; |
| 1474 | } |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1475 | _PyUnicode_CONVERT_BYTES(wchar_t, unsigned char, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1476 | _PyUnicode_WSTR(unicode), end, |
| 1477 | PyUnicode_1BYTE_DATA(unicode)); |
| 1478 | PyUnicode_1BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1479 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1480 | _PyUnicode_STATE(unicode).kind = PyUnicode_1BYTE_KIND; |
| 1481 | if (maxchar < 128) { |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1482 | _PyUnicode_STATE(unicode).ascii = 1; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1483 | _PyUnicode_UTF8(unicode) = _PyUnicode_DATA_ANY(unicode); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1484 | _PyUnicode_UTF8_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1485 | } |
| 1486 | else { |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1487 | _PyUnicode_STATE(unicode).ascii = 0; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1488 | _PyUnicode_UTF8(unicode) = NULL; |
| 1489 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1490 | } |
| 1491 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1492 | _PyUnicode_WSTR(unicode) = NULL; |
| 1493 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1494 | } |
| 1495 | /* In this case we might have to convert down from 4-byte native |
| 1496 | wchar_t to 2-byte unicode. */ |
| 1497 | else if (maxchar < 65536) { |
| 1498 | assert(num_surrogates == 0 && |
| 1499 | "FindMaxCharAndNumSurrogatePairs() messed up"); |
| 1500 | |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1501 | #if SIZEOF_WCHAR_T == 2 |
| 1502 | /* We can share representations and are done. */ |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1503 | _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1504 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1505 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1506 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1507 | _PyUnicode_UTF8(unicode) = NULL; |
| 1508 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1509 | #else |
| 1510 | /* sizeof(wchar_t) == 4 */ |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1511 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC( |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1512 | 2 * (_PyUnicode_WSTR_LENGTH(unicode) + 1)); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1513 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1514 | PyErr_NoMemory(); |
| 1515 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1516 | } |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1517 | _PyUnicode_CONVERT_BYTES(wchar_t, Py_UCS2, |
| 1518 | _PyUnicode_WSTR(unicode), end, |
| 1519 | PyUnicode_2BYTE_DATA(unicode)); |
| 1520 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1521 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1522 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1523 | _PyUnicode_UTF8(unicode) = NULL; |
| 1524 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1525 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1526 | _PyUnicode_WSTR(unicode) = NULL; |
| 1527 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1528 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1529 | } |
| 1530 | /* maxchar exeeds 16 bit, wee need 4 bytes for unicode characters */ |
| 1531 | else { |
| 1532 | #if SIZEOF_WCHAR_T == 2 |
| 1533 | /* in case the native representation is 2-bytes, we need to allocate a |
| 1534 | new normalized 4-byte version. */ |
| 1535 | length_wo_surrogates = _PyUnicode_WSTR_LENGTH(unicode) - num_surrogates; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1536 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(4 * (length_wo_surrogates + 1)); |
| 1537 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1538 | PyErr_NoMemory(); |
| 1539 | return -1; |
| 1540 | } |
| 1541 | _PyUnicode_LENGTH(unicode) = length_wo_surrogates; |
| 1542 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1543 | _PyUnicode_UTF8(unicode) = NULL; |
| 1544 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 126c559 | 2011-10-03 04:17:10 +0200 | [diff] [blame] | 1545 | /* unicode_convert_wchar_to_ucs4() requires a ready string */ |
| 1546 | _PyUnicode_STATE(unicode).ready = 1; |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1547 | unicode_convert_wchar_to_ucs4(_PyUnicode_WSTR(unicode), end, unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1548 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1549 | _PyUnicode_WSTR(unicode) = NULL; |
| 1550 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1551 | #else |
| 1552 | assert(num_surrogates == 0); |
| 1553 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1554 | _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1555 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1556 | _PyUnicode_UTF8(unicode) = NULL; |
| 1557 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1558 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; |
| 1559 | #endif |
| 1560 | PyUnicode_4BYTE_DATA(unicode)[_PyUnicode_LENGTH(unicode)] = '\0'; |
| 1561 | } |
| 1562 | _PyUnicode_STATE(unicode).ready = 1; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1563 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1564 | return 0; |
| 1565 | } |
| 1566 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1567 | static void |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 1568 | unicode_dealloc(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1569 | { |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1570 | switch (PyUnicode_CHECK_INTERNED(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1571 | case SSTATE_NOT_INTERNED: |
| 1572 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1573 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1574 | case SSTATE_INTERNED_MORTAL: |
| 1575 | /* revive dead object temporarily for DelItem */ |
| 1576 | Py_REFCNT(unicode) = 3; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 1577 | if (PyDict_DelItem(interned, unicode) != 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1578 | Py_FatalError( |
| 1579 | "deletion of interned string failed"); |
| 1580 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1581 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1582 | case SSTATE_INTERNED_IMMORTAL: |
| 1583 | Py_FatalError("Immortal interned string died."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1584 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1585 | default: |
| 1586 | Py_FatalError("Inconsistent interned string state."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1587 | } |
| 1588 | |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 1589 | if (_PyUnicode_HAS_WSTR_MEMORY(unicode)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1590 | PyObject_DEL(_PyUnicode_WSTR(unicode)); |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 1591 | if (_PyUnicode_HAS_UTF8_MEMORY(unicode)) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1592 | PyObject_DEL(_PyUnicode_UTF8(unicode)); |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1593 | if (!PyUnicode_IS_COMPACT(unicode) && _PyUnicode_DATA_ANY(unicode)) |
| 1594 | PyObject_DEL(_PyUnicode_DATA_ANY(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1595 | |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1596 | Py_TYPE(unicode)->tp_free(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1597 | } |
| 1598 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1599 | #ifdef Py_DEBUG |
| 1600 | static int |
| 1601 | unicode_is_singleton(PyObject *unicode) |
| 1602 | { |
| 1603 | PyASCIIObject *ascii = (PyASCIIObject *)unicode; |
| 1604 | if (unicode == unicode_empty) |
| 1605 | return 1; |
| 1606 | if (ascii->state.kind != PyUnicode_WCHAR_KIND && ascii->length == 1) |
| 1607 | { |
| 1608 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, 0); |
| 1609 | if (ch < 256 && unicode_latin1[ch] == unicode) |
| 1610 | return 1; |
| 1611 | } |
| 1612 | return 0; |
| 1613 | } |
| 1614 | #endif |
| 1615 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1616 | static int |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1617 | unicode_modifiable(PyObject *unicode) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1618 | { |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1619 | assert(_PyUnicode_CHECK(unicode)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1620 | if (Py_REFCNT(unicode) != 1) |
| 1621 | return 0; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1622 | if (_PyUnicode_HASH(unicode) != -1) |
| 1623 | return 0; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1624 | if (PyUnicode_CHECK_INTERNED(unicode)) |
| 1625 | return 0; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1626 | if (!PyUnicode_CheckExact(unicode)) |
| 1627 | return 0; |
Victor Stinner | 77bb47b | 2011-10-03 20:06:05 +0200 | [diff] [blame] | 1628 | #ifdef Py_DEBUG |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1629 | /* singleton refcount is greater than 1 */ |
| 1630 | assert(!unicode_is_singleton(unicode)); |
Victor Stinner | 77bb47b | 2011-10-03 20:06:05 +0200 | [diff] [blame] | 1631 | #endif |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1632 | return 1; |
| 1633 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1634 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1635 | static int |
| 1636 | unicode_resize(PyObject **p_unicode, Py_ssize_t length) |
| 1637 | { |
| 1638 | PyObject *unicode; |
| 1639 | Py_ssize_t old_length; |
| 1640 | |
| 1641 | assert(p_unicode != NULL); |
| 1642 | unicode = *p_unicode; |
| 1643 | |
| 1644 | assert(unicode != NULL); |
| 1645 | assert(PyUnicode_Check(unicode)); |
| 1646 | assert(0 <= length); |
| 1647 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1648 | if (_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1649 | old_length = PyUnicode_WSTR_LENGTH(unicode); |
| 1650 | else |
| 1651 | old_length = PyUnicode_GET_LENGTH(unicode); |
| 1652 | if (old_length == length) |
| 1653 | return 0; |
| 1654 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1655 | if (length == 0) { |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 1656 | _Py_INCREF_UNICODE_EMPTY(); |
| 1657 | if (!unicode_empty) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1658 | return -1; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1659 | Py_DECREF(*p_unicode); |
| 1660 | *p_unicode = unicode_empty; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1661 | return 0; |
| 1662 | } |
| 1663 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1664 | if (!unicode_modifiable(unicode)) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1665 | PyObject *copy = resize_copy(unicode, length); |
| 1666 | if (copy == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1667 | return -1; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1668 | Py_DECREF(*p_unicode); |
| 1669 | *p_unicode = copy; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1670 | return 0; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1671 | } |
| 1672 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1673 | if (PyUnicode_IS_COMPACT(unicode)) { |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1674 | PyObject *new_unicode = resize_compact(unicode, length); |
| 1675 | if (new_unicode == NULL) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1676 | return -1; |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1677 | *p_unicode = new_unicode; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1678 | return 0; |
Benjamin Peterson | 4bfce8f | 2011-10-03 19:35:07 -0400 | [diff] [blame] | 1679 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1680 | return resize_inplace(unicode, length); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1681 | } |
| 1682 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1683 | int |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1684 | PyUnicode_Resize(PyObject **p_unicode, Py_ssize_t length) |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1685 | { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1686 | PyObject *unicode; |
| 1687 | if (p_unicode == NULL) { |
| 1688 | PyErr_BadInternalCall(); |
| 1689 | return -1; |
| 1690 | } |
| 1691 | unicode = *p_unicode; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1692 | if (unicode == NULL || !PyUnicode_Check(unicode) || length < 0) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1693 | { |
| 1694 | PyErr_BadInternalCall(); |
| 1695 | return -1; |
| 1696 | } |
| 1697 | return unicode_resize(p_unicode, length); |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1698 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1699 | |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1700 | /* Copy a ASCII or latin1 char* string into a Python Unicode string. |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1701 | |
Victor Stinner | b429d3b | 2012-02-22 21:22:20 +0100 | [diff] [blame] | 1702 | WARNING: The function doesn't copy the terminating null character and |
| 1703 | doesn't check the maximum character (may write a latin1 character in an |
| 1704 | ASCII string). */ |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1705 | static void |
| 1706 | unicode_write_cstr(PyObject *unicode, Py_ssize_t index, |
| 1707 | const char *str, Py_ssize_t len) |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1708 | { |
| 1709 | enum PyUnicode_Kind kind = PyUnicode_KIND(unicode); |
| 1710 | void *data = PyUnicode_DATA(unicode); |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1711 | const char *end = str + len; |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1712 | |
| 1713 | switch (kind) { |
| 1714 | case PyUnicode_1BYTE_KIND: { |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1715 | assert(index + len <= PyUnicode_GET_LENGTH(unicode)); |
Victor Stinner | 8c6db45 | 2012-10-06 00:40:45 +0200 | [diff] [blame] | 1716 | #ifdef Py_DEBUG |
| 1717 | if (PyUnicode_IS_ASCII(unicode)) { |
| 1718 | Py_UCS4 maxchar = ucs1lib_find_max_char( |
| 1719 | (const Py_UCS1*)str, |
| 1720 | (const Py_UCS1*)str + len); |
| 1721 | assert(maxchar < 128); |
| 1722 | } |
| 1723 | #endif |
Antoine Pitrou | ba6bafc | 2012-02-22 16:41:50 +0100 | [diff] [blame] | 1724 | memcpy((char *) data + index, str, len); |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1725 | break; |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1726 | } |
| 1727 | case PyUnicode_2BYTE_KIND: { |
| 1728 | Py_UCS2 *start = (Py_UCS2 *)data + index; |
| 1729 | Py_UCS2 *ucs2 = start; |
| 1730 | assert(index <= PyUnicode_GET_LENGTH(unicode)); |
| 1731 | |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1732 | for (; str < end; ++ucs2, ++str) |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1733 | *ucs2 = (Py_UCS2)*str; |
| 1734 | |
| 1735 | assert((ucs2 - start) <= PyUnicode_GET_LENGTH(unicode)); |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1736 | break; |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1737 | } |
| 1738 | default: { |
| 1739 | Py_UCS4 *start = (Py_UCS4 *)data + index; |
| 1740 | Py_UCS4 *ucs4 = start; |
| 1741 | assert(kind == PyUnicode_4BYTE_KIND); |
| 1742 | assert(index <= PyUnicode_GET_LENGTH(unicode)); |
| 1743 | |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1744 | for (; str < end; ++ucs4, ++str) |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1745 | *ucs4 = (Py_UCS4)*str; |
| 1746 | |
| 1747 | assert((ucs4 - start) <= PyUnicode_GET_LENGTH(unicode)); |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1748 | } |
| 1749 | } |
| 1750 | } |
| 1751 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1752 | static PyObject* |
| 1753 | get_latin1_char(unsigned char ch) |
| 1754 | { |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1755 | PyObject *unicode = unicode_latin1[ch]; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1756 | if (!unicode) { |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1757 | unicode = PyUnicode_New(1, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1758 | if (!unicode) |
| 1759 | return NULL; |
| 1760 | PyUnicode_1BYTE_DATA(unicode)[0] = ch; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1761 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1762 | unicode_latin1[ch] = unicode; |
| 1763 | } |
| 1764 | Py_INCREF(unicode); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1765 | return unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1766 | } |
| 1767 | |
Victor Stinner | 985a82a | 2014-01-03 12:53:47 +0100 | [diff] [blame] | 1768 | static PyObject* |
| 1769 | unicode_char(Py_UCS4 ch) |
| 1770 | { |
| 1771 | PyObject *unicode; |
| 1772 | |
| 1773 | assert(ch <= MAX_UNICODE); |
| 1774 | |
Victor Stinner | f3b46b4 | 2014-01-03 13:16:00 +0100 | [diff] [blame] | 1775 | if (ch < 256) |
| 1776 | return get_latin1_char(ch); |
| 1777 | |
Victor Stinner | 985a82a | 2014-01-03 12:53:47 +0100 | [diff] [blame] | 1778 | unicode = PyUnicode_New(1, ch); |
| 1779 | if (unicode == NULL) |
| 1780 | return NULL; |
| 1781 | switch (PyUnicode_KIND(unicode)) { |
| 1782 | case PyUnicode_1BYTE_KIND: |
| 1783 | PyUnicode_1BYTE_DATA(unicode)[0] = (Py_UCS1)ch; |
| 1784 | break; |
| 1785 | case PyUnicode_2BYTE_KIND: |
| 1786 | PyUnicode_2BYTE_DATA(unicode)[0] = (Py_UCS2)ch; |
| 1787 | break; |
| 1788 | default: |
| 1789 | assert(PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND); |
| 1790 | PyUnicode_4BYTE_DATA(unicode)[0] = ch; |
| 1791 | } |
| 1792 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 1793 | return unicode; |
| 1794 | } |
| 1795 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1796 | PyObject * |
| 1797 | PyUnicode_FromUnicode(const Py_UNICODE *u, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1798 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1799 | PyObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1800 | Py_UCS4 maxchar = 0; |
| 1801 | Py_ssize_t num_surrogates; |
| 1802 | |
| 1803 | if (u == NULL) |
| 1804 | return (PyObject*)_PyUnicode_New(size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1805 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1806 | /* If the Unicode data is known at construction time, we can apply |
| 1807 | some optimizations which share commonly used objects. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1808 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1809 | /* Optimization for empty strings */ |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 1810 | if (size == 0) |
| 1811 | _Py_RETURN_UNICODE_EMPTY(); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1812 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1813 | /* Single character Unicode objects in the Latin-1 range are |
| 1814 | shared when using this constructor */ |
Victor Stinner | d21b58c | 2013-02-26 00:15:54 +0100 | [diff] [blame] | 1815 | if (size == 1 && (Py_UCS4)*u < 256) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1816 | return get_latin1_char((unsigned char)*u); |
| 1817 | |
| 1818 | /* If not empty and not single character, copy the Unicode data |
| 1819 | into the new object */ |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1820 | if (find_maxchar_surrogates(u, u + size, |
| 1821 | &maxchar, &num_surrogates) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1822 | return NULL; |
| 1823 | |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1824 | unicode = PyUnicode_New(size - num_surrogates, maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1825 | if (!unicode) |
| 1826 | return NULL; |
| 1827 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1828 | switch (PyUnicode_KIND(unicode)) { |
| 1829 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1830 | _PyUnicode_CONVERT_BYTES(Py_UNICODE, unsigned char, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1831 | u, u + size, PyUnicode_1BYTE_DATA(unicode)); |
| 1832 | break; |
| 1833 | case PyUnicode_2BYTE_KIND: |
| 1834 | #if Py_UNICODE_SIZE == 2 |
| 1835 | Py_MEMCPY(PyUnicode_2BYTE_DATA(unicode), u, size * 2); |
| 1836 | #else |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1837 | _PyUnicode_CONVERT_BYTES(Py_UNICODE, Py_UCS2, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1838 | u, u + size, PyUnicode_2BYTE_DATA(unicode)); |
| 1839 | #endif |
| 1840 | break; |
| 1841 | case PyUnicode_4BYTE_KIND: |
| 1842 | #if SIZEOF_WCHAR_T == 2 |
| 1843 | /* This is the only case which has to process surrogates, thus |
| 1844 | a simple copy loop is not enough and we need a function. */ |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1845 | unicode_convert_wchar_to_ucs4(u, u + size, unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1846 | #else |
| 1847 | assert(num_surrogates == 0); |
| 1848 | Py_MEMCPY(PyUnicode_4BYTE_DATA(unicode), u, size * 4); |
| 1849 | #endif |
| 1850 | break; |
| 1851 | default: |
| 1852 | assert(0 && "Impossible state"); |
| 1853 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1854 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1855 | return unicode_result(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1856 | } |
| 1857 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1858 | PyObject * |
| 1859 | PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size) |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1860 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1861 | if (size < 0) { |
| 1862 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1863 | "Negative size passed to PyUnicode_FromStringAndSize"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1864 | return NULL; |
| 1865 | } |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 1866 | if (u != NULL) |
| 1867 | return PyUnicode_DecodeUTF8Stateful(u, size, NULL, NULL); |
| 1868 | else |
| 1869 | return (PyObject *)_PyUnicode_New(size); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1870 | } |
| 1871 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1872 | PyObject * |
| 1873 | PyUnicode_FromString(const char *u) |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1874 | { |
| 1875 | size_t size = strlen(u); |
| 1876 | if (size > PY_SSIZE_T_MAX) { |
| 1877 | PyErr_SetString(PyExc_OverflowError, "input too long"); |
| 1878 | return NULL; |
| 1879 | } |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 1880 | return PyUnicode_DecodeUTF8Stateful(u, (Py_ssize_t)size, NULL, NULL); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1881 | } |
| 1882 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1883 | PyObject * |
| 1884 | _PyUnicode_FromId(_Py_Identifier *id) |
| 1885 | { |
| 1886 | if (!id->object) { |
Victor Stinner | d1cd99b | 2012-02-07 23:05:55 +0100 | [diff] [blame] | 1887 | id->object = PyUnicode_DecodeUTF8Stateful(id->string, |
| 1888 | strlen(id->string), |
| 1889 | NULL, NULL); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1890 | if (!id->object) |
| 1891 | return NULL; |
| 1892 | PyUnicode_InternInPlace(&id->object); |
| 1893 | assert(!id->next); |
| 1894 | id->next = static_strings; |
| 1895 | static_strings = id; |
| 1896 | } |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1897 | return id->object; |
| 1898 | } |
| 1899 | |
| 1900 | void |
| 1901 | _PyUnicode_ClearStaticStrings() |
| 1902 | { |
Benjamin Peterson | 0c270a8 | 2013-01-09 09:52:01 -0600 | [diff] [blame] | 1903 | _Py_Identifier *tmp, *s = static_strings; |
| 1904 | while (s) { |
| 1905 | Py_DECREF(s->object); |
| 1906 | s->object = NULL; |
| 1907 | tmp = s->next; |
| 1908 | s->next = NULL; |
| 1909 | s = tmp; |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1910 | } |
Benjamin Peterson | 0c270a8 | 2013-01-09 09:52:01 -0600 | [diff] [blame] | 1911 | static_strings = NULL; |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1912 | } |
| 1913 | |
Benjamin Peterson | 0df5429 | 2012-03-26 14:50:32 -0400 | [diff] [blame] | 1914 | /* Internal function, doesn't check maximum character */ |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1915 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1916 | PyObject* |
| 1917 | _PyUnicode_FromASCII(const char *buffer, Py_ssize_t size) |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1918 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1919 | const unsigned char *s = (const unsigned char *)buffer; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 1920 | PyObject *unicode; |
Victor Stinner | e6b2d44 | 2011-12-11 21:54:30 +0100 | [diff] [blame] | 1921 | if (size == 1) { |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1922 | #ifdef Py_DEBUG |
Victor Stinner | d21b58c | 2013-02-26 00:15:54 +0100 | [diff] [blame] | 1923 | assert((unsigned char)s[0] < 128); |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1924 | #endif |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1925 | return get_latin1_char(s[0]); |
Victor Stinner | e6b2d44 | 2011-12-11 21:54:30 +0100 | [diff] [blame] | 1926 | } |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 1927 | unicode = PyUnicode_New(size, 127); |
| 1928 | if (!unicode) |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1929 | return NULL; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 1930 | memcpy(PyUnicode_1BYTE_DATA(unicode), s, size); |
| 1931 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 1932 | return unicode; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1933 | } |
| 1934 | |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 1935 | static Py_UCS4 |
| 1936 | kind_maxchar_limit(unsigned int kind) |
| 1937 | { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 1938 | switch (kind) { |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 1939 | case PyUnicode_1BYTE_KIND: |
| 1940 | return 0x80; |
| 1941 | case PyUnicode_2BYTE_KIND: |
| 1942 | return 0x100; |
| 1943 | case PyUnicode_4BYTE_KIND: |
| 1944 | return 0x10000; |
| 1945 | default: |
| 1946 | assert(0 && "invalid kind"); |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1947 | return MAX_UNICODE; |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 1948 | } |
| 1949 | } |
| 1950 | |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 1951 | Py_LOCAL_INLINE(Py_UCS4) |
| 1952 | align_maxchar(Py_UCS4 maxchar) |
| 1953 | { |
| 1954 | if (maxchar <= 127) |
| 1955 | return 127; |
| 1956 | else if (maxchar <= 255) |
| 1957 | return 255; |
| 1958 | else if (maxchar <= 65535) |
| 1959 | return 65535; |
| 1960 | else |
| 1961 | return MAX_UNICODE; |
| 1962 | } |
| 1963 | |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1964 | static PyObject* |
Victor Stinner | d21b58c | 2013-02-26 00:15:54 +0100 | [diff] [blame] | 1965 | _PyUnicode_FromUCS1(const Py_UCS1* u, Py_ssize_t size) |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1966 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1967 | PyObject *res; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1968 | unsigned char max_char; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1969 | |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 1970 | if (size == 0) |
| 1971 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1972 | assert(size > 0); |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1973 | if (size == 1) |
| 1974 | return get_latin1_char(u[0]); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1975 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1976 | max_char = ucs1lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1977 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1978 | if (!res) |
| 1979 | return NULL; |
| 1980 | memcpy(PyUnicode_1BYTE_DATA(res), u, size); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1981 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1982 | return res; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1983 | } |
| 1984 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1985 | static PyObject* |
| 1986 | _PyUnicode_FromUCS2(const Py_UCS2 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1987 | { |
| 1988 | PyObject *res; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1989 | Py_UCS2 max_char; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1990 | |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 1991 | if (size == 0) |
| 1992 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1993 | assert(size > 0); |
Victor Stinner | 985a82a | 2014-01-03 12:53:47 +0100 | [diff] [blame] | 1994 | if (size == 1) |
| 1995 | return unicode_char(u[0]); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1996 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1997 | max_char = ucs2lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1998 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1999 | if (!res) |
| 2000 | return NULL; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 2001 | if (max_char >= 256) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2002 | memcpy(PyUnicode_2BYTE_DATA(res), u, sizeof(Py_UCS2)*size); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2003 | else { |
| 2004 | _PyUnicode_CONVERT_BYTES( |
| 2005 | Py_UCS2, Py_UCS1, u, u + size, PyUnicode_1BYTE_DATA(res)); |
| 2006 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2007 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2008 | return res; |
| 2009 | } |
| 2010 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 2011 | static PyObject* |
| 2012 | _PyUnicode_FromUCS4(const Py_UCS4 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2013 | { |
| 2014 | PyObject *res; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2015 | Py_UCS4 max_char; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 2016 | |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 2017 | if (size == 0) |
| 2018 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2019 | assert(size > 0); |
Victor Stinner | 985a82a | 2014-01-03 12:53:47 +0100 | [diff] [blame] | 2020 | if (size == 1) |
| 2021 | return unicode_char(u[0]); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2022 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2023 | max_char = ucs4lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 2024 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2025 | if (!res) |
| 2026 | return NULL; |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2027 | if (max_char < 256) |
| 2028 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS1, u, u + size, |
| 2029 | PyUnicode_1BYTE_DATA(res)); |
| 2030 | else if (max_char < 0x10000) |
| 2031 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS2, u, u + size, |
| 2032 | PyUnicode_2BYTE_DATA(res)); |
| 2033 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2034 | memcpy(PyUnicode_4BYTE_DATA(res), u, sizeof(Py_UCS4)*size); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2035 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2036 | return res; |
| 2037 | } |
| 2038 | |
| 2039 | PyObject* |
| 2040 | PyUnicode_FromKindAndData(int kind, const void *buffer, Py_ssize_t size) |
| 2041 | { |
Victor Stinner | cfed46e | 2011-11-22 01:29:14 +0100 | [diff] [blame] | 2042 | if (size < 0) { |
| 2043 | PyErr_SetString(PyExc_ValueError, "size must be positive"); |
| 2044 | return NULL; |
| 2045 | } |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 2046 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2047 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 2048 | return _PyUnicode_FromUCS1(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2049 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 2050 | return _PyUnicode_FromUCS2(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2051 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 2052 | return _PyUnicode_FromUCS4(buffer, size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 2053 | default: |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 2054 | PyErr_SetString(PyExc_SystemError, "invalid kind"); |
| 2055 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2056 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2057 | } |
| 2058 | |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2059 | Py_UCS4 |
| 2060 | _PyUnicode_FindMaxChar(PyObject *unicode, Py_ssize_t start, Py_ssize_t end) |
| 2061 | { |
| 2062 | enum PyUnicode_Kind kind; |
| 2063 | void *startptr, *endptr; |
| 2064 | |
| 2065 | assert(PyUnicode_IS_READY(unicode)); |
| 2066 | assert(0 <= start); |
| 2067 | assert(end <= PyUnicode_GET_LENGTH(unicode)); |
| 2068 | assert(start <= end); |
| 2069 | |
| 2070 | if (start == 0 && end == PyUnicode_GET_LENGTH(unicode)) |
| 2071 | return PyUnicode_MAX_CHAR_VALUE(unicode); |
| 2072 | |
| 2073 | if (start == end) |
| 2074 | return 127; |
| 2075 | |
Victor Stinner | 94d558b | 2012-04-27 22:26:58 +0200 | [diff] [blame] | 2076 | if (PyUnicode_IS_ASCII(unicode)) |
| 2077 | return 127; |
| 2078 | |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2079 | kind = PyUnicode_KIND(unicode); |
Benjamin Peterson | f3b7d86 | 2012-04-23 18:07:01 -0400 | [diff] [blame] | 2080 | startptr = PyUnicode_DATA(unicode); |
Benjamin Peterson | b9f4c9d | 2012-04-23 21:45:40 -0400 | [diff] [blame] | 2081 | endptr = (char *)startptr + end * kind; |
| 2082 | startptr = (char *)startptr + start * kind; |
Benjamin Peterson | 2844a7a | 2012-04-23 18:00:25 -0400 | [diff] [blame] | 2083 | switch(kind) { |
| 2084 | case PyUnicode_1BYTE_KIND: |
| 2085 | return ucs1lib_find_max_char(startptr, endptr); |
| 2086 | case PyUnicode_2BYTE_KIND: |
| 2087 | return ucs2lib_find_max_char(startptr, endptr); |
| 2088 | case PyUnicode_4BYTE_KIND: |
| 2089 | return ucs4lib_find_max_char(startptr, endptr); |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2090 | default: |
Benjamin Peterson | 2844a7a | 2012-04-23 18:00:25 -0400 | [diff] [blame] | 2091 | assert(0); |
| 2092 | return 0; |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2093 | } |
| 2094 | } |
| 2095 | |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2096 | /* Ensure that a string uses the most efficient storage, if it is not the |
| 2097 | case: create a new string with of the right kind. Write NULL into *p_unicode |
| 2098 | on error. */ |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 2099 | static void |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2100 | unicode_adjust_maxchar(PyObject **p_unicode) |
| 2101 | { |
| 2102 | PyObject *unicode, *copy; |
| 2103 | Py_UCS4 max_char; |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2104 | Py_ssize_t len; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2105 | unsigned int kind; |
| 2106 | |
| 2107 | assert(p_unicode != NULL); |
| 2108 | unicode = *p_unicode; |
| 2109 | assert(PyUnicode_IS_READY(unicode)); |
| 2110 | if (PyUnicode_IS_ASCII(unicode)) |
| 2111 | return; |
| 2112 | |
| 2113 | len = PyUnicode_GET_LENGTH(unicode); |
| 2114 | kind = PyUnicode_KIND(unicode); |
| 2115 | if (kind == PyUnicode_1BYTE_KIND) { |
| 2116 | const Py_UCS1 *u = PyUnicode_1BYTE_DATA(unicode); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2117 | max_char = ucs1lib_find_max_char(u, u + len); |
| 2118 | if (max_char >= 128) |
| 2119 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2120 | } |
| 2121 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 2122 | const Py_UCS2 *u = PyUnicode_2BYTE_DATA(unicode); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2123 | max_char = ucs2lib_find_max_char(u, u + len); |
| 2124 | if (max_char >= 256) |
| 2125 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2126 | } |
| 2127 | else { |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2128 | const Py_UCS4 *u = PyUnicode_4BYTE_DATA(unicode); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2129 | assert(kind == PyUnicode_4BYTE_KIND); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2130 | max_char = ucs4lib_find_max_char(u, u + len); |
| 2131 | if (max_char >= 0x10000) |
| 2132 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2133 | } |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2134 | copy = PyUnicode_New(len, max_char); |
Victor Stinner | ca439ee | 2012-06-16 03:17:34 +0200 | [diff] [blame] | 2135 | if (copy != NULL) |
| 2136 | _PyUnicode_FastCopyCharacters(copy, 0, unicode, 0, len); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2137 | Py_DECREF(unicode); |
| 2138 | *p_unicode = copy; |
| 2139 | } |
| 2140 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2141 | PyObject* |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 2142 | _PyUnicode_Copy(PyObject *unicode) |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2143 | { |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 2144 | Py_ssize_t length; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2145 | PyObject *copy; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2146 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2147 | if (!PyUnicode_Check(unicode)) { |
| 2148 | PyErr_BadInternalCall(); |
| 2149 | return NULL; |
| 2150 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 2151 | if (PyUnicode_READY(unicode) == -1) |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2152 | return NULL; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2153 | |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 2154 | length = PyUnicode_GET_LENGTH(unicode); |
| 2155 | copy = PyUnicode_New(length, PyUnicode_MAX_CHAR_VALUE(unicode)); |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2156 | if (!copy) |
| 2157 | return NULL; |
| 2158 | assert(PyUnicode_KIND(copy) == PyUnicode_KIND(unicode)); |
| 2159 | |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 2160 | Py_MEMCPY(PyUnicode_DATA(copy), PyUnicode_DATA(unicode), |
| 2161 | length * PyUnicode_KIND(unicode)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2162 | assert(_PyUnicode_CheckConsistency(copy, 1)); |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2163 | return copy; |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2164 | } |
| 2165 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2166 | |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2167 | /* Widen Unicode objects to larger buffers. Don't write terminating null |
| 2168 | character. Return NULL on error. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2169 | |
| 2170 | void* |
| 2171 | _PyUnicode_AsKind(PyObject *s, unsigned int kind) |
| 2172 | { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2173 | Py_ssize_t len; |
| 2174 | void *result; |
| 2175 | unsigned int skind; |
| 2176 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 2177 | if (PyUnicode_READY(s) == -1) |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2178 | return NULL; |
| 2179 | |
| 2180 | len = PyUnicode_GET_LENGTH(s); |
| 2181 | skind = PyUnicode_KIND(s); |
| 2182 | if (skind >= kind) { |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 2183 | PyErr_SetString(PyExc_SystemError, "invalid widening attempt"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2184 | return NULL; |
| 2185 | } |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 2186 | switch (kind) { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2187 | case PyUnicode_2BYTE_KIND: |
| 2188 | result = PyMem_Malloc(len * sizeof(Py_UCS2)); |
| 2189 | if (!result) |
| 2190 | return PyErr_NoMemory(); |
| 2191 | assert(skind == PyUnicode_1BYTE_KIND); |
| 2192 | _PyUnicode_CONVERT_BYTES( |
| 2193 | Py_UCS1, Py_UCS2, |
| 2194 | PyUnicode_1BYTE_DATA(s), |
| 2195 | PyUnicode_1BYTE_DATA(s) + len, |
| 2196 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2197 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2198 | case PyUnicode_4BYTE_KIND: |
| 2199 | result = PyMem_Malloc(len * sizeof(Py_UCS4)); |
| 2200 | if (!result) |
| 2201 | return PyErr_NoMemory(); |
| 2202 | if (skind == PyUnicode_2BYTE_KIND) { |
| 2203 | _PyUnicode_CONVERT_BYTES( |
| 2204 | Py_UCS2, Py_UCS4, |
| 2205 | PyUnicode_2BYTE_DATA(s), |
| 2206 | PyUnicode_2BYTE_DATA(s) + len, |
| 2207 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2208 | } |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2209 | else { |
| 2210 | assert(skind == PyUnicode_1BYTE_KIND); |
| 2211 | _PyUnicode_CONVERT_BYTES( |
| 2212 | Py_UCS1, Py_UCS4, |
| 2213 | PyUnicode_1BYTE_DATA(s), |
| 2214 | PyUnicode_1BYTE_DATA(s) + len, |
| 2215 | result); |
| 2216 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2217 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2218 | default: |
| 2219 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2220 | } |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 2221 | PyErr_SetString(PyExc_SystemError, "invalid kind"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2222 | return NULL; |
| 2223 | } |
| 2224 | |
| 2225 | static Py_UCS4* |
| 2226 | as_ucs4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 2227 | int copy_null) |
| 2228 | { |
| 2229 | int kind; |
| 2230 | void *data; |
| 2231 | Py_ssize_t len, targetlen; |
| 2232 | if (PyUnicode_READY(string) == -1) |
| 2233 | return NULL; |
| 2234 | kind = PyUnicode_KIND(string); |
| 2235 | data = PyUnicode_DATA(string); |
| 2236 | len = PyUnicode_GET_LENGTH(string); |
| 2237 | targetlen = len; |
| 2238 | if (copy_null) |
| 2239 | targetlen++; |
| 2240 | if (!target) { |
| 2241 | if (PY_SSIZE_T_MAX / sizeof(Py_UCS4) < targetlen) { |
| 2242 | PyErr_NoMemory(); |
| 2243 | return NULL; |
| 2244 | } |
| 2245 | target = PyMem_Malloc(targetlen * sizeof(Py_UCS4)); |
| 2246 | if (!target) { |
| 2247 | PyErr_NoMemory(); |
| 2248 | return NULL; |
| 2249 | } |
| 2250 | } |
| 2251 | else { |
| 2252 | if (targetsize < targetlen) { |
| 2253 | PyErr_Format(PyExc_SystemError, |
| 2254 | "string is longer than the buffer"); |
| 2255 | if (copy_null && 0 < targetsize) |
| 2256 | target[0] = 0; |
| 2257 | return NULL; |
| 2258 | } |
| 2259 | } |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2260 | if (kind == PyUnicode_1BYTE_KIND) { |
| 2261 | Py_UCS1 *start = (Py_UCS1 *) data; |
| 2262 | _PyUnicode_CONVERT_BYTES(Py_UCS1, Py_UCS4, start, start + len, target); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2263 | } |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2264 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 2265 | Py_UCS2 *start = (Py_UCS2 *) data; |
| 2266 | _PyUnicode_CONVERT_BYTES(Py_UCS2, Py_UCS4, start, start + len, target); |
| 2267 | } |
| 2268 | else { |
| 2269 | assert(kind == PyUnicode_4BYTE_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2270 | Py_MEMCPY(target, data, len * sizeof(Py_UCS4)); |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2271 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2272 | if (copy_null) |
| 2273 | target[len] = 0; |
| 2274 | return target; |
| 2275 | } |
| 2276 | |
| 2277 | Py_UCS4* |
| 2278 | PyUnicode_AsUCS4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 2279 | int copy_null) |
| 2280 | { |
Antoine Pitrou | de20b0b | 2011-11-10 21:47:38 +0100 | [diff] [blame] | 2281 | if (target == NULL || targetsize < 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2282 | PyErr_BadInternalCall(); |
| 2283 | return NULL; |
| 2284 | } |
| 2285 | return as_ucs4(string, target, targetsize, copy_null); |
| 2286 | } |
| 2287 | |
| 2288 | Py_UCS4* |
| 2289 | PyUnicode_AsUCS4Copy(PyObject *string) |
| 2290 | { |
| 2291 | return as_ucs4(string, NULL, 0, 1); |
| 2292 | } |
| 2293 | |
| 2294 | #ifdef HAVE_WCHAR_H |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 2295 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2296 | PyObject * |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 2297 | PyUnicode_FromWideChar(const wchar_t *w, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2298 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2299 | if (w == NULL) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2300 | if (size == 0) |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 2301 | _Py_RETURN_UNICODE_EMPTY(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2302 | PyErr_BadInternalCall(); |
| 2303 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2304 | } |
| 2305 | |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 2306 | if (size == -1) { |
| 2307 | size = wcslen(w); |
| 2308 | } |
| 2309 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2310 | return PyUnicode_FromUnicode(w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2311 | } |
| 2312 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2313 | #endif /* HAVE_WCHAR_H */ |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 2314 | |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2315 | static void |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2316 | makefmt(char *fmt, int longflag, int longlongflag, int size_tflag, |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2317 | char c) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2318 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2319 | *fmt++ = '%'; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2320 | if (longflag) |
| 2321 | *fmt++ = 'l'; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2322 | else if (longlongflag) { |
| 2323 | /* longlongflag should only ever be nonzero on machines with |
| 2324 | HAVE_LONG_LONG defined */ |
| 2325 | #ifdef HAVE_LONG_LONG |
| 2326 | char *f = PY_FORMAT_LONG_LONG; |
| 2327 | while (*f) |
| 2328 | *fmt++ = *f++; |
| 2329 | #else |
| 2330 | /* we shouldn't ever get here */ |
| 2331 | assert(0); |
| 2332 | *fmt++ = 'l'; |
| 2333 | #endif |
| 2334 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2335 | else if (size_tflag) { |
| 2336 | char *f = PY_FORMAT_SIZE_T; |
| 2337 | while (*f) |
| 2338 | *fmt++ = *f++; |
| 2339 | } |
| 2340 | *fmt++ = c; |
| 2341 | *fmt = '\0'; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2342 | } |
| 2343 | |
Victor Stinner | 15a1136 | 2012-10-06 23:48:20 +0200 | [diff] [blame] | 2344 | /* maximum number of characters required for output of %lld or %p. |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2345 | We need at most ceil(log10(256)*SIZEOF_LONG_LONG) digits, |
| 2346 | plus 1 for the sign. 53/22 is an upper bound for log10(256). */ |
| 2347 | #define MAX_LONG_LONG_CHARS (2 + (SIZEOF_LONG_LONG*53-1) / 22) |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2348 | |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2349 | static int |
| 2350 | unicode_fromformat_write_str(_PyUnicodeWriter *writer, PyObject *str, |
| 2351 | Py_ssize_t width, Py_ssize_t precision) |
| 2352 | { |
| 2353 | Py_ssize_t length, fill, arglen; |
| 2354 | Py_UCS4 maxchar; |
| 2355 | |
| 2356 | if (PyUnicode_READY(str) == -1) |
| 2357 | return -1; |
| 2358 | |
| 2359 | length = PyUnicode_GET_LENGTH(str); |
| 2360 | if ((precision == -1 || precision >= length) |
| 2361 | && width <= length) |
| 2362 | return _PyUnicodeWriter_WriteStr(writer, str); |
| 2363 | |
| 2364 | if (precision != -1) |
| 2365 | length = Py_MIN(precision, length); |
| 2366 | |
| 2367 | arglen = Py_MAX(length, width); |
| 2368 | if (PyUnicode_MAX_CHAR_VALUE(str) > writer->maxchar) |
| 2369 | maxchar = _PyUnicode_FindMaxChar(str, 0, length); |
| 2370 | else |
| 2371 | maxchar = writer->maxchar; |
| 2372 | |
| 2373 | if (_PyUnicodeWriter_Prepare(writer, arglen, maxchar) == -1) |
| 2374 | return -1; |
| 2375 | |
| 2376 | if (width > length) { |
| 2377 | fill = width - length; |
| 2378 | if (PyUnicode_Fill(writer->buffer, writer->pos, fill, ' ') == -1) |
| 2379 | return -1; |
| 2380 | writer->pos += fill; |
| 2381 | } |
| 2382 | |
| 2383 | _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos, |
| 2384 | str, 0, length); |
| 2385 | writer->pos += length; |
| 2386 | return 0; |
| 2387 | } |
| 2388 | |
| 2389 | static int |
| 2390 | unicode_fromformat_write_cstr(_PyUnicodeWriter *writer, const char *str, |
| 2391 | Py_ssize_t width, Py_ssize_t precision) |
| 2392 | { |
| 2393 | /* UTF-8 */ |
| 2394 | Py_ssize_t length; |
| 2395 | PyObject *unicode; |
| 2396 | int res; |
| 2397 | |
| 2398 | length = strlen(str); |
| 2399 | if (precision != -1) |
| 2400 | length = Py_MIN(length, precision); |
| 2401 | unicode = PyUnicode_DecodeUTF8Stateful(str, length, "replace", NULL); |
| 2402 | if (unicode == NULL) |
| 2403 | return -1; |
| 2404 | |
| 2405 | res = unicode_fromformat_write_str(writer, unicode, width, -1); |
| 2406 | Py_DECREF(unicode); |
| 2407 | return res; |
| 2408 | } |
| 2409 | |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2410 | static const char* |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2411 | unicode_fromformat_arg(_PyUnicodeWriter *writer, |
| 2412 | const char *f, va_list *vargs) |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2413 | { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2414 | const char *p; |
| 2415 | Py_ssize_t len; |
| 2416 | int zeropad; |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2417 | Py_ssize_t width; |
| 2418 | Py_ssize_t precision; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2419 | int longflag; |
| 2420 | int longlongflag; |
| 2421 | int size_tflag; |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2422 | Py_ssize_t fill; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2423 | |
| 2424 | p = f; |
| 2425 | f++; |
Victor Stinner | 4c63a97 | 2012-10-06 23:55:33 +0200 | [diff] [blame] | 2426 | zeropad = 0; |
| 2427 | if (*f == '0') { |
| 2428 | zeropad = 1; |
| 2429 | f++; |
| 2430 | } |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2431 | |
| 2432 | /* 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] | 2433 | width = -1; |
| 2434 | if (Py_ISDIGIT((unsigned)*f)) { |
| 2435 | width = *f - '0'; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2436 | f++; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2437 | while (Py_ISDIGIT((unsigned)*f)) { |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2438 | if (width > (PY_SSIZE_T_MAX - ((int)*f - '0')) / 10) { |
Victor Stinner | 3921e90 | 2012-10-06 23:05:00 +0200 | [diff] [blame] | 2439 | PyErr_SetString(PyExc_ValueError, |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2440 | "width too big"); |
Victor Stinner | 3921e90 | 2012-10-06 23:05:00 +0200 | [diff] [blame] | 2441 | return NULL; |
| 2442 | } |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2443 | width = (width * 10) + (*f - '0'); |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2444 | f++; |
| 2445 | } |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2446 | } |
| 2447 | precision = -1; |
| 2448 | if (*f == '.') { |
| 2449 | f++; |
| 2450 | if (Py_ISDIGIT((unsigned)*f)) { |
| 2451 | precision = (*f - '0'); |
| 2452 | f++; |
| 2453 | while (Py_ISDIGIT((unsigned)*f)) { |
| 2454 | if (precision > (PY_SSIZE_T_MAX - ((int)*f - '0')) / 10) { |
| 2455 | PyErr_SetString(PyExc_ValueError, |
| 2456 | "precision too big"); |
| 2457 | return NULL; |
| 2458 | } |
| 2459 | precision = (precision * 10) + (*f - '0'); |
| 2460 | f++; |
| 2461 | } |
| 2462 | } |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2463 | if (*f == '%') { |
| 2464 | /* "%.3%s" => f points to "3" */ |
| 2465 | f--; |
| 2466 | } |
| 2467 | } |
| 2468 | if (*f == '\0') { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2469 | /* bogus format "%.123" => go backward, f points to "3" */ |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2470 | f--; |
| 2471 | } |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2472 | |
| 2473 | /* Handle %ld, %lu, %lld and %llu. */ |
| 2474 | longflag = 0; |
| 2475 | longlongflag = 0; |
Victor Stinner | e7faec1 | 2011-03-02 00:01:53 +0000 | [diff] [blame] | 2476 | size_tflag = 0; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2477 | if (*f == 'l') { |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2478 | if (f[1] == 'd' || f[1] == 'u' || f[1] == 'i') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2479 | longflag = 1; |
| 2480 | ++f; |
| 2481 | } |
| 2482 | #ifdef HAVE_LONG_LONG |
| 2483 | else if (f[1] == 'l' && |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2484 | (f[2] == 'd' || f[2] == 'u' || f[2] == 'i')) { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2485 | longlongflag = 1; |
| 2486 | f += 2; |
| 2487 | } |
| 2488 | #endif |
| 2489 | } |
| 2490 | /* handle the size_t flag. */ |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2491 | 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] | 2492 | size_tflag = 1; |
| 2493 | ++f; |
| 2494 | } |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2495 | |
| 2496 | if (f[1] == '\0') |
| 2497 | writer->overallocate = 0; |
| 2498 | |
| 2499 | switch (*f) { |
| 2500 | case 'c': |
| 2501 | { |
| 2502 | int ordinal = va_arg(*vargs, int); |
Victor Stinner | ff5a848 | 2012-10-06 23:05:45 +0200 | [diff] [blame] | 2503 | if (ordinal < 0 || ordinal > MAX_UNICODE) { |
Serhiy Storchaka | c89533f | 2013-06-23 20:21:16 +0300 | [diff] [blame] | 2504 | PyErr_SetString(PyExc_OverflowError, |
Victor Stinner | ff5a848 | 2012-10-06 23:05:45 +0200 | [diff] [blame] | 2505 | "character argument not in range(0x110000)"); |
| 2506 | return NULL; |
| 2507 | } |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 2508 | if (_PyUnicodeWriter_WriteCharInline(writer, ordinal) < 0) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2509 | return NULL; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2510 | break; |
| 2511 | } |
| 2512 | |
| 2513 | case 'i': |
| 2514 | case 'd': |
| 2515 | case 'u': |
| 2516 | case 'x': |
| 2517 | { |
| 2518 | /* used by sprintf */ |
| 2519 | char fmt[10]; /* should be enough for "%0lld\0" */ |
Victor Stinner | 15a1136 | 2012-10-06 23:48:20 +0200 | [diff] [blame] | 2520 | char buffer[MAX_LONG_LONG_CHARS]; |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2521 | Py_ssize_t arglen; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2522 | |
| 2523 | if (*f == 'u') { |
| 2524 | makefmt(fmt, longflag, longlongflag, size_tflag, *f); |
| 2525 | |
| 2526 | if (longflag) |
| 2527 | len = sprintf(buffer, fmt, |
| 2528 | va_arg(*vargs, unsigned long)); |
| 2529 | #ifdef HAVE_LONG_LONG |
| 2530 | else if (longlongflag) |
| 2531 | len = sprintf(buffer, fmt, |
| 2532 | va_arg(*vargs, unsigned PY_LONG_LONG)); |
| 2533 | #endif |
| 2534 | else if (size_tflag) |
| 2535 | len = sprintf(buffer, fmt, |
| 2536 | va_arg(*vargs, size_t)); |
| 2537 | else |
| 2538 | len = sprintf(buffer, fmt, |
| 2539 | va_arg(*vargs, unsigned int)); |
| 2540 | } |
| 2541 | else if (*f == 'x') { |
| 2542 | makefmt(fmt, 0, 0, 0, 'x'); |
| 2543 | len = sprintf(buffer, fmt, va_arg(*vargs, int)); |
| 2544 | } |
| 2545 | else { |
| 2546 | makefmt(fmt, longflag, longlongflag, size_tflag, *f); |
| 2547 | |
| 2548 | if (longflag) |
| 2549 | len = sprintf(buffer, fmt, |
| 2550 | va_arg(*vargs, long)); |
| 2551 | #ifdef HAVE_LONG_LONG |
| 2552 | else if (longlongflag) |
| 2553 | len = sprintf(buffer, fmt, |
| 2554 | va_arg(*vargs, PY_LONG_LONG)); |
| 2555 | #endif |
| 2556 | else if (size_tflag) |
| 2557 | len = sprintf(buffer, fmt, |
| 2558 | va_arg(*vargs, Py_ssize_t)); |
| 2559 | else |
| 2560 | len = sprintf(buffer, fmt, |
| 2561 | va_arg(*vargs, int)); |
| 2562 | } |
| 2563 | assert(len >= 0); |
| 2564 | |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2565 | if (precision < len) |
| 2566 | precision = len; |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2567 | |
| 2568 | arglen = Py_MAX(precision, width); |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2569 | if (_PyUnicodeWriter_Prepare(writer, arglen, 127) == -1) |
| 2570 | return NULL; |
| 2571 | |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2572 | if (width > precision) { |
| 2573 | Py_UCS4 fillchar; |
| 2574 | fill = width - precision; |
| 2575 | fillchar = zeropad?'0':' '; |
Victor Stinner | 15a1136 | 2012-10-06 23:48:20 +0200 | [diff] [blame] | 2576 | if (PyUnicode_Fill(writer->buffer, writer->pos, fill, fillchar) == -1) |
| 2577 | return NULL; |
| 2578 | writer->pos += fill; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2579 | } |
Victor Stinner | 15a1136 | 2012-10-06 23:48:20 +0200 | [diff] [blame] | 2580 | if (precision > len) { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2581 | fill = precision - len; |
Victor Stinner | 15a1136 | 2012-10-06 23:48:20 +0200 | [diff] [blame] | 2582 | if (PyUnicode_Fill(writer->buffer, writer->pos, fill, '0') == -1) |
| 2583 | return NULL; |
| 2584 | writer->pos += fill; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2585 | } |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2586 | |
Victor Stinner | 4a58707 | 2013-11-19 12:54:53 +0100 | [diff] [blame] | 2587 | if (_PyUnicodeWriter_WriteASCIIString(writer, buffer, len) < 0) |
| 2588 | return NULL; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2589 | break; |
| 2590 | } |
| 2591 | |
| 2592 | case 'p': |
| 2593 | { |
| 2594 | char number[MAX_LONG_LONG_CHARS]; |
| 2595 | |
| 2596 | len = sprintf(number, "%p", va_arg(*vargs, void*)); |
| 2597 | assert(len >= 0); |
| 2598 | |
| 2599 | /* %p is ill-defined: ensure leading 0x. */ |
| 2600 | if (number[1] == 'X') |
| 2601 | number[1] = 'x'; |
| 2602 | else if (number[1] != 'x') { |
| 2603 | memmove(number + 2, number, |
| 2604 | strlen(number) + 1); |
| 2605 | number[0] = '0'; |
| 2606 | number[1] = 'x'; |
| 2607 | len += 2; |
| 2608 | } |
| 2609 | |
Victor Stinner | 4a58707 | 2013-11-19 12:54:53 +0100 | [diff] [blame] | 2610 | if (_PyUnicodeWriter_WriteASCIIString(writer, number, len) < 0) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2611 | return NULL; |
| 2612 | break; |
| 2613 | } |
| 2614 | |
| 2615 | case 's': |
| 2616 | { |
| 2617 | /* UTF-8 */ |
| 2618 | const char *s = va_arg(*vargs, const char*); |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2619 | if (unicode_fromformat_write_cstr(writer, s, width, precision) < 0) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2620 | return NULL; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2621 | break; |
| 2622 | } |
| 2623 | |
| 2624 | case 'U': |
| 2625 | { |
| 2626 | PyObject *obj = va_arg(*vargs, PyObject *); |
| 2627 | assert(obj && _PyUnicode_CHECK(obj)); |
| 2628 | |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2629 | if (unicode_fromformat_write_str(writer, obj, width, precision) == -1) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2630 | return NULL; |
| 2631 | break; |
| 2632 | } |
| 2633 | |
| 2634 | case 'V': |
| 2635 | { |
| 2636 | PyObject *obj = va_arg(*vargs, PyObject *); |
| 2637 | const char *str = va_arg(*vargs, const char *); |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2638 | if (obj) { |
| 2639 | assert(_PyUnicode_CHECK(obj)); |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2640 | if (unicode_fromformat_write_str(writer, obj, width, precision) == -1) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2641 | return NULL; |
| 2642 | } |
| 2643 | else { |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2644 | assert(str != NULL); |
| 2645 | if (unicode_fromformat_write_cstr(writer, str, width, precision) < 0) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2646 | return NULL; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2647 | } |
| 2648 | break; |
| 2649 | } |
| 2650 | |
| 2651 | case 'S': |
| 2652 | { |
| 2653 | PyObject *obj = va_arg(*vargs, PyObject *); |
| 2654 | PyObject *str; |
| 2655 | assert(obj); |
| 2656 | str = PyObject_Str(obj); |
| 2657 | if (!str) |
| 2658 | return NULL; |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2659 | if (unicode_fromformat_write_str(writer, str, width, precision) == -1) { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2660 | Py_DECREF(str); |
| 2661 | return NULL; |
| 2662 | } |
| 2663 | Py_DECREF(str); |
| 2664 | break; |
| 2665 | } |
| 2666 | |
| 2667 | case 'R': |
| 2668 | { |
| 2669 | PyObject *obj = va_arg(*vargs, PyObject *); |
| 2670 | PyObject *repr; |
| 2671 | assert(obj); |
| 2672 | repr = PyObject_Repr(obj); |
| 2673 | if (!repr) |
| 2674 | return NULL; |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2675 | if (unicode_fromformat_write_str(writer, repr, width, precision) == -1) { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2676 | Py_DECREF(repr); |
| 2677 | return NULL; |
| 2678 | } |
| 2679 | Py_DECREF(repr); |
| 2680 | break; |
| 2681 | } |
| 2682 | |
| 2683 | case 'A': |
| 2684 | { |
| 2685 | PyObject *obj = va_arg(*vargs, PyObject *); |
| 2686 | PyObject *ascii; |
| 2687 | assert(obj); |
| 2688 | ascii = PyObject_ASCII(obj); |
| 2689 | if (!ascii) |
| 2690 | return NULL; |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2691 | if (unicode_fromformat_write_str(writer, ascii, width, precision) == -1) { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2692 | Py_DECREF(ascii); |
| 2693 | return NULL; |
| 2694 | } |
| 2695 | Py_DECREF(ascii); |
| 2696 | break; |
| 2697 | } |
| 2698 | |
| 2699 | case '%': |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 2700 | if (_PyUnicodeWriter_WriteCharInline(writer, '%') < 0) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2701 | return NULL; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2702 | break; |
| 2703 | |
| 2704 | default: |
| 2705 | /* if we stumble upon an unknown formatting code, copy the rest |
| 2706 | of the format string to the output string. (we cannot just |
| 2707 | skip the code, since there's no way to know what's in the |
| 2708 | argument list) */ |
| 2709 | len = strlen(p); |
Victor Stinner | 4a58707 | 2013-11-19 12:54:53 +0100 | [diff] [blame] | 2710 | if (_PyUnicodeWriter_WriteLatin1String(writer, p, len) == -1) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2711 | return NULL; |
| 2712 | f = p+len; |
| 2713 | return f; |
| 2714 | } |
| 2715 | |
| 2716 | f++; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2717 | return f; |
| 2718 | } |
| 2719 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2720 | PyObject * |
| 2721 | PyUnicode_FromFormatV(const char *format, va_list vargs) |
| 2722 | { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2723 | va_list vargs2; |
| 2724 | const char *f; |
| 2725 | _PyUnicodeWriter writer; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2726 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 2727 | _PyUnicodeWriter_Init(&writer); |
| 2728 | writer.min_length = strlen(format) + 100; |
| 2729 | writer.overallocate = 1; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2730 | |
| 2731 | /* va_list may be an array (of 1 item) on some platforms (ex: AMD64). |
| 2732 | Copy it to be able to pass a reference to a subfunction. */ |
| 2733 | Py_VA_COPY(vargs2, vargs); |
| 2734 | |
| 2735 | for (f = format; *f; ) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2736 | if (*f == '%') { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2737 | f = unicode_fromformat_arg(&writer, f, &vargs2); |
| 2738 | if (f == NULL) |
| 2739 | goto fail; |
Victor Stinner | 1205f27 | 2010-09-11 00:54:47 +0000 | [diff] [blame] | 2740 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2741 | else { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2742 | const char *p; |
| 2743 | Py_ssize_t len; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2744 | |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2745 | p = f; |
| 2746 | do |
| 2747 | { |
| 2748 | if ((unsigned char)*p > 127) { |
| 2749 | PyErr_Format(PyExc_ValueError, |
| 2750 | "PyUnicode_FromFormatV() expects an ASCII-encoded format " |
| 2751 | "string, got a non-ASCII byte: 0x%02x", |
| 2752 | (unsigned char)*p); |
| 2753 | return NULL; |
| 2754 | } |
| 2755 | p++; |
| 2756 | } |
| 2757 | while (*p != '\0' && *p != '%'); |
| 2758 | len = p - f; |
| 2759 | |
| 2760 | if (*p == '\0') |
| 2761 | writer.overallocate = 0; |
Victor Stinner | 4a58707 | 2013-11-19 12:54:53 +0100 | [diff] [blame] | 2762 | |
| 2763 | if (_PyUnicodeWriter_WriteASCIIString(&writer, f, len) < 0) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2764 | goto fail; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2765 | |
| 2766 | f = p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2767 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2768 | } |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2769 | return _PyUnicodeWriter_Finish(&writer); |
| 2770 | |
| 2771 | fail: |
| 2772 | _PyUnicodeWriter_Dealloc(&writer); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2773 | return NULL; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2774 | } |
| 2775 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2776 | PyObject * |
| 2777 | PyUnicode_FromFormat(const char *format, ...) |
| 2778 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2779 | PyObject* ret; |
| 2780 | va_list vargs; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2781 | |
| 2782 | #ifdef HAVE_STDARG_PROTOTYPES |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2783 | va_start(vargs, format); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2784 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2785 | va_start(vargs); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2786 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2787 | ret = PyUnicode_FromFormatV(format, vargs); |
| 2788 | va_end(vargs); |
| 2789 | return ret; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2790 | } |
| 2791 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2792 | #ifdef HAVE_WCHAR_H |
| 2793 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2794 | /* Helper function for PyUnicode_AsWideChar() and PyUnicode_AsWideCharString(): |
| 2795 | convert a Unicode object to a wide character string. |
| 2796 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2797 | - 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] | 2798 | character) required to convert the unicode object. Ignore size argument. |
| 2799 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2800 | - Otherwise: return the number of wide characters (excluding the null |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2801 | character) written into w. Write at most size wide characters (including |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2802 | the null character). */ |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2803 | static Py_ssize_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2804 | unicode_aswidechar(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2805 | wchar_t *w, |
| 2806 | Py_ssize_t size) |
| 2807 | { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2808 | Py_ssize_t res; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2809 | const wchar_t *wstr; |
| 2810 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2811 | wstr = PyUnicode_AsUnicodeAndSize(unicode, &res); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2812 | if (wstr == NULL) |
| 2813 | return -1; |
| 2814 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2815 | if (w != NULL) { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2816 | if (size > res) |
| 2817 | size = res + 1; |
| 2818 | else |
| 2819 | res = size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2820 | Py_MEMCPY(w, wstr, size * sizeof(wchar_t)); |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2821 | return res; |
| 2822 | } |
| 2823 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2824 | return res + 1; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2825 | } |
| 2826 | |
| 2827 | Py_ssize_t |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2828 | PyUnicode_AsWideChar(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2829 | wchar_t *w, |
| 2830 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2831 | { |
| 2832 | if (unicode == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2833 | PyErr_BadInternalCall(); |
| 2834 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2835 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2836 | return unicode_aswidechar(unicode, w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2837 | } |
| 2838 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2839 | wchar_t* |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 2840 | PyUnicode_AsWideCharString(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2841 | Py_ssize_t *size) |
| 2842 | { |
| 2843 | wchar_t* buffer; |
| 2844 | Py_ssize_t buflen; |
| 2845 | |
| 2846 | if (unicode == NULL) { |
| 2847 | PyErr_BadInternalCall(); |
| 2848 | return NULL; |
| 2849 | } |
| 2850 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2851 | buflen = unicode_aswidechar(unicode, NULL, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2852 | if (buflen == -1) |
| 2853 | return NULL; |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2854 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < buflen) { |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2855 | PyErr_NoMemory(); |
| 2856 | return NULL; |
| 2857 | } |
| 2858 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2859 | buffer = PyMem_MALLOC(buflen * sizeof(wchar_t)); |
| 2860 | if (buffer == NULL) { |
| 2861 | PyErr_NoMemory(); |
| 2862 | return NULL; |
| 2863 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2864 | buflen = unicode_aswidechar(unicode, buffer, buflen); |
Stefan Krah | 8528c31 | 2012-08-19 21:52:43 +0200 | [diff] [blame] | 2865 | if (buflen == -1) { |
| 2866 | PyMem_FREE(buffer); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2867 | return NULL; |
Stefan Krah | 8528c31 | 2012-08-19 21:52:43 +0200 | [diff] [blame] | 2868 | } |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2869 | if (size != NULL) |
| 2870 | *size = buflen; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2871 | return buffer; |
| 2872 | } |
| 2873 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2874 | #endif /* HAVE_WCHAR_H */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2875 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2876 | PyObject * |
| 2877 | PyUnicode_FromOrdinal(int ordinal) |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2878 | { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 2879 | if (ordinal < 0 || ordinal > MAX_UNICODE) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2880 | PyErr_SetString(PyExc_ValueError, |
| 2881 | "chr() arg not in range(0x110000)"); |
| 2882 | return NULL; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2883 | } |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 2884 | |
Victor Stinner | 985a82a | 2014-01-03 12:53:47 +0100 | [diff] [blame] | 2885 | return unicode_char((Py_UCS4)ordinal); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2886 | } |
| 2887 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2888 | PyObject * |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 2889 | PyUnicode_FromObject(PyObject *obj) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2890 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2891 | /* XXX Perhaps we should make this API an alias of |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2892 | PyObject_Str() instead ?! */ |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2893 | if (PyUnicode_CheckExact(obj)) { |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 2894 | if (PyUnicode_READY(obj) == -1) |
Victor Stinner | d3a83d5 | 2011-10-01 03:09:33 +0200 | [diff] [blame] | 2895 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2896 | Py_INCREF(obj); |
| 2897 | return obj; |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2898 | } |
| 2899 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2900 | /* For a Unicode subtype that's not a Unicode object, |
| 2901 | return a true Unicode object with the same data. */ |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 2902 | return _PyUnicode_Copy(obj); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2903 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2904 | PyErr_Format(PyExc_TypeError, |
| 2905 | "Can't convert '%.100s' object to str implicitly", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2906 | Py_TYPE(obj)->tp_name); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2907 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2908 | } |
| 2909 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2910 | PyObject * |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 2911 | PyUnicode_FromEncodedObject(PyObject *obj, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2912 | const char *encoding, |
| 2913 | const char *errors) |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2914 | { |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2915 | Py_buffer buffer; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2916 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2917 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2918 | if (obj == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2919 | PyErr_BadInternalCall(); |
| 2920 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2921 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2922 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2923 | /* Decoding bytes objects is the most common case and should be fast */ |
| 2924 | if (PyBytes_Check(obj)) { |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 2925 | if (PyBytes_GET_SIZE(obj) == 0) |
| 2926 | _Py_RETURN_UNICODE_EMPTY(); |
| 2927 | v = PyUnicode_Decode( |
| 2928 | PyBytes_AS_STRING(obj), PyBytes_GET_SIZE(obj), |
| 2929 | encoding, errors); |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2930 | return v; |
| 2931 | } |
| 2932 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2933 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2934 | PyErr_SetString(PyExc_TypeError, |
| 2935 | "decoding str is not supported"); |
| 2936 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2937 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2938 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2939 | /* Retrieve a bytes buffer view through the PEP 3118 buffer interface */ |
| 2940 | if (PyObject_GetBuffer(obj, &buffer, PyBUF_SIMPLE) < 0) { |
| 2941 | PyErr_Format(PyExc_TypeError, |
| 2942 | "coercing to str: need bytes, bytearray " |
| 2943 | "or buffer-like object, %.80s found", |
| 2944 | Py_TYPE(obj)->tp_name); |
| 2945 | return NULL; |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 2946 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2947 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2948 | if (buffer.len == 0) { |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 2949 | PyBuffer_Release(&buffer); |
| 2950 | _Py_RETURN_UNICODE_EMPTY(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2951 | } |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 2952 | |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 2953 | v = PyUnicode_Decode((char*) buffer.buf, buffer.len, encoding, errors); |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2954 | PyBuffer_Release(&buffer); |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2955 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2956 | } |
| 2957 | |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2958 | /* Convert encoding to lower case and replace '_' with '-' in order to |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2959 | catch e.g. UTF_8. Return 0 on error (encoding is longer than lower_len-1), |
| 2960 | 1 on success. */ |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 2961 | int |
| 2962 | _Py_normalize_encoding(const char *encoding, |
| 2963 | char *lower, |
| 2964 | size_t lower_len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2965 | { |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2966 | const char *e; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2967 | char *l; |
| 2968 | char *l_end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2969 | |
Benjamin Peterson | 7a6debe | 2011-10-15 09:25:28 -0400 | [diff] [blame] | 2970 | if (encoding == NULL) { |
Victor Stinner | 66b3270 | 2013-11-07 23:12:23 +0100 | [diff] [blame] | 2971 | /* 6 == strlen("utf-8") + 1 */ |
Victor Stinner | df23e30 | 2013-11-07 13:33:36 +0100 | [diff] [blame] | 2972 | if (lower_len < 6) |
| 2973 | return 0; |
Benjamin Peterson | 7a6debe | 2011-10-15 09:25:28 -0400 | [diff] [blame] | 2974 | strcpy(lower, "utf-8"); |
| 2975 | return 1; |
| 2976 | } |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2977 | e = encoding; |
| 2978 | l = lower; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2979 | l_end = &lower[lower_len - 1]; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2980 | while (*e) { |
| 2981 | if (l == l_end) |
| 2982 | return 0; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 2983 | if (Py_ISUPPER(*e)) { |
| 2984 | *l++ = Py_TOLOWER(*e++); |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2985 | } |
| 2986 | else if (*e == '_') { |
| 2987 | *l++ = '-'; |
| 2988 | e++; |
| 2989 | } |
| 2990 | else { |
| 2991 | *l++ = *e++; |
| 2992 | } |
| 2993 | } |
| 2994 | *l = '\0'; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2995 | return 1; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2996 | } |
| 2997 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2998 | PyObject * |
| 2999 | PyUnicode_Decode(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3000 | Py_ssize_t size, |
| 3001 | const char *encoding, |
| 3002 | const char *errors) |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3003 | { |
| 3004 | PyObject *buffer = NULL, *unicode; |
| 3005 | Py_buffer info; |
| 3006 | char lower[11]; /* Enough for any encoding shortcut */ |
| 3007 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3008 | /* Shortcuts for common default encodings */ |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3009 | if (_Py_normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3010 | if ((strcmp(lower, "utf-8") == 0) || |
| 3011 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 3012 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3013 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3014 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | fa3ba4c | 2013-10-29 11:34:05 +0100 | [diff] [blame] | 3015 | (strcmp(lower, "iso-8859-1") == 0) || |
| 3016 | (strcmp(lower, "iso8859-1") == 0)) |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3017 | return PyUnicode_DecodeLatin1(s, size, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3018 | #ifdef HAVE_MBCS |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3019 | else if (strcmp(lower, "mbcs") == 0) |
| 3020 | return PyUnicode_DecodeMBCS(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 3021 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3022 | else if (strcmp(lower, "ascii") == 0) |
| 3023 | return PyUnicode_DecodeASCII(s, size, errors); |
| 3024 | else if (strcmp(lower, "utf-16") == 0) |
| 3025 | return PyUnicode_DecodeUTF16(s, size, errors, 0); |
| 3026 | else if (strcmp(lower, "utf-32") == 0) |
| 3027 | return PyUnicode_DecodeUTF32(s, size, errors, 0); |
| 3028 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3029 | |
| 3030 | /* Decode via the codec registry */ |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 3031 | buffer = NULL; |
Antoine Pitrou | c3b3924 | 2009-01-03 16:59:18 +0000 | [diff] [blame] | 3032 | 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] | 3033 | goto onError; |
Antoine Pitrou | ee58fa4 | 2008-08-19 18:22:14 +0000 | [diff] [blame] | 3034 | buffer = PyMemoryView_FromBuffer(&info); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3035 | if (buffer == NULL) |
| 3036 | goto onError; |
Nick Coghlan | c72e4e6 | 2013-11-22 22:39:36 +1000 | [diff] [blame] | 3037 | unicode = _PyCodec_DecodeText(buffer, encoding, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3038 | if (unicode == NULL) |
| 3039 | goto onError; |
| 3040 | if (!PyUnicode_Check(unicode)) { |
| 3041 | PyErr_Format(PyExc_TypeError, |
Nick Coghlan | 8b097b4 | 2013-11-13 23:49:21 +1000 | [diff] [blame] | 3042 | "'%.400s' decoder returned '%.400s' instead of 'str'; " |
| 3043 | "use codecs.decode() to decode to arbitrary types", |
| 3044 | encoding, |
| 3045 | Py_TYPE(unicode)->tp_name, Py_TYPE(unicode)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3046 | Py_DECREF(unicode); |
| 3047 | goto onError; |
| 3048 | } |
| 3049 | Py_DECREF(buffer); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 3050 | return unicode_result(unicode); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3051 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3052 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3053 | Py_XDECREF(buffer); |
| 3054 | return NULL; |
| 3055 | } |
| 3056 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3057 | PyObject * |
| 3058 | PyUnicode_AsDecodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3059 | const char *encoding, |
| 3060 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3061 | { |
| 3062 | PyObject *v; |
| 3063 | |
| 3064 | if (!PyUnicode_Check(unicode)) { |
| 3065 | PyErr_BadArgument(); |
| 3066 | goto onError; |
| 3067 | } |
| 3068 | |
| 3069 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3070 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3071 | |
| 3072 | /* Decode via the codec registry */ |
| 3073 | v = PyCodec_Decode(unicode, encoding, errors); |
| 3074 | if (v == NULL) |
| 3075 | goto onError; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 3076 | return unicode_result(v); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3077 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3078 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3079 | return NULL; |
| 3080 | } |
| 3081 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3082 | PyObject * |
| 3083 | PyUnicode_AsDecodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3084 | const char *encoding, |
| 3085 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3086 | { |
| 3087 | PyObject *v; |
| 3088 | |
| 3089 | if (!PyUnicode_Check(unicode)) { |
| 3090 | PyErr_BadArgument(); |
| 3091 | goto onError; |
| 3092 | } |
| 3093 | |
| 3094 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3095 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3096 | |
| 3097 | /* Decode via the codec registry */ |
| 3098 | v = PyCodec_Decode(unicode, encoding, errors); |
| 3099 | if (v == NULL) |
| 3100 | goto onError; |
| 3101 | if (!PyUnicode_Check(v)) { |
| 3102 | PyErr_Format(PyExc_TypeError, |
Nick Coghlan | 8b097b4 | 2013-11-13 23:49:21 +1000 | [diff] [blame] | 3103 | "'%.400s' decoder returned '%.400s' instead of 'str'; " |
| 3104 | "use codecs.decode() to decode to arbitrary types", |
| 3105 | encoding, |
| 3106 | Py_TYPE(unicode)->tp_name, Py_TYPE(unicode)->tp_name); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3107 | Py_DECREF(v); |
| 3108 | goto onError; |
| 3109 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 3110 | return unicode_result(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3111 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3112 | onError: |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3113 | return NULL; |
| 3114 | } |
| 3115 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3116 | PyObject * |
| 3117 | PyUnicode_Encode(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3118 | Py_ssize_t size, |
| 3119 | const char *encoding, |
| 3120 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3121 | { |
| 3122 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3123 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3124 | unicode = PyUnicode_FromUnicode(s, size); |
| 3125 | if (unicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3126 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3127 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 3128 | Py_DECREF(unicode); |
| 3129 | return v; |
| 3130 | } |
| 3131 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3132 | PyObject * |
| 3133 | PyUnicode_AsEncodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3134 | const char *encoding, |
| 3135 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3136 | { |
| 3137 | PyObject *v; |
| 3138 | |
| 3139 | if (!PyUnicode_Check(unicode)) { |
| 3140 | PyErr_BadArgument(); |
| 3141 | goto onError; |
| 3142 | } |
| 3143 | |
| 3144 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3145 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3146 | |
| 3147 | /* Encode via the codec registry */ |
| 3148 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3149 | if (v == NULL) |
| 3150 | goto onError; |
| 3151 | return v; |
| 3152 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3153 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3154 | return NULL; |
| 3155 | } |
| 3156 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3157 | static size_t |
| 3158 | wcstombs_errorpos(const wchar_t *wstr) |
| 3159 | { |
| 3160 | size_t len; |
| 3161 | #if SIZEOF_WCHAR_T == 2 |
| 3162 | wchar_t buf[3]; |
| 3163 | #else |
| 3164 | wchar_t buf[2]; |
| 3165 | #endif |
| 3166 | char outbuf[MB_LEN_MAX]; |
| 3167 | const wchar_t *start, *previous; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3168 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3169 | #if SIZEOF_WCHAR_T == 2 |
| 3170 | buf[2] = 0; |
| 3171 | #else |
| 3172 | buf[1] = 0; |
| 3173 | #endif |
| 3174 | start = wstr; |
| 3175 | while (*wstr != L'\0') |
| 3176 | { |
| 3177 | previous = wstr; |
| 3178 | #if SIZEOF_WCHAR_T == 2 |
| 3179 | if (Py_UNICODE_IS_HIGH_SURROGATE(wstr[0]) |
| 3180 | && Py_UNICODE_IS_LOW_SURROGATE(wstr[1])) |
| 3181 | { |
| 3182 | buf[0] = wstr[0]; |
| 3183 | buf[1] = wstr[1]; |
| 3184 | wstr += 2; |
| 3185 | } |
| 3186 | else { |
| 3187 | buf[0] = *wstr; |
| 3188 | buf[1] = 0; |
| 3189 | wstr++; |
| 3190 | } |
| 3191 | #else |
| 3192 | buf[0] = *wstr; |
| 3193 | wstr++; |
| 3194 | #endif |
| 3195 | len = wcstombs(outbuf, buf, sizeof(outbuf)); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3196 | if (len == (size_t)-1) |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3197 | return previous - start; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3198 | } |
| 3199 | |
| 3200 | /* failed to find the unencodable character */ |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3201 | return 0; |
| 3202 | } |
| 3203 | |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3204 | static int |
| 3205 | locale_error_handler(const char *errors, int *surrogateescape) |
| 3206 | { |
| 3207 | if (errors == NULL) { |
| 3208 | *surrogateescape = 0; |
| 3209 | return 0; |
| 3210 | } |
| 3211 | |
| 3212 | if (strcmp(errors, "strict") == 0) { |
| 3213 | *surrogateescape = 0; |
| 3214 | return 0; |
| 3215 | } |
Victor Stinner | 8dbd421 | 2012-12-04 09:30:24 +0100 | [diff] [blame] | 3216 | if (strcmp(errors, "surrogateescape") == 0) { |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3217 | *surrogateescape = 1; |
| 3218 | return 0; |
| 3219 | } |
| 3220 | PyErr_Format(PyExc_ValueError, |
| 3221 | "only 'strict' and 'surrogateescape' error handlers " |
| 3222 | "are supported, not '%s'", |
| 3223 | errors); |
| 3224 | return -1; |
| 3225 | } |
| 3226 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3227 | PyObject * |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3228 | PyUnicode_EncodeLocale(PyObject *unicode, const char *errors) |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3229 | { |
| 3230 | Py_ssize_t wlen, wlen2; |
| 3231 | wchar_t *wstr; |
| 3232 | PyObject *bytes = NULL; |
| 3233 | char *errmsg; |
Raymond Hettinger | e56666d | 2013-08-04 11:51:03 -0700 | [diff] [blame] | 3234 | PyObject *reason = NULL; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3235 | PyObject *exc; |
| 3236 | size_t error_pos; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3237 | int surrogateescape; |
| 3238 | |
| 3239 | if (locale_error_handler(errors, &surrogateescape) < 0) |
| 3240 | return NULL; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3241 | |
| 3242 | wstr = PyUnicode_AsWideCharString(unicode, &wlen); |
| 3243 | if (wstr == NULL) |
| 3244 | return NULL; |
| 3245 | |
| 3246 | wlen2 = wcslen(wstr); |
| 3247 | if (wlen2 != wlen) { |
| 3248 | PyMem_Free(wstr); |
| 3249 | PyErr_SetString(PyExc_TypeError, "embedded null character"); |
| 3250 | return NULL; |
| 3251 | } |
| 3252 | |
| 3253 | if (surrogateescape) { |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3254 | /* "surrogateescape" error handler */ |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3255 | char *str; |
| 3256 | |
| 3257 | str = _Py_wchar2char(wstr, &error_pos); |
| 3258 | if (str == NULL) { |
| 3259 | if (error_pos == (size_t)-1) { |
| 3260 | PyErr_NoMemory(); |
| 3261 | PyMem_Free(wstr); |
| 3262 | return NULL; |
| 3263 | } |
| 3264 | else { |
| 3265 | goto encode_error; |
| 3266 | } |
| 3267 | } |
| 3268 | PyMem_Free(wstr); |
| 3269 | |
| 3270 | bytes = PyBytes_FromString(str); |
| 3271 | PyMem_Free(str); |
| 3272 | } |
| 3273 | else { |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3274 | /* strict mode */ |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3275 | size_t len, len2; |
| 3276 | |
| 3277 | len = wcstombs(NULL, wstr, 0); |
| 3278 | if (len == (size_t)-1) { |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3279 | error_pos = (size_t)-1; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3280 | goto encode_error; |
| 3281 | } |
| 3282 | |
| 3283 | bytes = PyBytes_FromStringAndSize(NULL, len); |
| 3284 | if (bytes == NULL) { |
| 3285 | PyMem_Free(wstr); |
| 3286 | return NULL; |
| 3287 | } |
| 3288 | |
| 3289 | len2 = wcstombs(PyBytes_AS_STRING(bytes), wstr, len+1); |
| 3290 | if (len2 == (size_t)-1 || len2 > len) { |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3291 | error_pos = (size_t)-1; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3292 | goto encode_error; |
| 3293 | } |
| 3294 | PyMem_Free(wstr); |
| 3295 | } |
| 3296 | return bytes; |
| 3297 | |
| 3298 | encode_error: |
| 3299 | errmsg = strerror(errno); |
| 3300 | assert(errmsg != NULL); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3301 | |
| 3302 | if (error_pos == (size_t)-1) |
| 3303 | error_pos = wcstombs_errorpos(wstr); |
| 3304 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3305 | PyMem_Free(wstr); |
| 3306 | Py_XDECREF(bytes); |
| 3307 | |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3308 | if (errmsg != NULL) { |
| 3309 | size_t errlen; |
| 3310 | wstr = _Py_char2wchar(errmsg, &errlen); |
| 3311 | if (wstr != NULL) { |
| 3312 | reason = PyUnicode_FromWideChar(wstr, errlen); |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 3313 | PyMem_RawFree(wstr); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3314 | } else |
| 3315 | errmsg = NULL; |
| 3316 | } |
| 3317 | if (errmsg == NULL) |
Victor Stinner | 1f33f2b | 2011-12-17 04:45:09 +0100 | [diff] [blame] | 3318 | reason = PyUnicode_FromString( |
| 3319 | "wcstombs() encountered an unencodable " |
| 3320 | "wide character"); |
| 3321 | if (reason == NULL) |
| 3322 | return NULL; |
| 3323 | |
| 3324 | exc = PyObject_CallFunction(PyExc_UnicodeEncodeError, "sOnnO", |
| 3325 | "locale", unicode, |
| 3326 | (Py_ssize_t)error_pos, |
| 3327 | (Py_ssize_t)(error_pos+1), |
| 3328 | reason); |
| 3329 | Py_DECREF(reason); |
| 3330 | if (exc != NULL) { |
| 3331 | PyCodec_StrictErrors(exc); |
| 3332 | Py_XDECREF(exc); |
| 3333 | } |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3334 | return NULL; |
| 3335 | } |
| 3336 | |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3337 | PyObject * |
| 3338 | PyUnicode_EncodeFSDefault(PyObject *unicode) |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3339 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3340 | #ifdef HAVE_MBCS |
Victor Stinner | ac931b1 | 2011-11-20 18:27:03 +0100 | [diff] [blame] | 3341 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, NULL); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3342 | #elif defined(__APPLE__) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3343 | return _PyUnicode_AsUTF8String(unicode, "surrogateescape"); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3344 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 3345 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 3346 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 3347 | cannot use it to encode and decode filenames before it is loaded. Load |
| 3348 | the Python codec requires to encode at least its own filename. Use the C |
| 3349 | version of the locale codec until the codec registry is initialized and |
| 3350 | the Python codec is loaded. |
| 3351 | |
| 3352 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 3353 | cannot only rely on it: check also interp->fscodec_initialized for |
| 3354 | subinterpreters. */ |
| 3355 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3356 | return PyUnicode_AsEncodedString(unicode, |
| 3357 | Py_FileSystemDefaultEncoding, |
| 3358 | "surrogateescape"); |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 3359 | } |
| 3360 | else { |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3361 | return PyUnicode_EncodeLocale(unicode, "surrogateescape"); |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 3362 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3363 | #endif |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3364 | } |
| 3365 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3366 | PyObject * |
| 3367 | PyUnicode_AsEncodedString(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3368 | const char *encoding, |
| 3369 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3370 | { |
| 3371 | PyObject *v; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3372 | char lower[11]; /* Enough for any encoding shortcut */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3373 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3374 | if (!PyUnicode_Check(unicode)) { |
| 3375 | PyErr_BadArgument(); |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3376 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3377 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3378 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3379 | /* Shortcuts for common default encodings */ |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3380 | if (_Py_normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3381 | if ((strcmp(lower, "utf-8") == 0) || |
| 3382 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3383 | { |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3384 | if (errors == NULL || strcmp(errors, "strict") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3385 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3386 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3387 | return _PyUnicode_AsUTF8String(unicode, errors); |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3388 | } |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3389 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3390 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | fa3ba4c | 2013-10-29 11:34:05 +0100 | [diff] [blame] | 3391 | (strcmp(lower, "iso-8859-1") == 0) || |
| 3392 | (strcmp(lower, "iso8859-1") == 0)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3393 | return _PyUnicode_AsLatin1String(unicode, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3394 | #ifdef HAVE_MBCS |
Victor Stinner | ac931b1 | 2011-11-20 18:27:03 +0100 | [diff] [blame] | 3395 | else if (strcmp(lower, "mbcs") == 0) |
| 3396 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 3397 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3398 | else if (strcmp(lower, "ascii") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3399 | return _PyUnicode_AsASCIIString(unicode, errors); |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3400 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3401 | |
| 3402 | /* Encode via the codec registry */ |
Nick Coghlan | c72e4e6 | 2013-11-22 22:39:36 +1000 | [diff] [blame] | 3403 | v = _PyCodec_EncodeText(unicode, encoding, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3404 | if (v == NULL) |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3405 | return NULL; |
| 3406 | |
| 3407 | /* The normal path */ |
| 3408 | if (PyBytes_Check(v)) |
| 3409 | return v; |
| 3410 | |
| 3411 | /* 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] | 3412 | if (PyByteArray_Check(v)) { |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3413 | int error; |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3414 | PyObject *b; |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3415 | |
| 3416 | error = PyErr_WarnFormat(PyExc_RuntimeWarning, 1, |
Nick Coghlan | 8b097b4 | 2013-11-13 23:49:21 +1000 | [diff] [blame] | 3417 | "encoder %s returned bytearray instead of bytes; " |
| 3418 | "use codecs.encode() to encode to arbitrary types", |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3419 | encoding); |
| 3420 | if (error) { |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3421 | Py_DECREF(v); |
| 3422 | return NULL; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3423 | } |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3424 | |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3425 | b = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(v), Py_SIZE(v)); |
| 3426 | Py_DECREF(v); |
| 3427 | return b; |
| 3428 | } |
| 3429 | |
| 3430 | PyErr_Format(PyExc_TypeError, |
Nick Coghlan | 8b097b4 | 2013-11-13 23:49:21 +1000 | [diff] [blame] | 3431 | "'%.400s' encoder returned '%.400s' instead of 'bytes'; " |
| 3432 | "use codecs.encode() to encode to arbitrary types", |
| 3433 | encoding, |
| 3434 | Py_TYPE(v)->tp_name, Py_TYPE(v)->tp_name); |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3435 | Py_DECREF(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3436 | return NULL; |
| 3437 | } |
| 3438 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3439 | PyObject * |
| 3440 | PyUnicode_AsEncodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3441 | const char *encoding, |
| 3442 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3443 | { |
| 3444 | PyObject *v; |
| 3445 | |
| 3446 | if (!PyUnicode_Check(unicode)) { |
| 3447 | PyErr_BadArgument(); |
| 3448 | goto onError; |
| 3449 | } |
| 3450 | |
| 3451 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3452 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3453 | |
| 3454 | /* Encode via the codec registry */ |
| 3455 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3456 | if (v == NULL) |
| 3457 | goto onError; |
| 3458 | if (!PyUnicode_Check(v)) { |
| 3459 | PyErr_Format(PyExc_TypeError, |
Nick Coghlan | 8b097b4 | 2013-11-13 23:49:21 +1000 | [diff] [blame] | 3460 | "'%.400s' encoder returned '%.400s' instead of 'str'; " |
| 3461 | "use codecs.encode() to encode to arbitrary types", |
| 3462 | encoding, |
| 3463 | Py_TYPE(v)->tp_name, Py_TYPE(v)->tp_name); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3464 | Py_DECREF(v); |
| 3465 | goto onError; |
| 3466 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3467 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3468 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3469 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3470 | return NULL; |
| 3471 | } |
| 3472 | |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3473 | static size_t |
| 3474 | mbstowcs_errorpos(const char *str, size_t len) |
| 3475 | { |
| 3476 | #ifdef HAVE_MBRTOWC |
| 3477 | const char *start = str; |
| 3478 | mbstate_t mbs; |
| 3479 | size_t converted; |
| 3480 | wchar_t ch; |
| 3481 | |
| 3482 | memset(&mbs, 0, sizeof mbs); |
| 3483 | while (len) |
| 3484 | { |
| 3485 | converted = mbrtowc(&ch, (char*)str, len, &mbs); |
| 3486 | if (converted == 0) |
| 3487 | /* Reached end of string */ |
| 3488 | break; |
| 3489 | if (converted == (size_t)-1 || converted == (size_t)-2) { |
| 3490 | /* Conversion error or incomplete character */ |
| 3491 | return str - start; |
| 3492 | } |
| 3493 | else { |
| 3494 | str += converted; |
| 3495 | len -= converted; |
| 3496 | } |
| 3497 | } |
| 3498 | /* failed to find the undecodable byte sequence */ |
| 3499 | return 0; |
| 3500 | #endif |
| 3501 | return 0; |
| 3502 | } |
| 3503 | |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3504 | PyObject* |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3505 | PyUnicode_DecodeLocaleAndSize(const char *str, Py_ssize_t len, |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3506 | const char *errors) |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3507 | { |
| 3508 | wchar_t smallbuf[256]; |
| 3509 | size_t smallbuf_len = Py_ARRAY_LENGTH(smallbuf); |
| 3510 | wchar_t *wstr; |
| 3511 | size_t wlen, wlen2; |
| 3512 | PyObject *unicode; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3513 | int surrogateescape; |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3514 | size_t error_pos; |
| 3515 | char *errmsg; |
| 3516 | PyObject *reason, *exc; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3517 | |
| 3518 | if (locale_error_handler(errors, &surrogateescape) < 0) |
| 3519 | return NULL; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3520 | |
| 3521 | if (str[len] != '\0' || len != strlen(str)) { |
| 3522 | PyErr_SetString(PyExc_TypeError, "embedded null character"); |
| 3523 | return NULL; |
| 3524 | } |
| 3525 | |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3526 | if (surrogateescape) { |
| 3527 | /* "surrogateescape" error handler */ |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3528 | wstr = _Py_char2wchar(str, &wlen); |
| 3529 | if (wstr == NULL) { |
| 3530 | if (wlen == (size_t)-1) |
| 3531 | PyErr_NoMemory(); |
| 3532 | else |
| 3533 | PyErr_SetFromErrno(PyExc_OSError); |
| 3534 | return NULL; |
| 3535 | } |
| 3536 | |
| 3537 | unicode = PyUnicode_FromWideChar(wstr, wlen); |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 3538 | PyMem_RawFree(wstr); |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3539 | } |
| 3540 | else { |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3541 | /* strict mode */ |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3542 | #ifndef HAVE_BROKEN_MBSTOWCS |
| 3543 | wlen = mbstowcs(NULL, str, 0); |
| 3544 | #else |
| 3545 | wlen = len; |
| 3546 | #endif |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3547 | if (wlen == (size_t)-1) |
| 3548 | goto decode_error; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3549 | if (wlen+1 <= smallbuf_len) { |
| 3550 | wstr = smallbuf; |
| 3551 | } |
| 3552 | else { |
| 3553 | if (wlen > PY_SSIZE_T_MAX / sizeof(wchar_t) - 1) |
| 3554 | return PyErr_NoMemory(); |
| 3555 | |
| 3556 | wstr = PyMem_Malloc((wlen+1) * sizeof(wchar_t)); |
| 3557 | if (!wstr) |
| 3558 | return PyErr_NoMemory(); |
| 3559 | } |
| 3560 | |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3561 | wlen2 = mbstowcs(wstr, str, wlen+1); |
| 3562 | if (wlen2 == (size_t)-1) { |
| 3563 | if (wstr != smallbuf) |
| 3564 | PyMem_Free(wstr); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3565 | goto decode_error; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3566 | } |
| 3567 | #ifdef HAVE_BROKEN_MBSTOWCS |
| 3568 | assert(wlen2 == wlen); |
| 3569 | #endif |
| 3570 | unicode = PyUnicode_FromWideChar(wstr, wlen2); |
| 3571 | if (wstr != smallbuf) |
| 3572 | PyMem_Free(wstr); |
| 3573 | } |
| 3574 | return unicode; |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3575 | |
| 3576 | decode_error: |
| 3577 | errmsg = strerror(errno); |
| 3578 | assert(errmsg != NULL); |
| 3579 | |
| 3580 | error_pos = mbstowcs_errorpos(str, len); |
| 3581 | if (errmsg != NULL) { |
| 3582 | size_t errlen; |
| 3583 | wstr = _Py_char2wchar(errmsg, &errlen); |
| 3584 | if (wstr != NULL) { |
| 3585 | reason = PyUnicode_FromWideChar(wstr, errlen); |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 3586 | PyMem_RawFree(wstr); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3587 | } else |
| 3588 | errmsg = NULL; |
| 3589 | } |
| 3590 | if (errmsg == NULL) |
| 3591 | reason = PyUnicode_FromString( |
| 3592 | "mbstowcs() encountered an invalid multibyte sequence"); |
| 3593 | if (reason == NULL) |
| 3594 | return NULL; |
| 3595 | |
| 3596 | exc = PyObject_CallFunction(PyExc_UnicodeDecodeError, "sy#nnO", |
| 3597 | "locale", str, len, |
| 3598 | (Py_ssize_t)error_pos, |
| 3599 | (Py_ssize_t)(error_pos+1), |
| 3600 | reason); |
| 3601 | Py_DECREF(reason); |
| 3602 | if (exc != NULL) { |
| 3603 | PyCodec_StrictErrors(exc); |
| 3604 | Py_XDECREF(exc); |
| 3605 | } |
| 3606 | return NULL; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3607 | } |
| 3608 | |
| 3609 | PyObject* |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3610 | PyUnicode_DecodeLocale(const char *str, const char *errors) |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3611 | { |
| 3612 | Py_ssize_t size = (Py_ssize_t)strlen(str); |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3613 | return PyUnicode_DecodeLocaleAndSize(str, size, errors); |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3614 | } |
| 3615 | |
| 3616 | |
| 3617 | PyObject* |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3618 | PyUnicode_DecodeFSDefault(const char *s) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3619 | Py_ssize_t size = (Py_ssize_t)strlen(s); |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3620 | return PyUnicode_DecodeFSDefaultAndSize(s, size); |
| 3621 | } |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3622 | |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3623 | PyObject* |
| 3624 | PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size) |
| 3625 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3626 | #ifdef HAVE_MBCS |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3627 | return PyUnicode_DecodeMBCS(s, size, NULL); |
| 3628 | #elif defined(__APPLE__) |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 3629 | return PyUnicode_DecodeUTF8Stateful(s, size, "surrogateescape", NULL); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3630 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 3631 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 3632 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 3633 | cannot use it to encode and decode filenames before it is loaded. Load |
| 3634 | the Python codec requires to encode at least its own filename. Use the C |
| 3635 | version of the locale codec until the codec registry is initialized and |
| 3636 | the Python codec is loaded. |
| 3637 | |
| 3638 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 3639 | cannot only rely on it: check also interp->fscodec_initialized for |
| 3640 | subinterpreters. */ |
| 3641 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3642 | return PyUnicode_Decode(s, size, |
| 3643 | Py_FileSystemDefaultEncoding, |
Victor Stinner | b9a20ad | 2010-04-30 16:37:52 +0000 | [diff] [blame] | 3644 | "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3645 | } |
| 3646 | else { |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3647 | return PyUnicode_DecodeLocaleAndSize(s, size, "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3648 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3649 | #endif |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3650 | } |
| 3651 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3652 | |
| 3653 | int |
Victor Stinner | fe75fb4 | 2012-10-23 02:52:18 +0200 | [diff] [blame] | 3654 | _PyUnicode_HasNULChars(PyObject* str) |
Antoine Pitrou | 1334884 | 2012-01-29 18:36:34 +0100 | [diff] [blame] | 3655 | { |
Victor Stinner | fe75fb4 | 2012-10-23 02:52:18 +0200 | [diff] [blame] | 3656 | Py_ssize_t pos; |
Antoine Pitrou | 1334884 | 2012-01-29 18:36:34 +0100 | [diff] [blame] | 3657 | |
Victor Stinner | fe75fb4 | 2012-10-23 02:52:18 +0200 | [diff] [blame] | 3658 | if (PyUnicode_READY(str) == -1) |
Antoine Pitrou | 1334884 | 2012-01-29 18:36:34 +0100 | [diff] [blame] | 3659 | return -1; |
Victor Stinner | fe75fb4 | 2012-10-23 02:52:18 +0200 | [diff] [blame] | 3660 | pos = findchar(PyUnicode_DATA(str), PyUnicode_KIND(str), |
| 3661 | PyUnicode_GET_LENGTH(str), '\0', 1); |
| 3662 | if (pos == -1) |
| 3663 | return 0; |
| 3664 | else |
| 3665 | return 1; |
Antoine Pitrou | 1334884 | 2012-01-29 18:36:34 +0100 | [diff] [blame] | 3666 | } |
| 3667 | |
Antoine Pitrou | 1334884 | 2012-01-29 18:36:34 +0100 | [diff] [blame] | 3668 | int |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3669 | PyUnicode_FSConverter(PyObject* arg, void* addr) |
| 3670 | { |
| 3671 | PyObject *output = NULL; |
| 3672 | Py_ssize_t size; |
| 3673 | void *data; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3674 | if (arg == NULL) { |
| 3675 | Py_DECREF(*(PyObject**)addr); |
| 3676 | return 1; |
| 3677 | } |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 3678 | if (PyBytes_Check(arg)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3679 | output = arg; |
| 3680 | Py_INCREF(output); |
| 3681 | } |
| 3682 | else { |
| 3683 | arg = PyUnicode_FromObject(arg); |
| 3684 | if (!arg) |
| 3685 | return 0; |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3686 | output = PyUnicode_EncodeFSDefault(arg); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3687 | Py_DECREF(arg); |
| 3688 | if (!output) |
| 3689 | return 0; |
| 3690 | if (!PyBytes_Check(output)) { |
| 3691 | Py_DECREF(output); |
| 3692 | PyErr_SetString(PyExc_TypeError, "encoder failed to return bytes"); |
| 3693 | return 0; |
| 3694 | } |
| 3695 | } |
Victor Stinner | 0ea2a46 | 2010-04-30 00:22:08 +0000 | [diff] [blame] | 3696 | size = PyBytes_GET_SIZE(output); |
| 3697 | data = PyBytes_AS_STRING(output); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3698 | if (size != strlen(data)) { |
Benjamin Peterson | 7a6b44a | 2011-08-18 13:51:47 -0500 | [diff] [blame] | 3699 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3700 | Py_DECREF(output); |
| 3701 | return 0; |
| 3702 | } |
| 3703 | *(PyObject**)addr = output; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3704 | return Py_CLEANUP_SUPPORTED; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3705 | } |
| 3706 | |
| 3707 | |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3708 | int |
| 3709 | PyUnicode_FSDecoder(PyObject* arg, void* addr) |
| 3710 | { |
| 3711 | PyObject *output = NULL; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3712 | if (arg == NULL) { |
| 3713 | Py_DECREF(*(PyObject**)addr); |
| 3714 | return 1; |
| 3715 | } |
| 3716 | if (PyUnicode_Check(arg)) { |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 3717 | if (PyUnicode_READY(arg) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3718 | return 0; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3719 | output = arg; |
| 3720 | Py_INCREF(output); |
| 3721 | } |
| 3722 | else { |
| 3723 | arg = PyBytes_FromObject(arg); |
| 3724 | if (!arg) |
| 3725 | return 0; |
| 3726 | output = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AS_STRING(arg), |
| 3727 | PyBytes_GET_SIZE(arg)); |
| 3728 | Py_DECREF(arg); |
| 3729 | if (!output) |
| 3730 | return 0; |
| 3731 | if (!PyUnicode_Check(output)) { |
| 3732 | Py_DECREF(output); |
| 3733 | PyErr_SetString(PyExc_TypeError, "decoder failed to return unicode"); |
| 3734 | return 0; |
| 3735 | } |
| 3736 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 3737 | if (PyUnicode_READY(output) == -1) { |
Victor Stinner | 065836e | 2011-10-27 01:56:33 +0200 | [diff] [blame] | 3738 | Py_DECREF(output); |
| 3739 | return 0; |
| 3740 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3741 | if (findchar(PyUnicode_DATA(output), PyUnicode_KIND(output), |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 3742 | PyUnicode_GET_LENGTH(output), 0, 1) >= 0) { |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3743 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 3744 | Py_DECREF(output); |
| 3745 | return 0; |
| 3746 | } |
| 3747 | *(PyObject**)addr = output; |
| 3748 | return Py_CLEANUP_SUPPORTED; |
| 3749 | } |
| 3750 | |
| 3751 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3752 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3753 | PyUnicode_AsUTF8AndSize(PyObject *unicode, Py_ssize_t *psize) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3754 | { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 3755 | PyObject *bytes; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3756 | |
Neal Norwitz | e0a0a6e | 2007-08-25 01:04:21 +0000 | [diff] [blame] | 3757 | if (!PyUnicode_Check(unicode)) { |
| 3758 | PyErr_BadArgument(); |
| 3759 | return NULL; |
| 3760 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3761 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3762 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3763 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3764 | if (PyUnicode_UTF8(unicode) == NULL) { |
| 3765 | assert(!PyUnicode_IS_COMPACT_ASCII(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3766 | bytes = _PyUnicode_AsUTF8String(unicode, "strict"); |
| 3767 | if (bytes == NULL) |
| 3768 | return NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3769 | _PyUnicode_UTF8(unicode) = PyObject_MALLOC(PyBytes_GET_SIZE(bytes) + 1); |
| 3770 | if (_PyUnicode_UTF8(unicode) == NULL) { |
Victor Stinner | a5afb58 | 2013-10-29 01:28:23 +0100 | [diff] [blame] | 3771 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3772 | Py_DECREF(bytes); |
| 3773 | return NULL; |
| 3774 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3775 | _PyUnicode_UTF8_LENGTH(unicode) = PyBytes_GET_SIZE(bytes); |
| 3776 | Py_MEMCPY(_PyUnicode_UTF8(unicode), |
| 3777 | PyBytes_AS_STRING(bytes), |
| 3778 | _PyUnicode_UTF8_LENGTH(unicode) + 1); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3779 | Py_DECREF(bytes); |
| 3780 | } |
| 3781 | |
| 3782 | if (psize) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3783 | *psize = PyUnicode_UTF8_LENGTH(unicode); |
| 3784 | return PyUnicode_UTF8(unicode); |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3785 | } |
| 3786 | |
| 3787 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3788 | PyUnicode_AsUTF8(PyObject *unicode) |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3789 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3790 | return PyUnicode_AsUTF8AndSize(unicode, NULL); |
| 3791 | } |
| 3792 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3793 | Py_UNICODE * |
| 3794 | PyUnicode_AsUnicodeAndSize(PyObject *unicode, Py_ssize_t *size) |
| 3795 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3796 | const unsigned char *one_byte; |
| 3797 | #if SIZEOF_WCHAR_T == 4 |
| 3798 | const Py_UCS2 *two_bytes; |
| 3799 | #else |
| 3800 | const Py_UCS4 *four_bytes; |
| 3801 | const Py_UCS4 *ucs4_end; |
| 3802 | Py_ssize_t num_surrogates; |
| 3803 | #endif |
| 3804 | wchar_t *w; |
| 3805 | wchar_t *wchar_end; |
| 3806 | |
| 3807 | if (!PyUnicode_Check(unicode)) { |
| 3808 | PyErr_BadArgument(); |
| 3809 | return NULL; |
| 3810 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3811 | if (_PyUnicode_WSTR(unicode) == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3812 | /* Non-ASCII compact unicode object */ |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3813 | assert(_PyUnicode_KIND(unicode) != 0); |
| 3814 | assert(PyUnicode_IS_READY(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3815 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3816 | if (PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3817 | #if SIZEOF_WCHAR_T == 2 |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3818 | four_bytes = PyUnicode_4BYTE_DATA(unicode); |
| 3819 | ucs4_end = four_bytes + _PyUnicode_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3820 | num_surrogates = 0; |
| 3821 | |
| 3822 | for (; four_bytes < ucs4_end; ++four_bytes) { |
| 3823 | if (*four_bytes > 0xFFFF) |
| 3824 | ++num_surrogates; |
| 3825 | } |
| 3826 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3827 | _PyUnicode_WSTR(unicode) = (wchar_t *) PyObject_MALLOC( |
| 3828 | sizeof(wchar_t) * (_PyUnicode_LENGTH(unicode) + 1 + num_surrogates)); |
| 3829 | if (!_PyUnicode_WSTR(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3830 | PyErr_NoMemory(); |
| 3831 | return NULL; |
| 3832 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3833 | _PyUnicode_WSTR_LENGTH(unicode) = _PyUnicode_LENGTH(unicode) + num_surrogates; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3834 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3835 | w = _PyUnicode_WSTR(unicode); |
| 3836 | wchar_end = w + _PyUnicode_WSTR_LENGTH(unicode); |
| 3837 | four_bytes = PyUnicode_4BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3838 | for (; four_bytes < ucs4_end; ++four_bytes, ++w) { |
| 3839 | if (*four_bytes > 0xFFFF) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 3840 | assert(*four_bytes <= MAX_UNICODE); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3841 | /* encode surrogate pair in this case */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 3842 | *w++ = Py_UNICODE_HIGH_SURROGATE(*four_bytes); |
| 3843 | *w = Py_UNICODE_LOW_SURROGATE(*four_bytes); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3844 | } |
| 3845 | else |
| 3846 | *w = *four_bytes; |
| 3847 | |
| 3848 | if (w > wchar_end) { |
| 3849 | assert(0 && "Miscalculated string end"); |
| 3850 | } |
| 3851 | } |
| 3852 | *w = 0; |
| 3853 | #else |
| 3854 | /* sizeof(wchar_t) == 4 */ |
| 3855 | Py_FatalError("Impossible unicode object state, wstr and str " |
| 3856 | "should share memory already."); |
| 3857 | return NULL; |
| 3858 | #endif |
| 3859 | } |
| 3860 | else { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3861 | _PyUnicode_WSTR(unicode) = (wchar_t *) PyObject_MALLOC(sizeof(wchar_t) * |
| 3862 | (_PyUnicode_LENGTH(unicode) + 1)); |
| 3863 | if (!_PyUnicode_WSTR(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3864 | PyErr_NoMemory(); |
| 3865 | return NULL; |
| 3866 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3867 | if (!PyUnicode_IS_COMPACT_ASCII(unicode)) |
| 3868 | _PyUnicode_WSTR_LENGTH(unicode) = _PyUnicode_LENGTH(unicode); |
| 3869 | w = _PyUnicode_WSTR(unicode); |
| 3870 | wchar_end = w + _PyUnicode_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3871 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3872 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) { |
| 3873 | one_byte = PyUnicode_1BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3874 | for (; w < wchar_end; ++one_byte, ++w) |
| 3875 | *w = *one_byte; |
| 3876 | /* null-terminate the wstr */ |
| 3877 | *w = 0; |
| 3878 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3879 | else if (PyUnicode_KIND(unicode) == PyUnicode_2BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3880 | #if SIZEOF_WCHAR_T == 4 |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3881 | two_bytes = PyUnicode_2BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3882 | for (; w < wchar_end; ++two_bytes, ++w) |
| 3883 | *w = *two_bytes; |
| 3884 | /* null-terminate the wstr */ |
| 3885 | *w = 0; |
| 3886 | #else |
| 3887 | /* sizeof(wchar_t) == 2 */ |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3888 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 3889 | _PyUnicode_WSTR(unicode) = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3890 | Py_FatalError("Impossible unicode object state, wstr " |
| 3891 | "and str should share memory already."); |
| 3892 | return NULL; |
| 3893 | #endif |
| 3894 | } |
| 3895 | else { |
| 3896 | assert(0 && "This should never happen."); |
| 3897 | } |
| 3898 | } |
| 3899 | } |
| 3900 | if (size != NULL) |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3901 | *size = PyUnicode_WSTR_LENGTH(unicode); |
| 3902 | return _PyUnicode_WSTR(unicode); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3903 | } |
| 3904 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3905 | Py_UNICODE * |
| 3906 | PyUnicode_AsUnicode(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3907 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3908 | return PyUnicode_AsUnicodeAndSize(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3909 | } |
| 3910 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3911 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3912 | Py_ssize_t |
| 3913 | PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3914 | { |
| 3915 | if (!PyUnicode_Check(unicode)) { |
| 3916 | PyErr_BadArgument(); |
| 3917 | goto onError; |
| 3918 | } |
| 3919 | return PyUnicode_GET_SIZE(unicode); |
| 3920 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3921 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3922 | return -1; |
| 3923 | } |
| 3924 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3925 | Py_ssize_t |
| 3926 | PyUnicode_GetLength(PyObject *unicode) |
| 3927 | { |
Victor Stinner | 0762133 | 2012-06-16 04:53:46 +0200 | [diff] [blame] | 3928 | if (!PyUnicode_Check(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3929 | PyErr_BadArgument(); |
| 3930 | return -1; |
| 3931 | } |
Victor Stinner | 0762133 | 2012-06-16 04:53:46 +0200 | [diff] [blame] | 3932 | if (PyUnicode_READY(unicode) == -1) |
| 3933 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3934 | return PyUnicode_GET_LENGTH(unicode); |
| 3935 | } |
| 3936 | |
| 3937 | Py_UCS4 |
| 3938 | PyUnicode_ReadChar(PyObject *unicode, Py_ssize_t index) |
| 3939 | { |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 3940 | void *data; |
| 3941 | int kind; |
| 3942 | |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 3943 | if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) { |
| 3944 | PyErr_BadArgument(); |
| 3945 | return (Py_UCS4)-1; |
| 3946 | } |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 3947 | if (index < 0 || index >= PyUnicode_GET_LENGTH(unicode)) { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 3948 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3949 | return (Py_UCS4)-1; |
| 3950 | } |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 3951 | data = PyUnicode_DATA(unicode); |
| 3952 | kind = PyUnicode_KIND(unicode); |
| 3953 | return PyUnicode_READ(kind, data, index); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3954 | } |
| 3955 | |
| 3956 | int |
| 3957 | PyUnicode_WriteChar(PyObject *unicode, Py_ssize_t index, Py_UCS4 ch) |
| 3958 | { |
| 3959 | if (!PyUnicode_Check(unicode) || !PyUnicode_IS_COMPACT(unicode)) { |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3960 | PyErr_BadArgument(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3961 | return -1; |
| 3962 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 3963 | assert(PyUnicode_IS_READY(unicode)); |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 3964 | if (index < 0 || index >= PyUnicode_GET_LENGTH(unicode)) { |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3965 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 3966 | return -1; |
| 3967 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 3968 | if (unicode_check_modifiable(unicode)) |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3969 | return -1; |
Victor Stinner | c9590ad | 2012-03-04 01:34:37 +0100 | [diff] [blame] | 3970 | if (ch > PyUnicode_MAX_CHAR_VALUE(unicode)) { |
| 3971 | PyErr_SetString(PyExc_ValueError, "character out of range"); |
| 3972 | return -1; |
| 3973 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3974 | PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), |
| 3975 | index, ch); |
| 3976 | return 0; |
| 3977 | } |
| 3978 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3979 | const char * |
| 3980 | PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3981 | { |
Victor Stinner | 42cb462 | 2010-09-01 19:39:01 +0000 | [diff] [blame] | 3982 | return "utf-8"; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3983 | } |
| 3984 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 3985 | /* create or adjust a UnicodeDecodeError */ |
| 3986 | static void |
| 3987 | make_decode_exception(PyObject **exceptionObject, |
| 3988 | const char *encoding, |
| 3989 | const char *input, Py_ssize_t length, |
| 3990 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 3991 | const char *reason) |
| 3992 | { |
| 3993 | if (*exceptionObject == NULL) { |
| 3994 | *exceptionObject = PyUnicodeDecodeError_Create( |
| 3995 | encoding, input, length, startpos, endpos, reason); |
| 3996 | } |
| 3997 | else { |
| 3998 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, startpos)) |
| 3999 | goto onError; |
| 4000 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, endpos)) |
| 4001 | goto onError; |
| 4002 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 4003 | goto onError; |
| 4004 | } |
| 4005 | return; |
| 4006 | |
| 4007 | onError: |
| 4008 | Py_DECREF(*exceptionObject); |
| 4009 | *exceptionObject = NULL; |
| 4010 | } |
| 4011 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4012 | #ifdef HAVE_MBCS |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4013 | /* error handling callback helper: |
| 4014 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 4015 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4016 | and adjust various state variables. |
| 4017 | return 0 on success, -1 on error |
| 4018 | */ |
| 4019 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4020 | static int |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4021 | unicode_decode_call_errorhandler_wchar( |
| 4022 | const char *errors, PyObject **errorHandler, |
| 4023 | const char *encoding, const char *reason, |
| 4024 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 4025 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
| 4026 | PyObject **output, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4027 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 4028 | 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] | 4029 | |
| 4030 | PyObject *restuple = NULL; |
| 4031 | PyObject *repunicode = NULL; |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 4032 | Py_ssize_t outsize; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4033 | Py_ssize_t insize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4034 | Py_ssize_t requiredsize; |
| 4035 | Py_ssize_t newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4036 | PyObject *inputobj = NULL; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4037 | wchar_t *repwstr; |
| 4038 | Py_ssize_t repwlen; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4039 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4040 | assert (_PyUnicode_KIND(*output) == PyUnicode_WCHAR_KIND); |
| 4041 | outsize = _PyUnicode_WSTR_LENGTH(*output); |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 4042 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4043 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4044 | *errorHandler = PyCodec_LookupError(errors); |
| 4045 | if (*errorHandler == NULL) |
| 4046 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4047 | } |
| 4048 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4049 | make_decode_exception(exceptionObject, |
| 4050 | encoding, |
| 4051 | *input, *inend - *input, |
| 4052 | *startinpos, *endinpos, |
| 4053 | reason); |
| 4054 | if (*exceptionObject == NULL) |
| 4055 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4056 | |
| 4057 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 4058 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4059 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4060 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 4061 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4062 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4063 | } |
| 4064 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4065 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4066 | |
| 4067 | /* Copy back the bytes variables, which might have been modified by the |
| 4068 | callback */ |
| 4069 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 4070 | if (!inputobj) |
| 4071 | goto onError; |
| 4072 | if (!PyBytes_Check(inputobj)) { |
| 4073 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
| 4074 | } |
| 4075 | *input = PyBytes_AS_STRING(inputobj); |
| 4076 | insize = PyBytes_GET_SIZE(inputobj); |
| 4077 | *inend = *input + insize; |
| 4078 | /* we can DECREF safely, as the exception has another reference, |
| 4079 | so the object won't go away. */ |
| 4080 | Py_DECREF(inputobj); |
| 4081 | |
| 4082 | if (newpos<0) |
| 4083 | newpos = insize+newpos; |
| 4084 | if (newpos<0 || newpos>insize) { |
| 4085 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos); |
| 4086 | goto onError; |
| 4087 | } |
| 4088 | |
| 4089 | repwstr = PyUnicode_AsUnicodeAndSize(repunicode, &repwlen); |
| 4090 | if (repwstr == NULL) |
| 4091 | goto onError; |
| 4092 | /* need more space? (at least enough for what we |
| 4093 | have+the replacement+the rest of the string (starting |
| 4094 | at the new input position), so we won't have to check space |
| 4095 | when there are no errors in the rest of the string) */ |
| 4096 | requiredsize = *outpos + repwlen + insize-newpos; |
| 4097 | if (requiredsize > outsize) { |
| 4098 | if (requiredsize < 2*outsize) |
| 4099 | requiredsize = 2*outsize; |
| 4100 | if (unicode_resize(output, requiredsize) < 0) |
| 4101 | goto onError; |
| 4102 | } |
| 4103 | wcsncpy(_PyUnicode_WSTR(*output) + *outpos, repwstr, repwlen); |
| 4104 | *outpos += repwlen; |
| 4105 | |
| 4106 | *endinpos = newpos; |
| 4107 | *inptr = *input + newpos; |
| 4108 | |
| 4109 | /* we made it! */ |
| 4110 | Py_XDECREF(restuple); |
| 4111 | return 0; |
| 4112 | |
| 4113 | onError: |
| 4114 | Py_XDECREF(restuple); |
| 4115 | return -1; |
| 4116 | } |
| 4117 | #endif /* HAVE_MBCS */ |
| 4118 | |
| 4119 | static int |
| 4120 | unicode_decode_call_errorhandler_writer( |
| 4121 | const char *errors, PyObject **errorHandler, |
| 4122 | const char *encoding, const char *reason, |
| 4123 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 4124 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
| 4125 | _PyUnicodeWriter *writer /* PyObject **output, Py_ssize_t *outpos */) |
| 4126 | { |
| 4127 | static char *argparse = "O!n;decoding error handler must return (str, int) tuple"; |
| 4128 | |
| 4129 | PyObject *restuple = NULL; |
| 4130 | PyObject *repunicode = NULL; |
| 4131 | Py_ssize_t insize; |
| 4132 | Py_ssize_t newpos; |
Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 4133 | Py_ssize_t replen; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4134 | PyObject *inputobj = NULL; |
| 4135 | |
| 4136 | if (*errorHandler == NULL) { |
| 4137 | *errorHandler = PyCodec_LookupError(errors); |
| 4138 | if (*errorHandler == NULL) |
| 4139 | goto onError; |
| 4140 | } |
| 4141 | |
| 4142 | make_decode_exception(exceptionObject, |
| 4143 | encoding, |
| 4144 | *input, *inend - *input, |
| 4145 | *startinpos, *endinpos, |
| 4146 | reason); |
| 4147 | if (*exceptionObject == NULL) |
| 4148 | goto onError; |
| 4149 | |
| 4150 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 4151 | if (restuple == NULL) |
| 4152 | goto onError; |
| 4153 | if (!PyTuple_Check(restuple)) { |
| 4154 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
| 4155 | goto onError; |
| 4156 | } |
| 4157 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4158 | goto onError; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4159 | |
| 4160 | /* Copy back the bytes variables, which might have been modified by the |
| 4161 | callback */ |
| 4162 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 4163 | if (!inputobj) |
| 4164 | goto onError; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4165 | if (!PyBytes_Check(inputobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4166 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4167 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4168 | *input = PyBytes_AS_STRING(inputobj); |
| 4169 | insize = PyBytes_GET_SIZE(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4170 | *inend = *input + insize; |
Walter Dörwald | 36f938f | 2007-08-10 10:11:43 +0000 | [diff] [blame] | 4171 | /* we can DECREF safely, as the exception has another reference, |
| 4172 | so the object won't go away. */ |
| 4173 | Py_DECREF(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4174 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4175 | if (newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4176 | newpos = insize+newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4177 | if (newpos<0 || newpos>insize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4178 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos); |
| 4179 | goto onError; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4180 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4181 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 4182 | if (PyUnicode_READY(repunicode) < 0) |
| 4183 | goto onError; |
Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 4184 | replen = PyUnicode_GET_LENGTH(repunicode); |
| 4185 | writer->min_length += replen; |
| 4186 | if (replen > 1) |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 4187 | writer->overallocate = 1; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4188 | if (_PyUnicodeWriter_WriteStr(writer, repunicode) == -1) |
Victor Stinner | 376cfa1 | 2013-04-17 23:58:16 +0200 | [diff] [blame] | 4189 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4190 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4191 | *endinpos = newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4192 | *inptr = *input + newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4193 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4194 | /* we made it! */ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4195 | Py_XDECREF(restuple); |
| 4196 | return 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4197 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4198 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4199 | Py_XDECREF(restuple); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4200 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4201 | } |
| 4202 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4203 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 4204 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4205 | /* See RFC2152 for details. We encode conservatively and decode liberally. */ |
| 4206 | |
| 4207 | /* Three simple macros defining base-64. */ |
| 4208 | |
| 4209 | /* Is c a base-64 character? */ |
| 4210 | |
| 4211 | #define IS_BASE64(c) \ |
| 4212 | (((c) >= 'A' && (c) <= 'Z') || \ |
| 4213 | ((c) >= 'a' && (c) <= 'z') || \ |
| 4214 | ((c) >= '0' && (c) <= '9') || \ |
| 4215 | (c) == '+' || (c) == '/') |
| 4216 | |
| 4217 | /* given that c is a base-64 character, what is its base-64 value? */ |
| 4218 | |
| 4219 | #define FROM_BASE64(c) \ |
| 4220 | (((c) >= 'A' && (c) <= 'Z') ? (c) - 'A' : \ |
| 4221 | ((c) >= 'a' && (c) <= 'z') ? (c) - 'a' + 26 : \ |
| 4222 | ((c) >= '0' && (c) <= '9') ? (c) - '0' + 52 : \ |
| 4223 | (c) == '+' ? 62 : 63) |
| 4224 | |
| 4225 | /* What is the base-64 character of the bottom 6 bits of n? */ |
| 4226 | |
| 4227 | #define TO_BASE64(n) \ |
| 4228 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 4229 | |
| 4230 | /* DECODE_DIRECT: this byte encountered in a UTF-7 string should be |
| 4231 | * decoded as itself. We are permissive on decoding; the only ASCII |
| 4232 | * byte not decoding to itself is the + which begins a base64 |
| 4233 | * string. */ |
| 4234 | |
| 4235 | #define DECODE_DIRECT(c) \ |
| 4236 | ((c) <= 127 && (c) != '+') |
| 4237 | |
| 4238 | /* The UTF-7 encoder treats ASCII characters differently according to |
| 4239 | * whether they are Set D, Set O, Whitespace, or special (i.e. none of |
| 4240 | * the above). See RFC2152. This array identifies these different |
| 4241 | * sets: |
| 4242 | * 0 : "Set D" |
| 4243 | * alphanumeric and '(),-./:? |
| 4244 | * 1 : "Set O" |
| 4245 | * !"#$%&*;<=>@[]^_`{|} |
| 4246 | * 2 : "whitespace" |
| 4247 | * ht nl cr sp |
| 4248 | * 3 : special (must be base64 encoded) |
| 4249 | * everything else (i.e. +\~ and non-printing codes 0-8 11-12 14-31 127) |
| 4250 | */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4251 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4252 | static |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4253 | char utf7_category[128] = { |
| 4254 | /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */ |
| 4255 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 3, 3, 2, 3, 3, |
| 4256 | /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */ |
| 4257 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 4258 | /* sp ! " # $ % & ' ( ) * + , - . / */ |
| 4259 | 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 3, 0, 0, 0, 0, |
| 4260 | /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */ |
| 4261 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, |
| 4262 | /* @ A B C D E F G H I J K L M N O */ |
| 4263 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4264 | /* P Q R S T U V W X Y Z [ \ ] ^ _ */ |
| 4265 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 1, 1, 1, |
| 4266 | /* ` a b c d e f g h i j k l m n o */ |
| 4267 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4268 | /* p q r s t u v w x y z { | } ~ del */ |
| 4269 | 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] | 4270 | }; |
| 4271 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4272 | /* ENCODE_DIRECT: this character should be encoded as itself. The |
| 4273 | * answer depends on whether we are encoding set O as itself, and also |
| 4274 | * on whether we are encoding whitespace as itself. RFC2152 makes it |
| 4275 | * clear that the answers to these questions vary between |
| 4276 | * applications, so this code needs to be flexible. */ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 4277 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4278 | #define ENCODE_DIRECT(c, directO, directWS) \ |
| 4279 | ((c) < 128 && (c) > 0 && \ |
| 4280 | ((utf7_category[(c)] == 0) || \ |
| 4281 | (directWS && (utf7_category[(c)] == 2)) || \ |
| 4282 | (directO && (utf7_category[(c)] == 1)))) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4283 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4284 | PyObject * |
| 4285 | PyUnicode_DecodeUTF7(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4286 | Py_ssize_t size, |
| 4287 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4288 | { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4289 | return PyUnicode_DecodeUTF7Stateful(s, size, errors, NULL); |
| 4290 | } |
| 4291 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4292 | /* The decoder. The only state we preserve is our read position, |
| 4293 | * i.e. how many characters we have consumed. So if we end in the |
| 4294 | * middle of a shift sequence we have to back off the read position |
| 4295 | * and the output to the beginning of the sequence, otherwise we lose |
| 4296 | * all the shift state (seen bits, number of bits seen, high |
| 4297 | * surrogate). */ |
| 4298 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4299 | PyObject * |
| 4300 | PyUnicode_DecodeUTF7Stateful(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4301 | Py_ssize_t size, |
| 4302 | const char *errors, |
| 4303 | Py_ssize_t *consumed) |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4304 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4305 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4306 | Py_ssize_t startinpos; |
| 4307 | Py_ssize_t endinpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4308 | const char *e; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4309 | _PyUnicodeWriter writer; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4310 | const char *errmsg = ""; |
| 4311 | int inShift = 0; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4312 | Py_ssize_t shiftOutStart; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4313 | unsigned int base64bits = 0; |
| 4314 | unsigned long base64buffer = 0; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 4315 | Py_UCS4 surrogate = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4316 | PyObject *errorHandler = NULL; |
| 4317 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4318 | |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4319 | if (size == 0) { |
| 4320 | if (consumed) |
| 4321 | *consumed = 0; |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 4322 | _Py_RETURN_UNICODE_EMPTY(); |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4323 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4324 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4325 | /* Start off assuming it's all ASCII. Widen later as necessary. */ |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 4326 | _PyUnicodeWriter_Init(&writer); |
| 4327 | writer.min_length = size; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4328 | |
| 4329 | shiftOutStart = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4330 | e = s + size; |
| 4331 | |
| 4332 | while (s < e) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4333 | Py_UCS4 ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4334 | restart: |
Antoine Pitrou | 5ffd9e9 | 2008-07-25 18:05:24 +0000 | [diff] [blame] | 4335 | ch = (unsigned char) *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4336 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4337 | if (inShift) { /* in a base-64 section */ |
| 4338 | if (IS_BASE64(ch)) { /* consume a base-64 character */ |
| 4339 | base64buffer = (base64buffer << 6) | FROM_BASE64(ch); |
| 4340 | base64bits += 6; |
| 4341 | s++; |
| 4342 | if (base64bits >= 16) { |
| 4343 | /* we have enough bits for a UTF-16 value */ |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 4344 | Py_UCS4 outCh = (Py_UCS4)(base64buffer >> (base64bits-16)); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4345 | base64bits -= 16; |
| 4346 | base64buffer &= (1 << base64bits) - 1; /* clear high bits */ |
Serhiy Storchaka | 35804e4 | 2013-10-19 20:38:19 +0300 | [diff] [blame] | 4347 | assert(outCh <= 0xffff); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4348 | if (surrogate) { |
| 4349 | /* expecting a second surrogate */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4350 | if (Py_UNICODE_IS_LOW_SURROGATE(outCh)) { |
| 4351 | Py_UCS4 ch2 = Py_UNICODE_JOIN_SURROGATES(surrogate, outCh); |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4352 | if (_PyUnicodeWriter_WriteCharInline(&writer, ch2) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4353 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4354 | surrogate = 0; |
Antoine Pitrou | 5418ee0 | 2011-11-15 01:42:21 +0100 | [diff] [blame] | 4355 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4356 | } |
| 4357 | else { |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4358 | if (_PyUnicodeWriter_WriteCharInline(&writer, surrogate) < 0) |
Antoine Pitrou | 78edf75 | 2011-11-15 01:44:16 +0100 | [diff] [blame] | 4359 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4360 | surrogate = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4361 | } |
| 4362 | } |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4363 | if (Py_UNICODE_IS_HIGH_SURROGATE(outCh)) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4364 | /* first surrogate */ |
| 4365 | surrogate = outCh; |
| 4366 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4367 | else { |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4368 | if (_PyUnicodeWriter_WriteCharInline(&writer, outCh) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4369 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4370 | } |
| 4371 | } |
| 4372 | } |
| 4373 | else { /* now leaving a base-64 section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4374 | inShift = 0; |
| 4375 | s++; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4376 | if (surrogate) { |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4377 | if (_PyUnicodeWriter_WriteCharInline(&writer, surrogate) < 0) |
Antoine Pitrou | 78edf75 | 2011-11-15 01:44:16 +0100 | [diff] [blame] | 4378 | goto onError; |
Antoine Pitrou | 5418ee0 | 2011-11-15 01:42:21 +0100 | [diff] [blame] | 4379 | surrogate = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4380 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4381 | if (base64bits > 0) { /* left-over bits */ |
| 4382 | if (base64bits >= 6) { |
| 4383 | /* We've seen at least one base-64 character */ |
| 4384 | errmsg = "partial character in shift sequence"; |
| 4385 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4386 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4387 | else { |
| 4388 | /* Some bits remain; they should be zero */ |
| 4389 | if (base64buffer != 0) { |
| 4390 | errmsg = "non-zero padding bits in shift sequence"; |
| 4391 | goto utf7Error; |
| 4392 | } |
| 4393 | } |
| 4394 | } |
| 4395 | if (ch != '-') { |
| 4396 | /* '-' is absorbed; other terminating |
| 4397 | characters are preserved */ |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4398 | if (_PyUnicodeWriter_WriteCharInline(&writer, ch) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4399 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4400 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4401 | } |
| 4402 | } |
| 4403 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4404 | startinpos = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4405 | s++; /* consume '+' */ |
| 4406 | if (s < e && *s == '-') { /* '+-' encodes '+' */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4407 | s++; |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4408 | if (_PyUnicodeWriter_WriteCharInline(&writer, '+') < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4409 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4410 | } |
| 4411 | else { /* begin base64-encoded section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4412 | inShift = 1; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4413 | shiftOutStart = writer.pos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4414 | base64bits = 0; |
Serhiy Storchaka | 35804e4 | 2013-10-19 20:38:19 +0300 | [diff] [blame] | 4415 | base64buffer = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4416 | } |
| 4417 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4418 | else if (DECODE_DIRECT(ch)) { /* character decodes as itself */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4419 | s++; |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4420 | if (_PyUnicodeWriter_WriteCharInline(&writer, ch) < 0) |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4421 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4422 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4423 | else { |
| 4424 | startinpos = s-starts; |
| 4425 | s++; |
| 4426 | errmsg = "unexpected special character"; |
| 4427 | goto utf7Error; |
| 4428 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4429 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4430 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4431 | endinpos = s-starts; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4432 | if (unicode_decode_call_errorhandler_writer( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4433 | errors, &errorHandler, |
| 4434 | "utf7", errmsg, |
| 4435 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4436 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4437 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4438 | } |
| 4439 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4440 | /* end of string */ |
| 4441 | |
| 4442 | if (inShift && !consumed) { /* in shift sequence, no more to follow */ |
| 4443 | /* if we're in an inconsistent state, that's an error */ |
| 4444 | if (surrogate || |
| 4445 | (base64bits >= 6) || |
| 4446 | (base64bits > 0 && base64buffer != 0)) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4447 | endinpos = size; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4448 | if (unicode_decode_call_errorhandler_writer( |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4449 | errors, &errorHandler, |
| 4450 | "utf7", "unterminated shift sequence", |
| 4451 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4452 | &writer)) |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4453 | goto onError; |
| 4454 | if (s < e) |
| 4455 | goto restart; |
| 4456 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4457 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4458 | |
| 4459 | /* return state */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4460 | if (consumed) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4461 | if (inShift) { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4462 | *consumed = startinpos; |
Serhiy Storchaka | 6cbf151 | 2014-02-08 14:06:33 +0200 | [diff] [blame] | 4463 | if (writer.pos != shiftOutStart && writer.maxchar > 127) { |
Serhiy Storchaka | 016a3f3 | 2014-02-08 14:01:29 +0200 | [diff] [blame] | 4464 | PyObject *result = PyUnicode_FromKindAndData( |
Serhiy Storchaka | 6cbf151 | 2014-02-08 14:06:33 +0200 | [diff] [blame] | 4465 | writer.kind, writer.data, shiftOutStart); |
| 4466 | Py_XDECREF(errorHandler); |
| 4467 | Py_XDECREF(exc); |
| 4468 | _PyUnicodeWriter_Dealloc(&writer); |
| 4469 | return result; |
Serhiy Storchaka | 016a3f3 | 2014-02-08 14:01:29 +0200 | [diff] [blame] | 4470 | } |
Serhiy Storchaka | 6cbf151 | 2014-02-08 14:06:33 +0200 | [diff] [blame] | 4471 | writer.pos = shiftOutStart; /* back off output */ |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4472 | } |
| 4473 | else { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4474 | *consumed = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4475 | } |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4476 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4477 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4478 | Py_XDECREF(errorHandler); |
| 4479 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4480 | return _PyUnicodeWriter_Finish(&writer); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4481 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4482 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4483 | Py_XDECREF(errorHandler); |
| 4484 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4485 | _PyUnicodeWriter_Dealloc(&writer); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4486 | return NULL; |
| 4487 | } |
| 4488 | |
| 4489 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4490 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4491 | _PyUnicode_EncodeUTF7(PyObject *str, |
| 4492 | int base64SetO, |
| 4493 | int base64WhiteSpace, |
| 4494 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4495 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4496 | int kind; |
| 4497 | void *data; |
| 4498 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4499 | PyObject *v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4500 | int inShift = 0; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4501 | Py_ssize_t i; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4502 | unsigned int base64bits = 0; |
| 4503 | unsigned long base64buffer = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4504 | char * out; |
| 4505 | char * start; |
| 4506 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 4507 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4508 | return NULL; |
| 4509 | kind = PyUnicode_KIND(str); |
| 4510 | data = PyUnicode_DATA(str); |
| 4511 | len = PyUnicode_GET_LENGTH(str); |
| 4512 | |
| 4513 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4514 | return PyBytes_FromStringAndSize(NULL, 0); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4515 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4516 | /* It might be possible to tighten this worst case */ |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 4517 | if (len > PY_SSIZE_T_MAX / 8) |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4518 | return PyErr_NoMemory(); |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 4519 | v = PyBytes_FromStringAndSize(NULL, len * 8); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4520 | if (v == NULL) |
| 4521 | return NULL; |
| 4522 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4523 | start = out = PyBytes_AS_STRING(v); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4524 | for (i = 0; i < len; ++i) { |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 4525 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4526 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4527 | if (inShift) { |
| 4528 | if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 4529 | /* shifting out */ |
| 4530 | if (base64bits) { /* output remaining bits */ |
| 4531 | *out++ = TO_BASE64(base64buffer << (6-base64bits)); |
| 4532 | base64buffer = 0; |
| 4533 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4534 | } |
| 4535 | inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4536 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 4537 | so no '-' is required, except if the character is itself a '-' */ |
| 4538 | if (IS_BASE64(ch) || ch == '-') { |
| 4539 | *out++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4540 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4541 | *out++ = (char) ch; |
| 4542 | } |
| 4543 | else { |
| 4544 | goto encode_char; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4545 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4546 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4547 | else { /* not in a shift sequence */ |
| 4548 | if (ch == '+') { |
| 4549 | *out++ = '+'; |
| 4550 | *out++ = '-'; |
| 4551 | } |
| 4552 | else if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 4553 | *out++ = (char) ch; |
| 4554 | } |
| 4555 | else { |
| 4556 | *out++ = '+'; |
| 4557 | inShift = 1; |
| 4558 | goto encode_char; |
| 4559 | } |
| 4560 | } |
| 4561 | continue; |
| 4562 | encode_char: |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4563 | if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 4564 | assert(ch <= MAX_UNICODE); |
Victor Stinner | 0d3721d | 2011-11-22 03:27:53 +0100 | [diff] [blame] | 4565 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4566 | /* code first surrogate */ |
| 4567 | base64bits += 16; |
Victor Stinner | 76df43d | 2012-10-30 01:42:39 +0100 | [diff] [blame] | 4568 | base64buffer = (base64buffer << 16) | Py_UNICODE_HIGH_SURROGATE(ch); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4569 | while (base64bits >= 6) { |
| 4570 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 4571 | base64bits -= 6; |
| 4572 | } |
| 4573 | /* prepare second surrogate */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4574 | ch = Py_UNICODE_LOW_SURROGATE(ch); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4575 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4576 | base64bits += 16; |
| 4577 | base64buffer = (base64buffer << 16) | ch; |
| 4578 | while (base64bits >= 6) { |
| 4579 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 4580 | base64bits -= 6; |
| 4581 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 4582 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4583 | if (base64bits) |
| 4584 | *out++= TO_BASE64(base64buffer << (6-base64bits) ); |
| 4585 | if (inShift) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4586 | *out++ = '-'; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4587 | if (_PyBytes_Resize(&v, out - start) < 0) |
| 4588 | return NULL; |
| 4589 | return v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4590 | } |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4591 | PyObject * |
| 4592 | PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
| 4593 | Py_ssize_t size, |
| 4594 | int base64SetO, |
| 4595 | int base64WhiteSpace, |
| 4596 | const char *errors) |
| 4597 | { |
| 4598 | PyObject *result; |
| 4599 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 4600 | if (tmp == NULL) |
| 4601 | return NULL; |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 4602 | result = _PyUnicode_EncodeUTF7(tmp, base64SetO, |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4603 | base64WhiteSpace, errors); |
| 4604 | Py_DECREF(tmp); |
| 4605 | return result; |
| 4606 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4607 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4608 | #undef IS_BASE64 |
| 4609 | #undef FROM_BASE64 |
| 4610 | #undef TO_BASE64 |
| 4611 | #undef DECODE_DIRECT |
| 4612 | #undef ENCODE_DIRECT |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4613 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4614 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 4615 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4616 | PyObject * |
| 4617 | PyUnicode_DecodeUTF8(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4618 | Py_ssize_t size, |
| 4619 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4620 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4621 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 4622 | } |
| 4623 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4624 | #include "stringlib/asciilib.h" |
| 4625 | #include "stringlib/codecs.h" |
| 4626 | #include "stringlib/undef.h" |
| 4627 | |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4628 | #include "stringlib/ucs1lib.h" |
| 4629 | #include "stringlib/codecs.h" |
| 4630 | #include "stringlib/undef.h" |
| 4631 | |
| 4632 | #include "stringlib/ucs2lib.h" |
| 4633 | #include "stringlib/codecs.h" |
| 4634 | #include "stringlib/undef.h" |
| 4635 | |
| 4636 | #include "stringlib/ucs4lib.h" |
| 4637 | #include "stringlib/codecs.h" |
| 4638 | #include "stringlib/undef.h" |
| 4639 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4640 | /* Mask to quickly check whether a C 'long' contains a |
| 4641 | non-ASCII, UTF8-encoded char. */ |
| 4642 | #if (SIZEOF_LONG == 8) |
Mark Dickinson | 01ac8b6 | 2012-07-07 14:08:48 +0200 | [diff] [blame] | 4643 | # define ASCII_CHAR_MASK 0x8080808080808080UL |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4644 | #elif (SIZEOF_LONG == 4) |
Mark Dickinson | 01ac8b6 | 2012-07-07 14:08:48 +0200 | [diff] [blame] | 4645 | # define ASCII_CHAR_MASK 0x80808080UL |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4646 | #else |
| 4647 | # error C 'long' size should be either 4 or 8! |
| 4648 | #endif |
| 4649 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4650 | static Py_ssize_t |
| 4651 | 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] | 4652 | { |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4653 | const char *p = start; |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 4654 | 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] | 4655 | |
Antoine Pitrou | 8b0e984 | 2013-05-11 15:58:34 +0200 | [diff] [blame] | 4656 | /* |
| 4657 | * Issue #17237: m68k is a bit different from most architectures in |
| 4658 | * that objects do not use "natural alignment" - for example, int and |
| 4659 | * long are only aligned at 2-byte boundaries. Therefore the assert() |
| 4660 | * won't work; also, tests have shown that skipping the "optimised |
| 4661 | * version" will even speed up m68k. |
| 4662 | */ |
| 4663 | #if !defined(__m68k__) |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4664 | #if SIZEOF_LONG <= SIZEOF_VOID_P |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 4665 | assert(_Py_IS_ALIGNED(dest, SIZEOF_LONG)); |
| 4666 | if (_Py_IS_ALIGNED(p, SIZEOF_LONG)) { |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4667 | /* Fast path, see in STRINGLIB(utf8_decode) for |
| 4668 | an explanation. */ |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 4669 | /* Help allocation */ |
| 4670 | const char *_p = p; |
| 4671 | Py_UCS1 * q = dest; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4672 | while (_p < aligned_end) { |
| 4673 | unsigned long value = *(const unsigned long *) _p; |
| 4674 | if (value & ASCII_CHAR_MASK) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4675 | break; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4676 | *((unsigned long *)q) = value; |
| 4677 | _p += SIZEOF_LONG; |
| 4678 | q += SIZEOF_LONG; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4679 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4680 | p = _p; |
| 4681 | while (p < end) { |
| 4682 | if ((unsigned char)*p & 0x80) |
| 4683 | break; |
| 4684 | *q++ = *p++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4685 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4686 | return p - start; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4687 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4688 | #endif |
Antoine Pitrou | 8b0e984 | 2013-05-11 15:58:34 +0200 | [diff] [blame] | 4689 | #endif |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4690 | while (p < end) { |
| 4691 | /* Fast path, see in STRINGLIB(utf8_decode) in stringlib/codecs.h |
| 4692 | for an explanation. */ |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 4693 | if (_Py_IS_ALIGNED(p, SIZEOF_LONG)) { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 4694 | /* Help allocation */ |
| 4695 | const char *_p = p; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4696 | while (_p < aligned_end) { |
| 4697 | unsigned long value = *(unsigned long *) _p; |
| 4698 | if (value & ASCII_CHAR_MASK) |
| 4699 | break; |
| 4700 | _p += SIZEOF_LONG; |
| 4701 | } |
| 4702 | p = _p; |
| 4703 | if (_p == end) |
| 4704 | break; |
| 4705 | } |
| 4706 | if ((unsigned char)*p & 0x80) |
| 4707 | break; |
| 4708 | ++p; |
| 4709 | } |
| 4710 | memcpy(dest, start, p - start); |
| 4711 | return p - start; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4712 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4713 | |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4714 | PyObject * |
| 4715 | PyUnicode_DecodeUTF8Stateful(const char *s, |
| 4716 | Py_ssize_t size, |
| 4717 | const char *errors, |
| 4718 | Py_ssize_t *consumed) |
| 4719 | { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4720 | _PyUnicodeWriter writer; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4721 | const char *starts = s; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4722 | const char *end = s + size; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4723 | |
| 4724 | Py_ssize_t startinpos; |
| 4725 | Py_ssize_t endinpos; |
| 4726 | const char *errmsg = ""; |
| 4727 | PyObject *errorHandler = NULL; |
| 4728 | PyObject *exc = NULL; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4729 | |
| 4730 | if (size == 0) { |
| 4731 | if (consumed) |
| 4732 | *consumed = 0; |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 4733 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4734 | } |
| 4735 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4736 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
| 4737 | if (size == 1 && (unsigned char)s[0] < 128) { |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4738 | if (consumed) |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4739 | *consumed = 1; |
| 4740 | return get_latin1_char((unsigned char)s[0]); |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4741 | } |
| 4742 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 4743 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 4744 | writer.min_length = size; |
| 4745 | if (_PyUnicodeWriter_Prepare(&writer, writer.min_length, 127) == -1) |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4746 | goto onError; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4747 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4748 | writer.pos = ascii_decode(s, end, writer.data); |
| 4749 | s += writer.pos; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4750 | while (s < end) { |
| 4751 | Py_UCS4 ch; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4752 | int kind = writer.kind; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4753 | if (kind == PyUnicode_1BYTE_KIND) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4754 | if (PyUnicode_IS_ASCII(writer.buffer)) |
| 4755 | ch = asciilib_utf8_decode(&s, end, writer.data, &writer.pos); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4756 | else |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4757 | ch = ucs1lib_utf8_decode(&s, end, writer.data, &writer.pos); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4758 | } else if (kind == PyUnicode_2BYTE_KIND) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4759 | ch = ucs2lib_utf8_decode(&s, end, writer.data, &writer.pos); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4760 | } else { |
| 4761 | assert(kind == PyUnicode_4BYTE_KIND); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4762 | ch = ucs4lib_utf8_decode(&s, end, writer.data, &writer.pos); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4763 | } |
| 4764 | |
| 4765 | switch (ch) { |
| 4766 | case 0: |
| 4767 | if (s == end || consumed) |
| 4768 | goto End; |
| 4769 | errmsg = "unexpected end of data"; |
| 4770 | startinpos = s - starts; |
Ezio Melotti | f7ed5d1 | 2012-11-04 23:21:38 +0200 | [diff] [blame] | 4771 | endinpos = end - starts; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4772 | break; |
| 4773 | case 1: |
| 4774 | errmsg = "invalid start byte"; |
| 4775 | startinpos = s - starts; |
| 4776 | endinpos = startinpos + 1; |
| 4777 | break; |
| 4778 | case 2: |
Ezio Melotti | f7ed5d1 | 2012-11-04 23:21:38 +0200 | [diff] [blame] | 4779 | case 3: |
| 4780 | case 4: |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4781 | errmsg = "invalid continuation byte"; |
| 4782 | startinpos = s - starts; |
Ezio Melotti | f7ed5d1 | 2012-11-04 23:21:38 +0200 | [diff] [blame] | 4783 | endinpos = startinpos + ch - 1; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4784 | break; |
| 4785 | default: |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4786 | if (_PyUnicodeWriter_WriteCharInline(&writer, ch) < 0) |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4787 | goto onError; |
| 4788 | continue; |
| 4789 | } |
| 4790 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4791 | if (unicode_decode_call_errorhandler_writer( |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4792 | errors, &errorHandler, |
| 4793 | "utf-8", errmsg, |
| 4794 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4795 | &writer)) |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4796 | goto onError; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4797 | } |
| 4798 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4799 | End: |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4800 | if (consumed) |
| 4801 | *consumed = s - starts; |
| 4802 | |
| 4803 | Py_XDECREF(errorHandler); |
| 4804 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4805 | return _PyUnicodeWriter_Finish(&writer); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4806 | |
| 4807 | onError: |
| 4808 | Py_XDECREF(errorHandler); |
| 4809 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4810 | _PyUnicodeWriter_Dealloc(&writer); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4811 | return NULL; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4812 | } |
| 4813 | |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4814 | #ifdef __APPLE__ |
| 4815 | |
| 4816 | /* Simplified UTF-8 decoder using surrogateescape error handler, |
Victor Stinner | 0d92c4f | 2012-11-12 23:32:21 +0100 | [diff] [blame] | 4817 | used to decode the command line arguments on Mac OS X. |
| 4818 | |
| 4819 | Return a pointer to a newly allocated wide character string (use |
Victor Stinner | 6f8eeee | 2013-07-07 22:57:45 +0200 | [diff] [blame] | 4820 | PyMem_RawFree() to free the memory), or NULL on memory allocation error. */ |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4821 | |
| 4822 | wchar_t* |
| 4823 | _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size) |
| 4824 | { |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4825 | const char *e; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4826 | wchar_t *unicode; |
| 4827 | Py_ssize_t outpos; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4828 | |
| 4829 | /* Note: size will always be longer than the resulting Unicode |
| 4830 | character count */ |
Victor Stinner | 0d92c4f | 2012-11-12 23:32:21 +0100 | [diff] [blame] | 4831 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < (size + 1)) |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4832 | return NULL; |
Victor Stinner | 6f8eeee | 2013-07-07 22:57:45 +0200 | [diff] [blame] | 4833 | unicode = PyMem_RawMalloc((size + 1) * sizeof(wchar_t)); |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4834 | if (!unicode) |
| 4835 | return NULL; |
| 4836 | |
| 4837 | /* Unpack UTF-8 encoded data */ |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4838 | e = s + size; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4839 | outpos = 0; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4840 | while (s < e) { |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4841 | Py_UCS4 ch; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4842 | #if SIZEOF_WCHAR_T == 4 |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4843 | ch = ucs4lib_utf8_decode(&s, e, (Py_UCS4 *)unicode, &outpos); |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4844 | #else |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4845 | ch = ucs2lib_utf8_decode(&s, e, (Py_UCS2 *)unicode, &outpos); |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4846 | #endif |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4847 | if (ch > 0xFF) { |
| 4848 | #if SIZEOF_WCHAR_T == 4 |
| 4849 | assert(0); |
| 4850 | #else |
| 4851 | assert(Py_UNICODE_IS_SURROGATE(ch)); |
| 4852 | /* compute and append the two surrogates: */ |
| 4853 | unicode[outpos++] = (wchar_t)Py_UNICODE_HIGH_SURROGATE(ch); |
| 4854 | unicode[outpos++] = (wchar_t)Py_UNICODE_LOW_SURROGATE(ch); |
| 4855 | #endif |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4856 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4857 | else { |
| 4858 | if (!ch && s == e) |
| 4859 | break; |
| 4860 | /* surrogateescape */ |
| 4861 | unicode[outpos++] = 0xDC00 + (unsigned char)*s++; |
| 4862 | } |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4863 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4864 | unicode[outpos] = L'\0'; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4865 | return unicode; |
| 4866 | } |
| 4867 | |
| 4868 | #endif /* __APPLE__ */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4869 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4870 | /* Primary internal function which creates utf8 encoded bytes objects. |
| 4871 | |
| 4872 | Allocation strategy: if the string is short, convert into a stack buffer |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4873 | and allocate exactly as much space needed at the end. Else allocate the |
| 4874 | maximum possible needed (4 result bytes per Unicode character), and return |
| 4875 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4876 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 4877 | PyObject * |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4878 | _PyUnicode_AsUTF8String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4879 | { |
Victor Stinner | 6099a03 | 2011-12-18 14:22:26 +0100 | [diff] [blame] | 4880 | enum PyUnicode_Kind kind; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4881 | void *data; |
| 4882 | Py_ssize_t size; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 4883 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4884 | if (!PyUnicode_Check(unicode)) { |
| 4885 | PyErr_BadArgument(); |
| 4886 | return NULL; |
| 4887 | } |
| 4888 | |
| 4889 | if (PyUnicode_READY(unicode) == -1) |
| 4890 | return NULL; |
| 4891 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 4892 | if (PyUnicode_UTF8(unicode)) |
| 4893 | return PyBytes_FromStringAndSize(PyUnicode_UTF8(unicode), |
| 4894 | PyUnicode_UTF8_LENGTH(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4895 | |
| 4896 | kind = PyUnicode_KIND(unicode); |
| 4897 | data = PyUnicode_DATA(unicode); |
| 4898 | size = PyUnicode_GET_LENGTH(unicode); |
| 4899 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 4900 | switch (kind) { |
Victor Stinner | 6099a03 | 2011-12-18 14:22:26 +0100 | [diff] [blame] | 4901 | default: |
| 4902 | assert(0); |
| 4903 | case PyUnicode_1BYTE_KIND: |
| 4904 | /* the string cannot be ASCII, or PyUnicode_UTF8() would be set */ |
| 4905 | assert(!PyUnicode_IS_ASCII(unicode)); |
| 4906 | return ucs1lib_utf8_encoder(unicode, data, size, errors); |
| 4907 | case PyUnicode_2BYTE_KIND: |
| 4908 | return ucs2lib_utf8_encoder(unicode, data, size, errors); |
| 4909 | case PyUnicode_4BYTE_KIND: |
| 4910 | return ucs4lib_utf8_encoder(unicode, data, size, errors); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4911 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4912 | } |
| 4913 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4914 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4915 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
| 4916 | Py_ssize_t size, |
| 4917 | const char *errors) |
| 4918 | { |
| 4919 | PyObject *v, *unicode; |
| 4920 | |
| 4921 | unicode = PyUnicode_FromUnicode(s, size); |
| 4922 | if (unicode == NULL) |
| 4923 | return NULL; |
| 4924 | v = _PyUnicode_AsUTF8String(unicode, errors); |
| 4925 | Py_DECREF(unicode); |
| 4926 | return v; |
| 4927 | } |
| 4928 | |
| 4929 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4930 | PyUnicode_AsUTF8String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4931 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4932 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4933 | } |
| 4934 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4935 | /* --- UTF-32 Codec ------------------------------------------------------- */ |
| 4936 | |
| 4937 | PyObject * |
| 4938 | PyUnicode_DecodeUTF32(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4939 | Py_ssize_t size, |
| 4940 | const char *errors, |
| 4941 | int *byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4942 | { |
| 4943 | return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); |
| 4944 | } |
| 4945 | |
| 4946 | PyObject * |
| 4947 | PyUnicode_DecodeUTF32Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4948 | Py_ssize_t size, |
| 4949 | const char *errors, |
| 4950 | int *byteorder, |
| 4951 | Py_ssize_t *consumed) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4952 | { |
| 4953 | const char *starts = s; |
| 4954 | Py_ssize_t startinpos; |
| 4955 | Py_ssize_t endinpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4956 | _PyUnicodeWriter writer; |
Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 4957 | const unsigned char *q, *e; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4958 | int le, bo = 0; /* assume native ordering by default */ |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 4959 | const char *encoding; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4960 | const char *errmsg = ""; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4961 | PyObject *errorHandler = NULL; |
| 4962 | PyObject *exc = NULL; |
Victor Stinner | 313a120 | 2010-06-11 23:56:51 +0000 | [diff] [blame] | 4963 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4964 | q = (unsigned char *)s; |
| 4965 | e = q + size; |
| 4966 | |
| 4967 | if (byteorder) |
| 4968 | bo = *byteorder; |
| 4969 | |
| 4970 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 4971 | byte order setting accordingly. In native mode, the leading BOM |
| 4972 | mark is skipped, in all other modes, it is copied to the output |
| 4973 | stream as-is (giving a ZWNBSP character). */ |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4974 | if (bo == 0 && size >= 4) { |
| 4975 | Py_UCS4 bom = (q[3] << 24) | (q[2] << 16) | (q[1] << 8) | q[0]; |
| 4976 | if (bom == 0x0000FEFF) { |
| 4977 | bo = -1; |
| 4978 | q += 4; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4979 | } |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4980 | else if (bom == 0xFFFE0000) { |
| 4981 | bo = 1; |
| 4982 | q += 4; |
| 4983 | } |
| 4984 | if (byteorder) |
| 4985 | *byteorder = bo; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4986 | } |
| 4987 | |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4988 | if (q == e) { |
| 4989 | if (consumed) |
| 4990 | *consumed = size; |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 4991 | _Py_RETURN_UNICODE_EMPTY(); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4992 | } |
| 4993 | |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4994 | #ifdef WORDS_BIGENDIAN |
| 4995 | le = bo < 0; |
| 4996 | #else |
| 4997 | le = bo <= 0; |
| 4998 | #endif |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 4999 | encoding = le ? "utf-32-le" : "utf-32-be"; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5000 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 5001 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 5002 | writer.min_length = (e - q + 3) / 4; |
| 5003 | if (_PyUnicodeWriter_Prepare(&writer, writer.min_length, 127) == -1) |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5004 | goto onError; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5005 | |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5006 | while (1) { |
| 5007 | Py_UCS4 ch = 0; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5008 | Py_UCS4 maxch = PyUnicode_MAX_CHAR_VALUE(writer.buffer); |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 5009 | |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5010 | if (e - q >= 4) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5011 | enum PyUnicode_Kind kind = writer.kind; |
| 5012 | void *data = writer.data; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5013 | const unsigned char *last = e - 4; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5014 | Py_ssize_t pos = writer.pos; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5015 | if (le) { |
| 5016 | do { |
| 5017 | ch = (q[3] << 24) | (q[2] << 16) | (q[1] << 8) | q[0]; |
| 5018 | if (ch > maxch) |
| 5019 | break; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5020 | if (kind != PyUnicode_1BYTE_KIND && |
| 5021 | Py_UNICODE_IS_SURROGATE(ch)) |
| 5022 | break; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5023 | PyUnicode_WRITE(kind, data, pos++, ch); |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5024 | q += 4; |
| 5025 | } while (q <= last); |
| 5026 | } |
| 5027 | else { |
| 5028 | do { |
| 5029 | ch = (q[0] << 24) | (q[1] << 16) | (q[2] << 8) | q[3]; |
| 5030 | if (ch > maxch) |
| 5031 | break; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5032 | if (kind != PyUnicode_1BYTE_KIND && |
| 5033 | Py_UNICODE_IS_SURROGATE(ch)) |
| 5034 | break; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5035 | PyUnicode_WRITE(kind, data, pos++, ch); |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5036 | q += 4; |
| 5037 | } while (q <= last); |
| 5038 | } |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5039 | writer.pos = pos; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5040 | } |
| 5041 | |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5042 | if (Py_UNICODE_IS_SURROGATE(ch)) { |
| 5043 | errmsg = "codepoint in surrogate code point range(0xd800, 0xe000)"; |
| 5044 | startinpos = ((const char *)q) - starts; |
| 5045 | endinpos = startinpos + 4; |
| 5046 | } |
| 5047 | else if (ch <= maxch) { |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5048 | if (q == e || consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5049 | break; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5050 | /* remaining bytes at the end? (size should be divisible by 4) */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5051 | errmsg = "truncated data"; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5052 | startinpos = ((const char *)q) - starts; |
| 5053 | endinpos = ((const char *)e) - starts; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5054 | } |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5055 | else { |
| 5056 | if (ch < 0x110000) { |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 5057 | if (_PyUnicodeWriter_WriteCharInline(&writer, ch) < 0) |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5058 | goto onError; |
| 5059 | q += 4; |
| 5060 | continue; |
| 5061 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5062 | errmsg = "codepoint not in range(0x110000)"; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5063 | startinpos = ((const char *)q) - starts; |
| 5064 | endinpos = startinpos + 4; |
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 | |
| 5067 | /* The remaining input chars are ignored if the callback |
| 5068 | chooses to skip the input */ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5069 | if (unicode_decode_call_errorhandler_writer( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5070 | errors, &errorHandler, |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5071 | encoding, errmsg, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5072 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5073 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5074 | goto onError; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5075 | } |
| 5076 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5077 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5078 | *consumed = (const char *)q-starts; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5079 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5080 | Py_XDECREF(errorHandler); |
| 5081 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5082 | return _PyUnicodeWriter_Finish(&writer); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5083 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5084 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5085 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5086 | Py_XDECREF(errorHandler); |
| 5087 | Py_XDECREF(exc); |
| 5088 | return NULL; |
| 5089 | } |
| 5090 | |
| 5091 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5092 | _PyUnicode_EncodeUTF32(PyObject *str, |
| 5093 | const char *errors, |
| 5094 | int byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5095 | { |
Serhiy Storchaka | 3079328 | 2014-01-04 22:44:01 +0200 | [diff] [blame] | 5096 | int kind; |
| 5097 | void *data; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5098 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5099 | PyObject *v; |
Serhiy Storchaka | 3079328 | 2014-01-04 22:44:01 +0200 | [diff] [blame] | 5100 | unsigned char *p; |
| 5101 | Py_ssize_t nsize, i; |
| 5102 | /* Offsets from p for storing byte pairs in the right order. */ |
Christian Heimes | 743e0cd | 2012-10-17 23:52:17 +0200 | [diff] [blame] | 5103 | #if PY_LITTLE_ENDIAN |
Serhiy Storchaka | 3079328 | 2014-01-04 22:44:01 +0200 | [diff] [blame] | 5104 | int iorder[] = {0, 1, 2, 3}; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5105 | #else |
Serhiy Storchaka | 3079328 | 2014-01-04 22:44:01 +0200 | [diff] [blame] | 5106 | int iorder[] = {3, 2, 1, 0}; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5107 | #endif |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5108 | const char *encoding; |
| 5109 | PyObject *errorHandler = NULL; |
| 5110 | PyObject *exc = NULL; |
| 5111 | PyObject *rep = NULL; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5112 | |
Serhiy Storchaka | 3079328 | 2014-01-04 22:44:01 +0200 | [diff] [blame] | 5113 | #define STORECHAR(CH) \ |
| 5114 | do { \ |
| 5115 | p[iorder[3]] = ((CH) >> 24) & 0xff; \ |
| 5116 | p[iorder[2]] = ((CH) >> 16) & 0xff; \ |
| 5117 | p[iorder[1]] = ((CH) >> 8) & 0xff; \ |
| 5118 | p[iorder[0]] = (CH) & 0xff; \ |
| 5119 | p += 4; \ |
| 5120 | } while(0) |
| 5121 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5122 | if (!PyUnicode_Check(str)) { |
| 5123 | PyErr_BadArgument(); |
| 5124 | return NULL; |
| 5125 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 5126 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5127 | return NULL; |
| 5128 | kind = PyUnicode_KIND(str); |
| 5129 | data = PyUnicode_DATA(str); |
| 5130 | len = PyUnicode_GET_LENGTH(str); |
| 5131 | |
Serhiy Storchaka | 583a939 | 2014-01-04 19:25:37 +0200 | [diff] [blame] | 5132 | nsize = len + (byteorder == 0); |
Serhiy Storchaka | 3079328 | 2014-01-04 22:44:01 +0200 | [diff] [blame] | 5133 | if (nsize > PY_SSIZE_T_MAX / 4) |
| 5134 | return PyErr_NoMemory(); |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 5135 | v = PyBytes_FromStringAndSize(NULL, nsize * 4); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5136 | if (v == NULL) |
| 5137 | return NULL; |
| 5138 | |
Serhiy Storchaka | 3079328 | 2014-01-04 22:44:01 +0200 | [diff] [blame] | 5139 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5140 | if (byteorder == 0) |
Serhiy Storchaka | 3079328 | 2014-01-04 22:44:01 +0200 | [diff] [blame] | 5141 | STORECHAR(0xFEFF); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5142 | if (len == 0) |
Serhiy Storchaka | 3079328 | 2014-01-04 22:44:01 +0200 | [diff] [blame] | 5143 | return v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5144 | |
Serhiy Storchaka | 3079328 | 2014-01-04 22:44:01 +0200 | [diff] [blame] | 5145 | if (byteorder == -1) { |
| 5146 | /* force LE */ |
| 5147 | iorder[0] = 0; |
| 5148 | iorder[1] = 1; |
| 5149 | iorder[2] = 2; |
| 5150 | iorder[3] = 3; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5151 | encoding = "utf-32-le"; |
Serhiy Storchaka | 3079328 | 2014-01-04 22:44:01 +0200 | [diff] [blame] | 5152 | } |
| 5153 | else if (byteorder == 1) { |
| 5154 | /* force BE */ |
| 5155 | iorder[0] = 3; |
| 5156 | iorder[1] = 2; |
| 5157 | iorder[2] = 1; |
| 5158 | iorder[3] = 0; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5159 | encoding = "utf-32-be"; |
Serhiy Storchaka | 3079328 | 2014-01-04 22:44:01 +0200 | [diff] [blame] | 5160 | } |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5161 | else |
| 5162 | encoding = "utf-32"; |
| 5163 | |
| 5164 | if (kind == PyUnicode_1BYTE_KIND) { |
Serhiy Storchaka | 3079328 | 2014-01-04 22:44:01 +0200 | [diff] [blame] | 5165 | for (i = 0; i < len; i++) |
| 5166 | STORECHAR(PyUnicode_READ(kind, data, i)); |
| 5167 | return v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5168 | } |
| 5169 | |
Serhiy Storchaka | 3079328 | 2014-01-04 22:44:01 +0200 | [diff] [blame] | 5170 | for (i = 0; i < len;) { |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5171 | Py_ssize_t repsize, moreunits; |
Serhiy Storchaka | 3079328 | 2014-01-04 22:44:01 +0200 | [diff] [blame] | 5172 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 5173 | i++; |
| 5174 | assert(ch <= MAX_UNICODE); |
| 5175 | if (!Py_UNICODE_IS_SURROGATE(ch)) { |
| 5176 | STORECHAR(ch); |
| 5177 | continue; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5178 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5179 | |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5180 | rep = unicode_encode_call_errorhandler( |
| 5181 | errors, &errorHandler, |
| 5182 | encoding, "surrogates not allowed", |
Serhiy Storchaka | 3079328 | 2014-01-04 22:44:01 +0200 | [diff] [blame] | 5183 | str, &exc, i-1, i, &i); |
| 5184 | |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5185 | if (!rep) |
| 5186 | goto error; |
| 5187 | |
| 5188 | if (PyBytes_Check(rep)) { |
| 5189 | repsize = PyBytes_GET_SIZE(rep); |
| 5190 | if (repsize & 3) { |
| 5191 | raise_encode_exception(&exc, encoding, |
Serhiy Storchaka | 3079328 | 2014-01-04 22:44:01 +0200 | [diff] [blame] | 5192 | str, i - 1, i, |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5193 | "surrogates not allowed"); |
| 5194 | goto error; |
| 5195 | } |
| 5196 | moreunits = repsize / 4; |
| 5197 | } |
| 5198 | else { |
| 5199 | assert(PyUnicode_Check(rep)); |
| 5200 | if (PyUnicode_READY(rep) < 0) |
| 5201 | goto error; |
| 5202 | moreunits = repsize = PyUnicode_GET_LENGTH(rep); |
| 5203 | if (!PyUnicode_IS_ASCII(rep)) { |
| 5204 | raise_encode_exception(&exc, encoding, |
Serhiy Storchaka | 3079328 | 2014-01-04 22:44:01 +0200 | [diff] [blame] | 5205 | str, i - 1, i, |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5206 | "surrogates not allowed"); |
| 5207 | goto error; |
| 5208 | } |
| 5209 | } |
| 5210 | |
| 5211 | /* four bytes are reserved for each surrogate */ |
| 5212 | if (moreunits > 1) { |
Serhiy Storchaka | 3079328 | 2014-01-04 22:44:01 +0200 | [diff] [blame] | 5213 | Py_ssize_t outpos = p - (unsigned char*) PyBytes_AS_STRING(v); |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5214 | Py_ssize_t morebytes = 4 * (moreunits - 1); |
| 5215 | if (PyBytes_GET_SIZE(v) > PY_SSIZE_T_MAX - morebytes) { |
| 5216 | /* integer overflow */ |
| 5217 | PyErr_NoMemory(); |
| 5218 | goto error; |
| 5219 | } |
| 5220 | if (_PyBytes_Resize(&v, PyBytes_GET_SIZE(v) + morebytes) < 0) |
| 5221 | goto error; |
Serhiy Storchaka | 3079328 | 2014-01-04 22:44:01 +0200 | [diff] [blame] | 5222 | p = (unsigned char*) PyBytes_AS_STRING(v) + outpos; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5223 | } |
| 5224 | |
| 5225 | if (PyBytes_Check(rep)) { |
Serhiy Storchaka | 3079328 | 2014-01-04 22:44:01 +0200 | [diff] [blame] | 5226 | Py_MEMCPY(p, PyBytes_AS_STRING(rep), repsize); |
| 5227 | p += repsize; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5228 | } else /* rep is unicode */ { |
Serhiy Storchaka | 3079328 | 2014-01-04 22:44:01 +0200 | [diff] [blame] | 5229 | const Py_UCS1 *repdata; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5230 | assert(PyUnicode_KIND(rep) == PyUnicode_1BYTE_KIND); |
Serhiy Storchaka | 3079328 | 2014-01-04 22:44:01 +0200 | [diff] [blame] | 5231 | repdata = PyUnicode_1BYTE_DATA(rep); |
| 5232 | while (repsize--) { |
| 5233 | Py_UCS4 ch = *repdata++; |
| 5234 | STORECHAR(ch); |
| 5235 | } |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5236 | } |
| 5237 | |
| 5238 | Py_CLEAR(rep); |
| 5239 | } |
| 5240 | |
| 5241 | /* Cut back to size actually needed. This is necessary for, for example, |
| 5242 | encoding of a string containing isolated surrogates and the 'ignore' |
| 5243 | handler is used. */ |
Serhiy Storchaka | 3079328 | 2014-01-04 22:44:01 +0200 | [diff] [blame] | 5244 | nsize = p - (unsigned char*) PyBytes_AS_STRING(v); |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5245 | if (nsize != PyBytes_GET_SIZE(v)) |
| 5246 | _PyBytes_Resize(&v, nsize); |
| 5247 | Py_XDECREF(errorHandler); |
| 5248 | Py_XDECREF(exc); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5249 | return v; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5250 | error: |
| 5251 | Py_XDECREF(rep); |
| 5252 | Py_XDECREF(errorHandler); |
| 5253 | Py_XDECREF(exc); |
| 5254 | Py_XDECREF(v); |
| 5255 | return NULL; |
Serhiy Storchaka | 3079328 | 2014-01-04 22:44:01 +0200 | [diff] [blame] | 5256 | #undef STORECHAR |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5257 | } |
| 5258 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5259 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5260 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
| 5261 | Py_ssize_t size, |
| 5262 | const char *errors, |
| 5263 | int byteorder) |
| 5264 | { |
| 5265 | PyObject *result; |
| 5266 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5267 | if (tmp == NULL) |
| 5268 | return NULL; |
| 5269 | result = _PyUnicode_EncodeUTF32(tmp, errors, byteorder); |
| 5270 | Py_DECREF(tmp); |
| 5271 | return result; |
| 5272 | } |
| 5273 | |
| 5274 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5275 | PyUnicode_AsUTF32String(PyObject *unicode) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5276 | { |
Victor Stinner | b960b34 | 2011-11-20 19:12:52 +0100 | [diff] [blame] | 5277 | return _PyUnicode_EncodeUTF32(unicode, NULL, 0); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5278 | } |
| 5279 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5280 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 5281 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5282 | PyObject * |
| 5283 | PyUnicode_DecodeUTF16(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5284 | Py_ssize_t size, |
| 5285 | const char *errors, |
| 5286 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5287 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5288 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 5289 | } |
| 5290 | |
| 5291 | PyObject * |
| 5292 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5293 | Py_ssize_t size, |
| 5294 | const char *errors, |
| 5295 | int *byteorder, |
| 5296 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5297 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5298 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5299 | Py_ssize_t startinpos; |
| 5300 | Py_ssize_t endinpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5301 | _PyUnicodeWriter writer; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5302 | const unsigned char *q, *e; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5303 | int bo = 0; /* assume native ordering by default */ |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5304 | int native_ordering; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 5305 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5306 | PyObject *errorHandler = NULL; |
| 5307 | PyObject *exc = NULL; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5308 | const char *encoding; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5309 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5310 | q = (unsigned char *)s; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5311 | e = q + size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5312 | |
| 5313 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5314 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5315 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5316 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 5317 | byte order setting accordingly. In native mode, the leading BOM |
| 5318 | mark is skipped, in all other modes, it is copied to the output |
| 5319 | stream as-is (giving a ZWNBSP character). */ |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5320 | if (bo == 0 && size >= 2) { |
| 5321 | const Py_UCS4 bom = (q[1] << 8) | q[0]; |
| 5322 | if (bom == 0xFEFF) { |
| 5323 | q += 2; |
| 5324 | bo = -1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5325 | } |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5326 | else if (bom == 0xFFFE) { |
| 5327 | q += 2; |
| 5328 | bo = 1; |
| 5329 | } |
| 5330 | if (byteorder) |
| 5331 | *byteorder = bo; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5332 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5333 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5334 | if (q == e) { |
| 5335 | if (consumed) |
| 5336 | *consumed = size; |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 5337 | _Py_RETURN_UNICODE_EMPTY(); |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5338 | } |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5339 | |
Christian Heimes | 743e0cd | 2012-10-17 23:52:17 +0200 | [diff] [blame] | 5340 | #if PY_LITTLE_ENDIAN |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5341 | native_ordering = bo <= 0; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5342 | encoding = bo <= 0 ? "utf-16-le" : "utf-16-be"; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5343 | #else |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5344 | native_ordering = bo >= 0; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5345 | encoding = bo >= 0 ? "utf-16-be" : "utf-16-le"; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5346 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5347 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5348 | /* Note: size will always be longer than the resulting Unicode |
| 5349 | character count */ |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 5350 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 5351 | writer.min_length = (e - q + 1) / 2; |
| 5352 | if (_PyUnicodeWriter_Prepare(&writer, writer.min_length, 127) == -1) |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5353 | goto onError; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5354 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5355 | while (1) { |
| 5356 | Py_UCS4 ch = 0; |
| 5357 | if (e - q >= 2) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5358 | int kind = writer.kind; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5359 | if (kind == PyUnicode_1BYTE_KIND) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5360 | if (PyUnicode_IS_ASCII(writer.buffer)) |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5361 | ch = asciilib_utf16_decode(&q, e, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5362 | (Py_UCS1*)writer.data, &writer.pos, |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5363 | native_ordering); |
| 5364 | else |
| 5365 | ch = ucs1lib_utf16_decode(&q, e, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5366 | (Py_UCS1*)writer.data, &writer.pos, |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5367 | native_ordering); |
| 5368 | } else if (kind == PyUnicode_2BYTE_KIND) { |
| 5369 | ch = ucs2lib_utf16_decode(&q, e, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5370 | (Py_UCS2*)writer.data, &writer.pos, |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5371 | native_ordering); |
| 5372 | } else { |
| 5373 | assert(kind == PyUnicode_4BYTE_KIND); |
| 5374 | ch = ucs4lib_utf16_decode(&q, e, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5375 | (Py_UCS4*)writer.data, &writer.pos, |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5376 | native_ordering); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5377 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5378 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5379 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5380 | switch (ch) |
| 5381 | { |
| 5382 | case 0: |
| 5383 | /* remaining byte at the end? (size should be even) */ |
| 5384 | if (q == e || consumed) |
| 5385 | goto End; |
| 5386 | errmsg = "truncated data"; |
| 5387 | startinpos = ((const char *)q) - starts; |
| 5388 | endinpos = ((const char *)e) - starts; |
| 5389 | break; |
| 5390 | /* The remaining input chars are ignored if the callback |
| 5391 | chooses to skip the input */ |
| 5392 | case 1: |
Serhiy Storchaka | 48e188e | 2013-01-08 23:14:24 +0200 | [diff] [blame] | 5393 | q -= 2; |
| 5394 | if (consumed) |
Serhiy Storchaka | ae3b32a | 2013-01-08 23:40:52 +0200 | [diff] [blame] | 5395 | goto End; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5396 | errmsg = "unexpected end of data"; |
Serhiy Storchaka | 48e188e | 2013-01-08 23:14:24 +0200 | [diff] [blame] | 5397 | startinpos = ((const char *)q) - starts; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5398 | endinpos = ((const char *)e) - starts; |
| 5399 | break; |
| 5400 | case 2: |
| 5401 | errmsg = "illegal encoding"; |
| 5402 | startinpos = ((const char *)q) - 2 - starts; |
| 5403 | endinpos = startinpos + 2; |
| 5404 | break; |
| 5405 | case 3: |
| 5406 | errmsg = "illegal UTF-16 surrogate"; |
| 5407 | startinpos = ((const char *)q) - 4 - starts; |
| 5408 | endinpos = startinpos + 2; |
| 5409 | break; |
| 5410 | default: |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 5411 | if (_PyUnicodeWriter_WriteCharInline(&writer, ch) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5412 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5413 | continue; |
| 5414 | } |
| 5415 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5416 | if (unicode_decode_call_errorhandler_writer( |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5417 | errors, |
| 5418 | &errorHandler, |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5419 | encoding, errmsg, |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5420 | &starts, |
| 5421 | (const char **)&e, |
| 5422 | &startinpos, |
| 5423 | &endinpos, |
| 5424 | &exc, |
| 5425 | (const char **)&q, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5426 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5427 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5428 | } |
| 5429 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5430 | End: |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5431 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5432 | *consumed = (const char *)q-starts; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5433 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5434 | Py_XDECREF(errorHandler); |
| 5435 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5436 | return _PyUnicodeWriter_Finish(&writer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5437 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5438 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5439 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5440 | Py_XDECREF(errorHandler); |
| 5441 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5442 | return NULL; |
| 5443 | } |
| 5444 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5445 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5446 | _PyUnicode_EncodeUTF16(PyObject *str, |
| 5447 | const char *errors, |
| 5448 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5449 | { |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5450 | enum PyUnicode_Kind kind; |
| 5451 | const void *data; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5452 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5453 | PyObject *v; |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5454 | unsigned short *out; |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5455 | Py_ssize_t pairs; |
Christian Heimes | 743e0cd | 2012-10-17 23:52:17 +0200 | [diff] [blame] | 5456 | #if PY_BIG_ENDIAN |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5457 | int native_ordering = byteorder >= 0; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5458 | #else |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5459 | int native_ordering = byteorder <= 0; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5460 | #endif |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5461 | const char *encoding; |
| 5462 | Py_ssize_t nsize, pos; |
| 5463 | PyObject *errorHandler = NULL; |
| 5464 | PyObject *exc = NULL; |
| 5465 | PyObject *rep = NULL; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5466 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5467 | if (!PyUnicode_Check(str)) { |
| 5468 | PyErr_BadArgument(); |
| 5469 | return NULL; |
| 5470 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 5471 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5472 | return NULL; |
| 5473 | kind = PyUnicode_KIND(str); |
| 5474 | data = PyUnicode_DATA(str); |
| 5475 | len = PyUnicode_GET_LENGTH(str); |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 5476 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5477 | pairs = 0; |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5478 | if (kind == PyUnicode_4BYTE_KIND) { |
| 5479 | const Py_UCS4 *in = (const Py_UCS4 *)data; |
| 5480 | const Py_UCS4 *end = in + len; |
| 5481 | while (in < end) |
| 5482 | if (*in++ >= 0x10000) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5483 | pairs++; |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5484 | } |
| 5485 | if (len > PY_SSIZE_T_MAX / 2 - pairs - (byteorder == 0)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5486 | return PyErr_NoMemory(); |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5487 | nsize = len + pairs + (byteorder == 0); |
| 5488 | v = PyBytes_FromStringAndSize(NULL, nsize * 2); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5489 | if (v == NULL) |
| 5490 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5491 | |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5492 | /* output buffer is 2-bytes aligned */ |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 5493 | assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(v), 2)); |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5494 | out = (unsigned short *)PyBytes_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5495 | if (byteorder == 0) |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5496 | *out++ = 0xFEFF; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5497 | if (len == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5498 | goto done; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5499 | |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5500 | if (kind == PyUnicode_1BYTE_KIND) { |
| 5501 | ucs1lib_utf16_encode((const Py_UCS1 *)data, len, &out, native_ordering); |
| 5502 | goto done; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5503 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5504 | |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5505 | if (byteorder < 0) |
| 5506 | encoding = "utf-16-le"; |
| 5507 | else if (byteorder > 0) |
| 5508 | encoding = "utf-16-be"; |
| 5509 | else |
| 5510 | encoding = "utf-16"; |
| 5511 | |
| 5512 | pos = 0; |
| 5513 | while (pos < len) { |
| 5514 | Py_ssize_t repsize, moreunits; |
| 5515 | |
| 5516 | if (kind == PyUnicode_2BYTE_KIND) { |
| 5517 | pos += ucs2lib_utf16_encode((const Py_UCS2 *)data + pos, len - pos, |
| 5518 | &out, native_ordering); |
| 5519 | } |
| 5520 | else { |
| 5521 | assert(kind == PyUnicode_4BYTE_KIND); |
| 5522 | pos += ucs4lib_utf16_encode((const Py_UCS4 *)data + pos, len - pos, |
| 5523 | &out, native_ordering); |
| 5524 | } |
| 5525 | if (pos == len) |
| 5526 | break; |
| 5527 | |
| 5528 | rep = unicode_encode_call_errorhandler( |
| 5529 | errors, &errorHandler, |
| 5530 | encoding, "surrogates not allowed", |
| 5531 | str, &exc, pos, pos + 1, &pos); |
| 5532 | if (!rep) |
| 5533 | goto error; |
| 5534 | |
| 5535 | if (PyBytes_Check(rep)) { |
| 5536 | repsize = PyBytes_GET_SIZE(rep); |
| 5537 | if (repsize & 1) { |
| 5538 | raise_encode_exception(&exc, encoding, |
| 5539 | str, pos - 1, pos, |
| 5540 | "surrogates not allowed"); |
| 5541 | goto error; |
| 5542 | } |
| 5543 | moreunits = repsize / 2; |
| 5544 | } |
| 5545 | else { |
| 5546 | assert(PyUnicode_Check(rep)); |
| 5547 | if (PyUnicode_READY(rep) < 0) |
| 5548 | goto error; |
| 5549 | moreunits = repsize = PyUnicode_GET_LENGTH(rep); |
| 5550 | if (!PyUnicode_IS_ASCII(rep)) { |
| 5551 | raise_encode_exception(&exc, encoding, |
| 5552 | str, pos - 1, pos, |
| 5553 | "surrogates not allowed"); |
| 5554 | goto error; |
| 5555 | } |
| 5556 | } |
| 5557 | |
| 5558 | /* two bytes are reserved for each surrogate */ |
| 5559 | if (moreunits > 1) { |
| 5560 | Py_ssize_t outpos = out - (unsigned short*) PyBytes_AS_STRING(v); |
| 5561 | Py_ssize_t morebytes = 2 * (moreunits - 1); |
| 5562 | if (PyBytes_GET_SIZE(v) > PY_SSIZE_T_MAX - morebytes) { |
| 5563 | /* integer overflow */ |
| 5564 | PyErr_NoMemory(); |
| 5565 | goto error; |
| 5566 | } |
| 5567 | if (_PyBytes_Resize(&v, PyBytes_GET_SIZE(v) + morebytes) < 0) |
| 5568 | goto error; |
| 5569 | out = (unsigned short*) PyBytes_AS_STRING(v) + outpos; |
| 5570 | } |
| 5571 | |
| 5572 | if (PyBytes_Check(rep)) { |
| 5573 | Py_MEMCPY(out, PyBytes_AS_STRING(rep), repsize); |
| 5574 | out += moreunits; |
| 5575 | } else /* rep is unicode */ { |
| 5576 | assert(PyUnicode_KIND(rep) == PyUnicode_1BYTE_KIND); |
| 5577 | ucs1lib_utf16_encode(PyUnicode_1BYTE_DATA(rep), repsize, |
| 5578 | &out, native_ordering); |
| 5579 | } |
| 5580 | |
| 5581 | Py_CLEAR(rep); |
| 5582 | } |
| 5583 | |
| 5584 | /* Cut back to size actually needed. This is necessary for, for example, |
| 5585 | encoding of a string containing isolated surrogates and the 'ignore' handler |
| 5586 | is used. */ |
| 5587 | nsize = (unsigned char*) out - (unsigned char*) PyBytes_AS_STRING(v); |
| 5588 | if (nsize != PyBytes_GET_SIZE(v)) |
| 5589 | _PyBytes_Resize(&v, nsize); |
| 5590 | Py_XDECREF(errorHandler); |
| 5591 | Py_XDECREF(exc); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5592 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5593 | return v; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5594 | error: |
| 5595 | Py_XDECREF(rep); |
| 5596 | Py_XDECREF(errorHandler); |
| 5597 | Py_XDECREF(exc); |
| 5598 | Py_XDECREF(v); |
| 5599 | return NULL; |
| 5600 | #undef STORECHAR |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5601 | } |
| 5602 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5603 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5604 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
| 5605 | Py_ssize_t size, |
| 5606 | const char *errors, |
| 5607 | int byteorder) |
| 5608 | { |
| 5609 | PyObject *result; |
| 5610 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5611 | if (tmp == NULL) |
| 5612 | return NULL; |
| 5613 | result = _PyUnicode_EncodeUTF16(tmp, errors, byteorder); |
| 5614 | Py_DECREF(tmp); |
| 5615 | return result; |
| 5616 | } |
| 5617 | |
| 5618 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5619 | PyUnicode_AsUTF16String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5620 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5621 | return _PyUnicode_EncodeUTF16(unicode, NULL, 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5622 | } |
| 5623 | |
| 5624 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 5625 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5626 | /* Helper function for PyUnicode_DecodeUnicodeEscape, determines |
| 5627 | if all the escapes in the string make it still a valid ASCII string. |
| 5628 | Returns -1 if any escapes were found which cause the string to |
| 5629 | pop out of ASCII range. Otherwise returns the length of the |
| 5630 | required buffer to hold the string. |
| 5631 | */ |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 5632 | static Py_ssize_t |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5633 | length_of_escaped_ascii_string(const char *s, Py_ssize_t size) |
| 5634 | { |
| 5635 | const unsigned char *p = (const unsigned char *)s; |
| 5636 | const unsigned char *end = p + size; |
| 5637 | Py_ssize_t length = 0; |
| 5638 | |
| 5639 | if (size < 0) |
| 5640 | return -1; |
| 5641 | |
| 5642 | for (; p < end; ++p) { |
| 5643 | if (*p > 127) { |
| 5644 | /* Non-ASCII */ |
| 5645 | return -1; |
| 5646 | } |
| 5647 | else if (*p != '\\') { |
| 5648 | /* Normal character */ |
| 5649 | ++length; |
| 5650 | } |
| 5651 | else { |
| 5652 | /* Backslash-escape, check next char */ |
| 5653 | ++p; |
| 5654 | /* Escape sequence reaches till end of string or |
| 5655 | non-ASCII follow-up. */ |
| 5656 | if (p >= end || *p > 127) |
| 5657 | return -1; |
| 5658 | switch (*p) { |
| 5659 | case '\n': |
| 5660 | /* backslash + \n result in zero characters */ |
| 5661 | break; |
| 5662 | case '\\': case '\'': case '\"': |
| 5663 | case 'b': case 'f': case 't': |
| 5664 | case 'n': case 'r': case 'v': case 'a': |
| 5665 | ++length; |
| 5666 | break; |
| 5667 | case '0': case '1': case '2': case '3': |
| 5668 | case '4': case '5': case '6': case '7': |
| 5669 | case 'x': case 'u': case 'U': case 'N': |
| 5670 | /* these do not guarantee ASCII characters */ |
| 5671 | return -1; |
| 5672 | default: |
| 5673 | /* count the backslash + the other character */ |
| 5674 | length += 2; |
| 5675 | } |
| 5676 | } |
| 5677 | } |
| 5678 | return length; |
| 5679 | } |
| 5680 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5681 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 5682 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5683 | PyObject * |
| 5684 | PyUnicode_DecodeUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5685 | Py_ssize_t size, |
Victor Stinner | c17f540 | 2011-09-29 00:16:58 +0200 | [diff] [blame] | 5686 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5687 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5688 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5689 | Py_ssize_t startinpos; |
| 5690 | Py_ssize_t endinpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5691 | _PyUnicodeWriter writer; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5692 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5693 | char* message; |
| 5694 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5695 | PyObject *errorHandler = NULL; |
| 5696 | PyObject *exc = NULL; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5697 | Py_ssize_t len; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5698 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5699 | len = length_of_escaped_ascii_string(s, size); |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 5700 | if (len == 0) |
| 5701 | _Py_RETURN_UNICODE_EMPTY(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5702 | |
| 5703 | /* After length_of_escaped_ascii_string() there are two alternatives, |
| 5704 | either the string is pure ASCII with named escapes like \n, etc. |
| 5705 | and we determined it's exact size (common case) |
| 5706 | or it contains \x, \u, ... escape sequences. then we create a |
| 5707 | legacy wchar string and resize it at the end of this function. */ |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 5708 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5709 | if (len > 0) { |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 5710 | writer.min_length = len; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5711 | } |
| 5712 | else { |
| 5713 | /* Escaped strings will always be longer than the resulting |
| 5714 | Unicode string, so we start with size here and then reduce the |
| 5715 | length after conversion to the true value. |
| 5716 | (but if the error callback returns a long replacement string |
| 5717 | we'll have to allocate more space) */ |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 5718 | writer.min_length = size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5719 | } |
| 5720 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5721 | if (size == 0) |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5722 | return _PyUnicodeWriter_Finish(&writer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5723 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5724 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5725 | while (s < end) { |
| 5726 | unsigned char c; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 5727 | Py_UCS4 x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5728 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5729 | |
| 5730 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 5731 | if (*s != '\\') { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5732 | x = (unsigned char)*s; |
| 5733 | s++; |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 5734 | if (_PyUnicodeWriter_WriteCharInline(&writer, x) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5735 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5736 | continue; |
| 5737 | } |
| 5738 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5739 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5740 | /* \ - Escapes */ |
| 5741 | s++; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5742 | c = *s++; |
| 5743 | if (s > end) |
| 5744 | c = '\0'; /* Invalid after \ */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5745 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5746 | switch (c) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5747 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5748 | /* \x escapes */ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5749 | #define WRITECHAR(ch) \ |
| 5750 | do { \ |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 5751 | if (_PyUnicodeWriter_WriteCharInline(&writer, (ch)) < 0) \ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5752 | goto onError; \ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5753 | } while(0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5754 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5755 | case '\n': break; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5756 | case '\\': WRITECHAR('\\'); break; |
| 5757 | case '\'': WRITECHAR('\''); break; |
| 5758 | case '\"': WRITECHAR('\"'); break; |
| 5759 | case 'b': WRITECHAR('\b'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5760 | /* FF */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5761 | case 'f': WRITECHAR('\014'); break; |
| 5762 | case 't': WRITECHAR('\t'); break; |
| 5763 | case 'n': WRITECHAR('\n'); break; |
| 5764 | case 'r': WRITECHAR('\r'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5765 | /* VT */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5766 | case 'v': WRITECHAR('\013'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5767 | /* BEL, not classic C */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5768 | case 'a': WRITECHAR('\007'); break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5769 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5770 | /* \OOO (octal) escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5771 | case '0': case '1': case '2': case '3': |
| 5772 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5773 | x = s[-1] - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5774 | if (s < end && '0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5775 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5776 | if (s < end && '0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5777 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5778 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5779 | WRITECHAR(x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5780 | break; |
| 5781 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5782 | /* hex escapes */ |
| 5783 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5784 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5785 | digits = 2; |
| 5786 | message = "truncated \\xXX escape"; |
| 5787 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5788 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5789 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5790 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5791 | digits = 4; |
| 5792 | message = "truncated \\uXXXX escape"; |
| 5793 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5794 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5795 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5796 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5797 | digits = 8; |
| 5798 | message = "truncated \\UXXXXXXXX escape"; |
| 5799 | hexescape: |
| 5800 | chr = 0; |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5801 | if (end - s < digits) { |
| 5802 | /* count only hex digits */ |
| 5803 | for (; s < end; ++s) { |
| 5804 | c = (unsigned char)*s; |
| 5805 | if (!Py_ISXDIGIT(c)) |
| 5806 | goto error; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5807 | } |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5808 | goto error; |
| 5809 | } |
| 5810 | for (; digits--; ++s) { |
| 5811 | c = (unsigned char)*s; |
| 5812 | if (!Py_ISXDIGIT(c)) |
| 5813 | goto error; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5814 | chr = (chr<<4) & ~0xF; |
| 5815 | if (c >= '0' && c <= '9') |
| 5816 | chr += c - '0'; |
| 5817 | else if (c >= 'a' && c <= 'f') |
| 5818 | chr += 10 + c - 'a'; |
| 5819 | else |
| 5820 | chr += 10 + c - 'A'; |
| 5821 | } |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 5822 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5823 | /* _decoding_error will have already written into the |
| 5824 | target buffer. */ |
| 5825 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5826 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5827 | /* when we get here, chr is a 32-bit unicode character */ |
Serhiy Storchaka | 24193de | 2013-01-29 10:28:07 +0200 | [diff] [blame] | 5828 | message = "illegal Unicode character"; |
| 5829 | if (chr > MAX_UNICODE) |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5830 | goto error; |
Serhiy Storchaka | 24193de | 2013-01-29 10:28:07 +0200 | [diff] [blame] | 5831 | WRITECHAR(chr); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5832 | break; |
| 5833 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5834 | /* \N{name} */ |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5835 | case 'N': |
| 5836 | message = "malformed \\N character escape"; |
| 5837 | if (ucnhash_CAPI == NULL) { |
| 5838 | /* load the unicode data module */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5839 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCapsule_Import( |
| 5840 | PyUnicodeData_CAPSULE_NAME, 1); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5841 | if (ucnhash_CAPI == NULL) |
| 5842 | goto ucnhashError; |
| 5843 | } |
| 5844 | if (*s == '{') { |
| 5845 | const char *start = s+1; |
| 5846 | /* look for the closing brace */ |
| 5847 | while (*s != '}' && s < end) |
| 5848 | s++; |
| 5849 | if (s > start && s < end && *s == '}') { |
| 5850 | /* found a name. look it up in the unicode database */ |
| 5851 | message = "unknown Unicode character name"; |
| 5852 | s++; |
Serhiy Storchaka | 4f5f0e5 | 2013-01-21 11:38:00 +0200 | [diff] [blame] | 5853 | if (s - start - 1 <= INT_MAX && |
Serhiy Storchaka | c35f3a9 | 2013-01-21 11:42:57 +0200 | [diff] [blame] | 5854 | ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), |
Ezio Melotti | 931b8aa | 2011-10-21 21:57:36 +0300 | [diff] [blame] | 5855 | &chr, 0)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5856 | goto store; |
| 5857 | } |
| 5858 | } |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5859 | goto error; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5860 | |
| 5861 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5862 | if (s > end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5863 | message = "\\ at end of string"; |
| 5864 | s--; |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5865 | goto error; |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5866 | } |
| 5867 | else { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5868 | WRITECHAR('\\'); |
Serhiy Storchaka | 73e3880 | 2013-01-25 23:52:21 +0200 | [diff] [blame] | 5869 | WRITECHAR((unsigned char)s[-1]); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5870 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5871 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5872 | } |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5873 | continue; |
| 5874 | |
| 5875 | error: |
| 5876 | endinpos = s-starts; |
Serhiy Storchaka | 8fe5a9f | 2013-01-29 10:37:39 +0200 | [diff] [blame] | 5877 | if (unicode_decode_call_errorhandler_writer( |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5878 | errors, &errorHandler, |
| 5879 | "unicodeescape", message, |
| 5880 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Serhiy Storchaka | 8fe5a9f | 2013-01-29 10:37:39 +0200 | [diff] [blame] | 5881 | &writer)) |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5882 | goto onError; |
| 5883 | continue; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5884 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5885 | #undef WRITECHAR |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5886 | |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 5887 | Py_XDECREF(errorHandler); |
| 5888 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5889 | return _PyUnicodeWriter_Finish(&writer); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5890 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5891 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5892 | PyErr_SetString( |
| 5893 | PyExc_UnicodeError, |
| 5894 | "\\N escapes not supported (can't load unicodedata module)" |
| 5895 | ); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5896 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5897 | Py_XDECREF(errorHandler); |
| 5898 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 5899 | return NULL; |
| 5900 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5901 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5902 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5903 | Py_XDECREF(errorHandler); |
| 5904 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5905 | return NULL; |
| 5906 | } |
| 5907 | |
| 5908 | /* Return a Unicode-Escape string version of the Unicode object. |
| 5909 | |
| 5910 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 5911 | appropriate. |
| 5912 | |
| 5913 | */ |
| 5914 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5915 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5916 | PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5917 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5918 | Py_ssize_t i, len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5919 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5920 | char *p; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5921 | int kind; |
| 5922 | void *data; |
| 5923 | Py_ssize_t expandsize = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5924 | |
Ezio Melotti | e7f9037 | 2012-10-05 03:33:31 +0300 | [diff] [blame] | 5925 | /* Initial allocation is based on the longest-possible character |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5926 | escape. |
| 5927 | |
Ezio Melotti | e7f9037 | 2012-10-05 03:33:31 +0300 | [diff] [blame] | 5928 | For UCS1 strings it's '\xxx', 4 bytes per source character. |
| 5929 | For UCS2 strings it's '\uxxxx', 6 bytes per source character. |
| 5930 | For UCS4 strings it's '\U00xxxxxx', 10 bytes per source character. |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5931 | */ |
| 5932 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5933 | if (!PyUnicode_Check(unicode)) { |
| 5934 | PyErr_BadArgument(); |
| 5935 | return NULL; |
| 5936 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 5937 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5938 | return NULL; |
| 5939 | len = PyUnicode_GET_LENGTH(unicode); |
| 5940 | kind = PyUnicode_KIND(unicode); |
| 5941 | data = PyUnicode_DATA(unicode); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 5942 | switch (kind) { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5943 | case PyUnicode_1BYTE_KIND: expandsize = 4; break; |
| 5944 | case PyUnicode_2BYTE_KIND: expandsize = 6; break; |
| 5945 | case PyUnicode_4BYTE_KIND: expandsize = 10; break; |
| 5946 | } |
| 5947 | |
| 5948 | if (len == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5949 | return PyBytes_FromStringAndSize(NULL, 0); |
| 5950 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5951 | if (len > (PY_SSIZE_T_MAX - 2 - 1) / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5952 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5953 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5954 | repr = PyBytes_FromStringAndSize(NULL, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5955 | 2 |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5956 | + expandsize*len |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5957 | + 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5958 | if (repr == NULL) |
| 5959 | return NULL; |
| 5960 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5961 | p = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5962 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5963 | for (i = 0; i < len; i++) { |
Victor Stinner | 3326cb6 | 2011-11-10 20:15:25 +0100 | [diff] [blame] | 5964 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5965 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5966 | /* Escape backslashes */ |
| 5967 | if (ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5968 | *p++ = '\\'; |
| 5969 | *p++ = (char) ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5970 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5971 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5972 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5973 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 5974 | else if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 5975 | assert(ch <= MAX_UNICODE); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5976 | *p++ = '\\'; |
| 5977 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 5978 | *p++ = Py_hexdigits[(ch >> 28) & 0x0000000F]; |
| 5979 | *p++ = Py_hexdigits[(ch >> 24) & 0x0000000F]; |
| 5980 | *p++ = Py_hexdigits[(ch >> 20) & 0x0000000F]; |
| 5981 | *p++ = Py_hexdigits[(ch >> 16) & 0x0000000F]; |
| 5982 | *p++ = Py_hexdigits[(ch >> 12) & 0x0000000F]; |
| 5983 | *p++ = Py_hexdigits[(ch >> 8) & 0x0000000F]; |
| 5984 | *p++ = Py_hexdigits[(ch >> 4) & 0x0000000F]; |
| 5985 | *p++ = Py_hexdigits[ch & 0x0000000F]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5986 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5987 | } |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5988 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5989 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5990 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5991 | *p++ = '\\'; |
| 5992 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 5993 | *p++ = Py_hexdigits[(ch >> 12) & 0x000F]; |
| 5994 | *p++ = Py_hexdigits[(ch >> 8) & 0x000F]; |
| 5995 | *p++ = Py_hexdigits[(ch >> 4) & 0x000F]; |
| 5996 | *p++ = Py_hexdigits[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5997 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5998 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5999 | /* Map special whitespace to '\t', \n', '\r' */ |
| 6000 | else if (ch == '\t') { |
| 6001 | *p++ = '\\'; |
| 6002 | *p++ = 't'; |
| 6003 | } |
| 6004 | else if (ch == '\n') { |
| 6005 | *p++ = '\\'; |
| 6006 | *p++ = 'n'; |
| 6007 | } |
| 6008 | else if (ch == '\r') { |
| 6009 | *p++ = '\\'; |
| 6010 | *p++ = 'r'; |
| 6011 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 6012 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 6013 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 6014 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6015 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 6016 | *p++ = 'x'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6017 | *p++ = Py_hexdigits[(ch >> 4) & 0x000F]; |
| 6018 | *p++ = Py_hexdigits[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6019 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 6020 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6021 | /* Copy everything else as-is */ |
| 6022 | else |
| 6023 | *p++ = (char) ch; |
| 6024 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6025 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6026 | assert(p - PyBytes_AS_STRING(repr) > 0); |
| 6027 | if (_PyBytes_Resize(&repr, p - PyBytes_AS_STRING(repr)) < 0) |
| 6028 | return NULL; |
| 6029 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6030 | } |
| 6031 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6032 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6033 | PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
| 6034 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6035 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6036 | PyObject *result; |
| 6037 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 6038 | if (tmp == NULL) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6039 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6040 | result = PyUnicode_AsUnicodeEscapeString(tmp); |
| 6041 | Py_DECREF(tmp); |
| 6042 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6043 | } |
| 6044 | |
| 6045 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 6046 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6047 | PyObject * |
| 6048 | PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6049 | Py_ssize_t size, |
| 6050 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6051 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6052 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6053 | Py_ssize_t startinpos; |
| 6054 | Py_ssize_t endinpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6055 | _PyUnicodeWriter writer; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6056 | const char *end; |
| 6057 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6058 | PyObject *errorHandler = NULL; |
| 6059 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6060 | |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 6061 | if (size == 0) |
| 6062 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6063 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6064 | /* Escaped strings will always be longer than the resulting |
| 6065 | 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] | 6066 | length after conversion to the true value. (But decoding error |
| 6067 | handler might have to resize the string) */ |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 6068 | _PyUnicodeWriter_Init(&writer); |
| 6069 | writer.min_length = size; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6070 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6071 | end = s + size; |
| 6072 | while (s < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6073 | unsigned char c; |
| 6074 | Py_UCS4 x; |
| 6075 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6076 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6077 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6078 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 6079 | if (*s != '\\') { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6080 | x = (unsigned char)*s++; |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 6081 | if (_PyUnicodeWriter_WriteCharInline(&writer, x) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6082 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6083 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6084 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6085 | startinpos = s-starts; |
| 6086 | |
| 6087 | /* \u-escapes are only interpreted iff the number of leading |
| 6088 | backslashes if odd */ |
| 6089 | bs = s; |
| 6090 | for (;s < end;) { |
| 6091 | if (*s != '\\') |
| 6092 | break; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6093 | x = (unsigned char)*s++; |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 6094 | if (_PyUnicodeWriter_WriteCharInline(&writer, x) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6095 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6096 | } |
| 6097 | if (((s - bs) & 1) == 0 || |
| 6098 | s >= end || |
| 6099 | (*s != 'u' && *s != 'U')) { |
| 6100 | continue; |
| 6101 | } |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6102 | writer.pos--; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6103 | count = *s=='u' ? 4 : 8; |
| 6104 | s++; |
| 6105 | |
| 6106 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6107 | for (x = 0, i = 0; i < count; ++i, ++s) { |
| 6108 | c = (unsigned char)*s; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 6109 | if (!Py_ISXDIGIT(c)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6110 | endinpos = s-starts; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6111 | if (unicode_decode_call_errorhandler_writer( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6112 | errors, &errorHandler, |
| 6113 | "rawunicodeescape", "truncated \\uXXXX", |
| 6114 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6115 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6116 | goto onError; |
| 6117 | goto nextByte; |
| 6118 | } |
| 6119 | x = (x<<4) & ~0xF; |
| 6120 | if (c >= '0' && c <= '9') |
| 6121 | x += c - '0'; |
| 6122 | else if (c >= 'a' && c <= 'f') |
| 6123 | x += 10 + c - 'a'; |
| 6124 | else |
| 6125 | x += 10 + c - 'A'; |
| 6126 | } |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 6127 | if (x <= MAX_UNICODE) { |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 6128 | if (_PyUnicodeWriter_WriteCharInline(&writer, x) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6129 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6130 | } |
| 6131 | else { |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 6132 | endinpos = s-starts; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6133 | if (unicode_decode_call_errorhandler_writer( |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6134 | errors, &errorHandler, |
| 6135 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6136 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6137 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6138 | goto onError; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6139 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6140 | nextByte: |
| 6141 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6142 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6143 | Py_XDECREF(errorHandler); |
| 6144 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6145 | return _PyUnicodeWriter_Finish(&writer); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6146 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6147 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6148 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6149 | Py_XDECREF(errorHandler); |
| 6150 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6151 | return NULL; |
| 6152 | } |
| 6153 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6154 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6155 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6156 | PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6157 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6158 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6159 | char *p; |
| 6160 | char *q; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6161 | Py_ssize_t expandsize, pos; |
| 6162 | int kind; |
| 6163 | void *data; |
| 6164 | Py_ssize_t len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6165 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6166 | if (!PyUnicode_Check(unicode)) { |
| 6167 | PyErr_BadArgument(); |
| 6168 | return NULL; |
| 6169 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6170 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6171 | return NULL; |
| 6172 | kind = PyUnicode_KIND(unicode); |
| 6173 | data = PyUnicode_DATA(unicode); |
| 6174 | len = PyUnicode_GET_LENGTH(unicode); |
Benjamin Peterson | 1518e87 | 2011-11-23 10:44:52 -0600 | [diff] [blame] | 6175 | /* 4 byte characters can take up 10 bytes, 2 byte characters can take up 6 |
| 6176 | bytes, and 1 byte characters 4. */ |
| 6177 | expandsize = kind * 2 + 2; |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 6178 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6179 | if (len > PY_SSIZE_T_MAX / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6180 | return PyErr_NoMemory(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6181 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6182 | repr = PyBytes_FromStringAndSize(NULL, expandsize * len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6183 | if (repr == NULL) |
| 6184 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6185 | if (len == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6186 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6187 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6188 | p = q = PyBytes_AS_STRING(repr); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6189 | for (pos = 0; pos < len; pos++) { |
| 6190 | Py_UCS4 ch = PyUnicode_READ(kind, data, pos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6191 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 6192 | if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 6193 | assert(ch <= MAX_UNICODE); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6194 | *p++ = '\\'; |
| 6195 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6196 | *p++ = Py_hexdigits[(ch >> 28) & 0xf]; |
| 6197 | *p++ = Py_hexdigits[(ch >> 24) & 0xf]; |
| 6198 | *p++ = Py_hexdigits[(ch >> 20) & 0xf]; |
| 6199 | *p++ = Py_hexdigits[(ch >> 16) & 0xf]; |
| 6200 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 6201 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 6202 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 6203 | *p++ = Py_hexdigits[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6204 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6205 | /* Map 16-bit characters to '\uxxxx' */ |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6206 | else if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6207 | *p++ = '\\'; |
| 6208 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6209 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 6210 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 6211 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 6212 | *p++ = Py_hexdigits[ch & 15]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6213 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6214 | /* Copy everything else as-is */ |
| 6215 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6216 | *p++ = (char) ch; |
| 6217 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6218 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6219 | assert(p > q); |
| 6220 | if (_PyBytes_Resize(&repr, p - q) < 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6221 | return NULL; |
| 6222 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6223 | } |
| 6224 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6225 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6226 | PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
| 6227 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6228 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6229 | PyObject *result; |
| 6230 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 6231 | if (tmp == NULL) |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 6232 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6233 | result = PyUnicode_AsRawUnicodeEscapeString(tmp); |
| 6234 | Py_DECREF(tmp); |
| 6235 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6236 | } |
| 6237 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6238 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 6239 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6240 | PyObject * |
| 6241 | _PyUnicode_DecodeUnicodeInternal(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6242 | Py_ssize_t size, |
| 6243 | const char *errors) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6244 | { |
| 6245 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6246 | Py_ssize_t startinpos; |
| 6247 | Py_ssize_t endinpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6248 | _PyUnicodeWriter writer; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6249 | const char *end; |
| 6250 | const char *reason; |
| 6251 | PyObject *errorHandler = NULL; |
| 6252 | PyObject *exc = NULL; |
| 6253 | |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6254 | if (PyErr_WarnEx(PyExc_DeprecationWarning, |
Ezio Melotti | 11060a4 | 2011-11-16 09:39:10 +0200 | [diff] [blame] | 6255 | "unicode_internal codec has been deprecated", |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6256 | 1)) |
| 6257 | return NULL; |
| 6258 | |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 6259 | if (size == 0) |
| 6260 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6261 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 6262 | _PyUnicodeWriter_Init(&writer); |
| 6263 | if (size / Py_UNICODE_SIZE > PY_SSIZE_T_MAX - 1) { |
| 6264 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6265 | goto onError; |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 6266 | } |
| 6267 | writer.min_length = (size + (Py_UNICODE_SIZE - 1)) / Py_UNICODE_SIZE; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6268 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 6269 | end = s + size; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6270 | while (s < end) { |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6271 | Py_UNICODE uch; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6272 | Py_UCS4 ch; |
Serhiy Storchaka | 03ee12e | 2013-02-07 16:25:25 +0200 | [diff] [blame] | 6273 | if (end - s < Py_UNICODE_SIZE) { |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6274 | endinpos = end-starts; |
| 6275 | reason = "truncated input"; |
| 6276 | goto error; |
| 6277 | } |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6278 | /* We copy the raw representation one byte at a time because the |
| 6279 | pointer may be unaligned (see test_codeccallbacks). */ |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6280 | ((char *) &uch)[0] = s[0]; |
| 6281 | ((char *) &uch)[1] = s[1]; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6282 | #ifdef Py_UNICODE_WIDE |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6283 | ((char *) &uch)[2] = s[2]; |
| 6284 | ((char *) &uch)[3] = s[3]; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6285 | #endif |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6286 | ch = uch; |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6287 | #ifdef Py_UNICODE_WIDE |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6288 | /* We have to sanity check the raw data, otherwise doom looms for |
| 6289 | some malformed UCS-4 data. */ |
Serhiy Storchaka | 03ee12e | 2013-02-07 16:25:25 +0200 | [diff] [blame] | 6290 | if (ch > 0x10ffff) { |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6291 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 6292 | reason = "illegal code point (> 0x10FFFF)"; |
| 6293 | goto error; |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6294 | } |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6295 | #endif |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6296 | s += Py_UNICODE_SIZE; |
| 6297 | #ifndef Py_UNICODE_WIDE |
Serhiy Storchaka | 03ee12e | 2013-02-07 16:25:25 +0200 | [diff] [blame] | 6298 | if (Py_UNICODE_IS_HIGH_SURROGATE(ch) && end - s >= Py_UNICODE_SIZE) |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6299 | { |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6300 | Py_UNICODE uch2; |
| 6301 | ((char *) &uch2)[0] = s[0]; |
| 6302 | ((char *) &uch2)[1] = s[1]; |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 6303 | if (Py_UNICODE_IS_LOW_SURROGATE(uch2)) |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6304 | { |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 6305 | ch = Py_UNICODE_JOIN_SURROGATES(uch, uch2); |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6306 | s += Py_UNICODE_SIZE; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6307 | } |
| 6308 | } |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6309 | #endif |
| 6310 | |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 6311 | if (_PyUnicodeWriter_WriteCharInline(&writer, ch) < 0) |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6312 | goto onError; |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6313 | continue; |
| 6314 | |
| 6315 | error: |
| 6316 | startinpos = s - starts; |
Serhiy Storchaka | d0c79dc | 2013-02-07 16:26:55 +0200 | [diff] [blame] | 6317 | if (unicode_decode_call_errorhandler_writer( |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6318 | errors, &errorHandler, |
| 6319 | "unicode_internal", reason, |
| 6320 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Serhiy Storchaka | d0c79dc | 2013-02-07 16:26:55 +0200 | [diff] [blame] | 6321 | &writer)) |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6322 | goto onError; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6323 | } |
| 6324 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6325 | Py_XDECREF(errorHandler); |
| 6326 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6327 | return _PyUnicodeWriter_Finish(&writer); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6328 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6329 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6330 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6331 | Py_XDECREF(errorHandler); |
| 6332 | Py_XDECREF(exc); |
| 6333 | return NULL; |
| 6334 | } |
| 6335 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6336 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 6337 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6338 | PyObject * |
| 6339 | PyUnicode_DecodeLatin1(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6340 | Py_ssize_t size, |
| 6341 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6342 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6343 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 6344 | return _PyUnicode_FromUCS1((unsigned char*)s, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6345 | } |
| 6346 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6347 | /* create or adjust a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6348 | static void |
| 6349 | make_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6350 | const char *encoding, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6351 | PyObject *unicode, |
| 6352 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6353 | const char *reason) |
| 6354 | { |
| 6355 | if (*exceptionObject == NULL) { |
| 6356 | *exceptionObject = PyObject_CallFunction( |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6357 | PyExc_UnicodeEncodeError, "sOnns", |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6358 | encoding, unicode, startpos, endpos, reason); |
| 6359 | } |
| 6360 | else { |
| 6361 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 6362 | goto onError; |
| 6363 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 6364 | goto onError; |
| 6365 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 6366 | goto onError; |
| 6367 | return; |
| 6368 | onError: |
| 6369 | Py_DECREF(*exceptionObject); |
| 6370 | *exceptionObject = NULL; |
| 6371 | } |
| 6372 | } |
| 6373 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6374 | /* raises a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6375 | static void |
| 6376 | raise_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6377 | const char *encoding, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6378 | PyObject *unicode, |
| 6379 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6380 | const char *reason) |
| 6381 | { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6382 | make_encode_exception(exceptionObject, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6383 | encoding, unicode, startpos, endpos, reason); |
| 6384 | if (*exceptionObject != NULL) |
| 6385 | PyCodec_StrictErrors(*exceptionObject); |
| 6386 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6387 | |
| 6388 | /* error handling callback helper: |
| 6389 | build arguments, call the callback and check the arguments, |
| 6390 | put the result into newpos and return the replacement string, which |
| 6391 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6392 | static PyObject * |
| 6393 | unicode_encode_call_errorhandler(const char *errors, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6394 | PyObject **errorHandler, |
| 6395 | const char *encoding, const char *reason, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6396 | PyObject *unicode, PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6397 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6398 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6399 | { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6400 | 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] | 6401 | Py_ssize_t len; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6402 | PyObject *restuple; |
| 6403 | PyObject *resunicode; |
| 6404 | |
| 6405 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6406 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6407 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6408 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6409 | } |
| 6410 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6411 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6412 | return NULL; |
| 6413 | len = PyUnicode_GET_LENGTH(unicode); |
| 6414 | |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6415 | make_encode_exception(exceptionObject, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6416 | encoding, unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6417 | if (*exceptionObject == 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 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6421 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6422 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6423 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6424 | if (!PyTuple_Check(restuple)) { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6425 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6426 | Py_DECREF(restuple); |
| 6427 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6428 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6429 | if (!PyArg_ParseTuple(restuple, argparse, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6430 | &resunicode, newpos)) { |
| 6431 | Py_DECREF(restuple); |
| 6432 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6433 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6434 | if (!PyUnicode_Check(resunicode) && !PyBytes_Check(resunicode)) { |
| 6435 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
| 6436 | Py_DECREF(restuple); |
| 6437 | return NULL; |
| 6438 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6439 | if (*newpos<0) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6440 | *newpos = len + *newpos; |
| 6441 | if (*newpos<0 || *newpos>len) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6442 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 6443 | Py_DECREF(restuple); |
| 6444 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 6445 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6446 | Py_INCREF(resunicode); |
| 6447 | Py_DECREF(restuple); |
| 6448 | return resunicode; |
| 6449 | } |
| 6450 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6451 | static PyObject * |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6452 | unicode_encode_ucs1(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6453 | const char *errors, |
Victor Stinner | fcd9653 | 2011-11-04 00:28:50 +0100 | [diff] [blame] | 6454 | unsigned int limit) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6455 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6456 | /* input state */ |
| 6457 | Py_ssize_t pos=0, size; |
| 6458 | int kind; |
| 6459 | void *data; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6460 | /* output object */ |
| 6461 | PyObject *res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6462 | /* pointer into the output */ |
| 6463 | char *str; |
| 6464 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6465 | Py_ssize_t ressize; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6466 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 6467 | 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] | 6468 | PyObject *errorHandler = NULL; |
| 6469 | PyObject *exc = NULL; |
| 6470 | /* the following variable is used for caching string comparisons |
| 6471 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 6472 | int known_errorHandler = -1; |
| 6473 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6474 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6475 | return NULL; |
| 6476 | size = PyUnicode_GET_LENGTH(unicode); |
| 6477 | kind = PyUnicode_KIND(unicode); |
| 6478 | data = PyUnicode_DATA(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6479 | /* allocate enough for a simple encoding without |
| 6480 | replacements, if we need more, we'll resize */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6481 | if (size == 0) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6482 | return PyBytes_FromStringAndSize(NULL, 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6483 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6484 | if (res == NULL) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6485 | return NULL; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6486 | str = PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6487 | ressize = size; |
| 6488 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6489 | while (pos < size) { |
| 6490 | Py_UCS4 c = PyUnicode_READ(kind, data, pos); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6491 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6492 | /* can we encode this? */ |
| 6493 | if (c<limit) { |
| 6494 | /* no overflow check, because we know that the space is enough */ |
| 6495 | *str++ = (char)c; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6496 | ++pos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6497 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6498 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6499 | Py_ssize_t requiredsize; |
| 6500 | PyObject *repunicode; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6501 | Py_ssize_t repsize, newpos, respos, i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6502 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6503 | Py_ssize_t collstart = pos; |
| 6504 | Py_ssize_t collend = pos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6505 | /* find all unecodable characters */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6506 | while ((collend < size) && (PyUnicode_READ(kind, data, collend)>=limit)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6507 | ++collend; |
| 6508 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 6509 | if (known_errorHandler==-1) { |
| 6510 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 6511 | known_errorHandler = 1; |
| 6512 | else if (!strcmp(errors, "replace")) |
| 6513 | known_errorHandler = 2; |
| 6514 | else if (!strcmp(errors, "ignore")) |
| 6515 | known_errorHandler = 3; |
| 6516 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 6517 | known_errorHandler = 4; |
| 6518 | else |
| 6519 | known_errorHandler = 0; |
| 6520 | } |
| 6521 | switch (known_errorHandler) { |
| 6522 | case 1: /* strict */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6523 | raise_encode_exception(&exc, encoding, unicode, collstart, collend, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6524 | goto onError; |
| 6525 | case 2: /* replace */ |
| 6526 | while (collstart++<collend) |
| 6527 | *str++ = '?'; /* fall through */ |
| 6528 | case 3: /* ignore */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6529 | pos = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6530 | break; |
| 6531 | case 4: /* xmlcharrefreplace */ |
| 6532 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6533 | /* determine replacement size */ |
| 6534 | for (i = collstart, repsize = 0; i < collend; ++i) { |
| 6535 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 6536 | if (ch < 10) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6537 | repsize += 2+1+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6538 | else if (ch < 100) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6539 | repsize += 2+2+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6540 | else if (ch < 1000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6541 | repsize += 2+3+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6542 | else if (ch < 10000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6543 | repsize += 2+4+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6544 | else if (ch < 100000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6545 | repsize += 2+5+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6546 | else if (ch < 1000000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6547 | repsize += 2+6+1; |
Victor Stinner | 0d3721d | 2011-11-22 03:27:53 +0100 | [diff] [blame] | 6548 | else { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 6549 | assert(ch <= MAX_UNICODE); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6550 | repsize += 2+7+1; |
Victor Stinner | 0d3721d | 2011-11-22 03:27:53 +0100 | [diff] [blame] | 6551 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6552 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6553 | requiredsize = respos+repsize+(size-collend); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6554 | if (requiredsize > ressize) { |
| 6555 | if (requiredsize<2*ressize) |
| 6556 | requiredsize = 2*ressize; |
| 6557 | if (_PyBytes_Resize(&res, requiredsize)) |
| 6558 | goto onError; |
| 6559 | str = PyBytes_AS_STRING(res) + respos; |
| 6560 | ressize = requiredsize; |
| 6561 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6562 | /* generate replacement */ |
| 6563 | for (i = collstart; i < collend; ++i) { |
| 6564 | str += sprintf(str, "&#%d;", PyUnicode_READ(kind, data, i)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6565 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6566 | pos = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6567 | break; |
| 6568 | default: |
| 6569 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6570 | encoding, reason, unicode, &exc, |
| 6571 | collstart, collend, &newpos); |
| 6572 | if (repunicode == NULL || (PyUnicode_Check(repunicode) && |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6573 | PyUnicode_READY(repunicode) == -1)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6574 | goto onError; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6575 | if (PyBytes_Check(repunicode)) { |
| 6576 | /* Directly copy bytes result to output. */ |
| 6577 | repsize = PyBytes_Size(repunicode); |
| 6578 | if (repsize > 1) { |
| 6579 | /* Make room for all additional bytes. */ |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6580 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6581 | if (_PyBytes_Resize(&res, ressize+repsize-1)) { |
| 6582 | Py_DECREF(repunicode); |
| 6583 | goto onError; |
| 6584 | } |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6585 | str = PyBytes_AS_STRING(res) + respos; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6586 | ressize += repsize-1; |
| 6587 | } |
| 6588 | memcpy(str, PyBytes_AsString(repunicode), repsize); |
| 6589 | str += repsize; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6590 | pos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6591 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6592 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6593 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6594 | /* need more space? (at least enough for what we |
| 6595 | have+the replacement+the rest of the string, so |
| 6596 | we won't have to check space for encodable characters) */ |
| 6597 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6598 | repsize = PyUnicode_GET_LENGTH(repunicode); |
| 6599 | requiredsize = respos+repsize+(size-collend); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6600 | if (requiredsize > ressize) { |
| 6601 | if (requiredsize<2*ressize) |
| 6602 | requiredsize = 2*ressize; |
| 6603 | if (_PyBytes_Resize(&res, requiredsize)) { |
| 6604 | Py_DECREF(repunicode); |
| 6605 | goto onError; |
| 6606 | } |
| 6607 | str = PyBytes_AS_STRING(res) + respos; |
| 6608 | ressize = requiredsize; |
| 6609 | } |
| 6610 | /* check if there is anything unencodable in the replacement |
| 6611 | and copy it to the output */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6612 | for (i = 0; repsize-->0; ++i, ++str) { |
| 6613 | c = PyUnicode_READ_CHAR(repunicode, i); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6614 | if (c >= limit) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6615 | raise_encode_exception(&exc, encoding, unicode, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6616 | pos, pos+1, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6617 | Py_DECREF(repunicode); |
| 6618 | goto onError; |
| 6619 | } |
| 6620 | *str = (char)c; |
| 6621 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6622 | pos = newpos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6623 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6624 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6625 | } |
| 6626 | } |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6627 | /* Resize if we allocated to much */ |
| 6628 | size = str - PyBytes_AS_STRING(res); |
| 6629 | if (size < ressize) { /* If this falls res will be NULL */ |
Alexandre Vassalotti | bad1b92 | 2008-12-27 09:49:09 +0000 | [diff] [blame] | 6630 | assert(size >= 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6631 | if (_PyBytes_Resize(&res, size) < 0) |
| 6632 | goto onError; |
| 6633 | } |
| 6634 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6635 | Py_XDECREF(errorHandler); |
| 6636 | Py_XDECREF(exc); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6637 | return res; |
| 6638 | |
| 6639 | onError: |
| 6640 | Py_XDECREF(res); |
| 6641 | Py_XDECREF(errorHandler); |
| 6642 | Py_XDECREF(exc); |
| 6643 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6644 | } |
| 6645 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6646 | /* Deprecated */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6647 | PyObject * |
| 6648 | PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6649 | Py_ssize_t size, |
| 6650 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6651 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6652 | PyObject *result; |
| 6653 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 6654 | if (unicode == NULL) |
| 6655 | return NULL; |
| 6656 | result = unicode_encode_ucs1(unicode, errors, 256); |
| 6657 | Py_DECREF(unicode); |
| 6658 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6659 | } |
| 6660 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6661 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6662 | _PyUnicode_AsLatin1String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6663 | { |
| 6664 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6665 | PyErr_BadArgument(); |
| 6666 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6667 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6668 | if (PyUnicode_READY(unicode) == -1) |
| 6669 | return NULL; |
| 6670 | /* Fast path: if it is a one-byte string, construct |
| 6671 | bytes object directly. */ |
| 6672 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) |
| 6673 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6674 | PyUnicode_GET_LENGTH(unicode)); |
| 6675 | /* Non-Latin-1 characters present. Defer to above function to |
| 6676 | raise the exception. */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6677 | return unicode_encode_ucs1(unicode, errors, 256); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6678 | } |
| 6679 | |
| 6680 | PyObject* |
| 6681 | PyUnicode_AsLatin1String(PyObject *unicode) |
| 6682 | { |
| 6683 | return _PyUnicode_AsLatin1String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6684 | } |
| 6685 | |
| 6686 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 6687 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6688 | PyObject * |
| 6689 | PyUnicode_DecodeASCII(const char *s, |
| 6690 | Py_ssize_t size, |
| 6691 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6692 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6693 | const char *starts = s; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6694 | _PyUnicodeWriter writer; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6695 | int kind; |
| 6696 | void *data; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6697 | Py_ssize_t startinpos; |
| 6698 | Py_ssize_t endinpos; |
| 6699 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6700 | const char *e; |
| 6701 | PyObject *errorHandler = NULL; |
| 6702 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6703 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6704 | if (size == 0) |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 6705 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 6706 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6707 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6708 | if (size == 1 && (unsigned char)s[0] < 128) |
| 6709 | return get_latin1_char((unsigned char)s[0]); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6710 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 6711 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 6712 | writer.min_length = size; |
| 6713 | if (_PyUnicodeWriter_Prepare(&writer, writer.min_length, 127) < 0) |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 6714 | return NULL; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6715 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6716 | e = s + size; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6717 | data = writer.data; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6718 | outpos = ascii_decode(s, e, (Py_UCS1 *)data); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6719 | writer.pos = outpos; |
| 6720 | if (writer.pos == size) |
| 6721 | return _PyUnicodeWriter_Finish(&writer); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6722 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6723 | s += writer.pos; |
| 6724 | kind = writer.kind; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6725 | while (s < e) { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 6726 | unsigned char c = (unsigned char)*s; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6727 | if (c < 128) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6728 | PyUnicode_WRITE(kind, data, writer.pos, c); |
| 6729 | writer.pos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6730 | ++s; |
| 6731 | } |
| 6732 | else { |
| 6733 | startinpos = s-starts; |
| 6734 | endinpos = startinpos + 1; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6735 | if (unicode_decode_call_errorhandler_writer( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6736 | errors, &errorHandler, |
| 6737 | "ascii", "ordinal not in range(128)", |
| 6738 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6739 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6740 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6741 | kind = writer.kind; |
| 6742 | data = writer.data; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6743 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6744 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6745 | Py_XDECREF(errorHandler); |
| 6746 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6747 | return _PyUnicodeWriter_Finish(&writer); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6748 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6749 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6750 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6751 | Py_XDECREF(errorHandler); |
| 6752 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6753 | return NULL; |
| 6754 | } |
| 6755 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6756 | /* Deprecated */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6757 | PyObject * |
| 6758 | PyUnicode_EncodeASCII(const Py_UNICODE *p, |
| 6759 | Py_ssize_t size, |
| 6760 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6761 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6762 | PyObject *result; |
| 6763 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 6764 | if (unicode == NULL) |
| 6765 | return NULL; |
| 6766 | result = unicode_encode_ucs1(unicode, errors, 128); |
| 6767 | Py_DECREF(unicode); |
| 6768 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6769 | } |
| 6770 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6771 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6772 | _PyUnicode_AsASCIIString(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6773 | { |
| 6774 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6775 | PyErr_BadArgument(); |
| 6776 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6777 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6778 | if (PyUnicode_READY(unicode) == -1) |
| 6779 | return NULL; |
| 6780 | /* Fast path: if it is an ASCII-only string, construct bytes object |
| 6781 | directly. Else defer to above function to raise the exception. */ |
Victor Stinner | af03757 | 2013-04-14 18:44:10 +0200 | [diff] [blame] | 6782 | if (PyUnicode_IS_ASCII(unicode)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6783 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6784 | PyUnicode_GET_LENGTH(unicode)); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6785 | return unicode_encode_ucs1(unicode, errors, 128); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6786 | } |
| 6787 | |
| 6788 | PyObject * |
| 6789 | PyUnicode_AsASCIIString(PyObject *unicode) |
| 6790 | { |
| 6791 | return _PyUnicode_AsASCIIString(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6792 | } |
| 6793 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 6794 | #ifdef HAVE_MBCS |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6795 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6796 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6797 | |
Hirokazu Yamamoto | 3530246 | 2009-03-21 13:23:27 +0000 | [diff] [blame] | 6798 | #if SIZEOF_INT < SIZEOF_SIZE_T |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6799 | #define NEED_RETRY |
| 6800 | #endif |
| 6801 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6802 | #ifndef WC_ERR_INVALID_CHARS |
| 6803 | # define WC_ERR_INVALID_CHARS 0x0080 |
| 6804 | #endif |
| 6805 | |
| 6806 | static char* |
| 6807 | code_page_name(UINT code_page, PyObject **obj) |
| 6808 | { |
| 6809 | *obj = NULL; |
| 6810 | if (code_page == CP_ACP) |
| 6811 | return "mbcs"; |
| 6812 | if (code_page == CP_UTF7) |
| 6813 | return "CP_UTF7"; |
| 6814 | if (code_page == CP_UTF8) |
| 6815 | return "CP_UTF8"; |
| 6816 | |
| 6817 | *obj = PyBytes_FromFormat("cp%u", code_page); |
| 6818 | if (*obj == NULL) |
| 6819 | return NULL; |
| 6820 | return PyBytes_AS_STRING(*obj); |
| 6821 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6822 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6823 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6824 | is_dbcs_lead_byte(UINT code_page, const char *s, int offset) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6825 | { |
| 6826 | const char *curr = s + offset; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6827 | const char *prev; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6828 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6829 | if (!IsDBCSLeadByteEx(code_page, *curr)) |
| 6830 | return 0; |
| 6831 | |
| 6832 | prev = CharPrevExA(code_page, s, curr, 0); |
| 6833 | if (prev == curr) |
| 6834 | return 1; |
| 6835 | /* FIXME: This code is limited to "true" double-byte encodings, |
| 6836 | as it assumes an incomplete character consists of a single |
| 6837 | byte. */ |
| 6838 | if (curr - prev == 2) |
| 6839 | return 1; |
| 6840 | if (!IsDBCSLeadByteEx(code_page, *prev)) |
| 6841 | return 1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6842 | return 0; |
| 6843 | } |
| 6844 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6845 | static DWORD |
| 6846 | decode_code_page_flags(UINT code_page) |
| 6847 | { |
| 6848 | if (code_page == CP_UTF7) { |
| 6849 | /* The CP_UTF7 decoder only supports flags=0 */ |
| 6850 | return 0; |
| 6851 | } |
| 6852 | else |
| 6853 | return MB_ERR_INVALID_CHARS; |
| 6854 | } |
| 6855 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6856 | /* |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6857 | * Decode a byte string from a Windows code page into unicode object in strict |
| 6858 | * mode. |
| 6859 | * |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 6860 | * Returns consumed size if succeed, returns -2 on decode error, or raise an |
| 6861 | * OSError and returns -1 on other error. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6862 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6863 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6864 | decode_code_page_strict(UINT code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6865 | PyObject **v, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6866 | const char *in, |
| 6867 | int insize) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6868 | { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6869 | const DWORD flags = decode_code_page_flags(code_page); |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 6870 | wchar_t *out; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6871 | DWORD outsize; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6872 | |
| 6873 | /* First get the size of the result */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6874 | assert(insize > 0); |
| 6875 | outsize = MultiByteToWideChar(code_page, flags, in, insize, NULL, 0); |
| 6876 | if (outsize <= 0) |
| 6877 | goto error; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6878 | |
| 6879 | if (*v == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6880 | /* Create unicode object */ |
Victor Stinner | ab59594 | 2011-12-17 04:59:06 +0100 | [diff] [blame] | 6881 | /* FIXME: don't use _PyUnicode_New(), but allocate a wchar_t* buffer */ |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6882 | *v = (PyObject*)_PyUnicode_New(outsize); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6883 | if (*v == NULL) |
| 6884 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6885 | out = PyUnicode_AS_UNICODE(*v); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6886 | } |
| 6887 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6888 | /* Extend unicode object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6889 | Py_ssize_t n = PyUnicode_GET_SIZE(*v); |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 6890 | if (unicode_resize(v, n + outsize) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6891 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6892 | out = PyUnicode_AS_UNICODE(*v) + n; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6893 | } |
| 6894 | |
| 6895 | /* Do the conversion */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6896 | outsize = MultiByteToWideChar(code_page, flags, in, insize, out, outsize); |
| 6897 | if (outsize <= 0) |
| 6898 | goto error; |
| 6899 | return insize; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6900 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6901 | error: |
| 6902 | if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION) |
| 6903 | return -2; |
| 6904 | PyErr_SetFromWindowsErr(0); |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6905 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6906 | } |
| 6907 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6908 | /* |
| 6909 | * Decode a byte string from a code page into unicode object with an error |
| 6910 | * handler. |
| 6911 | * |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 6912 | * Returns consumed size if succeed, or raise an OSError or |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6913 | * UnicodeDecodeError exception and returns -1 on error. |
| 6914 | */ |
| 6915 | static int |
| 6916 | decode_code_page_errors(UINT code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6917 | PyObject **v, |
| 6918 | const char *in, const int size, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6919 | const char *errors) |
| 6920 | { |
| 6921 | const char *startin = in; |
| 6922 | const char *endin = in + size; |
| 6923 | const DWORD flags = decode_code_page_flags(code_page); |
| 6924 | /* Ideally, we should get reason from FormatMessage. This is the Windows |
| 6925 | 2000 English version of the message. */ |
| 6926 | const char *reason = "No mapping for the Unicode character exists " |
| 6927 | "in the target code page."; |
| 6928 | /* each step cannot decode more than 1 character, but a character can be |
| 6929 | represented as a surrogate pair */ |
| 6930 | wchar_t buffer[2], *startout, *out; |
Victor Stinner | 9f067f4 | 2013-06-05 00:21:31 +0200 | [diff] [blame] | 6931 | int insize; |
| 6932 | Py_ssize_t outsize; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6933 | PyObject *errorHandler = NULL; |
| 6934 | PyObject *exc = NULL; |
| 6935 | PyObject *encoding_obj = NULL; |
| 6936 | char *encoding; |
| 6937 | DWORD err; |
| 6938 | int ret = -1; |
| 6939 | |
| 6940 | assert(size > 0); |
| 6941 | |
| 6942 | encoding = code_page_name(code_page, &encoding_obj); |
| 6943 | if (encoding == NULL) |
| 6944 | return -1; |
| 6945 | |
| 6946 | if (errors == NULL || strcmp(errors, "strict") == 0) { |
| 6947 | /* The last error was ERROR_NO_UNICODE_TRANSLATION, then we raise a |
| 6948 | UnicodeDecodeError. */ |
| 6949 | make_decode_exception(&exc, encoding, in, size, 0, 0, reason); |
| 6950 | if (exc != NULL) { |
| 6951 | PyCodec_StrictErrors(exc); |
| 6952 | Py_CLEAR(exc); |
| 6953 | } |
| 6954 | goto error; |
| 6955 | } |
| 6956 | |
| 6957 | if (*v == NULL) { |
| 6958 | /* Create unicode object */ |
| 6959 | if (size > PY_SSIZE_T_MAX / (Py_ssize_t)Py_ARRAY_LENGTH(buffer)) { |
| 6960 | PyErr_NoMemory(); |
| 6961 | goto error; |
| 6962 | } |
Victor Stinner | ab59594 | 2011-12-17 04:59:06 +0100 | [diff] [blame] | 6963 | /* FIXME: don't use _PyUnicode_New(), but allocate a wchar_t* buffer */ |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6964 | *v = (PyObject*)_PyUnicode_New(size * Py_ARRAY_LENGTH(buffer)); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6965 | if (*v == NULL) |
| 6966 | goto error; |
| 6967 | startout = PyUnicode_AS_UNICODE(*v); |
| 6968 | } |
| 6969 | else { |
| 6970 | /* Extend unicode object */ |
| 6971 | Py_ssize_t n = PyUnicode_GET_SIZE(*v); |
| 6972 | if (size > (PY_SSIZE_T_MAX - n) / (Py_ssize_t)Py_ARRAY_LENGTH(buffer)) { |
| 6973 | PyErr_NoMemory(); |
| 6974 | goto error; |
| 6975 | } |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 6976 | if (unicode_resize(v, n + size * Py_ARRAY_LENGTH(buffer)) < 0) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6977 | goto error; |
| 6978 | startout = PyUnicode_AS_UNICODE(*v) + n; |
| 6979 | } |
| 6980 | |
| 6981 | /* Decode the byte string character per character */ |
| 6982 | out = startout; |
| 6983 | while (in < endin) |
| 6984 | { |
| 6985 | /* Decode a character */ |
| 6986 | insize = 1; |
| 6987 | do |
| 6988 | { |
| 6989 | outsize = MultiByteToWideChar(code_page, flags, |
| 6990 | in, insize, |
| 6991 | buffer, Py_ARRAY_LENGTH(buffer)); |
| 6992 | if (outsize > 0) |
| 6993 | break; |
| 6994 | err = GetLastError(); |
| 6995 | if (err != ERROR_NO_UNICODE_TRANSLATION |
| 6996 | && err != ERROR_INSUFFICIENT_BUFFER) |
| 6997 | { |
| 6998 | PyErr_SetFromWindowsErr(0); |
| 6999 | goto error; |
| 7000 | } |
| 7001 | insize++; |
| 7002 | } |
| 7003 | /* 4=maximum length of a UTF-8 sequence */ |
| 7004 | while (insize <= 4 && (in + insize) <= endin); |
| 7005 | |
| 7006 | if (outsize <= 0) { |
| 7007 | Py_ssize_t startinpos, endinpos, outpos; |
| 7008 | |
| 7009 | startinpos = in - startin; |
| 7010 | endinpos = startinpos + 1; |
| 7011 | outpos = out - PyUnicode_AS_UNICODE(*v); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7012 | if (unicode_decode_call_errorhandler_wchar( |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7013 | errors, &errorHandler, |
| 7014 | encoding, reason, |
| 7015 | &startin, &endin, &startinpos, &endinpos, &exc, &in, |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 7016 | v, &outpos)) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7017 | { |
| 7018 | goto error; |
| 7019 | } |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 7020 | out = PyUnicode_AS_UNICODE(*v) + outpos; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7021 | } |
| 7022 | else { |
| 7023 | in += insize; |
| 7024 | memcpy(out, buffer, outsize * sizeof(wchar_t)); |
| 7025 | out += outsize; |
| 7026 | } |
| 7027 | } |
| 7028 | |
| 7029 | /* write a NUL character at the end */ |
| 7030 | *out = 0; |
| 7031 | |
| 7032 | /* Extend unicode object */ |
| 7033 | outsize = out - startout; |
| 7034 | assert(outsize <= PyUnicode_WSTR_LENGTH(*v)); |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 7035 | if (unicode_resize(v, outsize) < 0) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7036 | goto error; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7037 | ret = size; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7038 | |
| 7039 | error: |
| 7040 | Py_XDECREF(encoding_obj); |
| 7041 | Py_XDECREF(errorHandler); |
| 7042 | Py_XDECREF(exc); |
| 7043 | return ret; |
| 7044 | } |
| 7045 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7046 | static PyObject * |
| 7047 | decode_code_page_stateful(int code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7048 | const char *s, Py_ssize_t size, |
| 7049 | const char *errors, Py_ssize_t *consumed) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7050 | { |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7051 | PyObject *v = NULL; |
| 7052 | int chunk_size, final, converted, done; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7053 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7054 | if (code_page < 0) { |
| 7055 | PyErr_SetString(PyExc_ValueError, "invalid code page number"); |
| 7056 | return NULL; |
| 7057 | } |
| 7058 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7059 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7060 | *consumed = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7061 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7062 | do |
| 7063 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7064 | #ifdef NEED_RETRY |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7065 | if (size > INT_MAX) { |
| 7066 | chunk_size = INT_MAX; |
| 7067 | final = 0; |
| 7068 | done = 0; |
| 7069 | } |
| 7070 | else |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7071 | #endif |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7072 | { |
| 7073 | chunk_size = (int)size; |
| 7074 | final = (consumed == NULL); |
| 7075 | done = 1; |
| 7076 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7077 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7078 | /* Skip trailing lead-byte unless 'final' is set */ |
| 7079 | if (!final && is_dbcs_lead_byte(code_page, s, chunk_size - 1)) |
| 7080 | --chunk_size; |
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 | if (chunk_size == 0 && done) { |
| 7083 | if (v != NULL) |
| 7084 | break; |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 7085 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7086 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7087 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7088 | |
| 7089 | converted = decode_code_page_strict(code_page, &v, |
| 7090 | s, chunk_size); |
| 7091 | if (converted == -2) |
| 7092 | converted = decode_code_page_errors(code_page, &v, |
| 7093 | s, chunk_size, |
| 7094 | errors); |
| 7095 | assert(converted != 0); |
| 7096 | |
| 7097 | if (converted < 0) { |
| 7098 | Py_XDECREF(v); |
| 7099 | return NULL; |
| 7100 | } |
| 7101 | |
| 7102 | if (consumed) |
| 7103 | *consumed += converted; |
| 7104 | |
| 7105 | s += converted; |
| 7106 | size -= converted; |
| 7107 | } while (!done); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7108 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 7109 | return unicode_result(v); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7110 | } |
| 7111 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7112 | PyObject * |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7113 | PyUnicode_DecodeCodePageStateful(int code_page, |
| 7114 | const char *s, |
| 7115 | Py_ssize_t size, |
| 7116 | const char *errors, |
| 7117 | Py_ssize_t *consumed) |
| 7118 | { |
| 7119 | return decode_code_page_stateful(code_page, s, size, errors, consumed); |
| 7120 | } |
| 7121 | |
| 7122 | PyObject * |
| 7123 | PyUnicode_DecodeMBCSStateful(const char *s, |
| 7124 | Py_ssize_t size, |
| 7125 | const char *errors, |
| 7126 | Py_ssize_t *consumed) |
| 7127 | { |
| 7128 | return decode_code_page_stateful(CP_ACP, s, size, errors, consumed); |
| 7129 | } |
| 7130 | |
| 7131 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7132 | PyUnicode_DecodeMBCS(const char *s, |
| 7133 | Py_ssize_t size, |
| 7134 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7135 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7136 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 7137 | } |
| 7138 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7139 | static DWORD |
| 7140 | encode_code_page_flags(UINT code_page, const char *errors) |
| 7141 | { |
| 7142 | if (code_page == CP_UTF8) { |
| 7143 | if (winver.dwMajorVersion >= 6) |
| 7144 | /* CP_UTF8 supports WC_ERR_INVALID_CHARS on Windows Vista |
| 7145 | and later */ |
| 7146 | return WC_ERR_INVALID_CHARS; |
| 7147 | else |
| 7148 | /* CP_UTF8 only supports flags=0 on Windows older than Vista */ |
| 7149 | return 0; |
| 7150 | } |
| 7151 | else if (code_page == CP_UTF7) { |
| 7152 | /* CP_UTF7 only supports flags=0 */ |
| 7153 | return 0; |
| 7154 | } |
| 7155 | else { |
| 7156 | if (errors != NULL && strcmp(errors, "replace") == 0) |
| 7157 | return 0; |
| 7158 | else |
| 7159 | return WC_NO_BEST_FIT_CHARS; |
| 7160 | } |
| 7161 | } |
| 7162 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7163 | /* |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7164 | * Encode a Unicode string to a Windows code page into a byte string in strict |
| 7165 | * mode. |
| 7166 | * |
| 7167 | * Returns consumed characters if succeed, returns -2 on encode error, or raise |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 7168 | * an OSError and returns -1 on other error. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7169 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7170 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7171 | encode_code_page_strict(UINT code_page, PyObject **outbytes, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7172 | PyObject *unicode, Py_ssize_t offset, int len, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7173 | const char* errors) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7174 | { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7175 | BOOL usedDefaultChar = FALSE; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7176 | BOOL *pusedDefaultChar = &usedDefaultChar; |
| 7177 | int outsize; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7178 | PyObject *exc = NULL; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 7179 | wchar_t *p; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7180 | Py_ssize_t size; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7181 | const DWORD flags = encode_code_page_flags(code_page, NULL); |
| 7182 | char *out; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7183 | /* Create a substring so that we can get the UTF-16 representation |
| 7184 | of just the slice under consideration. */ |
| 7185 | PyObject *substring; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7186 | |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7187 | assert(len > 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7188 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7189 | if (code_page != CP_UTF8 && code_page != CP_UTF7) |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7190 | pusedDefaultChar = &usedDefaultChar; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7191 | else |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7192 | pusedDefaultChar = NULL; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7193 | |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7194 | substring = PyUnicode_Substring(unicode, offset, offset+len); |
| 7195 | if (substring == NULL) |
| 7196 | return -1; |
| 7197 | p = PyUnicode_AsUnicodeAndSize(substring, &size); |
| 7198 | if (p == NULL) { |
| 7199 | Py_DECREF(substring); |
| 7200 | return -1; |
| 7201 | } |
Victor Stinner | 9f067f4 | 2013-06-05 00:21:31 +0200 | [diff] [blame] | 7202 | assert(size <= INT_MAX); |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7203 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7204 | /* First get the size of the result */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7205 | outsize = WideCharToMultiByte(code_page, flags, |
Victor Stinner | 9f067f4 | 2013-06-05 00:21:31 +0200 | [diff] [blame] | 7206 | p, (int)size, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7207 | NULL, 0, |
| 7208 | NULL, pusedDefaultChar); |
| 7209 | if (outsize <= 0) |
| 7210 | goto error; |
| 7211 | /* If we used a default char, then we failed! */ |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7212 | if (pusedDefaultChar && *pusedDefaultChar) { |
| 7213 | Py_DECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7214 | return -2; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7215 | } |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7216 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7217 | if (*outbytes == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7218 | /* Create string object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7219 | *outbytes = PyBytes_FromStringAndSize(NULL, outsize); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7220 | if (*outbytes == NULL) { |
| 7221 | Py_DECREF(substring); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7222 | return -1; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7223 | } |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7224 | out = PyBytes_AS_STRING(*outbytes); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7225 | } |
| 7226 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7227 | /* Extend string object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7228 | const Py_ssize_t n = PyBytes_Size(*outbytes); |
| 7229 | if (outsize > PY_SSIZE_T_MAX - n) { |
| 7230 | PyErr_NoMemory(); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7231 | Py_DECREF(substring); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7232 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7233 | } |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7234 | if (_PyBytes_Resize(outbytes, n + outsize) < 0) { |
| 7235 | Py_DECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7236 | return -1; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7237 | } |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7238 | out = PyBytes_AS_STRING(*outbytes) + n; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7239 | } |
| 7240 | |
| 7241 | /* Do the conversion */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7242 | outsize = WideCharToMultiByte(code_page, flags, |
Victor Stinner | 9f067f4 | 2013-06-05 00:21:31 +0200 | [diff] [blame] | 7243 | p, (int)size, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7244 | out, outsize, |
| 7245 | NULL, pusedDefaultChar); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7246 | Py_CLEAR(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7247 | if (outsize <= 0) |
| 7248 | goto error; |
| 7249 | if (pusedDefaultChar && *pusedDefaultChar) |
| 7250 | return -2; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7251 | return 0; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7252 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7253 | error: |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7254 | Py_XDECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7255 | if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION) |
| 7256 | return -2; |
| 7257 | PyErr_SetFromWindowsErr(0); |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7258 | return -1; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7259 | } |
| 7260 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7261 | /* |
| 7262 | * Encode a Unicode string to a Windows code page into a byte string using a |
| 7263 | * error handler. |
| 7264 | * |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 7265 | * Returns consumed characters if succeed, or raise an OSError and returns |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7266 | * -1 on other error. |
| 7267 | */ |
| 7268 | static int |
| 7269 | encode_code_page_errors(UINT code_page, PyObject **outbytes, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7270 | PyObject *unicode, Py_ssize_t unicode_offset, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7271 | Py_ssize_t insize, const char* errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7272 | { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7273 | const DWORD flags = encode_code_page_flags(code_page, errors); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7274 | Py_ssize_t pos = unicode_offset; |
| 7275 | Py_ssize_t endin = unicode_offset + insize; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7276 | /* Ideally, we should get reason from FormatMessage. This is the Windows |
| 7277 | 2000 English version of the message. */ |
| 7278 | const char *reason = "invalid character"; |
| 7279 | /* 4=maximum length of a UTF-8 sequence */ |
| 7280 | char buffer[4]; |
| 7281 | BOOL usedDefaultChar = FALSE, *pusedDefaultChar; |
| 7282 | Py_ssize_t outsize; |
| 7283 | char *out; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7284 | PyObject *errorHandler = NULL; |
| 7285 | PyObject *exc = NULL; |
| 7286 | PyObject *encoding_obj = NULL; |
| 7287 | char *encoding; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7288 | Py_ssize_t newpos, newoutsize; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7289 | PyObject *rep; |
| 7290 | int ret = -1; |
| 7291 | |
| 7292 | assert(insize > 0); |
| 7293 | |
| 7294 | encoding = code_page_name(code_page, &encoding_obj); |
| 7295 | if (encoding == NULL) |
| 7296 | return -1; |
| 7297 | |
| 7298 | if (errors == NULL || strcmp(errors, "strict") == 0) { |
| 7299 | /* The last error was ERROR_NO_UNICODE_TRANSLATION, |
| 7300 | then we raise a UnicodeEncodeError. */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7301 | make_encode_exception(&exc, encoding, unicode, 0, 0, reason); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7302 | if (exc != NULL) { |
| 7303 | PyCodec_StrictErrors(exc); |
| 7304 | Py_DECREF(exc); |
| 7305 | } |
| 7306 | Py_XDECREF(encoding_obj); |
| 7307 | return -1; |
| 7308 | } |
| 7309 | |
| 7310 | if (code_page != CP_UTF8 && code_page != CP_UTF7) |
| 7311 | pusedDefaultChar = &usedDefaultChar; |
| 7312 | else |
| 7313 | pusedDefaultChar = NULL; |
| 7314 | |
| 7315 | if (Py_ARRAY_LENGTH(buffer) > PY_SSIZE_T_MAX / insize) { |
| 7316 | PyErr_NoMemory(); |
| 7317 | goto error; |
| 7318 | } |
| 7319 | outsize = insize * Py_ARRAY_LENGTH(buffer); |
| 7320 | |
| 7321 | if (*outbytes == NULL) { |
| 7322 | /* Create string object */ |
| 7323 | *outbytes = PyBytes_FromStringAndSize(NULL, outsize); |
| 7324 | if (*outbytes == NULL) |
| 7325 | goto error; |
| 7326 | out = PyBytes_AS_STRING(*outbytes); |
| 7327 | } |
| 7328 | else { |
| 7329 | /* Extend string object */ |
| 7330 | Py_ssize_t n = PyBytes_Size(*outbytes); |
| 7331 | if (n > PY_SSIZE_T_MAX - outsize) { |
| 7332 | PyErr_NoMemory(); |
| 7333 | goto error; |
| 7334 | } |
| 7335 | if (_PyBytes_Resize(outbytes, n + outsize) < 0) |
| 7336 | goto error; |
| 7337 | out = PyBytes_AS_STRING(*outbytes) + n; |
| 7338 | } |
| 7339 | |
| 7340 | /* Encode the string character per character */ |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7341 | while (pos < endin) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7342 | { |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7343 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, pos); |
| 7344 | wchar_t chars[2]; |
| 7345 | int charsize; |
| 7346 | if (ch < 0x10000) { |
| 7347 | chars[0] = (wchar_t)ch; |
| 7348 | charsize = 1; |
| 7349 | } |
| 7350 | else { |
Victor Stinner | 76df43d | 2012-10-30 01:42:39 +0100 | [diff] [blame] | 7351 | chars[0] = Py_UNICODE_HIGH_SURROGATE(ch); |
| 7352 | chars[1] = Py_UNICODE_LOW_SURROGATE(ch); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7353 | charsize = 2; |
| 7354 | } |
| 7355 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7356 | outsize = WideCharToMultiByte(code_page, flags, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7357 | chars, charsize, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7358 | buffer, Py_ARRAY_LENGTH(buffer), |
| 7359 | NULL, pusedDefaultChar); |
| 7360 | if (outsize > 0) { |
| 7361 | if (pusedDefaultChar == NULL || !(*pusedDefaultChar)) |
| 7362 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7363 | pos++; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7364 | memcpy(out, buffer, outsize); |
| 7365 | out += outsize; |
| 7366 | continue; |
| 7367 | } |
| 7368 | } |
| 7369 | else if (GetLastError() != ERROR_NO_UNICODE_TRANSLATION) { |
| 7370 | PyErr_SetFromWindowsErr(0); |
| 7371 | goto error; |
| 7372 | } |
| 7373 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7374 | rep = unicode_encode_call_errorhandler( |
| 7375 | errors, &errorHandler, encoding, reason, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7376 | unicode, &exc, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7377 | pos, pos + 1, &newpos); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7378 | if (rep == NULL) |
| 7379 | goto error; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7380 | pos = newpos; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7381 | |
| 7382 | if (PyBytes_Check(rep)) { |
| 7383 | outsize = PyBytes_GET_SIZE(rep); |
| 7384 | if (outsize != 1) { |
| 7385 | Py_ssize_t offset = out - PyBytes_AS_STRING(*outbytes); |
| 7386 | newoutsize = PyBytes_GET_SIZE(*outbytes) + (outsize - 1); |
| 7387 | if (_PyBytes_Resize(outbytes, newoutsize) < 0) { |
| 7388 | Py_DECREF(rep); |
| 7389 | goto error; |
| 7390 | } |
| 7391 | out = PyBytes_AS_STRING(*outbytes) + offset; |
| 7392 | } |
| 7393 | memcpy(out, PyBytes_AS_STRING(rep), outsize); |
| 7394 | out += outsize; |
| 7395 | } |
| 7396 | else { |
| 7397 | Py_ssize_t i; |
| 7398 | enum PyUnicode_Kind kind; |
| 7399 | void *data; |
| 7400 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7401 | if (PyUnicode_READY(rep) == -1) { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7402 | Py_DECREF(rep); |
| 7403 | goto error; |
| 7404 | } |
| 7405 | |
| 7406 | outsize = PyUnicode_GET_LENGTH(rep); |
| 7407 | if (outsize != 1) { |
| 7408 | Py_ssize_t offset = out - PyBytes_AS_STRING(*outbytes); |
| 7409 | newoutsize = PyBytes_GET_SIZE(*outbytes) + (outsize - 1); |
| 7410 | if (_PyBytes_Resize(outbytes, newoutsize) < 0) { |
| 7411 | Py_DECREF(rep); |
| 7412 | goto error; |
| 7413 | } |
| 7414 | out = PyBytes_AS_STRING(*outbytes) + offset; |
| 7415 | } |
| 7416 | kind = PyUnicode_KIND(rep); |
| 7417 | data = PyUnicode_DATA(rep); |
| 7418 | for (i=0; i < outsize; i++) { |
| 7419 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 7420 | if (ch > 127) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7421 | raise_encode_exception(&exc, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7422 | encoding, unicode, |
| 7423 | pos, pos + 1, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7424 | "unable to encode error handler result to ASCII"); |
| 7425 | Py_DECREF(rep); |
| 7426 | goto error; |
| 7427 | } |
| 7428 | *out = (unsigned char)ch; |
| 7429 | out++; |
| 7430 | } |
| 7431 | } |
| 7432 | Py_DECREF(rep); |
| 7433 | } |
| 7434 | /* write a NUL byte */ |
| 7435 | *out = 0; |
| 7436 | outsize = out - PyBytes_AS_STRING(*outbytes); |
| 7437 | assert(outsize <= PyBytes_GET_SIZE(*outbytes)); |
| 7438 | if (_PyBytes_Resize(outbytes, outsize) < 0) |
| 7439 | goto error; |
| 7440 | ret = 0; |
| 7441 | |
| 7442 | error: |
| 7443 | Py_XDECREF(encoding_obj); |
| 7444 | Py_XDECREF(errorHandler); |
| 7445 | Py_XDECREF(exc); |
| 7446 | return ret; |
| 7447 | } |
| 7448 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7449 | static PyObject * |
| 7450 | encode_code_page(int code_page, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7451 | PyObject *unicode, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7452 | const char *errors) |
| 7453 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7454 | Py_ssize_t len; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7455 | PyObject *outbytes = NULL; |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7456 | Py_ssize_t offset; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7457 | int chunk_len, ret, done; |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7458 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7459 | if (PyUnicode_READY(unicode) == -1) |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7460 | return NULL; |
| 7461 | len = PyUnicode_GET_LENGTH(unicode); |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 7462 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7463 | if (code_page < 0) { |
| 7464 | PyErr_SetString(PyExc_ValueError, "invalid code page number"); |
| 7465 | return NULL; |
| 7466 | } |
| 7467 | |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7468 | if (len == 0) |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7469 | return PyBytes_FromStringAndSize(NULL, 0); |
| 7470 | |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7471 | offset = 0; |
| 7472 | do |
| 7473 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7474 | #ifdef NEED_RETRY |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7475 | /* 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] | 7476 | chunks. */ |
| 7477 | if (len > INT_MAX/2) { |
| 7478 | chunk_len = INT_MAX/2; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7479 | done = 0; |
| 7480 | } |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7481 | else |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7482 | #endif |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7483 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7484 | chunk_len = (int)len; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7485 | done = 1; |
| 7486 | } |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7487 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7488 | ret = encode_code_page_strict(code_page, &outbytes, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7489 | unicode, offset, chunk_len, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7490 | errors); |
| 7491 | if (ret == -2) |
| 7492 | ret = encode_code_page_errors(code_page, &outbytes, |
| 7493 | unicode, offset, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7494 | chunk_len, errors); |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7495 | if (ret < 0) { |
| 7496 | Py_XDECREF(outbytes); |
| 7497 | return NULL; |
| 7498 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7499 | |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7500 | offset += chunk_len; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7501 | len -= chunk_len; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7502 | } while (!done); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7503 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7504 | return outbytes; |
| 7505 | } |
| 7506 | |
| 7507 | PyObject * |
| 7508 | PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
| 7509 | Py_ssize_t size, |
| 7510 | const char *errors) |
| 7511 | { |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7512 | PyObject *unicode, *res; |
| 7513 | unicode = PyUnicode_FromUnicode(p, size); |
| 7514 | if (unicode == NULL) |
| 7515 | return NULL; |
| 7516 | res = encode_code_page(CP_ACP, unicode, errors); |
| 7517 | Py_DECREF(unicode); |
| 7518 | return res; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7519 | } |
| 7520 | |
| 7521 | PyObject * |
| 7522 | PyUnicode_EncodeCodePage(int code_page, |
| 7523 | PyObject *unicode, |
| 7524 | const char *errors) |
| 7525 | { |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7526 | return encode_code_page(code_page, unicode, errors); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7527 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 7528 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7529 | PyObject * |
| 7530 | PyUnicode_AsMBCSString(PyObject *unicode) |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7531 | { |
| 7532 | if (!PyUnicode_Check(unicode)) { |
| 7533 | PyErr_BadArgument(); |
| 7534 | return NULL; |
| 7535 | } |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7536 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, NULL); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7537 | } |
| 7538 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7539 | #undef NEED_RETRY |
| 7540 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 7541 | #endif /* HAVE_MBCS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7542 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7543 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 7544 | |
Victor Stinner | fb161b1 | 2013-04-18 01:44:27 +0200 | [diff] [blame] | 7545 | static int |
| 7546 | charmap_decode_string(const char *s, |
| 7547 | Py_ssize_t size, |
| 7548 | PyObject *mapping, |
| 7549 | const char *errors, |
| 7550 | _PyUnicodeWriter *writer) |
| 7551 | { |
| 7552 | const char *starts = s; |
| 7553 | const char *e; |
| 7554 | Py_ssize_t startinpos, endinpos; |
| 7555 | PyObject *errorHandler = NULL, *exc = NULL; |
| 7556 | Py_ssize_t maplen; |
| 7557 | enum PyUnicode_Kind mapkind; |
| 7558 | void *mapdata; |
| 7559 | Py_UCS4 x; |
| 7560 | unsigned char ch; |
| 7561 | |
| 7562 | if (PyUnicode_READY(mapping) == -1) |
| 7563 | return -1; |
| 7564 | |
| 7565 | maplen = PyUnicode_GET_LENGTH(mapping); |
| 7566 | mapdata = PyUnicode_DATA(mapping); |
| 7567 | mapkind = PyUnicode_KIND(mapping); |
| 7568 | |
| 7569 | e = s + size; |
| 7570 | |
| 7571 | if (mapkind == PyUnicode_1BYTE_KIND && maplen >= 256) { |
| 7572 | /* fast-path for cp037, cp500 and iso8859_1 encodings. iso8859_1 |
| 7573 | * is disabled in encoding aliases, latin1 is preferred because |
| 7574 | * its implementation is faster. */ |
| 7575 | Py_UCS1 *mapdata_ucs1 = (Py_UCS1 *)mapdata; |
| 7576 | Py_UCS1 *outdata = (Py_UCS1 *)writer->data; |
| 7577 | Py_UCS4 maxchar = writer->maxchar; |
| 7578 | |
| 7579 | assert (writer->kind == PyUnicode_1BYTE_KIND); |
| 7580 | while (s < e) { |
| 7581 | ch = *s; |
| 7582 | x = mapdata_ucs1[ch]; |
| 7583 | if (x > maxchar) { |
| 7584 | if (_PyUnicodeWriter_Prepare(writer, 1, 0xff) == -1) |
| 7585 | goto onError; |
| 7586 | maxchar = writer->maxchar; |
| 7587 | outdata = (Py_UCS1 *)writer->data; |
| 7588 | } |
| 7589 | outdata[writer->pos] = x; |
| 7590 | writer->pos++; |
| 7591 | ++s; |
| 7592 | } |
| 7593 | return 0; |
| 7594 | } |
| 7595 | |
| 7596 | while (s < e) { |
| 7597 | if (mapkind == PyUnicode_2BYTE_KIND && maplen >= 256) { |
| 7598 | enum PyUnicode_Kind outkind = writer->kind; |
| 7599 | Py_UCS2 *mapdata_ucs2 = (Py_UCS2 *)mapdata; |
| 7600 | if (outkind == PyUnicode_1BYTE_KIND) { |
| 7601 | Py_UCS1 *outdata = (Py_UCS1 *)writer->data; |
| 7602 | Py_UCS4 maxchar = writer->maxchar; |
| 7603 | while (s < e) { |
| 7604 | ch = *s; |
| 7605 | x = mapdata_ucs2[ch]; |
| 7606 | if (x > maxchar) |
| 7607 | goto Error; |
| 7608 | outdata[writer->pos] = x; |
| 7609 | writer->pos++; |
| 7610 | ++s; |
| 7611 | } |
| 7612 | break; |
| 7613 | } |
| 7614 | else if (outkind == PyUnicode_2BYTE_KIND) { |
| 7615 | Py_UCS2 *outdata = (Py_UCS2 *)writer->data; |
| 7616 | while (s < e) { |
| 7617 | ch = *s; |
| 7618 | x = mapdata_ucs2[ch]; |
| 7619 | if (x == 0xFFFE) |
| 7620 | goto Error; |
| 7621 | outdata[writer->pos] = x; |
| 7622 | writer->pos++; |
| 7623 | ++s; |
| 7624 | } |
| 7625 | break; |
| 7626 | } |
| 7627 | } |
| 7628 | ch = *s; |
| 7629 | |
| 7630 | if (ch < maplen) |
| 7631 | x = PyUnicode_READ(mapkind, mapdata, ch); |
| 7632 | else |
| 7633 | x = 0xfffe; /* invalid value */ |
| 7634 | Error: |
| 7635 | if (x == 0xfffe) |
| 7636 | { |
| 7637 | /* undefined mapping */ |
| 7638 | startinpos = s-starts; |
| 7639 | endinpos = startinpos+1; |
| 7640 | if (unicode_decode_call_errorhandler_writer( |
| 7641 | errors, &errorHandler, |
| 7642 | "charmap", "character maps to <undefined>", |
| 7643 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 7644 | writer)) { |
| 7645 | goto onError; |
| 7646 | } |
| 7647 | continue; |
| 7648 | } |
| 7649 | |
| 7650 | if (_PyUnicodeWriter_WriteCharInline(writer, x) < 0) |
| 7651 | goto onError; |
| 7652 | ++s; |
| 7653 | } |
| 7654 | Py_XDECREF(errorHandler); |
| 7655 | Py_XDECREF(exc); |
| 7656 | return 0; |
| 7657 | |
| 7658 | onError: |
| 7659 | Py_XDECREF(errorHandler); |
| 7660 | Py_XDECREF(exc); |
| 7661 | return -1; |
| 7662 | } |
| 7663 | |
| 7664 | static int |
| 7665 | charmap_decode_mapping(const char *s, |
| 7666 | Py_ssize_t size, |
| 7667 | PyObject *mapping, |
| 7668 | const char *errors, |
| 7669 | _PyUnicodeWriter *writer) |
| 7670 | { |
| 7671 | const char *starts = s; |
| 7672 | const char *e; |
| 7673 | Py_ssize_t startinpos, endinpos; |
| 7674 | PyObject *errorHandler = NULL, *exc = NULL; |
| 7675 | unsigned char ch; |
Victor Stinner | f4f2424 | 2013-05-07 01:01:31 +0200 | [diff] [blame] | 7676 | PyObject *key, *item = NULL; |
Victor Stinner | fb161b1 | 2013-04-18 01:44:27 +0200 | [diff] [blame] | 7677 | |
| 7678 | e = s + size; |
| 7679 | |
| 7680 | while (s < e) { |
| 7681 | ch = *s; |
| 7682 | |
| 7683 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 7684 | key = PyLong_FromLong((long)ch); |
| 7685 | if (key == NULL) |
| 7686 | goto onError; |
| 7687 | |
| 7688 | item = PyObject_GetItem(mapping, key); |
| 7689 | Py_DECREF(key); |
| 7690 | if (item == NULL) { |
| 7691 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 7692 | /* No mapping found means: mapping is undefined. */ |
| 7693 | PyErr_Clear(); |
| 7694 | goto Undefined; |
| 7695 | } else |
| 7696 | goto onError; |
| 7697 | } |
| 7698 | |
| 7699 | /* Apply mapping */ |
| 7700 | if (item == Py_None) |
| 7701 | goto Undefined; |
| 7702 | if (PyLong_Check(item)) { |
| 7703 | long value = PyLong_AS_LONG(item); |
| 7704 | if (value == 0xFFFE) |
| 7705 | goto Undefined; |
| 7706 | if (value < 0 || value > MAX_UNICODE) { |
| 7707 | PyErr_Format(PyExc_TypeError, |
| 7708 | "character mapping must be in range(0x%lx)", |
| 7709 | (unsigned long)MAX_UNICODE + 1); |
| 7710 | goto onError; |
| 7711 | } |
| 7712 | |
| 7713 | if (_PyUnicodeWriter_WriteCharInline(writer, value) < 0) |
| 7714 | goto onError; |
| 7715 | } |
| 7716 | else if (PyUnicode_Check(item)) { |
| 7717 | if (PyUnicode_READY(item) == -1) |
| 7718 | goto onError; |
| 7719 | if (PyUnicode_GET_LENGTH(item) == 1) { |
| 7720 | Py_UCS4 value = PyUnicode_READ_CHAR(item, 0); |
| 7721 | if (value == 0xFFFE) |
| 7722 | goto Undefined; |
| 7723 | if (_PyUnicodeWriter_WriteCharInline(writer, value) < 0) |
| 7724 | goto onError; |
| 7725 | } |
| 7726 | else { |
| 7727 | writer->overallocate = 1; |
| 7728 | if (_PyUnicodeWriter_WriteStr(writer, item) == -1) |
| 7729 | goto onError; |
| 7730 | } |
| 7731 | } |
| 7732 | else { |
| 7733 | /* wrong return value */ |
| 7734 | PyErr_SetString(PyExc_TypeError, |
| 7735 | "character mapping must return integer, None or str"); |
| 7736 | goto onError; |
| 7737 | } |
| 7738 | Py_CLEAR(item); |
| 7739 | ++s; |
| 7740 | continue; |
| 7741 | |
| 7742 | Undefined: |
| 7743 | /* undefined mapping */ |
| 7744 | Py_CLEAR(item); |
| 7745 | startinpos = s-starts; |
| 7746 | endinpos = startinpos+1; |
| 7747 | if (unicode_decode_call_errorhandler_writer( |
| 7748 | errors, &errorHandler, |
| 7749 | "charmap", "character maps to <undefined>", |
| 7750 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 7751 | writer)) { |
| 7752 | goto onError; |
| 7753 | } |
| 7754 | } |
| 7755 | Py_XDECREF(errorHandler); |
| 7756 | Py_XDECREF(exc); |
| 7757 | return 0; |
| 7758 | |
| 7759 | onError: |
| 7760 | Py_XDECREF(item); |
| 7761 | Py_XDECREF(errorHandler); |
| 7762 | Py_XDECREF(exc); |
| 7763 | return -1; |
| 7764 | } |
| 7765 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7766 | PyObject * |
| 7767 | PyUnicode_DecodeCharmap(const char *s, |
| 7768 | Py_ssize_t size, |
| 7769 | PyObject *mapping, |
| 7770 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7771 | { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7772 | _PyUnicodeWriter writer; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7773 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7774 | /* Default to Latin-1 */ |
| 7775 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7776 | return PyUnicode_DecodeLatin1(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7777 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7778 | if (size == 0) |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 7779 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 7780 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 7781 | writer.min_length = size; |
| 7782 | if (_PyUnicodeWriter_Prepare(&writer, writer.min_length, 127) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7783 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7784 | |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7785 | if (PyUnicode_CheckExact(mapping)) { |
Victor Stinner | fb161b1 | 2013-04-18 01:44:27 +0200 | [diff] [blame] | 7786 | if (charmap_decode_string(s, size, mapping, errors, &writer) < 0) |
| 7787 | goto onError; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7788 | } |
| 7789 | else { |
Victor Stinner | fb161b1 | 2013-04-18 01:44:27 +0200 | [diff] [blame] | 7790 | if (charmap_decode_mapping(s, size, mapping, errors, &writer) < 0) |
| 7791 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7792 | } |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7793 | return _PyUnicodeWriter_Finish(&writer); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7794 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7795 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7796 | _PyUnicodeWriter_Dealloc(&writer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7797 | return NULL; |
| 7798 | } |
| 7799 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7800 | /* Charmap encoding: the lookup table */ |
| 7801 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7802 | struct encoding_map { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7803 | PyObject_HEAD |
| 7804 | unsigned char level1[32]; |
| 7805 | int count2, count3; |
| 7806 | unsigned char level23[1]; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7807 | }; |
| 7808 | |
| 7809 | static PyObject* |
| 7810 | encoding_map_size(PyObject *obj, PyObject* args) |
| 7811 | { |
| 7812 | struct encoding_map *map = (struct encoding_map*)obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7813 | return PyLong_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7814 | 128*map->count3); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7815 | } |
| 7816 | |
| 7817 | static PyMethodDef encoding_map_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7818 | {"size", encoding_map_size, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7819 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 7820 | { 0 } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7821 | }; |
| 7822 | |
| 7823 | static void |
| 7824 | encoding_map_dealloc(PyObject* o) |
| 7825 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7826 | PyObject_FREE(o); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7827 | } |
| 7828 | |
| 7829 | static PyTypeObject EncodingMapType = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7830 | PyVarObject_HEAD_INIT(NULL, 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7831 | "EncodingMap", /*tp_name*/ |
| 7832 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 7833 | 0, /*tp_itemsize*/ |
| 7834 | /* methods */ |
| 7835 | encoding_map_dealloc, /*tp_dealloc*/ |
| 7836 | 0, /*tp_print*/ |
| 7837 | 0, /*tp_getattr*/ |
| 7838 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 7839 | 0, /*tp_reserved*/ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7840 | 0, /*tp_repr*/ |
| 7841 | 0, /*tp_as_number*/ |
| 7842 | 0, /*tp_as_sequence*/ |
| 7843 | 0, /*tp_as_mapping*/ |
| 7844 | 0, /*tp_hash*/ |
| 7845 | 0, /*tp_call*/ |
| 7846 | 0, /*tp_str*/ |
| 7847 | 0, /*tp_getattro*/ |
| 7848 | 0, /*tp_setattro*/ |
| 7849 | 0, /*tp_as_buffer*/ |
| 7850 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 7851 | 0, /*tp_doc*/ |
| 7852 | 0, /*tp_traverse*/ |
| 7853 | 0, /*tp_clear*/ |
| 7854 | 0, /*tp_richcompare*/ |
| 7855 | 0, /*tp_weaklistoffset*/ |
| 7856 | 0, /*tp_iter*/ |
| 7857 | 0, /*tp_iternext*/ |
| 7858 | encoding_map_methods, /*tp_methods*/ |
| 7859 | 0, /*tp_members*/ |
| 7860 | 0, /*tp_getset*/ |
| 7861 | 0, /*tp_base*/ |
| 7862 | 0, /*tp_dict*/ |
| 7863 | 0, /*tp_descr_get*/ |
| 7864 | 0, /*tp_descr_set*/ |
| 7865 | 0, /*tp_dictoffset*/ |
| 7866 | 0, /*tp_init*/ |
| 7867 | 0, /*tp_alloc*/ |
| 7868 | 0, /*tp_new*/ |
| 7869 | 0, /*tp_free*/ |
| 7870 | 0, /*tp_is_gc*/ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7871 | }; |
| 7872 | |
| 7873 | PyObject* |
| 7874 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 7875 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7876 | PyObject *result; |
| 7877 | struct encoding_map *mresult; |
| 7878 | int i; |
| 7879 | int need_dict = 0; |
| 7880 | unsigned char level1[32]; |
| 7881 | unsigned char level2[512]; |
| 7882 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 7883 | int count2 = 0, count3 = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7884 | int kind; |
| 7885 | void *data; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7886 | Py_ssize_t length; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7887 | Py_UCS4 ch; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7888 | |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7889 | if (!PyUnicode_Check(string) || !PyUnicode_GET_LENGTH(string)) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7890 | PyErr_BadArgument(); |
| 7891 | return NULL; |
| 7892 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7893 | kind = PyUnicode_KIND(string); |
| 7894 | data = PyUnicode_DATA(string); |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7895 | length = PyUnicode_GET_LENGTH(string); |
| 7896 | length = Py_MIN(length, 256); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7897 | memset(level1, 0xFF, sizeof level1); |
| 7898 | memset(level2, 0xFF, sizeof level2); |
| 7899 | |
| 7900 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 7901 | or if there are non-BMP characters, we need to use |
| 7902 | a mapping dictionary. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7903 | if (PyUnicode_READ(kind, data, 0) != 0) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7904 | need_dict = 1; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7905 | for (i = 1; i < length; i++) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7906 | int l1, l2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7907 | ch = PyUnicode_READ(kind, data, i); |
| 7908 | if (ch == 0 || ch > 0xFFFF) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7909 | need_dict = 1; |
| 7910 | break; |
| 7911 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7912 | if (ch == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7913 | /* unmapped character */ |
| 7914 | continue; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7915 | l1 = ch >> 11; |
| 7916 | l2 = ch >> 7; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7917 | if (level1[l1] == 0xFF) |
| 7918 | level1[l1] = count2++; |
| 7919 | if (level2[l2] == 0xFF) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7920 | level2[l2] = count3++; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7921 | } |
| 7922 | |
| 7923 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 7924 | need_dict = 1; |
| 7925 | |
| 7926 | if (need_dict) { |
| 7927 | PyObject *result = PyDict_New(); |
| 7928 | PyObject *key, *value; |
| 7929 | if (!result) |
| 7930 | return NULL; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7931 | for (i = 0; i < length; i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7932 | key = PyLong_FromLong(PyUnicode_READ(kind, data, i)); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7933 | value = PyLong_FromLong(i); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7934 | if (!key || !value) |
| 7935 | goto failed1; |
| 7936 | if (PyDict_SetItem(result, key, value) == -1) |
| 7937 | goto failed1; |
| 7938 | Py_DECREF(key); |
| 7939 | Py_DECREF(value); |
| 7940 | } |
| 7941 | return result; |
| 7942 | failed1: |
| 7943 | Py_XDECREF(key); |
| 7944 | Py_XDECREF(value); |
| 7945 | Py_DECREF(result); |
| 7946 | return NULL; |
| 7947 | } |
| 7948 | |
| 7949 | /* Create a three-level trie */ |
| 7950 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 7951 | 16*count2 + 128*count3 - 1); |
| 7952 | if (!result) |
| 7953 | return PyErr_NoMemory(); |
| 7954 | PyObject_Init(result, &EncodingMapType); |
| 7955 | mresult = (struct encoding_map*)result; |
| 7956 | mresult->count2 = count2; |
| 7957 | mresult->count3 = count3; |
| 7958 | mlevel1 = mresult->level1; |
| 7959 | mlevel2 = mresult->level23; |
| 7960 | mlevel3 = mresult->level23 + 16*count2; |
| 7961 | memcpy(mlevel1, level1, 32); |
| 7962 | memset(mlevel2, 0xFF, 16*count2); |
| 7963 | memset(mlevel3, 0, 128*count3); |
| 7964 | count3 = 0; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7965 | for (i = 1; i < length; i++) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7966 | int o1, o2, o3, i2, i3; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7967 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 7968 | if (ch == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7969 | /* unmapped character */ |
| 7970 | continue; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7971 | o1 = ch>>11; |
| 7972 | o2 = (ch>>7) & 0xF; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7973 | i2 = 16*mlevel1[o1] + o2; |
| 7974 | if (mlevel2[i2] == 0xFF) |
| 7975 | mlevel2[i2] = count3++; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7976 | o3 = ch & 0x7F; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7977 | i3 = 128*mlevel2[i2] + o3; |
| 7978 | mlevel3[i3] = i; |
| 7979 | } |
| 7980 | return result; |
| 7981 | } |
| 7982 | |
| 7983 | static int |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 7984 | encoding_map_lookup(Py_UCS4 c, PyObject *mapping) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7985 | { |
| 7986 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 7987 | int l1 = c>>11; |
| 7988 | int l2 = (c>>7) & 0xF; |
| 7989 | int l3 = c & 0x7F; |
| 7990 | int i; |
| 7991 | |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 7992 | if (c > 0xFFFF) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7993 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7994 | if (c == 0) |
| 7995 | return 0; |
| 7996 | /* level 1*/ |
| 7997 | i = map->level1[l1]; |
| 7998 | if (i == 0xFF) { |
| 7999 | return -1; |
| 8000 | } |
| 8001 | /* level 2*/ |
| 8002 | i = map->level23[16*i+l2]; |
| 8003 | if (i == 0xFF) { |
| 8004 | return -1; |
| 8005 | } |
| 8006 | /* level 3 */ |
| 8007 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 8008 | if (i == 0) { |
| 8009 | return -1; |
| 8010 | } |
| 8011 | return i; |
| 8012 | } |
| 8013 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8014 | /* Lookup the character ch in the mapping. If the character |
| 8015 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 8016 | error occurred). */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8017 | static PyObject * |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 8018 | charmapencode_lookup(Py_UCS4 c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8019 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8020 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8021 | PyObject *x; |
| 8022 | |
| 8023 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8024 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8025 | x = PyObject_GetItem(mapping, w); |
| 8026 | Py_DECREF(w); |
| 8027 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8028 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 8029 | /* No mapping found means: mapping is undefined. */ |
| 8030 | PyErr_Clear(); |
| 8031 | x = Py_None; |
| 8032 | Py_INCREF(x); |
| 8033 | return x; |
| 8034 | } else |
| 8035 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8036 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 8037 | else if (x == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8038 | return x; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8039 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8040 | long value = PyLong_AS_LONG(x); |
| 8041 | if (value < 0 || value > 255) { |
| 8042 | PyErr_SetString(PyExc_TypeError, |
| 8043 | "character mapping must be in range(256)"); |
| 8044 | Py_DECREF(x); |
| 8045 | return NULL; |
| 8046 | } |
| 8047 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8048 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8049 | else if (PyBytes_Check(x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8050 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8051 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8052 | /* wrong return value */ |
| 8053 | PyErr_Format(PyExc_TypeError, |
| 8054 | "character mapping must return integer, bytes or None, not %.400s", |
| 8055 | x->ob_type->tp_name); |
| 8056 | Py_DECREF(x); |
| 8057 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8058 | } |
| 8059 | } |
| 8060 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8061 | static int |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8062 | charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8063 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8064 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
| 8065 | /* exponentially overallocate to minimize reallocations */ |
| 8066 | if (requiredsize < 2*outsize) |
| 8067 | requiredsize = 2*outsize; |
| 8068 | if (_PyBytes_Resize(outobj, requiredsize)) |
| 8069 | return -1; |
| 8070 | return 0; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8071 | } |
| 8072 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8073 | typedef enum charmapencode_result { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8074 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8075 | } charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8076 | /* 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] | 8077 | various state variables. Resize the output bytes object if not enough |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8078 | space is available. Return a new reference to the object that |
| 8079 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 8080 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 8081 | reallocation error occurred. The caller must decref the result */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8082 | static charmapencode_result |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 8083 | charmapencode_output(Py_UCS4 c, PyObject *mapping, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8084 | PyObject **outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8085 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8086 | PyObject *rep; |
| 8087 | char *outstart; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8088 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8089 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 8090 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8091 | int res = encoding_map_lookup(c, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8092 | Py_ssize_t requiredsize = *outpos+1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8093 | if (res == -1) |
| 8094 | return enc_FAILED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8095 | if (outsize<requiredsize) |
| 8096 | if (charmapencode_resize(outobj, outpos, requiredsize)) |
| 8097 | return enc_EXCEPTION; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8098 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8099 | outstart[(*outpos)++] = (char)res; |
| 8100 | return enc_SUCCESS; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8101 | } |
| 8102 | |
| 8103 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8104 | if (rep==NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8105 | return enc_EXCEPTION; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8106 | else if (rep==Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8107 | Py_DECREF(rep); |
| 8108 | return enc_FAILED; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8109 | } else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8110 | if (PyLong_Check(rep)) { |
| 8111 | Py_ssize_t requiredsize = *outpos+1; |
| 8112 | if (outsize<requiredsize) |
| 8113 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 8114 | Py_DECREF(rep); |
| 8115 | return enc_EXCEPTION; |
| 8116 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8117 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8118 | outstart[(*outpos)++] = (char)PyLong_AS_LONG(rep); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8119 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8120 | else { |
| 8121 | const char *repchars = PyBytes_AS_STRING(rep); |
| 8122 | Py_ssize_t repsize = PyBytes_GET_SIZE(rep); |
| 8123 | Py_ssize_t requiredsize = *outpos+repsize; |
| 8124 | if (outsize<requiredsize) |
| 8125 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 8126 | Py_DECREF(rep); |
| 8127 | return enc_EXCEPTION; |
| 8128 | } |
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 | memcpy(outstart + *outpos, repchars, repsize); |
| 8131 | *outpos += repsize; |
| 8132 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8133 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8134 | Py_DECREF(rep); |
| 8135 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8136 | } |
| 8137 | |
| 8138 | /* handle an error in PyUnicode_EncodeCharmap |
| 8139 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8140 | static int |
| 8141 | charmap_encoding_error( |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8142 | PyObject *unicode, Py_ssize_t *inpos, PyObject *mapping, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8143 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 8144 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8145 | PyObject **res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8146 | { |
| 8147 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8148 | Py_ssize_t size, repsize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8149 | Py_ssize_t newpos; |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8150 | enum PyUnicode_Kind kind; |
| 8151 | void *data; |
| 8152 | Py_ssize_t index; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8153 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8154 | Py_ssize_t collstartpos = *inpos; |
| 8155 | Py_ssize_t collendpos = *inpos+1; |
| 8156 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8157 | char *encoding = "charmap"; |
| 8158 | char *reason = "character maps to <undefined>"; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8159 | charmapencode_result x; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8160 | Py_UCS4 ch; |
Brian Curtin | 2787ea4 | 2011-11-02 15:09:37 -0500 | [diff] [blame] | 8161 | int val; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8162 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 8163 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8164 | return -1; |
| 8165 | size = PyUnicode_GET_LENGTH(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8166 | /* find all unencodable characters */ |
| 8167 | while (collendpos < size) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8168 | PyObject *rep; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 8169 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8170 | ch = PyUnicode_READ_CHAR(unicode, collendpos); |
Brian Curtin | 2787ea4 | 2011-11-02 15:09:37 -0500 | [diff] [blame] | 8171 | val = encoding_map_lookup(ch, mapping); |
| 8172 | if (val != -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8173 | break; |
| 8174 | ++collendpos; |
| 8175 | continue; |
| 8176 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8177 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8178 | ch = PyUnicode_READ_CHAR(unicode, collendpos); |
| 8179 | rep = charmapencode_lookup(ch, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8180 | if (rep==NULL) |
| 8181 | return -1; |
| 8182 | else if (rep!=Py_None) { |
| 8183 | Py_DECREF(rep); |
| 8184 | break; |
| 8185 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8186 | Py_DECREF(rep); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8187 | ++collendpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8188 | } |
| 8189 | /* cache callback name lookup |
| 8190 | * (if not done yet, i.e. it's the first error) */ |
| 8191 | if (*known_errorHandler==-1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8192 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 8193 | *known_errorHandler = 1; |
| 8194 | else if (!strcmp(errors, "replace")) |
| 8195 | *known_errorHandler = 2; |
| 8196 | else if (!strcmp(errors, "ignore")) |
| 8197 | *known_errorHandler = 3; |
| 8198 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 8199 | *known_errorHandler = 4; |
| 8200 | else |
| 8201 | *known_errorHandler = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8202 | } |
| 8203 | switch (*known_errorHandler) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8204 | case 1: /* strict */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8205 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8206 | return -1; |
| 8207 | case 2: /* replace */ |
| 8208 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8209 | x = charmapencode_output('?', mapping, res, respos); |
| 8210 | if (x==enc_EXCEPTION) { |
| 8211 | return -1; |
| 8212 | } |
| 8213 | else if (x==enc_FAILED) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8214 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8215 | return -1; |
| 8216 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8217 | } |
| 8218 | /* fall through */ |
| 8219 | case 3: /* ignore */ |
| 8220 | *inpos = collendpos; |
| 8221 | break; |
| 8222 | case 4: /* xmlcharrefreplace */ |
| 8223 | /* generate replacement (temporarily (mis)uses p) */ |
| 8224 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8225 | char buffer[2+29+1+1]; |
| 8226 | char *cp; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8227 | sprintf(buffer, "&#%d;", (int)PyUnicode_READ_CHAR(unicode, collpos)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8228 | for (cp = buffer; *cp; ++cp) { |
| 8229 | x = charmapencode_output(*cp, mapping, res, respos); |
| 8230 | if (x==enc_EXCEPTION) |
| 8231 | return -1; |
| 8232 | else if (x==enc_FAILED) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8233 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8234 | return -1; |
| 8235 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8236 | } |
| 8237 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8238 | *inpos = collendpos; |
| 8239 | break; |
| 8240 | default: |
| 8241 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8242 | encoding, reason, unicode, exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8243 | collstartpos, collendpos, &newpos); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8244 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8245 | return -1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8246 | if (PyBytes_Check(repunicode)) { |
| 8247 | /* Directly copy bytes result to output. */ |
| 8248 | Py_ssize_t outsize = PyBytes_Size(*res); |
| 8249 | Py_ssize_t requiredsize; |
| 8250 | repsize = PyBytes_Size(repunicode); |
| 8251 | requiredsize = *respos + repsize; |
| 8252 | if (requiredsize > outsize) |
| 8253 | /* Make room for all additional bytes. */ |
| 8254 | if (charmapencode_resize(res, respos, requiredsize)) { |
| 8255 | Py_DECREF(repunicode); |
| 8256 | return -1; |
| 8257 | } |
| 8258 | memcpy(PyBytes_AsString(*res) + *respos, |
| 8259 | PyBytes_AsString(repunicode), repsize); |
| 8260 | *respos += repsize; |
| 8261 | *inpos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8262 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8263 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8264 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8265 | /* generate replacement */ |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 8266 | if (PyUnicode_READY(repunicode) == -1) { |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8267 | Py_DECREF(repunicode); |
| 8268 | return -1; |
| 8269 | } |
Victor Stinner | 9e30aa5 | 2011-11-21 02:49:52 +0100 | [diff] [blame] | 8270 | repsize = PyUnicode_GET_LENGTH(repunicode); |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8271 | data = PyUnicode_DATA(repunicode); |
| 8272 | kind = PyUnicode_KIND(repunicode); |
| 8273 | for (index = 0; index < repsize; index++) { |
| 8274 | Py_UCS4 repch = PyUnicode_READ(kind, data, index); |
| 8275 | x = charmapencode_output(repch, mapping, res, respos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8276 | if (x==enc_EXCEPTION) { |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8277 | Py_DECREF(repunicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8278 | return -1; |
| 8279 | } |
| 8280 | else if (x==enc_FAILED) { |
| 8281 | Py_DECREF(repunicode); |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8282 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8283 | return -1; |
| 8284 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8285 | } |
| 8286 | *inpos = newpos; |
| 8287 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8288 | } |
| 8289 | return 0; |
| 8290 | } |
| 8291 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8292 | PyObject * |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8293 | _PyUnicode_EncodeCharmap(PyObject *unicode, |
| 8294 | PyObject *mapping, |
| 8295 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8296 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8297 | /* output object */ |
| 8298 | PyObject *res = NULL; |
| 8299 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8300 | Py_ssize_t inpos = 0; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8301 | Py_ssize_t size; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8302 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8303 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8304 | PyObject *errorHandler = NULL; |
| 8305 | PyObject *exc = NULL; |
| 8306 | /* the following variable is used for caching string comparisons |
| 8307 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 8308 | * 3=ignore, 4=xmlcharrefreplace */ |
| 8309 | int known_errorHandler = -1; |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 8310 | void *data; |
| 8311 | int kind; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8312 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 8313 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8314 | return NULL; |
| 8315 | size = PyUnicode_GET_LENGTH(unicode); |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 8316 | data = PyUnicode_DATA(unicode); |
| 8317 | kind = PyUnicode_KIND(unicode); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8318 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8319 | /* Default to Latin-1 */ |
| 8320 | if (mapping == NULL) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8321 | return unicode_encode_ucs1(unicode, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8322 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8323 | /* allocate enough for a simple encoding without |
| 8324 | replacements, if we need more, we'll resize */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8325 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8326 | if (res == NULL) |
| 8327 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 8328 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8329 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8330 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8331 | while (inpos<size) { |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 8332 | Py_UCS4 ch = PyUnicode_READ(kind, data, inpos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8333 | /* try to encode it */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8334 | charmapencode_result x = charmapencode_output(ch, mapping, &res, &respos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8335 | if (x==enc_EXCEPTION) /* error */ |
| 8336 | goto onError; |
| 8337 | if (x==enc_FAILED) { /* unencodable character */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8338 | if (charmap_encoding_error(unicode, &inpos, mapping, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8339 | &exc, |
| 8340 | &known_errorHandler, &errorHandler, errors, |
| 8341 | &res, &respos)) { |
| 8342 | goto onError; |
| 8343 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8344 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8345 | else |
| 8346 | /* done with this character => adjust input position */ |
| 8347 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8348 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8349 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8350 | /* Resize if we allocated to much */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8351 | if (respos<PyBytes_GET_SIZE(res)) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 8352 | if (_PyBytes_Resize(&res, respos) < 0) |
| 8353 | goto onError; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8354 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8355 | Py_XDECREF(exc); |
| 8356 | Py_XDECREF(errorHandler); |
| 8357 | return res; |
| 8358 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8359 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8360 | Py_XDECREF(res); |
| 8361 | Py_XDECREF(exc); |
| 8362 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8363 | return NULL; |
| 8364 | } |
| 8365 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8366 | /* Deprecated */ |
| 8367 | PyObject * |
| 8368 | PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
| 8369 | Py_ssize_t size, |
| 8370 | PyObject *mapping, |
| 8371 | const char *errors) |
| 8372 | { |
| 8373 | PyObject *result; |
| 8374 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 8375 | if (unicode == NULL) |
| 8376 | return NULL; |
| 8377 | result = _PyUnicode_EncodeCharmap(unicode, mapping, errors); |
| 8378 | Py_DECREF(unicode); |
Victor Stinner | fc026c9 | 2011-11-04 00:24:51 +0100 | [diff] [blame] | 8379 | return result; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8380 | } |
| 8381 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8382 | PyObject * |
| 8383 | PyUnicode_AsCharmapString(PyObject *unicode, |
| 8384 | PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8385 | { |
| 8386 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8387 | PyErr_BadArgument(); |
| 8388 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8389 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8390 | return _PyUnicode_EncodeCharmap(unicode, mapping, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8391 | } |
| 8392 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8393 | /* create or adjust a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8394 | static void |
| 8395 | make_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8396 | PyObject *unicode, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8397 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8398 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8399 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8400 | if (*exceptionObject == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8401 | *exceptionObject = _PyUnicodeTranslateError_Create( |
| 8402 | unicode, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8403 | } |
| 8404 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8405 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 8406 | goto onError; |
| 8407 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 8408 | goto onError; |
| 8409 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 8410 | goto onError; |
| 8411 | return; |
| 8412 | onError: |
| 8413 | Py_DECREF(*exceptionObject); |
| 8414 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8415 | } |
| 8416 | } |
| 8417 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8418 | /* error handling callback helper: |
| 8419 | build arguments, call the callback and check the arguments, |
| 8420 | put the result into newpos and return the replacement string, which |
| 8421 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8422 | static PyObject * |
| 8423 | unicode_translate_call_errorhandler(const char *errors, |
| 8424 | PyObject **errorHandler, |
| 8425 | const char *reason, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8426 | PyObject *unicode, PyObject **exceptionObject, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8427 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8428 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8429 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8430 | 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] | 8431 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8432 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8433 | PyObject *restuple; |
| 8434 | PyObject *resunicode; |
| 8435 | |
| 8436 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8437 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8438 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8439 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8440 | } |
| 8441 | |
| 8442 | make_translate_exception(exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8443 | unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8444 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8445 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8446 | |
| 8447 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8448 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8449 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8450 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8451 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 8452 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8453 | Py_DECREF(restuple); |
| 8454 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8455 | } |
| 8456 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8457 | &resunicode, &i_newpos)) { |
| 8458 | Py_DECREF(restuple); |
| 8459 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8460 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8461 | if (i_newpos<0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8462 | *newpos = PyUnicode_GET_LENGTH(unicode)+i_newpos; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8463 | else |
| 8464 | *newpos = i_newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8465 | if (*newpos<0 || *newpos>PyUnicode_GET_LENGTH(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8466 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 8467 | Py_DECREF(restuple); |
| 8468 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 8469 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8470 | Py_INCREF(resunicode); |
| 8471 | Py_DECREF(restuple); |
| 8472 | return resunicode; |
| 8473 | } |
| 8474 | |
| 8475 | /* Lookup the character ch in the mapping and put the result in result, |
| 8476 | which must be decrefed by the caller. |
| 8477 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8478 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8479 | charmaptranslate_lookup(Py_UCS4 c, PyObject *mapping, PyObject **result) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8480 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8481 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8482 | PyObject *x; |
| 8483 | |
| 8484 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8485 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8486 | x = PyObject_GetItem(mapping, w); |
| 8487 | Py_DECREF(w); |
| 8488 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8489 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 8490 | /* No mapping found means: use 1:1 mapping. */ |
| 8491 | PyErr_Clear(); |
| 8492 | *result = NULL; |
| 8493 | return 0; |
| 8494 | } else |
| 8495 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8496 | } |
| 8497 | else if (x == Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8498 | *result = x; |
| 8499 | return 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8500 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8501 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8502 | long value = PyLong_AS_LONG(x); |
| 8503 | long max = PyUnicode_GetMax(); |
| 8504 | if (value < 0 || value > max) { |
| 8505 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5a2f7e60 | 2007-10-24 21:13:09 +0000 | [diff] [blame] | 8506 | "character mapping must be in range(0x%x)", max+1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8507 | Py_DECREF(x); |
| 8508 | return -1; |
| 8509 | } |
| 8510 | *result = x; |
| 8511 | return 0; |
| 8512 | } |
| 8513 | else if (PyUnicode_Check(x)) { |
| 8514 | *result = x; |
| 8515 | return 0; |
| 8516 | } |
| 8517 | else { |
| 8518 | /* wrong return value */ |
| 8519 | PyErr_SetString(PyExc_TypeError, |
| 8520 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8521 | Py_DECREF(x); |
| 8522 | return -1; |
| 8523 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8524 | } |
| 8525 | /* ensure that *outobj is at least requiredsize characters long, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8526 | if not reallocate and adjust various state variables. |
| 8527 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8528 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8529 | charmaptranslate_makespace(Py_UCS4 **outobj, Py_ssize_t *psize, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8530 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8531 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8532 | Py_ssize_t oldsize = *psize; |
Kristjan Valur Jonsson | 85634d7 | 2012-05-31 09:37:31 +0000 | [diff] [blame] | 8533 | Py_UCS4 *new_outobj; |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 8534 | if (requiredsize > oldsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8535 | /* exponentially overallocate to minimize reallocations */ |
| 8536 | if (requiredsize < 2 * oldsize) |
| 8537 | requiredsize = 2 * oldsize; |
Kristjan Valur Jonsson | 85634d7 | 2012-05-31 09:37:31 +0000 | [diff] [blame] | 8538 | new_outobj = PyMem_Realloc(*outobj, requiredsize * sizeof(Py_UCS4)); |
| 8539 | if (new_outobj == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8540 | return -1; |
Kristjan Valur Jonsson | 85634d7 | 2012-05-31 09:37:31 +0000 | [diff] [blame] | 8541 | *outobj = new_outobj; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8542 | *psize = requiredsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8543 | } |
| 8544 | return 0; |
| 8545 | } |
| 8546 | /* lookup the character, put the result in the output string and adjust |
| 8547 | various state variables. Return a new reference to the object that |
| 8548 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 8549 | undefined (in which case no character was written). |
| 8550 | The called must decref result. |
| 8551 | Return 0 on success, -1 on error. */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8552 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8553 | charmaptranslate_output(PyObject *input, Py_ssize_t ipos, |
| 8554 | PyObject *mapping, Py_UCS4 **output, |
| 8555 | Py_ssize_t *osize, Py_ssize_t *opos, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8556 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8557 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8558 | Py_UCS4 curinp = PyUnicode_READ_CHAR(input, ipos); |
| 8559 | if (charmaptranslate_lookup(curinp, mapping, res)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8560 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8561 | if (*res==NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8562 | /* not found => default to 1:1 mapping */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8563 | (*output)[(*opos)++] = curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8564 | } |
| 8565 | else if (*res==Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8566 | ; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8567 | else if (PyLong_Check(*res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8568 | /* 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] | 8569 | (*output)[(*opos)++] = (Py_UCS4)PyLong_AS_LONG(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8570 | } |
| 8571 | else if (PyUnicode_Check(*res)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8572 | Py_ssize_t repsize; |
| 8573 | if (PyUnicode_READY(*res) == -1) |
| 8574 | return -1; |
| 8575 | repsize = PyUnicode_GET_LENGTH(*res); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8576 | if (repsize==1) { |
| 8577 | /* 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] | 8578 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8579 | } |
| 8580 | else if (repsize!=0) { |
| 8581 | /* more than one character */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8582 | Py_ssize_t requiredsize = *opos + |
| 8583 | (PyUnicode_GET_LENGTH(input) - ipos) + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8584 | repsize - 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8585 | Py_ssize_t i; |
| 8586 | if (charmaptranslate_makespace(output, osize, requiredsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8587 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8588 | for(i = 0; i < repsize; i++) |
| 8589 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, i); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8590 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8591 | } |
| 8592 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8593 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8594 | return 0; |
| 8595 | } |
| 8596 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8597 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8598 | _PyUnicode_TranslateCharmap(PyObject *input, |
| 8599 | PyObject *mapping, |
| 8600 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8601 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8602 | /* input object */ |
| 8603 | char *idata; |
| 8604 | Py_ssize_t size, i; |
| 8605 | int kind; |
| 8606 | /* output buffer */ |
| 8607 | Py_UCS4 *output = NULL; |
| 8608 | Py_ssize_t osize; |
| 8609 | PyObject *res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8610 | /* current output position */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8611 | Py_ssize_t opos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8612 | char *reason = "character maps to <undefined>"; |
| 8613 | PyObject *errorHandler = NULL; |
| 8614 | PyObject *exc = NULL; |
| 8615 | /* the following variable is used for caching string comparisons |
| 8616 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 8617 | * 3=ignore, 4=xmlcharrefreplace */ |
| 8618 | int known_errorHandler = -1; |
| 8619 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8620 | if (mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8621 | PyErr_BadArgument(); |
| 8622 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8623 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8624 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8625 | if (PyUnicode_READY(input) == -1) |
| 8626 | return NULL; |
| 8627 | idata = (char*)PyUnicode_DATA(input); |
| 8628 | kind = PyUnicode_KIND(input); |
| 8629 | size = PyUnicode_GET_LENGTH(input); |
| 8630 | i = 0; |
| 8631 | |
| 8632 | if (size == 0) { |
| 8633 | Py_INCREF(input); |
| 8634 | return input; |
| 8635 | } |
| 8636 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8637 | /* allocate enough for a simple 1:1 translation without |
| 8638 | replacements, if we need more, we'll resize */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8639 | osize = size; |
| 8640 | output = PyMem_Malloc(osize * sizeof(Py_UCS4)); |
| 8641 | opos = 0; |
| 8642 | if (output == NULL) { |
| 8643 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8644 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8645 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8646 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8647 | while (i<size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8648 | /* try to encode it */ |
| 8649 | PyObject *x = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8650 | if (charmaptranslate_output(input, i, mapping, |
| 8651 | &output, &osize, &opos, &x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8652 | Py_XDECREF(x); |
| 8653 | goto onError; |
| 8654 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8655 | Py_XDECREF(x); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8656 | if (x!=Py_None) /* it worked => adjust input pointer */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8657 | ++i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8658 | else { /* untranslatable character */ |
| 8659 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
| 8660 | Py_ssize_t repsize; |
| 8661 | Py_ssize_t newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8662 | Py_ssize_t uni2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8663 | /* startpos for collecting untranslatable chars */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8664 | Py_ssize_t collstart = i; |
| 8665 | Py_ssize_t collend = i+1; |
| 8666 | Py_ssize_t coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8667 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8668 | /* find all untranslatable characters */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8669 | while (collend < size) { |
| 8670 | if (charmaptranslate_lookup(PyUnicode_READ(kind,idata, collend), mapping, &x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8671 | goto onError; |
| 8672 | Py_XDECREF(x); |
| 8673 | if (x!=Py_None) |
| 8674 | break; |
| 8675 | ++collend; |
| 8676 | } |
| 8677 | /* cache callback name lookup |
| 8678 | * (if not done yet, i.e. it's the first error) */ |
| 8679 | if (known_errorHandler==-1) { |
| 8680 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 8681 | known_errorHandler = 1; |
| 8682 | else if (!strcmp(errors, "replace")) |
| 8683 | known_errorHandler = 2; |
| 8684 | else if (!strcmp(errors, "ignore")) |
| 8685 | known_errorHandler = 3; |
| 8686 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 8687 | known_errorHandler = 4; |
| 8688 | else |
| 8689 | known_errorHandler = 0; |
| 8690 | } |
| 8691 | switch (known_errorHandler) { |
| 8692 | case 1: /* strict */ |
Victor Stinner | 6fa6275 | 2012-10-23 02:51:50 +0200 | [diff] [blame] | 8693 | make_translate_exception(&exc, |
| 8694 | input, collstart, collend, reason); |
| 8695 | if (exc != NULL) |
| 8696 | PyCodec_StrictErrors(exc); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8697 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8698 | case 2: /* replace */ |
| 8699 | /* 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] | 8700 | for (coll = collstart; coll<collend; coll++) |
| 8701 | output[opos++] = '?'; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8702 | /* fall through */ |
| 8703 | case 3: /* ignore */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8704 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8705 | break; |
| 8706 | case 4: /* xmlcharrefreplace */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8707 | /* generate replacement (temporarily (mis)uses i) */ |
| 8708 | for (i = collstart; i < collend; ++i) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8709 | char buffer[2+29+1+1]; |
| 8710 | char *cp; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8711 | sprintf(buffer, "&#%d;", PyUnicode_READ(kind, idata, i)); |
| 8712 | if (charmaptranslate_makespace(&output, &osize, |
| 8713 | opos+strlen(buffer)+(size-collend))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8714 | goto onError; |
| 8715 | for (cp = buffer; *cp; ++cp) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8716 | output[opos++] = *cp; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8717 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8718 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8719 | break; |
| 8720 | default: |
| 8721 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8722 | reason, input, &exc, |
| 8723 | collstart, collend, &newpos); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 8724 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8725 | goto onError; |
Benjamin Peterson | 9ca3ffa | 2012-01-01 16:04:29 -0600 | [diff] [blame] | 8726 | if (PyUnicode_READY(repunicode) == -1) { |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 8727 | Py_DECREF(repunicode); |
| 8728 | goto onError; |
| 8729 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8730 | /* generate replacement */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8731 | repsize = PyUnicode_GET_LENGTH(repunicode); |
| 8732 | if (charmaptranslate_makespace(&output, &osize, |
| 8733 | opos+repsize+(size-collend))) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8734 | Py_DECREF(repunicode); |
| 8735 | goto onError; |
| 8736 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8737 | for (uni2 = 0; repsize-->0; ++uni2) |
| 8738 | output[opos++] = PyUnicode_READ_CHAR(repunicode, uni2); |
| 8739 | i = newpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8740 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8741 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8742 | } |
| 8743 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8744 | res = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, output, opos); |
| 8745 | if (!res) |
| 8746 | goto onError; |
| 8747 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8748 | Py_XDECREF(exc); |
| 8749 | Py_XDECREF(errorHandler); |
| 8750 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8751 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8752 | onError: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8753 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8754 | Py_XDECREF(exc); |
| 8755 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8756 | return NULL; |
| 8757 | } |
| 8758 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8759 | /* Deprecated. Use PyUnicode_Translate instead. */ |
| 8760 | PyObject * |
| 8761 | PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
| 8762 | Py_ssize_t size, |
| 8763 | PyObject *mapping, |
| 8764 | const char *errors) |
| 8765 | { |
Christian Heimes | 5f520f4 | 2012-09-11 14:03:25 +0200 | [diff] [blame] | 8766 | PyObject *result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8767 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 8768 | if (!unicode) |
| 8769 | return NULL; |
Christian Heimes | 5f520f4 | 2012-09-11 14:03:25 +0200 | [diff] [blame] | 8770 | result = _PyUnicode_TranslateCharmap(unicode, mapping, errors); |
| 8771 | Py_DECREF(unicode); |
| 8772 | return result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8773 | } |
| 8774 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8775 | PyObject * |
| 8776 | PyUnicode_Translate(PyObject *str, |
| 8777 | PyObject *mapping, |
| 8778 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8779 | { |
| 8780 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8781 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8782 | str = PyUnicode_FromObject(str); |
| 8783 | if (str == NULL) |
Christian Heimes | 5f520f4 | 2012-09-11 14:03:25 +0200 | [diff] [blame] | 8784 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8785 | result = _PyUnicode_TranslateCharmap(str, mapping, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8786 | Py_DECREF(str); |
| 8787 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8788 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8789 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8790 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8791 | fix_decimal_and_space_to_ascii(PyObject *self) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8792 | { |
| 8793 | /* No need to call PyUnicode_READY(self) because this function is only |
| 8794 | called as a callback from fixup() which does it already. */ |
| 8795 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8796 | const int kind = PyUnicode_KIND(self); |
| 8797 | void *data = PyUnicode_DATA(self); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 8798 | Py_UCS4 maxchar = 127, ch, fixed; |
Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame] | 8799 | int modified = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8800 | Py_ssize_t i; |
| 8801 | |
| 8802 | for (i = 0; i < len; ++i) { |
| 8803 | ch = PyUnicode_READ(kind, data, i); |
| 8804 | fixed = 0; |
| 8805 | if (ch > 127) { |
| 8806 | if (Py_UNICODE_ISSPACE(ch)) |
| 8807 | fixed = ' '; |
| 8808 | else { |
| 8809 | const int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8810 | if (decimal >= 0) |
| 8811 | fixed = '0' + decimal; |
| 8812 | } |
| 8813 | if (fixed != 0) { |
Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame] | 8814 | modified = 1; |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 8815 | maxchar = Py_MAX(maxchar, fixed); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8816 | PyUnicode_WRITE(kind, data, i, fixed); |
| 8817 | } |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 8818 | else |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 8819 | maxchar = Py_MAX(maxchar, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8820 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8821 | } |
| 8822 | |
Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame] | 8823 | return (modified) ? maxchar : 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8824 | } |
| 8825 | |
| 8826 | PyObject * |
| 8827 | _PyUnicode_TransformDecimalAndSpaceToASCII(PyObject *unicode) |
| 8828 | { |
| 8829 | if (!PyUnicode_Check(unicode)) { |
| 8830 | PyErr_BadInternalCall(); |
| 8831 | return NULL; |
| 8832 | } |
| 8833 | if (PyUnicode_READY(unicode) == -1) |
| 8834 | return NULL; |
| 8835 | if (PyUnicode_MAX_CHAR_VALUE(unicode) <= 127) { |
| 8836 | /* If the string is already ASCII, just return the same string */ |
| 8837 | Py_INCREF(unicode); |
| 8838 | return unicode; |
| 8839 | } |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8840 | return fixup(unicode, fix_decimal_and_space_to_ascii); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8841 | } |
| 8842 | |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8843 | PyObject * |
| 8844 | PyUnicode_TransformDecimalToASCII(Py_UNICODE *s, |
| 8845 | Py_ssize_t length) |
| 8846 | { |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8847 | PyObject *decimal; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8848 | Py_ssize_t i; |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8849 | Py_UCS4 maxchar; |
| 8850 | enum PyUnicode_Kind kind; |
| 8851 | void *data; |
| 8852 | |
Victor Stinner | 99d7ad0 | 2012-02-22 13:37:39 +0100 | [diff] [blame] | 8853 | maxchar = 127; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8854 | for (i = 0; i < length; i++) { |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8855 | Py_UNICODE ch = s[i]; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8856 | if (ch > 127) { |
| 8857 | int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8858 | if (decimal >= 0) |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8859 | ch = '0' + decimal; |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 8860 | maxchar = Py_MAX(maxchar, ch); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8861 | } |
| 8862 | } |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8863 | |
| 8864 | /* Copy to a new string */ |
| 8865 | decimal = PyUnicode_New(length, maxchar); |
| 8866 | if (decimal == NULL) |
| 8867 | return decimal; |
| 8868 | kind = PyUnicode_KIND(decimal); |
| 8869 | data = PyUnicode_DATA(decimal); |
| 8870 | /* Iterate over code points */ |
| 8871 | for (i = 0; i < length; i++) { |
| 8872 | Py_UNICODE ch = s[i]; |
| 8873 | if (ch > 127) { |
| 8874 | int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8875 | if (decimal >= 0) |
| 8876 | ch = '0' + decimal; |
| 8877 | } |
| 8878 | PyUnicode_WRITE(kind, data, i, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8879 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 8880 | return unicode_result(decimal); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8881 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8882 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 8883 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8884 | int |
| 8885 | PyUnicode_EncodeDecimal(Py_UNICODE *s, |
| 8886 | Py_ssize_t length, |
| 8887 | char *output, |
| 8888 | const char *errors) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8889 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8890 | PyObject *unicode; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8891 | Py_ssize_t i; |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8892 | enum PyUnicode_Kind kind; |
| 8893 | void *data; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8894 | |
| 8895 | if (output == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8896 | PyErr_BadArgument(); |
| 8897 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8898 | } |
| 8899 | |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8900 | unicode = PyUnicode_FromUnicode(s, length); |
| 8901 | if (unicode == NULL) |
| 8902 | return -1; |
| 8903 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 8904 | if (PyUnicode_READY(unicode) == -1) { |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8905 | Py_DECREF(unicode); |
| 8906 | return -1; |
| 8907 | } |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8908 | kind = PyUnicode_KIND(unicode); |
| 8909 | data = PyUnicode_DATA(unicode); |
| 8910 | |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8911 | for (i=0; i < length; ) { |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8912 | PyObject *exc; |
| 8913 | Py_UCS4 ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8914 | int decimal; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8915 | Py_ssize_t startpos; |
| 8916 | |
| 8917 | ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8918 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8919 | if (Py_UNICODE_ISSPACE(ch)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8920 | *output++ = ' '; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8921 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8922 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8923 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8924 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 8925 | if (decimal >= 0) { |
| 8926 | *output++ = '0' + decimal; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8927 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8928 | continue; |
| 8929 | } |
| 8930 | if (0 < ch && ch < 256) { |
| 8931 | *output++ = (char)ch; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8932 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8933 | continue; |
| 8934 | } |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8935 | |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8936 | startpos = i; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8937 | exc = NULL; |
| 8938 | raise_encode_exception(&exc, "decimal", unicode, |
| 8939 | startpos, startpos+1, |
| 8940 | "invalid decimal Unicode string"); |
| 8941 | Py_XDECREF(exc); |
| 8942 | Py_DECREF(unicode); |
| 8943 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8944 | } |
| 8945 | /* 0-terminate the output string */ |
| 8946 | *output++ = '\0'; |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8947 | Py_DECREF(unicode); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8948 | return 0; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8949 | } |
| 8950 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8951 | /* --- Helpers ------------------------------------------------------------ */ |
| 8952 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8953 | static Py_ssize_t |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 8954 | any_find_slice(int direction, PyObject* s1, PyObject* s2, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8955 | Py_ssize_t start, |
| 8956 | Py_ssize_t end) |
| 8957 | { |
| 8958 | int kind1, kind2, kind; |
| 8959 | void *buf1, *buf2; |
| 8960 | Py_ssize_t len1, len2, result; |
| 8961 | |
| 8962 | kind1 = PyUnicode_KIND(s1); |
| 8963 | kind2 = PyUnicode_KIND(s2); |
| 8964 | kind = kind1 > kind2 ? kind1 : kind2; |
| 8965 | buf1 = PyUnicode_DATA(s1); |
| 8966 | buf2 = PyUnicode_DATA(s2); |
| 8967 | if (kind1 != kind) |
| 8968 | buf1 = _PyUnicode_AsKind(s1, kind); |
| 8969 | if (!buf1) |
| 8970 | return -2; |
| 8971 | if (kind2 != kind) |
| 8972 | buf2 = _PyUnicode_AsKind(s2, kind); |
| 8973 | if (!buf2) { |
| 8974 | if (kind1 != kind) PyMem_Free(buf1); |
| 8975 | return -2; |
| 8976 | } |
| 8977 | len1 = PyUnicode_GET_LENGTH(s1); |
| 8978 | len2 = PyUnicode_GET_LENGTH(s2); |
| 8979 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 8980 | if (direction > 0) { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 8981 | switch (kind) { |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 8982 | case PyUnicode_1BYTE_KIND: |
| 8983 | if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2)) |
| 8984 | result = asciilib_find_slice(buf1, len1, buf2, len2, start, end); |
| 8985 | else |
| 8986 | result = ucs1lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 8987 | break; |
| 8988 | case PyUnicode_2BYTE_KIND: |
| 8989 | result = ucs2lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 8990 | break; |
| 8991 | case PyUnicode_4BYTE_KIND: |
| 8992 | result = ucs4lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 8993 | break; |
| 8994 | default: |
| 8995 | assert(0); result = -2; |
| 8996 | } |
| 8997 | } |
| 8998 | else { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 8999 | switch (kind) { |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9000 | case PyUnicode_1BYTE_KIND: |
| 9001 | if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2)) |
| 9002 | result = asciilib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9003 | else |
| 9004 | result = ucs1lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9005 | break; |
| 9006 | case PyUnicode_2BYTE_KIND: |
| 9007 | result = ucs2lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9008 | break; |
| 9009 | case PyUnicode_4BYTE_KIND: |
| 9010 | result = ucs4lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9011 | break; |
| 9012 | default: |
| 9013 | assert(0); result = -2; |
| 9014 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9015 | } |
| 9016 | |
| 9017 | if (kind1 != kind) |
| 9018 | PyMem_Free(buf1); |
| 9019 | if (kind2 != kind) |
| 9020 | PyMem_Free(buf2); |
| 9021 | |
| 9022 | return result; |
| 9023 | } |
| 9024 | |
| 9025 | Py_ssize_t |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9026 | _PyUnicode_InsertThousandsGrouping( |
| 9027 | PyObject *unicode, Py_ssize_t index, |
| 9028 | Py_ssize_t n_buffer, |
| 9029 | void *digits, Py_ssize_t n_digits, |
| 9030 | Py_ssize_t min_width, |
| 9031 | const char *grouping, PyObject *thousands_sep, |
| 9032 | Py_UCS4 *maxchar) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9033 | { |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9034 | unsigned int kind, thousands_sep_kind; |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9035 | char *data, *thousands_sep_data; |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9036 | Py_ssize_t thousands_sep_len; |
| 9037 | Py_ssize_t len; |
| 9038 | |
| 9039 | if (unicode != NULL) { |
| 9040 | kind = PyUnicode_KIND(unicode); |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9041 | data = (char *) PyUnicode_DATA(unicode) + index * kind; |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9042 | } |
| 9043 | else { |
| 9044 | kind = PyUnicode_1BYTE_KIND; |
| 9045 | data = NULL; |
| 9046 | } |
| 9047 | thousands_sep_kind = PyUnicode_KIND(thousands_sep); |
| 9048 | thousands_sep_data = PyUnicode_DATA(thousands_sep); |
| 9049 | thousands_sep_len = PyUnicode_GET_LENGTH(thousands_sep); |
| 9050 | if (unicode != NULL && thousands_sep_kind != kind) { |
Victor Stinner | 90f50d4 | 2012-02-24 01:44:47 +0100 | [diff] [blame] | 9051 | if (thousands_sep_kind < kind) { |
| 9052 | thousands_sep_data = _PyUnicode_AsKind(thousands_sep, kind); |
| 9053 | if (!thousands_sep_data) |
| 9054 | return -1; |
| 9055 | } |
| 9056 | else { |
| 9057 | data = _PyUnicode_AsKind(unicode, thousands_sep_kind); |
| 9058 | if (!data) |
| 9059 | return -1; |
| 9060 | } |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9061 | } |
| 9062 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9063 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9064 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9065 | if (unicode != NULL && PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9066 | len = asciilib_InsertThousandsGrouping( |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9067 | (Py_UCS1 *) data, n_buffer, (Py_UCS1 *) digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9068 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9069 | (Py_UCS1 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9070 | else |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9071 | len = ucs1lib_InsertThousandsGrouping( |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9072 | (Py_UCS1*)data, n_buffer, (Py_UCS1*)digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9073 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9074 | (Py_UCS1 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9075 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9076 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9077 | len = ucs2lib_InsertThousandsGrouping( |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9078 | (Py_UCS2 *) data, n_buffer, (Py_UCS2 *) digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9079 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9080 | (Py_UCS2 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9081 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9082 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9083 | len = ucs4lib_InsertThousandsGrouping( |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9084 | (Py_UCS4 *) data, n_buffer, (Py_UCS4 *) digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9085 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9086 | (Py_UCS4 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9087 | break; |
| 9088 | default: |
| 9089 | assert(0); |
| 9090 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9091 | } |
Victor Stinner | 90f50d4 | 2012-02-24 01:44:47 +0100 | [diff] [blame] | 9092 | if (unicode != NULL && thousands_sep_kind != kind) { |
| 9093 | if (thousands_sep_kind < kind) |
| 9094 | PyMem_Free(thousands_sep_data); |
| 9095 | else |
| 9096 | PyMem_Free(data); |
| 9097 | } |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9098 | if (unicode == NULL) { |
| 9099 | *maxchar = 127; |
| 9100 | if (len != n_digits) { |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9101 | *maxchar = Py_MAX(*maxchar, |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9102 | PyUnicode_MAX_CHAR_VALUE(thousands_sep)); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9103 | } |
| 9104 | } |
| 9105 | return len; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9106 | } |
| 9107 | |
| 9108 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9109 | /* helper macro to fixup start/end slice values */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9110 | #define ADJUST_INDICES(start, end, len) \ |
| 9111 | if (end > len) \ |
| 9112 | end = len; \ |
| 9113 | else if (end < 0) { \ |
| 9114 | end += len; \ |
| 9115 | if (end < 0) \ |
| 9116 | end = 0; \ |
| 9117 | } \ |
| 9118 | if (start < 0) { \ |
| 9119 | start += len; \ |
| 9120 | if (start < 0) \ |
| 9121 | start = 0; \ |
| 9122 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9123 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9124 | Py_ssize_t |
| 9125 | PyUnicode_Count(PyObject *str, |
| 9126 | PyObject *substr, |
| 9127 | Py_ssize_t start, |
| 9128 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9129 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9130 | Py_ssize_t result; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9131 | PyObject* str_obj; |
| 9132 | PyObject* sub_obj; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9133 | int kind1, kind2, kind; |
| 9134 | void *buf1 = NULL, *buf2 = NULL; |
| 9135 | Py_ssize_t len1, len2; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9136 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9137 | str_obj = PyUnicode_FromObject(str); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9138 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9139 | return -1; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9140 | sub_obj = PyUnicode_FromObject(substr); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9141 | if (!sub_obj) { |
| 9142 | Py_DECREF(str_obj); |
| 9143 | return -1; |
| 9144 | } |
Benjamin Peterson | 4c13a4a | 2012-01-02 09:07:38 -0600 | [diff] [blame] | 9145 | if (PyUnicode_READY(sub_obj) == -1 || PyUnicode_READY(str_obj) == -1) { |
Benjamin Peterson | 5e458f5 | 2012-01-02 10:12:13 -0600 | [diff] [blame] | 9146 | Py_DECREF(sub_obj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9147 | Py_DECREF(str_obj); |
| 9148 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9149 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9150 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9151 | kind1 = PyUnicode_KIND(str_obj); |
| 9152 | kind2 = PyUnicode_KIND(sub_obj); |
Antoine Pitrou | e45c0c5 | 2012-05-12 15:49:07 +0200 | [diff] [blame] | 9153 | kind = kind1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9154 | buf1 = PyUnicode_DATA(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9155 | buf2 = PyUnicode_DATA(sub_obj); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 9156 | if (kind2 != kind) { |
Antoine Pitrou | 758153b | 2012-05-12 15:51:51 +0200 | [diff] [blame] | 9157 | if (kind2 > kind) { |
| 9158 | Py_DECREF(sub_obj); |
| 9159 | Py_DECREF(str_obj); |
Antoine Pitrou | e45c0c5 | 2012-05-12 15:49:07 +0200 | [diff] [blame] | 9160 | return 0; |
Antoine Pitrou | 758153b | 2012-05-12 15:51:51 +0200 | [diff] [blame] | 9161 | } |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9162 | buf2 = _PyUnicode_AsKind(sub_obj, kind); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 9163 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9164 | if (!buf2) |
| 9165 | goto onError; |
| 9166 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 9167 | len2 = PyUnicode_GET_LENGTH(sub_obj); |
| 9168 | |
| 9169 | ADJUST_INDICES(start, end, len1); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9170 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9171 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9172 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sub_obj)) |
| 9173 | result = asciilib_count( |
| 9174 | ((Py_UCS1*)buf1) + start, end - start, |
| 9175 | buf2, len2, PY_SSIZE_T_MAX |
| 9176 | ); |
| 9177 | else |
| 9178 | result = ucs1lib_count( |
| 9179 | ((Py_UCS1*)buf1) + start, end - start, |
| 9180 | buf2, len2, PY_SSIZE_T_MAX |
| 9181 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9182 | break; |
| 9183 | case PyUnicode_2BYTE_KIND: |
| 9184 | result = ucs2lib_count( |
| 9185 | ((Py_UCS2*)buf1) + start, end - start, |
| 9186 | buf2, len2, PY_SSIZE_T_MAX |
| 9187 | ); |
| 9188 | break; |
| 9189 | case PyUnicode_4BYTE_KIND: |
| 9190 | result = ucs4lib_count( |
| 9191 | ((Py_UCS4*)buf1) + start, end - start, |
| 9192 | buf2, len2, PY_SSIZE_T_MAX |
| 9193 | ); |
| 9194 | break; |
| 9195 | default: |
| 9196 | assert(0); result = 0; |
| 9197 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9198 | |
| 9199 | Py_DECREF(sub_obj); |
| 9200 | Py_DECREF(str_obj); |
| 9201 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9202 | if (kind2 != kind) |
| 9203 | PyMem_Free(buf2); |
| 9204 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9205 | return result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9206 | onError: |
| 9207 | Py_DECREF(sub_obj); |
| 9208 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9209 | if (kind2 != kind && buf2) |
| 9210 | PyMem_Free(buf2); |
| 9211 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9212 | } |
| 9213 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9214 | Py_ssize_t |
| 9215 | PyUnicode_Find(PyObject *str, |
| 9216 | PyObject *sub, |
| 9217 | Py_ssize_t start, |
| 9218 | Py_ssize_t end, |
| 9219 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9220 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9221 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9222 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9223 | str = PyUnicode_FromObject(str); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9224 | if (!str) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9225 | return -2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9226 | sub = PyUnicode_FromObject(sub); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9227 | if (!sub) { |
| 9228 | Py_DECREF(str); |
| 9229 | return -2; |
| 9230 | } |
| 9231 | if (PyUnicode_READY(sub) == -1 || PyUnicode_READY(str) == -1) { |
| 9232 | Py_DECREF(sub); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9233 | Py_DECREF(str); |
| 9234 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9235 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9236 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9237 | result = any_find_slice(direction, |
| 9238 | str, sub, start, end |
| 9239 | ); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9240 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9241 | Py_DECREF(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9242 | Py_DECREF(sub); |
| 9243 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9244 | return result; |
| 9245 | } |
| 9246 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9247 | Py_ssize_t |
| 9248 | PyUnicode_FindChar(PyObject *str, Py_UCS4 ch, |
| 9249 | Py_ssize_t start, Py_ssize_t end, |
| 9250 | int direction) |
| 9251 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9252 | int kind; |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9253 | Py_ssize_t result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9254 | if (PyUnicode_READY(str) == -1) |
| 9255 | return -2; |
Victor Stinner | 267aa24 | 2011-10-02 01:08:37 +0200 | [diff] [blame] | 9256 | if (start < 0 || end < 0) { |
| 9257 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 9258 | return -2; |
| 9259 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9260 | if (end > PyUnicode_GET_LENGTH(str)) |
| 9261 | end = PyUnicode_GET_LENGTH(str); |
| 9262 | kind = PyUnicode_KIND(str); |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9263 | result = findchar(PyUnicode_1BYTE_DATA(str) + kind*start, |
| 9264 | kind, end-start, ch, direction); |
| 9265 | if (result == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9266 | return -1; |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9267 | else |
| 9268 | return start + result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9269 | } |
| 9270 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9271 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9272 | tailmatch(PyObject *self, |
| 9273 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9274 | Py_ssize_t start, |
| 9275 | Py_ssize_t end, |
| 9276 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9277 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9278 | int kind_self; |
| 9279 | int kind_sub; |
| 9280 | void *data_self; |
| 9281 | void *data_sub; |
| 9282 | Py_ssize_t offset; |
| 9283 | Py_ssize_t i; |
| 9284 | Py_ssize_t end_sub; |
| 9285 | |
| 9286 | if (PyUnicode_READY(self) == -1 || |
| 9287 | PyUnicode_READY(substring) == -1) |
Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame] | 9288 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9289 | |
| 9290 | if (PyUnicode_GET_LENGTH(substring) == 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9291 | return 1; |
| 9292 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9293 | ADJUST_INDICES(start, end, PyUnicode_GET_LENGTH(self)); |
| 9294 | end -= PyUnicode_GET_LENGTH(substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9295 | if (end < start) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9296 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9297 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9298 | kind_self = PyUnicode_KIND(self); |
| 9299 | data_self = PyUnicode_DATA(self); |
| 9300 | kind_sub = PyUnicode_KIND(substring); |
| 9301 | data_sub = PyUnicode_DATA(substring); |
| 9302 | end_sub = PyUnicode_GET_LENGTH(substring) - 1; |
| 9303 | |
| 9304 | if (direction > 0) |
| 9305 | offset = end; |
| 9306 | else |
| 9307 | offset = start; |
| 9308 | |
| 9309 | if (PyUnicode_READ(kind_self, data_self, offset) == |
| 9310 | PyUnicode_READ(kind_sub, data_sub, 0) && |
| 9311 | PyUnicode_READ(kind_self, data_self, offset + end_sub) == |
| 9312 | PyUnicode_READ(kind_sub, data_sub, end_sub)) { |
| 9313 | /* If both are of the same kind, memcmp is sufficient */ |
| 9314 | if (kind_self == kind_sub) { |
| 9315 | return ! memcmp((char *)data_self + |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9316 | (offset * PyUnicode_KIND(substring)), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9317 | data_sub, |
| 9318 | PyUnicode_GET_LENGTH(substring) * |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9319 | PyUnicode_KIND(substring)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9320 | } |
| 9321 | /* otherwise we have to compare each character by first accesing it */ |
| 9322 | else { |
| 9323 | /* We do not need to compare 0 and len(substring)-1 because |
| 9324 | the if statement above ensured already that they are equal |
| 9325 | when we end up here. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9326 | for (i = 1; i < end_sub; ++i) { |
| 9327 | if (PyUnicode_READ(kind_self, data_self, offset + i) != |
| 9328 | PyUnicode_READ(kind_sub, data_sub, i)) |
| 9329 | return 0; |
| 9330 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9331 | return 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9332 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9333 | } |
| 9334 | |
| 9335 | return 0; |
| 9336 | } |
| 9337 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9338 | Py_ssize_t |
| 9339 | PyUnicode_Tailmatch(PyObject *str, |
| 9340 | PyObject *substr, |
| 9341 | Py_ssize_t start, |
| 9342 | Py_ssize_t end, |
| 9343 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9344 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9345 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9346 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9347 | str = PyUnicode_FromObject(str); |
| 9348 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9349 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9350 | substr = PyUnicode_FromObject(substr); |
| 9351 | if (substr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9352 | Py_DECREF(str); |
| 9353 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9354 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9355 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9356 | result = tailmatch(str, substr, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9357 | start, end, direction); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9358 | Py_DECREF(str); |
| 9359 | Py_DECREF(substr); |
| 9360 | return result; |
| 9361 | } |
| 9362 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9363 | /* Apply fixfct filter to the Unicode object self and return a |
| 9364 | reference to the modified object */ |
| 9365 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9366 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9367 | fixup(PyObject *self, |
| 9368 | Py_UCS4 (*fixfct)(PyObject *s)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9369 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9370 | PyObject *u; |
| 9371 | Py_UCS4 maxchar_old, maxchar_new = 0; |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9372 | PyObject *v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9373 | |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 9374 | u = _PyUnicode_Copy(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9375 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9376 | return NULL; |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 9377 | maxchar_old = PyUnicode_MAX_CHAR_VALUE(u); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9378 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9379 | /* fix functions return the new maximum character in a string, |
| 9380 | if the kind of the resulting unicode object does not change, |
| 9381 | everything is fine. Otherwise we need to change the string kind |
| 9382 | and re-run the fix function. */ |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9383 | maxchar_new = fixfct(u); |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9384 | |
| 9385 | if (maxchar_new == 0) { |
| 9386 | /* no changes */; |
| 9387 | if (PyUnicode_CheckExact(self)) { |
| 9388 | Py_DECREF(u); |
| 9389 | Py_INCREF(self); |
| 9390 | return self; |
| 9391 | } |
| 9392 | else |
| 9393 | return u; |
| 9394 | } |
| 9395 | |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9396 | maxchar_new = align_maxchar(maxchar_new); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9397 | |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9398 | if (maxchar_new == maxchar_old) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9399 | return u; |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9400 | |
| 9401 | /* In case the maximum character changed, we need to |
| 9402 | convert the string to the new category. */ |
| 9403 | v = PyUnicode_New(PyUnicode_GET_LENGTH(self), maxchar_new); |
| 9404 | if (v == NULL) { |
| 9405 | Py_DECREF(u); |
| 9406 | return NULL; |
| 9407 | } |
| 9408 | if (maxchar_new > maxchar_old) { |
| 9409 | /* If the maxchar increased so that the kind changed, not all |
| 9410 | characters are representable anymore and we need to fix the |
| 9411 | string again. This only happens in very few cases. */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9412 | _PyUnicode_FastCopyCharacters(v, 0, |
| 9413 | self, 0, PyUnicode_GET_LENGTH(self)); |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9414 | maxchar_old = fixfct(v); |
| 9415 | assert(maxchar_old > 0 && maxchar_old <= maxchar_new); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9416 | } |
| 9417 | else { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9418 | _PyUnicode_FastCopyCharacters(v, 0, |
| 9419 | u, 0, PyUnicode_GET_LENGTH(self)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9420 | } |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9421 | Py_DECREF(u); |
| 9422 | assert(_PyUnicode_CheckConsistency(v, 1)); |
| 9423 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9424 | } |
| 9425 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9426 | static PyObject * |
| 9427 | ascii_upper_or_lower(PyObject *self, int lower) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9428 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9429 | Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9430 | char *resdata, *data = PyUnicode_DATA(self); |
| 9431 | PyObject *res; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9432 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9433 | res = PyUnicode_New(len, 127); |
| 9434 | if (res == NULL) |
| 9435 | return NULL; |
| 9436 | resdata = PyUnicode_DATA(res); |
| 9437 | if (lower) |
| 9438 | _Py_bytes_lower(resdata, data, len); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9439 | else |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9440 | _Py_bytes_upper(resdata, data, len); |
| 9441 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9442 | } |
| 9443 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9444 | static Py_UCS4 |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9445 | 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] | 9446 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9447 | Py_ssize_t j; |
| 9448 | int final_sigma; |
| 9449 | Py_UCS4 c; |
| 9450 | /* 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] | 9451 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9452 | \p{cased}\p{case-ignorable}*U+03A3!(\p{case-ignorable}*\p{cased}) |
| 9453 | |
| 9454 | where ! is a negation and \p{xxx} is a character with property xxx. |
| 9455 | */ |
| 9456 | for (j = i - 1; j >= 0; j--) { |
| 9457 | c = PyUnicode_READ(kind, data, j); |
| 9458 | if (!_PyUnicode_IsCaseIgnorable(c)) |
| 9459 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9460 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9461 | final_sigma = j >= 0 && _PyUnicode_IsCased(c); |
| 9462 | if (final_sigma) { |
| 9463 | for (j = i + 1; j < length; j++) { |
| 9464 | c = PyUnicode_READ(kind, data, j); |
| 9465 | if (!_PyUnicode_IsCaseIgnorable(c)) |
| 9466 | break; |
| 9467 | } |
| 9468 | final_sigma = j == length || !_PyUnicode_IsCased(c); |
| 9469 | } |
| 9470 | return (final_sigma) ? 0x3C2 : 0x3C3; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9471 | } |
| 9472 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9473 | static int |
| 9474 | lower_ucs4(int kind, void *data, Py_ssize_t length, Py_ssize_t i, |
| 9475 | Py_UCS4 c, Py_UCS4 *mapped) |
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 | /* Obscure special case. */ |
| 9478 | if (c == 0x3A3) { |
| 9479 | mapped[0] = handle_capital_sigma(kind, data, length, i); |
| 9480 | return 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9481 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9482 | return _PyUnicode_ToLowerFull(c, mapped); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9483 | } |
| 9484 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9485 | static Py_ssize_t |
| 9486 | 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] | 9487 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9488 | Py_ssize_t i, k = 0; |
| 9489 | int n_res, j; |
| 9490 | Py_UCS4 c, mapped[3]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9491 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9492 | c = PyUnicode_READ(kind, data, 0); |
| 9493 | n_res = _PyUnicode_ToUpperFull(c, mapped); |
| 9494 | for (j = 0; j < n_res; j++) { |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9495 | *maxchar = Py_MAX(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9496 | res[k++] = mapped[j]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9497 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9498 | for (i = 1; i < length; i++) { |
| 9499 | c = PyUnicode_READ(kind, data, i); |
| 9500 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9501 | for (j = 0; j < n_res; j++) { |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9502 | *maxchar = Py_MAX(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9503 | res[k++] = mapped[j]; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9504 | } |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9505 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9506 | return k; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9507 | } |
| 9508 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9509 | static Py_ssize_t |
| 9510 | do_swapcase(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) { |
| 9511 | Py_ssize_t i, k = 0; |
| 9512 | |
| 9513 | for (i = 0; i < length; i++) { |
| 9514 | Py_UCS4 c = PyUnicode_READ(kind, data, i), mapped[3]; |
| 9515 | int n_res, j; |
| 9516 | if (Py_UNICODE_ISUPPER(c)) { |
| 9517 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9518 | } |
| 9519 | else if (Py_UNICODE_ISLOWER(c)) { |
| 9520 | n_res = _PyUnicode_ToUpperFull(c, mapped); |
| 9521 | } |
| 9522 | else { |
| 9523 | n_res = 1; |
| 9524 | mapped[0] = c; |
| 9525 | } |
| 9526 | for (j = 0; j < n_res; j++) { |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9527 | *maxchar = Py_MAX(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9528 | res[k++] = mapped[j]; |
| 9529 | } |
| 9530 | } |
| 9531 | return k; |
| 9532 | } |
| 9533 | |
| 9534 | static Py_ssize_t |
| 9535 | do_upper_or_lower(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, |
| 9536 | Py_UCS4 *maxchar, int lower) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9537 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9538 | Py_ssize_t i, k = 0; |
| 9539 | |
| 9540 | for (i = 0; i < length; i++) { |
| 9541 | Py_UCS4 c = PyUnicode_READ(kind, data, i), mapped[3]; |
| 9542 | int n_res, j; |
| 9543 | if (lower) |
| 9544 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9545 | else |
| 9546 | n_res = _PyUnicode_ToUpperFull(c, mapped); |
| 9547 | for (j = 0; j < n_res; j++) { |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9548 | *maxchar = Py_MAX(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9549 | res[k++] = mapped[j]; |
| 9550 | } |
| 9551 | } |
| 9552 | return k; |
| 9553 | } |
| 9554 | |
| 9555 | static Py_ssize_t |
| 9556 | do_upper(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9557 | { |
| 9558 | return do_upper_or_lower(kind, data, length, res, maxchar, 0); |
| 9559 | } |
| 9560 | |
| 9561 | static Py_ssize_t |
| 9562 | do_lower(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9563 | { |
| 9564 | return do_upper_or_lower(kind, data, length, res, maxchar, 1); |
| 9565 | } |
| 9566 | |
Benjamin Peterson | e51757f | 2012-01-12 21:10:29 -0500 | [diff] [blame] | 9567 | static Py_ssize_t |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 9568 | do_casefold(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9569 | { |
| 9570 | Py_ssize_t i, k = 0; |
| 9571 | |
| 9572 | for (i = 0; i < length; i++) { |
| 9573 | Py_UCS4 c = PyUnicode_READ(kind, data, i); |
| 9574 | Py_UCS4 mapped[3]; |
| 9575 | int j, n_res = _PyUnicode_ToFoldedFull(c, mapped); |
| 9576 | for (j = 0; j < n_res; j++) { |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9577 | *maxchar = Py_MAX(*maxchar, mapped[j]); |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 9578 | res[k++] = mapped[j]; |
| 9579 | } |
| 9580 | } |
| 9581 | return k; |
| 9582 | } |
| 9583 | |
| 9584 | static Py_ssize_t |
Benjamin Peterson | e51757f | 2012-01-12 21:10:29 -0500 | [diff] [blame] | 9585 | do_title(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9586 | { |
| 9587 | Py_ssize_t i, k = 0; |
| 9588 | int previous_is_cased; |
| 9589 | |
| 9590 | previous_is_cased = 0; |
| 9591 | for (i = 0; i < length; i++) { |
| 9592 | const Py_UCS4 c = PyUnicode_READ(kind, data, i); |
| 9593 | Py_UCS4 mapped[3]; |
| 9594 | int n_res, j; |
| 9595 | |
| 9596 | if (previous_is_cased) |
| 9597 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9598 | else |
| 9599 | n_res = _PyUnicode_ToTitleFull(c, mapped); |
| 9600 | |
| 9601 | for (j = 0; j < n_res; j++) { |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9602 | *maxchar = Py_MAX(*maxchar, mapped[j]); |
Benjamin Peterson | e51757f | 2012-01-12 21:10:29 -0500 | [diff] [blame] | 9603 | res[k++] = mapped[j]; |
| 9604 | } |
| 9605 | |
| 9606 | previous_is_cased = _PyUnicode_IsCased(c); |
| 9607 | } |
| 9608 | return k; |
| 9609 | } |
| 9610 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9611 | static PyObject * |
| 9612 | case_operation(PyObject *self, |
| 9613 | Py_ssize_t (*perform)(int, void *, Py_ssize_t, Py_UCS4 *, Py_UCS4 *)) |
| 9614 | { |
| 9615 | PyObject *res = NULL; |
| 9616 | Py_ssize_t length, newlength = 0; |
| 9617 | int kind, outkind; |
| 9618 | void *data, *outdata; |
| 9619 | Py_UCS4 maxchar = 0, *tmp, *tmpend; |
| 9620 | |
Benjamin Peterson | eea4846 | 2012-01-16 14:28:50 -0500 | [diff] [blame] | 9621 | assert(PyUnicode_IS_READY(self)); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9622 | |
| 9623 | kind = PyUnicode_KIND(self); |
| 9624 | data = PyUnicode_DATA(self); |
| 9625 | length = PyUnicode_GET_LENGTH(self); |
| 9626 | tmp = PyMem_MALLOC(sizeof(Py_UCS4) * 3 * length); |
| 9627 | if (tmp == NULL) |
| 9628 | return PyErr_NoMemory(); |
| 9629 | newlength = perform(kind, data, length, tmp, &maxchar); |
| 9630 | res = PyUnicode_New(newlength, maxchar); |
| 9631 | if (res == NULL) |
| 9632 | goto leave; |
| 9633 | tmpend = tmp + newlength; |
| 9634 | outdata = PyUnicode_DATA(res); |
| 9635 | outkind = PyUnicode_KIND(res); |
| 9636 | switch (outkind) { |
| 9637 | case PyUnicode_1BYTE_KIND: |
| 9638 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS1, tmp, tmpend, outdata); |
| 9639 | break; |
| 9640 | case PyUnicode_2BYTE_KIND: |
| 9641 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS2, tmp, tmpend, outdata); |
| 9642 | break; |
| 9643 | case PyUnicode_4BYTE_KIND: |
| 9644 | memcpy(outdata, tmp, sizeof(Py_UCS4) * newlength); |
| 9645 | break; |
| 9646 | default: |
| 9647 | assert(0); |
| 9648 | break; |
| 9649 | } |
| 9650 | leave: |
| 9651 | PyMem_FREE(tmp); |
| 9652 | return res; |
| 9653 | } |
| 9654 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9655 | PyObject * |
| 9656 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9657 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9658 | PyObject *sep = NULL; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9659 | Py_ssize_t seplen; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9660 | PyObject *res = NULL; /* the result */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9661 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9662 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
| 9663 | PyObject **items; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9664 | PyObject *item; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9665 | Py_ssize_t sz, i, res_offset; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9666 | Py_UCS4 maxchar; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9667 | Py_UCS4 item_maxchar; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9668 | int use_memcpy; |
| 9669 | unsigned char *res_data = NULL, *sep_data = NULL; |
| 9670 | PyObject *last_obj; |
| 9671 | unsigned int kind = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9672 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9673 | fseq = PySequence_Fast(seq, ""); |
| 9674 | if (fseq == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9675 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9676 | } |
| 9677 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9678 | /* NOTE: the following code can't call back into Python code, |
| 9679 | * so we are sure that fseq won't be mutated. |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9680 | */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9681 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9682 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 9683 | /* If empty sequence, return u"". */ |
| 9684 | if (seqlen == 0) { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9685 | Py_DECREF(fseq); |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 9686 | _Py_RETURN_UNICODE_EMPTY(); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9687 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9688 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9689 | /* If singleton sequence with an exact Unicode, return that. */ |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9690 | last_obj = NULL; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9691 | items = PySequence_Fast_ITEMS(fseq); |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9692 | if (seqlen == 1) { |
| 9693 | if (PyUnicode_CheckExact(items[0])) { |
| 9694 | res = items[0]; |
| 9695 | Py_INCREF(res); |
| 9696 | Py_DECREF(fseq); |
| 9697 | return res; |
| 9698 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9699 | seplen = 0; |
Victor Stinner | c6f0df7 | 2011-10-06 15:58:54 +0200 | [diff] [blame] | 9700 | maxchar = 0; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9701 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9702 | else { |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9703 | /* Set up sep and seplen */ |
| 9704 | if (separator == NULL) { |
| 9705 | /* fall back to a blank space separator */ |
| 9706 | sep = PyUnicode_FromOrdinal(' '); |
| 9707 | if (!sep) |
| 9708 | goto onError; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9709 | seplen = 1; |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9710 | maxchar = 32; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9711 | } |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9712 | else { |
| 9713 | if (!PyUnicode_Check(separator)) { |
| 9714 | PyErr_Format(PyExc_TypeError, |
| 9715 | "separator: expected str instance," |
| 9716 | " %.80s found", |
| 9717 | Py_TYPE(separator)->tp_name); |
| 9718 | goto onError; |
| 9719 | } |
| 9720 | if (PyUnicode_READY(separator)) |
| 9721 | goto onError; |
| 9722 | sep = separator; |
| 9723 | seplen = PyUnicode_GET_LENGTH(separator); |
| 9724 | maxchar = PyUnicode_MAX_CHAR_VALUE(separator); |
| 9725 | /* inc refcount to keep this code path symmetric with the |
| 9726 | above case of a blank separator */ |
| 9727 | Py_INCREF(sep); |
| 9728 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9729 | last_obj = sep; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9730 | } |
| 9731 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9732 | /* There are at least two things to join, or else we have a subclass |
| 9733 | * of str in the sequence. |
| 9734 | * Do a pre-pass to figure out the total amount of space we'll |
| 9735 | * need (sz), and see whether all argument are strings. |
| 9736 | */ |
| 9737 | sz = 0; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9738 | #ifdef Py_DEBUG |
| 9739 | use_memcpy = 0; |
| 9740 | #else |
| 9741 | use_memcpy = 1; |
| 9742 | #endif |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9743 | for (i = 0; i < seqlen; i++) { |
| 9744 | const Py_ssize_t old_sz = sz; |
| 9745 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9746 | if (!PyUnicode_Check(item)) { |
| 9747 | PyErr_Format(PyExc_TypeError, |
| 9748 | "sequence item %zd: expected str instance," |
| 9749 | " %.80s found", |
| 9750 | i, Py_TYPE(item)->tp_name); |
| 9751 | goto onError; |
| 9752 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9753 | if (PyUnicode_READY(item) == -1) |
| 9754 | goto onError; |
| 9755 | sz += PyUnicode_GET_LENGTH(item); |
| 9756 | item_maxchar = PyUnicode_MAX_CHAR_VALUE(item); |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9757 | maxchar = Py_MAX(maxchar, item_maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9758 | if (i != 0) |
| 9759 | sz += seplen; |
| 9760 | if (sz < old_sz || sz > PY_SSIZE_T_MAX) { |
| 9761 | PyErr_SetString(PyExc_OverflowError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9762 | "join() result is too long for a Python string"); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9763 | goto onError; |
| 9764 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9765 | if (use_memcpy && last_obj != NULL) { |
| 9766 | if (PyUnicode_KIND(last_obj) != PyUnicode_KIND(item)) |
| 9767 | use_memcpy = 0; |
| 9768 | } |
| 9769 | last_obj = item; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9770 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9771 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9772 | res = PyUnicode_New(sz, maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9773 | if (res == NULL) |
| 9774 | goto onError; |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9775 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9776 | /* Catenate everything. */ |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9777 | #ifdef Py_DEBUG |
| 9778 | use_memcpy = 0; |
| 9779 | #else |
| 9780 | if (use_memcpy) { |
| 9781 | res_data = PyUnicode_1BYTE_DATA(res); |
| 9782 | kind = PyUnicode_KIND(res); |
| 9783 | if (seplen != 0) |
| 9784 | sep_data = PyUnicode_1BYTE_DATA(sep); |
| 9785 | } |
| 9786 | #endif |
Victor Stinner | 4560f9c | 2013-04-14 18:56:46 +0200 | [diff] [blame] | 9787 | if (use_memcpy) { |
| 9788 | for (i = 0; i < seqlen; ++i) { |
| 9789 | Py_ssize_t itemlen; |
| 9790 | item = items[i]; |
| 9791 | |
| 9792 | /* Copy item, and maybe the separator. */ |
| 9793 | if (i && seplen != 0) { |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9794 | Py_MEMCPY(res_data, |
| 9795 | sep_data, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9796 | kind * seplen); |
| 9797 | res_data += kind * seplen; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9798 | } |
Victor Stinner | 4560f9c | 2013-04-14 18:56:46 +0200 | [diff] [blame] | 9799 | |
| 9800 | itemlen = PyUnicode_GET_LENGTH(item); |
| 9801 | if (itemlen != 0) { |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9802 | Py_MEMCPY(res_data, |
| 9803 | PyUnicode_DATA(item), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9804 | kind * itemlen); |
| 9805 | res_data += kind * itemlen; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9806 | } |
Victor Stinner | 4560f9c | 2013-04-14 18:56:46 +0200 | [diff] [blame] | 9807 | } |
| 9808 | assert(res_data == PyUnicode_1BYTE_DATA(res) |
| 9809 | + kind * PyUnicode_GET_LENGTH(res)); |
| 9810 | } |
| 9811 | else { |
| 9812 | for (i = 0, res_offset = 0; i < seqlen; ++i) { |
| 9813 | Py_ssize_t itemlen; |
| 9814 | item = items[i]; |
| 9815 | |
| 9816 | /* Copy item, and maybe the separator. */ |
| 9817 | if (i && seplen != 0) { |
| 9818 | _PyUnicode_FastCopyCharacters(res, res_offset, sep, 0, seplen); |
| 9819 | res_offset += seplen; |
| 9820 | } |
| 9821 | |
| 9822 | itemlen = PyUnicode_GET_LENGTH(item); |
| 9823 | if (itemlen != 0) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9824 | _PyUnicode_FastCopyCharacters(res, res_offset, item, 0, itemlen); |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9825 | res_offset += itemlen; |
| 9826 | } |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9827 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9828 | assert(res_offset == PyUnicode_GET_LENGTH(res)); |
Victor Stinner | 4560f9c | 2013-04-14 18:56:46 +0200 | [diff] [blame] | 9829 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9830 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9831 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9832 | Py_XDECREF(sep); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9833 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9834 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9835 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9836 | onError: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9837 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9838 | Py_XDECREF(sep); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9839 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9840 | return NULL; |
| 9841 | } |
| 9842 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9843 | #define FILL(kind, data, value, start, length) \ |
| 9844 | do { \ |
| 9845 | Py_ssize_t i_ = 0; \ |
| 9846 | assert(kind != PyUnicode_WCHAR_KIND); \ |
| 9847 | switch ((kind)) { \ |
| 9848 | case PyUnicode_1BYTE_KIND: { \ |
| 9849 | unsigned char * to_ = (unsigned char *)((data)) + (start); \ |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 9850 | memset(to_, (unsigned char)value, (length)); \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9851 | break; \ |
| 9852 | } \ |
| 9853 | case PyUnicode_2BYTE_KIND: { \ |
| 9854 | Py_UCS2 * to_ = (Py_UCS2 *)((data)) + (start); \ |
| 9855 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 9856 | break; \ |
| 9857 | } \ |
Benjamin Peterson | e157cf1 | 2012-01-01 15:56:20 -0600 | [diff] [blame] | 9858 | case PyUnicode_4BYTE_KIND: { \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9859 | Py_UCS4 * to_ = (Py_UCS4 *)((data)) + (start); \ |
| 9860 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 9861 | break; \ |
Benjamin Peterson | e157cf1 | 2012-01-01 15:56:20 -0600 | [diff] [blame] | 9862 | default: assert(0); \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9863 | } \ |
| 9864 | } \ |
| 9865 | } while (0) |
| 9866 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9867 | void |
| 9868 | _PyUnicode_FastFill(PyObject *unicode, Py_ssize_t start, Py_ssize_t length, |
| 9869 | Py_UCS4 fill_char) |
| 9870 | { |
| 9871 | const enum PyUnicode_Kind kind = PyUnicode_KIND(unicode); |
| 9872 | const void *data = PyUnicode_DATA(unicode); |
| 9873 | assert(PyUnicode_IS_READY(unicode)); |
| 9874 | assert(unicode_modifiable(unicode)); |
| 9875 | assert(fill_char <= PyUnicode_MAX_CHAR_VALUE(unicode)); |
| 9876 | assert(start >= 0); |
| 9877 | assert(start + length <= PyUnicode_GET_LENGTH(unicode)); |
| 9878 | FILL(kind, data, fill_char, start, length); |
| 9879 | } |
| 9880 | |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 9881 | Py_ssize_t |
| 9882 | PyUnicode_Fill(PyObject *unicode, Py_ssize_t start, Py_ssize_t length, |
| 9883 | Py_UCS4 fill_char) |
| 9884 | { |
| 9885 | Py_ssize_t maxlen; |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 9886 | |
| 9887 | if (!PyUnicode_Check(unicode)) { |
| 9888 | PyErr_BadInternalCall(); |
| 9889 | return -1; |
| 9890 | } |
| 9891 | if (PyUnicode_READY(unicode) == -1) |
| 9892 | return -1; |
| 9893 | if (unicode_check_modifiable(unicode)) |
| 9894 | return -1; |
| 9895 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9896 | if (start < 0) { |
| 9897 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 9898 | return -1; |
| 9899 | } |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 9900 | if (fill_char > PyUnicode_MAX_CHAR_VALUE(unicode)) { |
| 9901 | PyErr_SetString(PyExc_ValueError, |
| 9902 | "fill character is bigger than " |
| 9903 | "the string maximum character"); |
| 9904 | return -1; |
| 9905 | } |
| 9906 | |
| 9907 | maxlen = PyUnicode_GET_LENGTH(unicode) - start; |
| 9908 | length = Py_MIN(maxlen, length); |
| 9909 | if (length <= 0) |
| 9910 | return 0; |
| 9911 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9912 | _PyUnicode_FastFill(unicode, start, length, fill_char); |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 9913 | return length; |
| 9914 | } |
| 9915 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9916 | static PyObject * |
| 9917 | pad(PyObject *self, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9918 | Py_ssize_t left, |
| 9919 | Py_ssize_t right, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9920 | Py_UCS4 fill) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9921 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9922 | PyObject *u; |
| 9923 | Py_UCS4 maxchar; |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9924 | int kind; |
| 9925 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9926 | |
| 9927 | if (left < 0) |
| 9928 | left = 0; |
| 9929 | if (right < 0) |
| 9930 | right = 0; |
| 9931 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 9932 | if (left == 0 && right == 0) |
| 9933 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9934 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9935 | if (left > PY_SSIZE_T_MAX - _PyUnicode_LENGTH(self) || |
| 9936 | right > PY_SSIZE_T_MAX - (left + _PyUnicode_LENGTH(self))) { |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 9937 | PyErr_SetString(PyExc_OverflowError, "padded string is too long"); |
| 9938 | return NULL; |
| 9939 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9940 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9941 | maxchar = Py_MAX(maxchar, fill); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9942 | u = PyUnicode_New(left + _PyUnicode_LENGTH(self) + right, maxchar); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9943 | if (!u) |
| 9944 | return NULL; |
| 9945 | |
| 9946 | kind = PyUnicode_KIND(u); |
| 9947 | data = PyUnicode_DATA(u); |
| 9948 | if (left) |
| 9949 | FILL(kind, data, fill, 0, left); |
| 9950 | if (right) |
| 9951 | FILL(kind, data, fill, left + _PyUnicode_LENGTH(self), right); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9952 | _PyUnicode_FastCopyCharacters(u, left, self, 0, _PyUnicode_LENGTH(self)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9953 | assert(_PyUnicode_CheckConsistency(u, 1)); |
| 9954 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9955 | } |
| 9956 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9957 | PyObject * |
| 9958 | PyUnicode_Splitlines(PyObject *string, int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9959 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9960 | PyObject *list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9961 | |
| 9962 | string = PyUnicode_FromObject(string); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9963 | if (string == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9964 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9965 | if (PyUnicode_READY(string) == -1) { |
| 9966 | Py_DECREF(string); |
| 9967 | return NULL; |
| 9968 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9969 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9970 | switch (PyUnicode_KIND(string)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9971 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9972 | if (PyUnicode_IS_ASCII(string)) |
| 9973 | list = asciilib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9974 | string, PyUnicode_1BYTE_DATA(string), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9975 | PyUnicode_GET_LENGTH(string), keepends); |
| 9976 | else |
| 9977 | list = ucs1lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9978 | string, PyUnicode_1BYTE_DATA(string), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9979 | PyUnicode_GET_LENGTH(string), keepends); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9980 | break; |
| 9981 | case PyUnicode_2BYTE_KIND: |
| 9982 | list = ucs2lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9983 | string, PyUnicode_2BYTE_DATA(string), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9984 | PyUnicode_GET_LENGTH(string), keepends); |
| 9985 | break; |
| 9986 | case PyUnicode_4BYTE_KIND: |
| 9987 | list = ucs4lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9988 | string, PyUnicode_4BYTE_DATA(string), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9989 | PyUnicode_GET_LENGTH(string), keepends); |
| 9990 | break; |
| 9991 | default: |
| 9992 | assert(0); |
| 9993 | list = 0; |
| 9994 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9995 | Py_DECREF(string); |
| 9996 | return list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9997 | } |
| 9998 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9999 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10000 | split(PyObject *self, |
| 10001 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10002 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10003 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10004 | int kind1, kind2, kind; |
| 10005 | void *buf1, *buf2; |
| 10006 | Py_ssize_t len1, len2; |
| 10007 | PyObject* out; |
| 10008 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10009 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10010 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10011 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10012 | if (PyUnicode_READY(self) == -1) |
| 10013 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10014 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10015 | if (substring == NULL) |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10016 | switch (PyUnicode_KIND(self)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10017 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10018 | if (PyUnicode_IS_ASCII(self)) |
| 10019 | return asciilib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10020 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10021 | PyUnicode_GET_LENGTH(self), maxcount |
| 10022 | ); |
| 10023 | else |
| 10024 | return ucs1lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10025 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10026 | PyUnicode_GET_LENGTH(self), maxcount |
| 10027 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10028 | case PyUnicode_2BYTE_KIND: |
| 10029 | return ucs2lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10030 | self, PyUnicode_2BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10031 | PyUnicode_GET_LENGTH(self), maxcount |
| 10032 | ); |
| 10033 | case PyUnicode_4BYTE_KIND: |
| 10034 | return ucs4lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10035 | self, PyUnicode_4BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10036 | PyUnicode_GET_LENGTH(self), maxcount |
| 10037 | ); |
| 10038 | default: |
| 10039 | assert(0); |
| 10040 | return NULL; |
| 10041 | } |
| 10042 | |
| 10043 | if (PyUnicode_READY(substring) == -1) |
| 10044 | return NULL; |
| 10045 | |
| 10046 | kind1 = PyUnicode_KIND(self); |
| 10047 | kind2 = PyUnicode_KIND(substring); |
| 10048 | kind = kind1 > kind2 ? kind1 : kind2; |
| 10049 | buf1 = PyUnicode_DATA(self); |
| 10050 | buf2 = PyUnicode_DATA(substring); |
| 10051 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10052 | buf1 = _PyUnicode_AsKind(self, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10053 | if (!buf1) |
| 10054 | return NULL; |
| 10055 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10056 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10057 | if (!buf2) { |
| 10058 | if (kind1 != kind) PyMem_Free(buf1); |
| 10059 | return NULL; |
| 10060 | } |
| 10061 | len1 = PyUnicode_GET_LENGTH(self); |
| 10062 | len2 = PyUnicode_GET_LENGTH(substring); |
| 10063 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10064 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10065 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10066 | if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) |
| 10067 | out = asciilib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10068 | self, buf1, len1, buf2, len2, maxcount); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10069 | else |
| 10070 | out = ucs1lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10071 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10072 | break; |
| 10073 | case PyUnicode_2BYTE_KIND: |
| 10074 | out = ucs2lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10075 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10076 | break; |
| 10077 | case PyUnicode_4BYTE_KIND: |
| 10078 | out = ucs4lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10079 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10080 | break; |
| 10081 | default: |
| 10082 | out = NULL; |
| 10083 | } |
| 10084 | if (kind1 != kind) |
| 10085 | PyMem_Free(buf1); |
| 10086 | if (kind2 != kind) |
| 10087 | PyMem_Free(buf2); |
| 10088 | return out; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10089 | } |
| 10090 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10091 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10092 | rsplit(PyObject *self, |
| 10093 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10094 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10095 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10096 | int kind1, kind2, kind; |
| 10097 | void *buf1, *buf2; |
| 10098 | Py_ssize_t len1, len2; |
| 10099 | PyObject* out; |
| 10100 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10101 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10102 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10103 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10104 | if (PyUnicode_READY(self) == -1) |
| 10105 | return NULL; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10106 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10107 | if (substring == NULL) |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10108 | switch (PyUnicode_KIND(self)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10109 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10110 | if (PyUnicode_IS_ASCII(self)) |
| 10111 | return asciilib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10112 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10113 | PyUnicode_GET_LENGTH(self), maxcount |
| 10114 | ); |
| 10115 | else |
| 10116 | return ucs1lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10117 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10118 | PyUnicode_GET_LENGTH(self), maxcount |
| 10119 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10120 | case PyUnicode_2BYTE_KIND: |
| 10121 | return ucs2lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10122 | self, PyUnicode_2BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10123 | PyUnicode_GET_LENGTH(self), maxcount |
| 10124 | ); |
| 10125 | case PyUnicode_4BYTE_KIND: |
| 10126 | return ucs4lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10127 | self, PyUnicode_4BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10128 | PyUnicode_GET_LENGTH(self), maxcount |
| 10129 | ); |
| 10130 | default: |
| 10131 | assert(0); |
| 10132 | return NULL; |
| 10133 | } |
| 10134 | |
| 10135 | if (PyUnicode_READY(substring) == -1) |
| 10136 | return NULL; |
| 10137 | |
| 10138 | kind1 = PyUnicode_KIND(self); |
| 10139 | kind2 = PyUnicode_KIND(substring); |
| 10140 | kind = kind1 > kind2 ? kind1 : kind2; |
| 10141 | buf1 = PyUnicode_DATA(self); |
| 10142 | buf2 = PyUnicode_DATA(substring); |
| 10143 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10144 | buf1 = _PyUnicode_AsKind(self, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10145 | if (!buf1) |
| 10146 | return NULL; |
| 10147 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10148 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10149 | if (!buf2) { |
| 10150 | if (kind1 != kind) PyMem_Free(buf1); |
| 10151 | return NULL; |
| 10152 | } |
| 10153 | len1 = PyUnicode_GET_LENGTH(self); |
| 10154 | len2 = PyUnicode_GET_LENGTH(substring); |
| 10155 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10156 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10157 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10158 | if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) |
| 10159 | out = asciilib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10160 | self, buf1, len1, buf2, len2, maxcount); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10161 | else |
| 10162 | out = ucs1lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10163 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10164 | break; |
| 10165 | case PyUnicode_2BYTE_KIND: |
| 10166 | out = ucs2lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10167 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10168 | break; |
| 10169 | case PyUnicode_4BYTE_KIND: |
| 10170 | out = ucs4lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10171 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10172 | break; |
| 10173 | default: |
| 10174 | out = NULL; |
| 10175 | } |
| 10176 | if (kind1 != kind) |
| 10177 | PyMem_Free(buf1); |
| 10178 | if (kind2 != kind) |
| 10179 | PyMem_Free(buf2); |
| 10180 | return out; |
| 10181 | } |
| 10182 | |
| 10183 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10184 | anylib_find(int kind, PyObject *str1, void *buf1, Py_ssize_t len1, |
| 10185 | 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] | 10186 | { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10187 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10188 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10189 | if (PyUnicode_IS_ASCII(str1) && PyUnicode_IS_ASCII(str2)) |
| 10190 | return asciilib_find(buf1, len1, buf2, len2, offset); |
| 10191 | else |
| 10192 | return ucs1lib_find(buf1, len1, buf2, len2, offset); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10193 | case PyUnicode_2BYTE_KIND: |
| 10194 | return ucs2lib_find(buf1, len1, buf2, len2, offset); |
| 10195 | case PyUnicode_4BYTE_KIND: |
| 10196 | return ucs4lib_find(buf1, len1, buf2, len2, offset); |
| 10197 | } |
| 10198 | assert(0); |
| 10199 | return -1; |
| 10200 | } |
| 10201 | |
| 10202 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10203 | anylib_count(int kind, PyObject *sstr, void* sbuf, Py_ssize_t slen, |
| 10204 | 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] | 10205 | { |
Benjamin Peterson | c0b95d1 | 2011-12-20 17:24:05 -0600 | [diff] [blame] | 10206 | switch (kind) { |
| 10207 | case PyUnicode_1BYTE_KIND: |
| 10208 | if (PyUnicode_IS_ASCII(sstr) && PyUnicode_IS_ASCII(str1)) |
| 10209 | return asciilib_count(sbuf, slen, buf1, len1, maxcount); |
| 10210 | else |
| 10211 | return ucs1lib_count(sbuf, slen, buf1, len1, maxcount); |
| 10212 | case PyUnicode_2BYTE_KIND: |
| 10213 | return ucs2lib_count(sbuf, slen, buf1, len1, maxcount); |
| 10214 | case PyUnicode_4BYTE_KIND: |
| 10215 | return ucs4lib_count(sbuf, slen, buf1, len1, maxcount); |
| 10216 | } |
| 10217 | assert(0); |
| 10218 | return 0; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10219 | } |
| 10220 | |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 10221 | static void |
| 10222 | replace_1char_inplace(PyObject *u, Py_ssize_t pos, |
| 10223 | Py_UCS4 u1, Py_UCS4 u2, Py_ssize_t maxcount) |
| 10224 | { |
| 10225 | int kind = PyUnicode_KIND(u); |
| 10226 | void *data = PyUnicode_DATA(u); |
| 10227 | Py_ssize_t len = PyUnicode_GET_LENGTH(u); |
| 10228 | if (kind == PyUnicode_1BYTE_KIND) { |
| 10229 | ucs1lib_replace_1char_inplace((Py_UCS1 *)data + pos, |
| 10230 | (Py_UCS1 *)data + len, |
| 10231 | u1, u2, maxcount); |
| 10232 | } |
| 10233 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 10234 | ucs2lib_replace_1char_inplace((Py_UCS2 *)data + pos, |
| 10235 | (Py_UCS2 *)data + len, |
| 10236 | u1, u2, maxcount); |
| 10237 | } |
| 10238 | else { |
| 10239 | assert(kind == PyUnicode_4BYTE_KIND); |
| 10240 | ucs4lib_replace_1char_inplace((Py_UCS4 *)data + pos, |
| 10241 | (Py_UCS4 *)data + len, |
| 10242 | u1, u2, maxcount); |
| 10243 | } |
| 10244 | } |
| 10245 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10246 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10247 | replace(PyObject *self, PyObject *str1, |
| 10248 | PyObject *str2, Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10249 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10250 | PyObject *u; |
| 10251 | char *sbuf = PyUnicode_DATA(self); |
| 10252 | char *buf1 = PyUnicode_DATA(str1); |
| 10253 | char *buf2 = PyUnicode_DATA(str2); |
| 10254 | int srelease = 0, release1 = 0, release2 = 0; |
| 10255 | int skind = PyUnicode_KIND(self); |
| 10256 | int kind1 = PyUnicode_KIND(str1); |
| 10257 | int kind2 = PyUnicode_KIND(str2); |
| 10258 | Py_ssize_t slen = PyUnicode_GET_LENGTH(self); |
| 10259 | Py_ssize_t len1 = PyUnicode_GET_LENGTH(str1); |
| 10260 | Py_ssize_t len2 = PyUnicode_GET_LENGTH(str2); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10261 | int mayshrink; |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 10262 | Py_UCS4 maxchar, maxchar_str1, maxchar_str2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10263 | |
| 10264 | if (maxcount < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10265 | maxcount = PY_SSIZE_T_MAX; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10266 | else if (maxcount == 0 || slen == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10267 | goto nothing; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10268 | |
Victor Stinner | 59de0ee | 2011-10-07 10:01:28 +0200 | [diff] [blame] | 10269 | if (str1 == str2) |
| 10270 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10271 | |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10272 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 10273 | maxchar_str1 = PyUnicode_MAX_CHAR_VALUE(str1); |
| 10274 | if (maxchar < maxchar_str1) |
| 10275 | /* substring too wide to be present */ |
| 10276 | goto nothing; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10277 | maxchar_str2 = PyUnicode_MAX_CHAR_VALUE(str2); |
| 10278 | /* Replacing str1 with str2 may cause a maxchar reduction in the |
| 10279 | result string. */ |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 10280 | mayshrink = (maxchar_str2 < maxchar_str1) && (maxchar == maxchar_str1); |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 10281 | maxchar = Py_MAX(maxchar, maxchar_str2); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10282 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10283 | if (len1 == len2) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10284 | /* same length */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10285 | if (len1 == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10286 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10287 | if (len1 == 1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10288 | /* replace characters */ |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10289 | Py_UCS4 u1, u2; |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 10290 | Py_ssize_t pos; |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10291 | |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 10292 | u1 = PyUnicode_READ(kind1, buf1, 0); |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 10293 | pos = findchar(sbuf, skind, slen, u1, 1); |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10294 | if (pos < 0) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10295 | goto nothing; |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 10296 | u2 = PyUnicode_READ(kind2, buf2, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10297 | u = PyUnicode_New(slen, maxchar); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10298 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10299 | goto error; |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10300 | |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 10301 | _PyUnicode_FastCopyCharacters(u, 0, self, 0, slen); |
| 10302 | replace_1char_inplace(u, pos, u1, u2, maxcount); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10303 | } |
| 10304 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10305 | int rkind = skind; |
| 10306 | char *res; |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10307 | Py_ssize_t i; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10308 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10309 | if (kind1 < rkind) { |
| 10310 | /* widen substring */ |
| 10311 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10312 | if (!buf1) goto error; |
| 10313 | release1 = 1; |
| 10314 | } |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10315 | i = anylib_find(rkind, self, sbuf, slen, str1, buf1, len1, 0); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10316 | if (i < 0) |
| 10317 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10318 | if (rkind > kind2) { |
| 10319 | /* widen replacement */ |
| 10320 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 10321 | if (!buf2) goto error; |
| 10322 | release2 = 1; |
| 10323 | } |
| 10324 | else if (rkind < kind2) { |
| 10325 | /* widen self and buf1 */ |
| 10326 | rkind = kind2; |
| 10327 | if (release1) PyMem_Free(buf1); |
Antoine Pitrou | 6d5ad22 | 2012-11-17 23:28:17 +0100 | [diff] [blame] | 10328 | release1 = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10329 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 10330 | if (!sbuf) goto error; |
| 10331 | srelease = 1; |
| 10332 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10333 | if (!buf1) goto error; |
| 10334 | release1 = 1; |
| 10335 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10336 | u = PyUnicode_New(slen, maxchar); |
| 10337 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10338 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10339 | assert(PyUnicode_KIND(u) == rkind); |
| 10340 | res = PyUnicode_DATA(u); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10341 | |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10342 | memcpy(res, sbuf, rkind * slen); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10343 | /* change everything in-place, starting with this one */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10344 | memcpy(res + rkind * i, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10345 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10346 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10347 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10348 | |
| 10349 | while ( --maxcount > 0) { |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10350 | i = anylib_find(rkind, self, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10351 | sbuf+rkind*i, slen-i, |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10352 | str1, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10353 | if (i == -1) |
| 10354 | break; |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10355 | memcpy(res + rkind * i, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10356 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10357 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10358 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10359 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10360 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10361 | } |
| 10362 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10363 | Py_ssize_t n, i, j, ires; |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 10364 | Py_ssize_t new_size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10365 | int rkind = skind; |
| 10366 | char *res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10367 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10368 | if (kind1 < rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10369 | /* widen substring */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10370 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10371 | if (!buf1) goto error; |
| 10372 | release1 = 1; |
| 10373 | } |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10374 | n = anylib_count(rkind, self, sbuf, slen, str1, buf1, len1, maxcount); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10375 | if (n == 0) |
| 10376 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10377 | if (kind2 < rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10378 | /* widen replacement */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10379 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 10380 | if (!buf2) goto error; |
| 10381 | release2 = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10382 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10383 | else if (kind2 > rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10384 | /* widen self and buf1 */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10385 | rkind = kind2; |
| 10386 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 10387 | if (!sbuf) goto error; |
| 10388 | srelease = 1; |
| 10389 | if (release1) PyMem_Free(buf1); |
Antoine Pitrou | 6d5ad22 | 2012-11-17 23:28:17 +0100 | [diff] [blame] | 10390 | release1 = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10391 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10392 | if (!buf1) goto error; |
| 10393 | release1 = 1; |
| 10394 | } |
| 10395 | /* new_size = PyUnicode_GET_LENGTH(self) + n * (PyUnicode_GET_LENGTH(str2) - |
| 10396 | PyUnicode_GET_LENGTH(str1))); */ |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 10397 | 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] | 10398 | PyErr_SetString(PyExc_OverflowError, |
| 10399 | "replace string is too long"); |
| 10400 | goto error; |
| 10401 | } |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 10402 | new_size = slen + n * (len2 - len1); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10403 | if (new_size == 0) { |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 10404 | _Py_INCREF_UNICODE_EMPTY(); |
| 10405 | if (!unicode_empty) |
| 10406 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10407 | u = unicode_empty; |
| 10408 | goto done; |
| 10409 | } |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 10410 | if (new_size > (PY_SSIZE_T_MAX >> (rkind-1))) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10411 | PyErr_SetString(PyExc_OverflowError, |
| 10412 | "replace string is too long"); |
| 10413 | goto error; |
| 10414 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10415 | u = PyUnicode_New(new_size, maxchar); |
| 10416 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10417 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10418 | assert(PyUnicode_KIND(u) == rkind); |
| 10419 | res = PyUnicode_DATA(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10420 | ires = i = 0; |
| 10421 | if (len1 > 0) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10422 | while (n-- > 0) { |
| 10423 | /* look for next match */ |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10424 | j = anylib_find(rkind, self, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10425 | sbuf + rkind * i, slen-i, |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10426 | str1, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10427 | if (j == -1) |
| 10428 | break; |
| 10429 | else if (j > i) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10430 | /* copy unchanged part [i:j] */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10431 | memcpy(res + rkind * ires, |
| 10432 | sbuf + rkind * i, |
| 10433 | rkind * (j-i)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10434 | ires += j - i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10435 | } |
| 10436 | /* copy substitution string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10437 | if (len2 > 0) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10438 | memcpy(res + rkind * ires, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10439 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10440 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10441 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10442 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10443 | i = j + len1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10444 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10445 | if (i < slen) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10446 | /* copy tail [i:] */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10447 | memcpy(res + rkind * ires, |
| 10448 | sbuf + rkind * i, |
| 10449 | rkind * (slen-i)); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10450 | } |
| 10451 | else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10452 | /* interleave */ |
| 10453 | while (n > 0) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10454 | memcpy(res + rkind * ires, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10455 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10456 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10457 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10458 | if (--n <= 0) |
| 10459 | break; |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10460 | memcpy(res + rkind * ires, |
| 10461 | sbuf + rkind * i, |
| 10462 | rkind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10463 | ires++; |
| 10464 | i++; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10465 | } |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10466 | memcpy(res + rkind * ires, |
| 10467 | sbuf + rkind * i, |
| 10468 | rkind * (slen-i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10469 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10470 | } |
| 10471 | |
| 10472 | if (mayshrink) { |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10473 | unicode_adjust_maxchar(&u); |
| 10474 | if (u == NULL) |
| 10475 | goto error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10476 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10477 | |
| 10478 | done: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10479 | if (srelease) |
| 10480 | PyMem_FREE(sbuf); |
| 10481 | if (release1) |
| 10482 | PyMem_FREE(buf1); |
| 10483 | if (release2) |
| 10484 | PyMem_FREE(buf2); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10485 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10486 | return u; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10487 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10488 | nothing: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10489 | /* nothing to replace; return original string (when possible) */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10490 | if (srelease) |
| 10491 | PyMem_FREE(sbuf); |
| 10492 | if (release1) |
| 10493 | PyMem_FREE(buf1); |
| 10494 | if (release2) |
| 10495 | PyMem_FREE(buf2); |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10496 | return unicode_result_unchanged(self); |
| 10497 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10498 | error: |
| 10499 | if (srelease && sbuf) |
| 10500 | PyMem_FREE(sbuf); |
| 10501 | if (release1 && buf1) |
| 10502 | PyMem_FREE(buf1); |
| 10503 | if (release2 && buf2) |
| 10504 | PyMem_FREE(buf2); |
| 10505 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10506 | } |
| 10507 | |
| 10508 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 10509 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10510 | PyDoc_STRVAR(title__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10511 | "S.title() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10512 | \n\ |
| 10513 | 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] | 10514 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10515 | |
| 10516 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10517 | unicode_title(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10518 | { |
Benjamin Peterson | eea4846 | 2012-01-16 14:28:50 -0500 | [diff] [blame] | 10519 | if (PyUnicode_READY(self) == -1) |
| 10520 | return NULL; |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 10521 | return case_operation(self, do_title); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10522 | } |
| 10523 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10524 | PyDoc_STRVAR(capitalize__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10525 | "S.capitalize() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10526 | \n\ |
| 10527 | 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] | 10528 | have upper case and the rest lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10529 | |
| 10530 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10531 | unicode_capitalize(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10532 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 10533 | if (PyUnicode_READY(self) == -1) |
| 10534 | return NULL; |
| 10535 | if (PyUnicode_GET_LENGTH(self) == 0) |
| 10536 | return unicode_result_unchanged(self); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 10537 | return case_operation(self, do_capitalize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10538 | } |
| 10539 | |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 10540 | PyDoc_STRVAR(casefold__doc__, |
| 10541 | "S.casefold() -> str\n\ |
| 10542 | \n\ |
| 10543 | Return a version of S suitable for caseless comparisons."); |
| 10544 | |
| 10545 | static PyObject * |
| 10546 | unicode_casefold(PyObject *self) |
| 10547 | { |
| 10548 | if (PyUnicode_READY(self) == -1) |
| 10549 | return NULL; |
| 10550 | if (PyUnicode_IS_ASCII(self)) |
| 10551 | return ascii_upper_or_lower(self, 1); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 10552 | return case_operation(self, do_casefold); |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 10553 | } |
| 10554 | |
| 10555 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10556 | /* Argument converter. Coerces to a single unicode character */ |
| 10557 | |
| 10558 | static int |
| 10559 | convert_uc(PyObject *obj, void *addr) |
| 10560 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10561 | Py_UCS4 *fillcharloc = (Py_UCS4 *)addr; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10562 | PyObject *uniobj; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10563 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10564 | uniobj = PyUnicode_FromObject(obj); |
| 10565 | if (uniobj == NULL) { |
| 10566 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10567 | "The fill character cannot be converted to Unicode"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10568 | return 0; |
| 10569 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10570 | if (PyUnicode_GET_LENGTH(uniobj) != 1) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10571 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10572 | "The fill character must be exactly one character long"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10573 | Py_DECREF(uniobj); |
| 10574 | return 0; |
| 10575 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10576 | *fillcharloc = PyUnicode_READ_CHAR(uniobj, 0); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10577 | Py_DECREF(uniobj); |
| 10578 | return 1; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10579 | } |
| 10580 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10581 | PyDoc_STRVAR(center__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10582 | "S.center(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10583 | \n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10584 | Return S centered in a string of length width. Padding is\n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10585 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10586 | |
| 10587 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10588 | unicode_center(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10589 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10590 | Py_ssize_t marg, left; |
| 10591 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10592 | Py_UCS4 fillchar = ' '; |
| 10593 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 10594 | 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] | 10595 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10596 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 10597 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10598 | return NULL; |
| 10599 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10600 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 10601 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10602 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10603 | marg = width - PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10604 | left = marg / 2 + (marg & width & 1); |
| 10605 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10606 | return pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10607 | } |
| 10608 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10609 | /* This function assumes that str1 and str2 are readied by the caller. */ |
| 10610 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10611 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10612 | unicode_compare(PyObject *str1, PyObject *str2) |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10613 | { |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10614 | #define COMPARE(TYPE1, TYPE2) \ |
| 10615 | do { \ |
| 10616 | TYPE1* p1 = (TYPE1 *)data1; \ |
| 10617 | TYPE2* p2 = (TYPE2 *)data2; \ |
| 10618 | TYPE1* end = p1 + len; \ |
| 10619 | Py_UCS4 c1, c2; \ |
| 10620 | for (; p1 != end; p1++, p2++) { \ |
| 10621 | c1 = *p1; \ |
| 10622 | c2 = *p2; \ |
| 10623 | if (c1 != c2) \ |
| 10624 | return (c1 < c2) ? -1 : 1; \ |
| 10625 | } \ |
| 10626 | } \ |
| 10627 | while (0) |
| 10628 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10629 | int kind1, kind2; |
| 10630 | void *data1, *data2; |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10631 | Py_ssize_t len1, len2, len; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10632 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10633 | kind1 = PyUnicode_KIND(str1); |
| 10634 | kind2 = PyUnicode_KIND(str2); |
| 10635 | data1 = PyUnicode_DATA(str1); |
| 10636 | data2 = PyUnicode_DATA(str2); |
| 10637 | len1 = PyUnicode_GET_LENGTH(str1); |
| 10638 | len2 = PyUnicode_GET_LENGTH(str2); |
Victor Stinner | 770e19e | 2012-10-04 22:59:45 +0200 | [diff] [blame] | 10639 | len = Py_MIN(len1, len2); |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10640 | |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10641 | switch(kind1) { |
| 10642 | case PyUnicode_1BYTE_KIND: |
| 10643 | { |
| 10644 | switch(kind2) { |
| 10645 | case PyUnicode_1BYTE_KIND: |
| 10646 | { |
| 10647 | int cmp = memcmp(data1, data2, len); |
| 10648 | /* normalize result of memcmp() into the range [-1; 1] */ |
| 10649 | if (cmp < 0) |
| 10650 | return -1; |
| 10651 | if (cmp > 0) |
| 10652 | return 1; |
| 10653 | break; |
Victor Stinner | 770e19e | 2012-10-04 22:59:45 +0200 | [diff] [blame] | 10654 | } |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10655 | case PyUnicode_2BYTE_KIND: |
| 10656 | COMPARE(Py_UCS1, Py_UCS2); |
| 10657 | break; |
| 10658 | case PyUnicode_4BYTE_KIND: |
| 10659 | COMPARE(Py_UCS1, Py_UCS4); |
| 10660 | break; |
| 10661 | default: |
| 10662 | assert(0); |
| 10663 | } |
| 10664 | break; |
| 10665 | } |
| 10666 | case PyUnicode_2BYTE_KIND: |
| 10667 | { |
| 10668 | switch(kind2) { |
| 10669 | case PyUnicode_1BYTE_KIND: |
| 10670 | COMPARE(Py_UCS2, Py_UCS1); |
| 10671 | break; |
| 10672 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | cd777ea | 2013-04-08 22:43:44 +0200 | [diff] [blame] | 10673 | { |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10674 | COMPARE(Py_UCS2, Py_UCS2); |
| 10675 | break; |
Victor Stinner | cd777ea | 2013-04-08 22:43:44 +0200 | [diff] [blame] | 10676 | } |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10677 | case PyUnicode_4BYTE_KIND: |
| 10678 | COMPARE(Py_UCS2, Py_UCS4); |
| 10679 | break; |
| 10680 | default: |
| 10681 | assert(0); |
| 10682 | } |
| 10683 | break; |
| 10684 | } |
| 10685 | case PyUnicode_4BYTE_KIND: |
| 10686 | { |
| 10687 | switch(kind2) { |
| 10688 | case PyUnicode_1BYTE_KIND: |
| 10689 | COMPARE(Py_UCS4, Py_UCS1); |
| 10690 | break; |
| 10691 | case PyUnicode_2BYTE_KIND: |
| 10692 | COMPARE(Py_UCS4, Py_UCS2); |
| 10693 | break; |
| 10694 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | cd777ea | 2013-04-08 22:43:44 +0200 | [diff] [blame] | 10695 | { |
| 10696 | #if defined(HAVE_WMEMCMP) && SIZEOF_WCHAR_T == 4 |
| 10697 | int cmp = wmemcmp((wchar_t *)data1, (wchar_t *)data2, len); |
| 10698 | /* normalize result of wmemcmp() into the range [-1; 1] */ |
| 10699 | if (cmp < 0) |
| 10700 | return -1; |
| 10701 | if (cmp > 0) |
| 10702 | return 1; |
| 10703 | #else |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10704 | COMPARE(Py_UCS4, Py_UCS4); |
Victor Stinner | cd777ea | 2013-04-08 22:43:44 +0200 | [diff] [blame] | 10705 | #endif |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10706 | break; |
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 | default: |
| 10709 | assert(0); |
| 10710 | } |
| 10711 | break; |
| 10712 | } |
| 10713 | default: |
| 10714 | assert(0); |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10715 | } |
| 10716 | |
Victor Stinner | 770e19e | 2012-10-04 22:59:45 +0200 | [diff] [blame] | 10717 | if (len1 == len2) |
| 10718 | return 0; |
| 10719 | if (len1 < len2) |
| 10720 | return -1; |
| 10721 | else |
| 10722 | return 1; |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10723 | |
| 10724 | #undef COMPARE |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10725 | } |
| 10726 | |
Victor Stinner | c8bc537 | 2013-11-04 11:08:10 +0100 | [diff] [blame] | 10727 | Py_LOCAL(int) |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10728 | unicode_compare_eq(PyObject *str1, PyObject *str2) |
| 10729 | { |
| 10730 | int kind; |
| 10731 | void *data1, *data2; |
| 10732 | Py_ssize_t len; |
| 10733 | int cmp; |
| 10734 | |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10735 | len = PyUnicode_GET_LENGTH(str1); |
| 10736 | if (PyUnicode_GET_LENGTH(str2) != len) |
| 10737 | return 0; |
| 10738 | kind = PyUnicode_KIND(str1); |
| 10739 | if (PyUnicode_KIND(str2) != kind) |
| 10740 | return 0; |
| 10741 | data1 = PyUnicode_DATA(str1); |
| 10742 | data2 = PyUnicode_DATA(str2); |
| 10743 | |
| 10744 | cmp = memcmp(data1, data2, len * kind); |
| 10745 | return (cmp == 0); |
| 10746 | } |
| 10747 | |
| 10748 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10749 | int |
| 10750 | PyUnicode_Compare(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10751 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10752 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 10753 | if (PyUnicode_READY(left) == -1 || |
| 10754 | PyUnicode_READY(right) == -1) |
| 10755 | return -1; |
Victor Stinner | f0c7b2a | 2013-11-04 11:27:14 +0100 | [diff] [blame] | 10756 | |
| 10757 | /* a string is equal to itself */ |
| 10758 | if (left == right) |
| 10759 | return 0; |
| 10760 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10761 | return unicode_compare(left, right); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10762 | } |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 10763 | PyErr_Format(PyExc_TypeError, |
| 10764 | "Can't compare %.100s and %.100s", |
| 10765 | left->ob_type->tp_name, |
| 10766 | right->ob_type->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10767 | return -1; |
| 10768 | } |
| 10769 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10770 | int |
Victor Stinner | ad14ccd | 2013-11-07 00:46:04 +0100 | [diff] [blame] | 10771 | _PyUnicode_CompareWithId(PyObject *left, _Py_Identifier *right) |
| 10772 | { |
| 10773 | PyObject *right_str = _PyUnicode_FromId(right); /* borrowed */ |
| 10774 | if (right_str == NULL) |
| 10775 | return -1; |
| 10776 | return PyUnicode_Compare(left, right_str); |
| 10777 | } |
| 10778 | |
| 10779 | int |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10780 | PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str) |
| 10781 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10782 | Py_ssize_t i; |
| 10783 | int kind; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10784 | Py_UCS4 chr; |
| 10785 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 10786 | assert(_PyUnicode_CHECK(uni)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10787 | if (PyUnicode_READY(uni) == -1) |
| 10788 | return -1; |
| 10789 | kind = PyUnicode_KIND(uni); |
Victor Stinner | 602f7cf | 2013-10-29 23:31:50 +0100 | [diff] [blame] | 10790 | if (kind == PyUnicode_1BYTE_KIND) { |
Victor Stinner | a6b9b07 | 2013-10-30 18:27:13 +0100 | [diff] [blame] | 10791 | const void *data = PyUnicode_1BYTE_DATA(uni); |
Victor Stinner | e1b1592 | 2013-11-03 13:53:12 +0100 | [diff] [blame] | 10792 | size_t len1 = (size_t)PyUnicode_GET_LENGTH(uni); |
Victor Stinner | 602f7cf | 2013-10-29 23:31:50 +0100 | [diff] [blame] | 10793 | size_t len, len2 = strlen(str); |
| 10794 | int cmp; |
| 10795 | |
| 10796 | len = Py_MIN(len1, len2); |
| 10797 | cmp = memcmp(data, str, len); |
Victor Stinner | 21ea21e | 2013-11-04 11:28:26 +0100 | [diff] [blame] | 10798 | if (cmp != 0) { |
| 10799 | if (cmp < 0) |
| 10800 | return -1; |
| 10801 | else |
| 10802 | return 1; |
| 10803 | } |
Victor Stinner | 602f7cf | 2013-10-29 23:31:50 +0100 | [diff] [blame] | 10804 | if (len1 > len2) |
| 10805 | return 1; /* uni is longer */ |
| 10806 | if (len2 > len1) |
| 10807 | return -1; /* str is longer */ |
| 10808 | return 0; |
| 10809 | } |
| 10810 | else { |
| 10811 | void *data = PyUnicode_DATA(uni); |
| 10812 | /* Compare Unicode string and source character set string */ |
| 10813 | for (i = 0; (chr = PyUnicode_READ(kind, data, i)) && str[i]; i++) |
| 10814 | if (chr != str[i]) |
| 10815 | return (chr < (unsigned char)(str[i])) ? -1 : 1; |
| 10816 | /* This check keeps Python strings that end in '\0' from comparing equal |
| 10817 | to C strings identical up to that point. */ |
| 10818 | if (PyUnicode_GET_LENGTH(uni) != i || chr) |
| 10819 | return 1; /* uni is longer */ |
| 10820 | if (str[i]) |
| 10821 | return -1; /* str is longer */ |
| 10822 | return 0; |
| 10823 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10824 | } |
| 10825 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10826 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10827 | #define TEST_COND(cond) \ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10828 | ((cond) ? Py_True : Py_False) |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10829 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10830 | PyObject * |
| 10831 | PyUnicode_RichCompare(PyObject *left, PyObject *right, int op) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10832 | { |
| 10833 | int result; |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10834 | PyObject *v; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10835 | |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10836 | if (!PyUnicode_Check(left) || !PyUnicode_Check(right)) |
| 10837 | Py_RETURN_NOTIMPLEMENTED; |
| 10838 | |
| 10839 | if (PyUnicode_READY(left) == -1 || |
| 10840 | PyUnicode_READY(right) == -1) |
| 10841 | return NULL; |
| 10842 | |
Victor Stinner | fd9e44d | 2013-11-04 11:23:05 +0100 | [diff] [blame] | 10843 | if (left == right) { |
| 10844 | switch (op) { |
| 10845 | case Py_EQ: |
| 10846 | case Py_LE: |
| 10847 | case Py_GE: |
| 10848 | /* a string is equal to itself */ |
| 10849 | v = Py_True; |
| 10850 | break; |
| 10851 | case Py_NE: |
| 10852 | case Py_LT: |
| 10853 | case Py_GT: |
| 10854 | v = Py_False; |
| 10855 | break; |
| 10856 | default: |
| 10857 | PyErr_BadArgument(); |
| 10858 | return NULL; |
| 10859 | } |
| 10860 | } |
| 10861 | else if (op == Py_EQ || op == Py_NE) { |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10862 | result = unicode_compare_eq(left, right); |
Victor Stinner | c8bc537 | 2013-11-04 11:08:10 +0100 | [diff] [blame] | 10863 | result ^= (op == Py_NE); |
| 10864 | v = TEST_COND(result); |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10865 | } |
| 10866 | else { |
Victor Stinner | 90db9c4 | 2012-10-04 21:53:50 +0200 | [diff] [blame] | 10867 | result = unicode_compare(left, right); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10868 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10869 | /* Convert the return value to a Boolean */ |
| 10870 | switch (op) { |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10871 | case Py_LE: |
| 10872 | v = TEST_COND(result <= 0); |
| 10873 | break; |
| 10874 | case Py_GE: |
| 10875 | v = TEST_COND(result >= 0); |
| 10876 | break; |
| 10877 | case Py_LT: |
| 10878 | v = TEST_COND(result == -1); |
| 10879 | break; |
| 10880 | case Py_GT: |
| 10881 | v = TEST_COND(result == 1); |
| 10882 | break; |
| 10883 | default: |
| 10884 | PyErr_BadArgument(); |
| 10885 | return NULL; |
| 10886 | } |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10887 | } |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10888 | Py_INCREF(v); |
| 10889 | return v; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10890 | } |
| 10891 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10892 | int |
| 10893 | PyUnicode_Contains(PyObject *container, PyObject *element) |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10894 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10895 | PyObject *str, *sub; |
Victor Stinner | 77282cb | 2013-04-14 19:22:47 +0200 | [diff] [blame] | 10896 | int kind1, kind2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10897 | void *buf1, *buf2; |
| 10898 | Py_ssize_t len1, len2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10899 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10900 | |
| 10901 | /* Coerce the two arguments */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10902 | sub = PyUnicode_FromObject(element); |
| 10903 | if (!sub) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10904 | PyErr_Format(PyExc_TypeError, |
| 10905 | "'in <string>' requires string as left operand, not %s", |
| 10906 | element->ob_type->tp_name); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10907 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10908 | } |
| 10909 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10910 | str = PyUnicode_FromObject(container); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 10911 | if (!str) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10912 | Py_DECREF(sub); |
| 10913 | return -1; |
| 10914 | } |
| 10915 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10916 | kind1 = PyUnicode_KIND(str); |
| 10917 | kind2 = PyUnicode_KIND(sub); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10918 | buf1 = PyUnicode_DATA(str); |
| 10919 | buf2 = PyUnicode_DATA(sub); |
Victor Stinner | 77282cb | 2013-04-14 19:22:47 +0200 | [diff] [blame] | 10920 | if (kind2 != kind1) { |
| 10921 | if (kind2 > kind1) { |
Antoine Pitrou | 758153b | 2012-05-12 15:51:51 +0200 | [diff] [blame] | 10922 | Py_DECREF(sub); |
| 10923 | Py_DECREF(str); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 10924 | return 0; |
Antoine Pitrou | 758153b | 2012-05-12 15:51:51 +0200 | [diff] [blame] | 10925 | } |
Victor Stinner | 77282cb | 2013-04-14 19:22:47 +0200 | [diff] [blame] | 10926 | buf2 = _PyUnicode_AsKind(sub, kind1); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 10927 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10928 | if (!buf2) { |
| 10929 | Py_DECREF(sub); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 10930 | Py_DECREF(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10931 | return -1; |
| 10932 | } |
| 10933 | len1 = PyUnicode_GET_LENGTH(str); |
| 10934 | len2 = PyUnicode_GET_LENGTH(sub); |
| 10935 | |
Victor Stinner | 77282cb | 2013-04-14 19:22:47 +0200 | [diff] [blame] | 10936 | switch (kind1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10937 | case PyUnicode_1BYTE_KIND: |
| 10938 | result = ucs1lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10939 | break; |
| 10940 | case PyUnicode_2BYTE_KIND: |
| 10941 | result = ucs2lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10942 | break; |
| 10943 | case PyUnicode_4BYTE_KIND: |
| 10944 | result = ucs4lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10945 | break; |
| 10946 | default: |
| 10947 | result = -1; |
| 10948 | assert(0); |
| 10949 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10950 | |
| 10951 | Py_DECREF(str); |
| 10952 | Py_DECREF(sub); |
| 10953 | |
Victor Stinner | 77282cb | 2013-04-14 19:22:47 +0200 | [diff] [blame] | 10954 | if (kind2 != kind1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10955 | PyMem_Free(buf2); |
| 10956 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10957 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10958 | } |
| 10959 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10960 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 10961 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10962 | PyObject * |
| 10963 | PyUnicode_Concat(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10964 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10965 | PyObject *u = NULL, *v = NULL, *w; |
Victor Stinner | 127226b | 2011-10-13 01:12:34 +0200 | [diff] [blame] | 10966 | Py_UCS4 maxchar, maxchar2; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10967 | Py_ssize_t u_len, v_len, new_len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10968 | |
| 10969 | /* Coerce the two arguments */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10970 | u = PyUnicode_FromObject(left); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10971 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10972 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10973 | v = PyUnicode_FromObject(right); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10974 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10975 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10976 | |
| 10977 | /* Shortcuts */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 10978 | if (v == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10979 | Py_DECREF(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10980 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10981 | } |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 10982 | if (u == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10983 | Py_DECREF(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10984 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10985 | } |
| 10986 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10987 | u_len = PyUnicode_GET_LENGTH(u); |
| 10988 | v_len = PyUnicode_GET_LENGTH(v); |
| 10989 | if (u_len > PY_SSIZE_T_MAX - v_len) { |
| 10990 | PyErr_SetString(PyExc_OverflowError, |
| 10991 | "strings are too large to concat"); |
| 10992 | goto onError; |
| 10993 | } |
| 10994 | new_len = u_len + v_len; |
| 10995 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10996 | maxchar = PyUnicode_MAX_CHAR_VALUE(u); |
Victor Stinner | 127226b | 2011-10-13 01:12:34 +0200 | [diff] [blame] | 10997 | maxchar2 = PyUnicode_MAX_CHAR_VALUE(v); |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 10998 | maxchar = Py_MAX(maxchar, maxchar2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10999 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11000 | /* Concat the two Unicode strings */ |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11001 | w = PyUnicode_New(new_len, maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11002 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11003 | goto onError; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 11004 | _PyUnicode_FastCopyCharacters(w, 0, u, 0, u_len); |
| 11005 | _PyUnicode_FastCopyCharacters(w, u_len, v, 0, v_len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11006 | Py_DECREF(u); |
| 11007 | Py_DECREF(v); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 11008 | assert(_PyUnicode_CheckConsistency(w, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11009 | return w; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11010 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11011 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11012 | Py_XDECREF(u); |
| 11013 | Py_XDECREF(v); |
| 11014 | return NULL; |
| 11015 | } |
| 11016 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 11017 | void |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11018 | PyUnicode_Append(PyObject **p_left, PyObject *right) |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 11019 | { |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11020 | PyObject *left, *res; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11021 | Py_UCS4 maxchar, maxchar2; |
| 11022 | Py_ssize_t left_len, right_len, new_len; |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11023 | |
| 11024 | if (p_left == NULL) { |
| 11025 | if (!PyErr_Occurred()) |
| 11026 | PyErr_BadInternalCall(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11027 | return; |
| 11028 | } |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11029 | left = *p_left; |
Victor Stinner | f033510 | 2013-04-14 19:13:03 +0200 | [diff] [blame] | 11030 | if (right == NULL || left == NULL |
| 11031 | || !PyUnicode_Check(left) || !PyUnicode_Check(right)) { |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11032 | if (!PyErr_Occurred()) |
| 11033 | PyErr_BadInternalCall(); |
| 11034 | goto error; |
| 11035 | } |
| 11036 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 11037 | if (PyUnicode_READY(left) == -1) |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 11038 | goto error; |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 11039 | if (PyUnicode_READY(right) == -1) |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 11040 | goto error; |
| 11041 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11042 | /* Shortcuts */ |
| 11043 | if (left == unicode_empty) { |
| 11044 | Py_DECREF(left); |
| 11045 | Py_INCREF(right); |
| 11046 | *p_left = right; |
| 11047 | return; |
| 11048 | } |
| 11049 | if (right == unicode_empty) |
| 11050 | return; |
| 11051 | |
| 11052 | left_len = PyUnicode_GET_LENGTH(left); |
| 11053 | right_len = PyUnicode_GET_LENGTH(right); |
| 11054 | if (left_len > PY_SSIZE_T_MAX - right_len) { |
| 11055 | PyErr_SetString(PyExc_OverflowError, |
| 11056 | "strings are too large to concat"); |
| 11057 | goto error; |
| 11058 | } |
| 11059 | new_len = left_len + right_len; |
| 11060 | |
| 11061 | if (unicode_modifiable(left) |
| 11062 | && PyUnicode_CheckExact(right) |
| 11063 | && PyUnicode_KIND(right) <= PyUnicode_KIND(left) |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 11064 | /* Don't resize for ascii += latin1. Convert ascii to latin1 requires |
| 11065 | to change the structure size, but characters are stored just after |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 11066 | the structure, and so it requires to move all characters which is |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 11067 | not so different than duplicating the string. */ |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11068 | && !(PyUnicode_IS_ASCII(left) && !PyUnicode_IS_ASCII(right))) |
| 11069 | { |
| 11070 | /* append inplace */ |
Victor Stinner | bb4503f | 2013-04-18 09:41:34 +0200 | [diff] [blame] | 11071 | if (unicode_resize(p_left, new_len) != 0) |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11072 | goto error; |
Victor Stinner | f033510 | 2013-04-14 19:13:03 +0200 | [diff] [blame] | 11073 | |
Victor Stinner | bb4503f | 2013-04-18 09:41:34 +0200 | [diff] [blame] | 11074 | /* copy 'right' into the newly allocated area of 'left' */ |
| 11075 | _PyUnicode_FastCopyCharacters(*p_left, left_len, right, 0, right_len); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11076 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11077 | else { |
| 11078 | maxchar = PyUnicode_MAX_CHAR_VALUE(left); |
| 11079 | maxchar2 = PyUnicode_MAX_CHAR_VALUE(right); |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 11080 | maxchar = Py_MAX(maxchar, maxchar2); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11081 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11082 | /* Concat the two Unicode strings */ |
| 11083 | res = PyUnicode_New(new_len, maxchar); |
| 11084 | if (res == NULL) |
| 11085 | goto error; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 11086 | _PyUnicode_FastCopyCharacters(res, 0, left, 0, left_len); |
| 11087 | _PyUnicode_FastCopyCharacters(res, left_len, right, 0, right_len); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11088 | Py_DECREF(left); |
Victor Stinner | bb4503f | 2013-04-18 09:41:34 +0200 | [diff] [blame] | 11089 | *p_left = res; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11090 | } |
| 11091 | assert(_PyUnicode_CheckConsistency(*p_left, 1)); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11092 | return; |
| 11093 | |
| 11094 | error: |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11095 | Py_CLEAR(*p_left); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 11096 | } |
| 11097 | |
| 11098 | void |
| 11099 | PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right) |
| 11100 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11101 | PyUnicode_Append(pleft, right); |
| 11102 | Py_XDECREF(right); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 11103 | } |
| 11104 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11105 | PyDoc_STRVAR(count__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11106 | "S.count(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11107 | \n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11108 | Return the number of non-overlapping occurrences of substring sub in\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11109 | 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] | 11110 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11111 | |
| 11112 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11113 | unicode_count(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11114 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11115 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11116 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11117 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11118 | PyObject *result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11119 | int kind1, kind2, kind; |
| 11120 | void *buf1, *buf2; |
| 11121 | Py_ssize_t len1, len2, iresult; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11122 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11123 | if (!stringlib_parse_args_finds_unicode("count", args, &substring, |
| 11124 | &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11125 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11126 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11127 | kind1 = PyUnicode_KIND(self); |
| 11128 | kind2 = PyUnicode_KIND(substring); |
Christian Heimes | d47802e | 2013-06-29 21:33:36 +0200 | [diff] [blame] | 11129 | if (kind2 > kind1) { |
| 11130 | Py_DECREF(substring); |
Benjamin Peterson | b63f49f | 2012-05-03 18:31:07 -0400 | [diff] [blame] | 11131 | return PyLong_FromLong(0); |
Christian Heimes | d47802e | 2013-06-29 21:33:36 +0200 | [diff] [blame] | 11132 | } |
Benjamin Peterson | b63f49f | 2012-05-03 18:31:07 -0400 | [diff] [blame] | 11133 | kind = kind1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11134 | buf1 = PyUnicode_DATA(self); |
| 11135 | buf2 = PyUnicode_DATA(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11136 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11137 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11138 | if (!buf2) { |
| 11139 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11140 | return NULL; |
| 11141 | } |
| 11142 | len1 = PyUnicode_GET_LENGTH(self); |
| 11143 | len2 = PyUnicode_GET_LENGTH(substring); |
| 11144 | |
| 11145 | ADJUST_INDICES(start, end, len1); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 11146 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11147 | case PyUnicode_1BYTE_KIND: |
| 11148 | iresult = ucs1lib_count( |
| 11149 | ((Py_UCS1*)buf1) + start, end - start, |
| 11150 | buf2, len2, PY_SSIZE_T_MAX |
| 11151 | ); |
| 11152 | break; |
| 11153 | case PyUnicode_2BYTE_KIND: |
| 11154 | iresult = ucs2lib_count( |
| 11155 | ((Py_UCS2*)buf1) + start, end - start, |
| 11156 | buf2, len2, PY_SSIZE_T_MAX |
| 11157 | ); |
| 11158 | break; |
| 11159 | case PyUnicode_4BYTE_KIND: |
| 11160 | iresult = ucs4lib_count( |
| 11161 | ((Py_UCS4*)buf1) + start, end - start, |
| 11162 | buf2, len2, PY_SSIZE_T_MAX |
| 11163 | ); |
| 11164 | break; |
| 11165 | default: |
| 11166 | assert(0); iresult = 0; |
| 11167 | } |
| 11168 | |
| 11169 | result = PyLong_FromSsize_t(iresult); |
| 11170 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11171 | if (kind2 != kind) |
| 11172 | PyMem_Free(buf2); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11173 | |
| 11174 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11175 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11176 | return result; |
| 11177 | } |
| 11178 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11179 | PyDoc_STRVAR(encode__doc__, |
Victor Stinner | c911bbf | 2010-11-07 19:04:46 +0000 | [diff] [blame] | 11180 | "S.encode(encoding='utf-8', errors='strict') -> bytes\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11181 | \n\ |
Victor Stinner | e14e212 | 2010-11-07 18:41:46 +0000 | [diff] [blame] | 11182 | Encode S using the codec registered for encoding. Default encoding\n\ |
| 11183 | 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] | 11184 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 11185 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 11186 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 11187 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11188 | |
| 11189 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11190 | unicode_encode(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11191 | { |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 11192 | static char *kwlist[] = {"encoding", "errors", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11193 | char *encoding = NULL; |
| 11194 | char *errors = NULL; |
Guido van Rossum | 35d9428 | 2007-08-27 18:20:11 +0000 | [diff] [blame] | 11195 | |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 11196 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:encode", |
| 11197 | kwlist, &encoding, &errors)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11198 | return NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11199 | return PyUnicode_AsEncodedString(self, encoding, errors); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 11200 | } |
| 11201 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11202 | PyDoc_STRVAR(expandtabs__doc__, |
Ezio Melotti | 745d54d | 2013-11-16 19:10:57 +0200 | [diff] [blame] | 11203 | "S.expandtabs(tabsize=8) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11204 | \n\ |
| 11205 | 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] | 11206 | 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] | 11207 | |
| 11208 | static PyObject* |
Ezio Melotti | 745d54d | 2013-11-16 19:10:57 +0200 | [diff] [blame] | 11209 | unicode_expandtabs(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11210 | { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11211 | Py_ssize_t i, j, line_pos, src_len, incr; |
| 11212 | Py_UCS4 ch; |
| 11213 | PyObject *u; |
| 11214 | void *src_data, *dest_data; |
Ezio Melotti | 745d54d | 2013-11-16 19:10:57 +0200 | [diff] [blame] | 11215 | static char *kwlist[] = {"tabsize", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11216 | int tabsize = 8; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11217 | int kind; |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 11218 | int found; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11219 | |
Ezio Melotti | 745d54d | 2013-11-16 19:10:57 +0200 | [diff] [blame] | 11220 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:expandtabs", |
| 11221 | kwlist, &tabsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11222 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11223 | |
Antoine Pitrou | 2242522 | 2011-10-04 19:10:51 +0200 | [diff] [blame] | 11224 | if (PyUnicode_READY(self) == -1) |
| 11225 | return NULL; |
| 11226 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 11227 | /* First pass: determine size of output string */ |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11228 | src_len = PyUnicode_GET_LENGTH(self); |
| 11229 | i = j = line_pos = 0; |
| 11230 | kind = PyUnicode_KIND(self); |
| 11231 | src_data = PyUnicode_DATA(self); |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 11232 | found = 0; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11233 | for (; i < src_len; i++) { |
| 11234 | ch = PyUnicode_READ(kind, src_data, i); |
| 11235 | if (ch == '\t') { |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 11236 | found = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11237 | if (tabsize > 0) { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11238 | incr = tabsize - (line_pos % tabsize); /* cannot overflow */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11239 | if (j > PY_SSIZE_T_MAX - incr) |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11240 | goto overflow; |
| 11241 | line_pos += incr; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11242 | j += incr; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11243 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11244 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11245 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11246 | if (j > PY_SSIZE_T_MAX - 1) |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11247 | goto overflow; |
| 11248 | line_pos++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11249 | j++; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11250 | if (ch == '\n' || ch == '\r') |
| 11251 | line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11252 | } |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11253 | } |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11254 | if (!found) |
| 11255 | return unicode_result_unchanged(self); |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 11256 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11257 | /* Second pass: create output string and fill it */ |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11258 | u = PyUnicode_New(j, PyUnicode_MAX_CHAR_VALUE(self)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11259 | if (!u) |
| 11260 | return NULL; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11261 | dest_data = PyUnicode_DATA(u); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11262 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11263 | i = j = line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11264 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11265 | for (; i < src_len; i++) { |
| 11266 | ch = PyUnicode_READ(kind, src_data, i); |
| 11267 | if (ch == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11268 | if (tabsize > 0) { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11269 | incr = tabsize - (line_pos % tabsize); |
| 11270 | line_pos += incr; |
Victor Stinner | da79e63 | 2012-02-22 13:37:04 +0100 | [diff] [blame] | 11271 | FILL(kind, dest_data, ' ', j, incr); |
| 11272 | j += incr; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11273 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11274 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11275 | else { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11276 | line_pos++; |
| 11277 | PyUnicode_WRITE(kind, dest_data, j, ch); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11278 | j++; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11279 | if (ch == '\n' || ch == '\r') |
| 11280 | line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11281 | } |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11282 | } |
| 11283 | assert (j == PyUnicode_GET_LENGTH(u)); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 11284 | return unicode_result(u); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11285 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11286 | overflow: |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11287 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 11288 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11289 | } |
| 11290 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11291 | PyDoc_STRVAR(find__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11292 | "S.find(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11293 | \n\ |
| 11294 | Return the lowest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 11295 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11296 | arguments start and end are interpreted as in slice notation.\n\ |
| 11297 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11298 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11299 | |
| 11300 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11301 | unicode_find(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11302 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11303 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 11304 | Py_ssize_t start; |
| 11305 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11306 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11307 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11308 | if (!stringlib_parse_args_finds_unicode("find", args, &substring, |
| 11309 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11310 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11311 | |
Christian Heimes | d47802e | 2013-06-29 21:33:36 +0200 | [diff] [blame] | 11312 | if (PyUnicode_READY(self) == -1) { |
| 11313 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11314 | return NULL; |
Christian Heimes | d47802e | 2013-06-29 21:33:36 +0200 | [diff] [blame] | 11315 | } |
| 11316 | if (PyUnicode_READY(substring) == -1) { |
| 11317 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11318 | return NULL; |
Christian Heimes | d47802e | 2013-06-29 21:33:36 +0200 | [diff] [blame] | 11319 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11320 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11321 | result = any_find_slice(1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11322 | |
| 11323 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11324 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11325 | if (result == -2) |
| 11326 | return NULL; |
| 11327 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11328 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11329 | } |
| 11330 | |
| 11331 | static PyObject * |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 11332 | unicode_getitem(PyObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11333 | { |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 11334 | void *data; |
| 11335 | enum PyUnicode_Kind kind; |
| 11336 | Py_UCS4 ch; |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 11337 | |
| 11338 | if (!PyUnicode_Check(self) || PyUnicode_READY(self) == -1) { |
| 11339 | PyErr_BadArgument(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11340 | return NULL; |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 11341 | } |
| 11342 | if (index < 0 || index >= PyUnicode_GET_LENGTH(self)) { |
| 11343 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 11344 | return NULL; |
| 11345 | } |
| 11346 | kind = PyUnicode_KIND(self); |
| 11347 | data = PyUnicode_DATA(self); |
| 11348 | ch = PyUnicode_READ(kind, data, index); |
Victor Stinner | 985a82a | 2014-01-03 12:53:47 +0100 | [diff] [blame] | 11349 | return unicode_char(ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11350 | } |
| 11351 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11352 | /* Believe it or not, this produces the same value for ASCII strings |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 11353 | as bytes_hash(). */ |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 11354 | static Py_hash_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11355 | unicode_hash(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11356 | { |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11357 | Py_ssize_t len; |
Gregory P. Smith | 27cbcd6 | 2012-12-10 18:15:46 -0800 | [diff] [blame] | 11358 | Py_uhash_t x; /* Unsigned for defined overflow behavior. */ |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11359 | |
Benjamin Peterson | f6622c8 | 2012-04-09 14:53:07 -0400 | [diff] [blame] | 11360 | #ifdef Py_DEBUG |
Benjamin Peterson | 69e9727 | 2012-02-21 11:08:50 -0500 | [diff] [blame] | 11361 | assert(_Py_HashSecret_Initialized); |
Benjamin Peterson | f6622c8 | 2012-04-09 14:53:07 -0400 | [diff] [blame] | 11362 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11363 | if (_PyUnicode_HASH(self) != -1) |
| 11364 | return _PyUnicode_HASH(self); |
| 11365 | if (PyUnicode_READY(self) == -1) |
| 11366 | return -1; |
| 11367 | len = PyUnicode_GET_LENGTH(self); |
Georg Brandl | 16fa2a1 | 2012-02-21 00:50:13 +0100 | [diff] [blame] | 11368 | /* |
| 11369 | We make the hash of the empty string be 0, rather than using |
| 11370 | (prefix ^ suffix), since this slightly obfuscates the hash secret |
| 11371 | */ |
| 11372 | if (len == 0) { |
| 11373 | _PyUnicode_HASH(self) = 0; |
| 11374 | return 0; |
| 11375 | } |
Christian Heimes | 985ecdc | 2013-11-20 11:46:18 +0100 | [diff] [blame] | 11376 | x = _Py_HashBytes(PyUnicode_DATA(self), |
| 11377 | PyUnicode_GET_LENGTH(self) * PyUnicode_KIND(self)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11378 | _PyUnicode_HASH(self) = x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11379 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11380 | } |
| 11381 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11382 | PyDoc_STRVAR(index__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11383 | "S.index(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11384 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11385 | 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] | 11386 | |
| 11387 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11388 | unicode_index(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11389 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11390 | Py_ssize_t result; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11391 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 11392 | Py_ssize_t start; |
| 11393 | Py_ssize_t end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11394 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11395 | if (!stringlib_parse_args_finds_unicode("index", args, &substring, |
| 11396 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11397 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11398 | |
Christian Heimes | d47a045 | 2013-06-29 21:21:37 +0200 | [diff] [blame] | 11399 | if (PyUnicode_READY(self) == -1) { |
| 11400 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11401 | return NULL; |
Christian Heimes | d47a045 | 2013-06-29 21:21:37 +0200 | [diff] [blame] | 11402 | } |
| 11403 | if (PyUnicode_READY(substring) == -1) { |
| 11404 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11405 | return NULL; |
Christian Heimes | d47a045 | 2013-06-29 21:21:37 +0200 | [diff] [blame] | 11406 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11407 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11408 | result = any_find_slice(1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11409 | |
| 11410 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11411 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11412 | if (result == -2) |
| 11413 | return NULL; |
| 11414 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11415 | if (result < 0) { |
| 11416 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 11417 | return NULL; |
| 11418 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11419 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11420 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11421 | } |
| 11422 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11423 | PyDoc_STRVAR(islower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11424 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11425 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11426 | 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] | 11427 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11428 | |
| 11429 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11430 | unicode_islower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11431 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11432 | Py_ssize_t i, length; |
| 11433 | int kind; |
| 11434 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11435 | int cased; |
| 11436 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11437 | if (PyUnicode_READY(self) == -1) |
| 11438 | return NULL; |
| 11439 | length = PyUnicode_GET_LENGTH(self); |
| 11440 | kind = PyUnicode_KIND(self); |
| 11441 | data = PyUnicode_DATA(self); |
| 11442 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11443 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11444 | if (length == 1) |
| 11445 | return PyBool_FromLong( |
| 11446 | Py_UNICODE_ISLOWER(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11447 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11448 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11449 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11450 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11451 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11452 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11453 | for (i = 0; i < length; i++) { |
| 11454 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11455 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11456 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 11457 | return PyBool_FromLong(0); |
| 11458 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 11459 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11460 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11461 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11462 | } |
| 11463 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11464 | PyDoc_STRVAR(isupper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11465 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11466 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11467 | 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] | 11468 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11469 | |
| 11470 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11471 | unicode_isupper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11472 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11473 | Py_ssize_t i, length; |
| 11474 | int kind; |
| 11475 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11476 | int cased; |
| 11477 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11478 | if (PyUnicode_READY(self) == -1) |
| 11479 | return NULL; |
| 11480 | length = PyUnicode_GET_LENGTH(self); |
| 11481 | kind = PyUnicode_KIND(self); |
| 11482 | data = PyUnicode_DATA(self); |
| 11483 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11484 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11485 | if (length == 1) |
| 11486 | return PyBool_FromLong( |
| 11487 | Py_UNICODE_ISUPPER(PyUnicode_READ(kind, data, 0)) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11488 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11489 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11490 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11491 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11492 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11493 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11494 | for (i = 0; i < length; i++) { |
| 11495 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11496 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11497 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 11498 | return PyBool_FromLong(0); |
| 11499 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 11500 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11501 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11502 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11503 | } |
| 11504 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11505 | PyDoc_STRVAR(istitle__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11506 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11507 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11508 | Return True if S is a titlecased string and there is at least one\n\ |
| 11509 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 11510 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 11511 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11512 | |
| 11513 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11514 | unicode_istitle(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11515 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11516 | Py_ssize_t i, length; |
| 11517 | int kind; |
| 11518 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11519 | int cased, previous_is_cased; |
| 11520 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11521 | if (PyUnicode_READY(self) == -1) |
| 11522 | return NULL; |
| 11523 | length = PyUnicode_GET_LENGTH(self); |
| 11524 | kind = PyUnicode_KIND(self); |
| 11525 | data = PyUnicode_DATA(self); |
| 11526 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11527 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11528 | if (length == 1) { |
| 11529 | Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11530 | return PyBool_FromLong((Py_UNICODE_ISTITLE(ch) != 0) || |
| 11531 | (Py_UNICODE_ISUPPER(ch) != 0)); |
| 11532 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11533 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11534 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11535 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11536 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11537 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11538 | cased = 0; |
| 11539 | previous_is_cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11540 | for (i = 0; i < length; i++) { |
| 11541 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11542 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11543 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 11544 | if (previous_is_cased) |
| 11545 | return PyBool_FromLong(0); |
| 11546 | previous_is_cased = 1; |
| 11547 | cased = 1; |
| 11548 | } |
| 11549 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 11550 | if (!previous_is_cased) |
| 11551 | return PyBool_FromLong(0); |
| 11552 | previous_is_cased = 1; |
| 11553 | cased = 1; |
| 11554 | } |
| 11555 | else |
| 11556 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11557 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11558 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11559 | } |
| 11560 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11561 | PyDoc_STRVAR(isspace__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11562 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11563 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11564 | Return True if all characters in S are whitespace\n\ |
| 11565 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11566 | |
| 11567 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11568 | unicode_isspace(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11569 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11570 | Py_ssize_t i, length; |
| 11571 | int kind; |
| 11572 | void *data; |
| 11573 | |
| 11574 | if (PyUnicode_READY(self) == -1) |
| 11575 | return NULL; |
| 11576 | length = PyUnicode_GET_LENGTH(self); |
| 11577 | kind = PyUnicode_KIND(self); |
| 11578 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11579 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11580 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11581 | if (length == 1) |
| 11582 | return PyBool_FromLong( |
| 11583 | Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11584 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11585 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11586 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11587 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11588 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11589 | for (i = 0; i < length; i++) { |
| 11590 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11591 | if (!Py_UNICODE_ISSPACE(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11592 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11593 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11594 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11595 | } |
| 11596 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11597 | PyDoc_STRVAR(isalpha__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11598 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11599 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11600 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11601 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11602 | |
| 11603 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11604 | unicode_isalpha(PyObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11605 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11606 | Py_ssize_t i, length; |
| 11607 | int kind; |
| 11608 | void *data; |
| 11609 | |
| 11610 | if (PyUnicode_READY(self) == -1) |
| 11611 | return NULL; |
| 11612 | length = PyUnicode_GET_LENGTH(self); |
| 11613 | kind = PyUnicode_KIND(self); |
| 11614 | data = PyUnicode_DATA(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11615 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11616 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11617 | if (length == 1) |
| 11618 | return PyBool_FromLong( |
| 11619 | Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, 0))); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11620 | |
| 11621 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11622 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11623 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11624 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11625 | for (i = 0; i < length; i++) { |
| 11626 | if (!Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11627 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11628 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11629 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11630 | } |
| 11631 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11632 | PyDoc_STRVAR(isalnum__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11633 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11634 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11635 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11636 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11637 | |
| 11638 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11639 | unicode_isalnum(PyObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11640 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11641 | int kind; |
| 11642 | void *data; |
| 11643 | Py_ssize_t len, i; |
| 11644 | |
| 11645 | if (PyUnicode_READY(self) == -1) |
| 11646 | return NULL; |
| 11647 | |
| 11648 | kind = PyUnicode_KIND(self); |
| 11649 | data = PyUnicode_DATA(self); |
| 11650 | len = PyUnicode_GET_LENGTH(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11651 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11652 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11653 | if (len == 1) { |
| 11654 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11655 | return PyBool_FromLong(Py_UNICODE_ISALNUM(ch)); |
| 11656 | } |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11657 | |
| 11658 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11659 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11660 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11661 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11662 | for (i = 0; i < len; i++) { |
| 11663 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11664 | if (!Py_UNICODE_ISALNUM(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11665 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11666 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11667 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11668 | } |
| 11669 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11670 | PyDoc_STRVAR(isdecimal__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11671 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11672 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11673 | 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] | 11674 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11675 | |
| 11676 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11677 | unicode_isdecimal(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11678 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11679 | Py_ssize_t i, length; |
| 11680 | int kind; |
| 11681 | void *data; |
| 11682 | |
| 11683 | if (PyUnicode_READY(self) == -1) |
| 11684 | return NULL; |
| 11685 | length = PyUnicode_GET_LENGTH(self); |
| 11686 | kind = PyUnicode_KIND(self); |
| 11687 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11688 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11689 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11690 | if (length == 1) |
| 11691 | return PyBool_FromLong( |
| 11692 | Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11693 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11694 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11695 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11696 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11697 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11698 | for (i = 0; i < length; i++) { |
| 11699 | if (!Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11700 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11701 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11702 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11703 | } |
| 11704 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11705 | PyDoc_STRVAR(isdigit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11706 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11707 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11708 | Return True if all characters in S are digits\n\ |
| 11709 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11710 | |
| 11711 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11712 | unicode_isdigit(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11713 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11714 | Py_ssize_t i, length; |
| 11715 | int kind; |
| 11716 | void *data; |
| 11717 | |
| 11718 | if (PyUnicode_READY(self) == -1) |
| 11719 | return NULL; |
| 11720 | length = PyUnicode_GET_LENGTH(self); |
| 11721 | kind = PyUnicode_KIND(self); |
| 11722 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11723 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11724 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11725 | if (length == 1) { |
| 11726 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11727 | return PyBool_FromLong(Py_UNICODE_ISDIGIT(ch)); |
| 11728 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11729 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11730 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11731 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11732 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11733 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11734 | for (i = 0; i < length; i++) { |
| 11735 | if (!Py_UNICODE_ISDIGIT(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11736 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11737 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11738 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11739 | } |
| 11740 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11741 | PyDoc_STRVAR(isnumeric__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11742 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11743 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11744 | 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] | 11745 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11746 | |
| 11747 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11748 | unicode_isnumeric(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11749 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11750 | Py_ssize_t i, length; |
| 11751 | int kind; |
| 11752 | void *data; |
| 11753 | |
| 11754 | if (PyUnicode_READY(self) == -1) |
| 11755 | return NULL; |
| 11756 | length = PyUnicode_GET_LENGTH(self); |
| 11757 | kind = PyUnicode_KIND(self); |
| 11758 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11759 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11760 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11761 | if (length == 1) |
| 11762 | return PyBool_FromLong( |
| 11763 | Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11764 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11765 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11766 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11767 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11768 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11769 | for (i = 0; i < length; i++) { |
| 11770 | if (!Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11771 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11772 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11773 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11774 | } |
| 11775 | |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11776 | int |
| 11777 | PyUnicode_IsIdentifier(PyObject *self) |
| 11778 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11779 | int kind; |
| 11780 | void *data; |
| 11781 | Py_ssize_t i; |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11782 | Py_UCS4 first; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11783 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11784 | if (PyUnicode_READY(self) == -1) { |
| 11785 | Py_FatalError("identifier not ready"); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11786 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11787 | } |
| 11788 | |
| 11789 | /* Special case for empty strings */ |
| 11790 | if (PyUnicode_GET_LENGTH(self) == 0) |
| 11791 | return 0; |
| 11792 | kind = PyUnicode_KIND(self); |
| 11793 | data = PyUnicode_DATA(self); |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11794 | |
| 11795 | /* PEP 3131 says that the first character must be in |
| 11796 | XID_Start and subsequent characters in XID_Continue, |
| 11797 | and for the ASCII range, the 2.x rules apply (i.e |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11798 | start with letters and underscore, continue with |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11799 | letters, digits, underscore). However, given the current |
| 11800 | definition of XID_Start and XID_Continue, it is sufficient |
| 11801 | to check just for these, except that _ must be allowed |
| 11802 | as starting an identifier. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11803 | first = PyUnicode_READ(kind, data, 0); |
Benjamin Peterson | f413b80 | 2011-08-12 22:17:18 -0500 | [diff] [blame] | 11804 | if (!_PyUnicode_IsXidStart(first) && first != 0x5F /* LOW LINE */) |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11805 | return 0; |
| 11806 | |
Benjamin Peterson | 9c6e6a0 | 2011-09-28 08:09:05 -0400 | [diff] [blame] | 11807 | for (i = 1; i < PyUnicode_GET_LENGTH(self); i++) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11808 | if (!_PyUnicode_IsXidContinue(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11809 | return 0; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11810 | return 1; |
| 11811 | } |
| 11812 | |
| 11813 | PyDoc_STRVAR(isidentifier__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11814 | "S.isidentifier() -> bool\n\ |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11815 | \n\ |
| 11816 | Return True if S is a valid identifier according\n\ |
Raymond Hettinger | 378170d | 2013-03-23 08:21:12 -0700 | [diff] [blame] | 11817 | to the language definition.\n\ |
| 11818 | \n\ |
| 11819 | Use keyword.iskeyword() to test for reserved identifiers\n\ |
| 11820 | such as \"def\" and \"class\".\n"); |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11821 | |
| 11822 | static PyObject* |
| 11823 | unicode_isidentifier(PyObject *self) |
| 11824 | { |
| 11825 | return PyBool_FromLong(PyUnicode_IsIdentifier(self)); |
| 11826 | } |
| 11827 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11828 | PyDoc_STRVAR(isprintable__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11829 | "S.isprintable() -> bool\n\ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11830 | \n\ |
| 11831 | Return True if all characters in S are considered\n\ |
| 11832 | printable in repr() or S is empty, False otherwise."); |
| 11833 | |
| 11834 | static PyObject* |
| 11835 | unicode_isprintable(PyObject *self) |
| 11836 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11837 | Py_ssize_t i, length; |
| 11838 | int kind; |
| 11839 | void *data; |
| 11840 | |
| 11841 | if (PyUnicode_READY(self) == -1) |
| 11842 | return NULL; |
| 11843 | length = PyUnicode_GET_LENGTH(self); |
| 11844 | kind = PyUnicode_KIND(self); |
| 11845 | data = PyUnicode_DATA(self); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11846 | |
| 11847 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11848 | if (length == 1) |
| 11849 | return PyBool_FromLong( |
| 11850 | Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, 0))); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11851 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11852 | for (i = 0; i < length; i++) { |
| 11853 | if (!Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, i))) { |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11854 | Py_RETURN_FALSE; |
| 11855 | } |
| 11856 | } |
| 11857 | Py_RETURN_TRUE; |
| 11858 | } |
| 11859 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11860 | PyDoc_STRVAR(join__doc__, |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 11861 | "S.join(iterable) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11862 | \n\ |
| 11863 | 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] | 11864 | iterable. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11865 | |
| 11866 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11867 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11868 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11869 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11870 | } |
| 11871 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11872 | static Py_ssize_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11873 | unicode_length(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11874 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11875 | if (PyUnicode_READY(self) == -1) |
| 11876 | return -1; |
| 11877 | return PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11878 | } |
| 11879 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11880 | PyDoc_STRVAR(ljust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11881 | "S.ljust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11882 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 11883 | 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] | 11884 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11885 | |
| 11886 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11887 | unicode_ljust(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11888 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11889 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11890 | Py_UCS4 fillchar = ' '; |
| 11891 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11892 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11893 | return NULL; |
| 11894 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 11895 | if (PyUnicode_READY(self) == -1) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11896 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11897 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11898 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 11899 | return unicode_result_unchanged(self); |
| 11900 | |
| 11901 | return pad(self, 0, width - PyUnicode_GET_LENGTH(self), fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11902 | } |
| 11903 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11904 | PyDoc_STRVAR(lower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11905 | "S.lower() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11906 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11907 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11908 | |
| 11909 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11910 | unicode_lower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11911 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 11912 | if (PyUnicode_READY(self) == -1) |
| 11913 | return NULL; |
| 11914 | if (PyUnicode_IS_ASCII(self)) |
| 11915 | return ascii_upper_or_lower(self, 1); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 11916 | return case_operation(self, do_lower); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11917 | } |
| 11918 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11919 | #define LEFTSTRIP 0 |
| 11920 | #define RIGHTSTRIP 1 |
| 11921 | #define BOTHSTRIP 2 |
| 11922 | |
| 11923 | /* Arrays indexed by above */ |
| 11924 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 11925 | |
| 11926 | #define STRIPNAME(i) (stripformat[i]+3) |
| 11927 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11928 | /* externally visible for str.strip(unicode) */ |
| 11929 | PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11930 | _PyUnicode_XStrip(PyObject *self, int striptype, PyObject *sepobj) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11931 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11932 | void *data; |
| 11933 | int kind; |
| 11934 | Py_ssize_t i, j, len; |
| 11935 | BLOOM_MASK sepmask; |
Victor Stinner | b3a6014 | 2013-04-09 22:19:21 +0200 | [diff] [blame] | 11936 | Py_ssize_t seplen; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11937 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11938 | if (PyUnicode_READY(self) == -1 || PyUnicode_READY(sepobj) == -1) |
| 11939 | return NULL; |
| 11940 | |
| 11941 | kind = PyUnicode_KIND(self); |
| 11942 | data = PyUnicode_DATA(self); |
| 11943 | len = PyUnicode_GET_LENGTH(self); |
Victor Stinner | b3a6014 | 2013-04-09 22:19:21 +0200 | [diff] [blame] | 11944 | seplen = PyUnicode_GET_LENGTH(sepobj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11945 | sepmask = make_bloom_mask(PyUnicode_KIND(sepobj), |
| 11946 | PyUnicode_DATA(sepobj), |
Victor Stinner | b3a6014 | 2013-04-09 22:19:21 +0200 | [diff] [blame] | 11947 | seplen); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11948 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11949 | i = 0; |
| 11950 | if (striptype != RIGHTSTRIP) { |
Victor Stinner | b3a6014 | 2013-04-09 22:19:21 +0200 | [diff] [blame] | 11951 | while (i < len) { |
| 11952 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 11953 | if (!BLOOM(sepmask, ch)) |
| 11954 | break; |
| 11955 | if (PyUnicode_FindChar(sepobj, ch, 0, seplen, 1) < 0) |
| 11956 | break; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11957 | i++; |
| 11958 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11959 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11960 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11961 | j = len; |
| 11962 | if (striptype != LEFTSTRIP) { |
Victor Stinner | b3a6014 | 2013-04-09 22:19:21 +0200 | [diff] [blame] | 11963 | j--; |
| 11964 | while (j >= i) { |
| 11965 | Py_UCS4 ch = PyUnicode_READ(kind, data, j); |
| 11966 | if (!BLOOM(sepmask, ch)) |
| 11967 | break; |
| 11968 | if (PyUnicode_FindChar(sepobj, ch, 0, seplen, 1) < 0) |
| 11969 | break; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11970 | j--; |
Victor Stinner | b3a6014 | 2013-04-09 22:19:21 +0200 | [diff] [blame] | 11971 | } |
| 11972 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11973 | j++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11974 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11975 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11976 | return PyUnicode_Substring(self, i, j); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11977 | } |
| 11978 | |
| 11979 | PyObject* |
| 11980 | PyUnicode_Substring(PyObject *self, Py_ssize_t start, Py_ssize_t end) |
| 11981 | { |
| 11982 | unsigned char *data; |
| 11983 | int kind; |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11984 | Py_ssize_t length; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11985 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 11986 | if (PyUnicode_READY(self) == -1) |
| 11987 | return NULL; |
| 11988 | |
Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 11989 | length = PyUnicode_GET_LENGTH(self); |
| 11990 | end = Py_MIN(end, length); |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 11991 | |
Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 11992 | if (start == 0 && end == length) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11993 | return unicode_result_unchanged(self); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11994 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 11995 | if (start < 0 || end < 0) { |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11996 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 11997 | return NULL; |
| 11998 | } |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 11999 | if (start >= length || end < start) |
| 12000 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 12001 | |
Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 12002 | length = end - start; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 12003 | if (PyUnicode_IS_ASCII(self)) { |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 12004 | data = PyUnicode_1BYTE_DATA(self); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12005 | return _PyUnicode_FromASCII((char*)(data + start), length); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 12006 | } |
| 12007 | else { |
| 12008 | kind = PyUnicode_KIND(self); |
| 12009 | data = PyUnicode_1BYTE_DATA(self); |
| 12010 | return PyUnicode_FromKindAndData(kind, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 12011 | data + kind * start, |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 12012 | length); |
| 12013 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12014 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12015 | |
| 12016 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12017 | do_strip(PyObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12018 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12019 | Py_ssize_t len, i, j; |
| 12020 | |
| 12021 | if (PyUnicode_READY(self) == -1) |
| 12022 | return NULL; |
| 12023 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12024 | len = PyUnicode_GET_LENGTH(self); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12025 | |
Victor Stinner | cc7af72 | 2013-04-09 22:39:24 +0200 | [diff] [blame] | 12026 | if (PyUnicode_IS_ASCII(self)) { |
| 12027 | Py_UCS1 *data = PyUnicode_1BYTE_DATA(self); |
| 12028 | |
| 12029 | i = 0; |
| 12030 | if (striptype != RIGHTSTRIP) { |
| 12031 | while (i < len) { |
Victor Stinner | d92e078 | 2013-04-14 19:17:42 +0200 | [diff] [blame] | 12032 | Py_UCS1 ch = data[i]; |
Victor Stinner | cc7af72 | 2013-04-09 22:39:24 +0200 | [diff] [blame] | 12033 | if (!_Py_ascii_whitespace[ch]) |
| 12034 | break; |
| 12035 | i++; |
| 12036 | } |
| 12037 | } |
| 12038 | |
| 12039 | j = len; |
| 12040 | if (striptype != LEFTSTRIP) { |
| 12041 | j--; |
| 12042 | while (j >= i) { |
Victor Stinner | d92e078 | 2013-04-14 19:17:42 +0200 | [diff] [blame] | 12043 | Py_UCS1 ch = data[j]; |
Victor Stinner | cc7af72 | 2013-04-09 22:39:24 +0200 | [diff] [blame] | 12044 | if (!_Py_ascii_whitespace[ch]) |
| 12045 | break; |
| 12046 | j--; |
| 12047 | } |
| 12048 | j++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12049 | } |
| 12050 | } |
Victor Stinner | cc7af72 | 2013-04-09 22:39:24 +0200 | [diff] [blame] | 12051 | else { |
| 12052 | int kind = PyUnicode_KIND(self); |
| 12053 | void *data = PyUnicode_DATA(self); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12054 | |
Victor Stinner | cc7af72 | 2013-04-09 22:39:24 +0200 | [diff] [blame] | 12055 | i = 0; |
| 12056 | if (striptype != RIGHTSTRIP) { |
| 12057 | while (i < len) { |
| 12058 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 12059 | if (!Py_UNICODE_ISSPACE(ch)) |
| 12060 | break; |
| 12061 | i++; |
| 12062 | } |
Victor Stinner | 9c79e41 | 2013-04-09 22:21:08 +0200 | [diff] [blame] | 12063 | } |
Victor Stinner | cc7af72 | 2013-04-09 22:39:24 +0200 | [diff] [blame] | 12064 | |
| 12065 | j = len; |
| 12066 | if (striptype != LEFTSTRIP) { |
| 12067 | j--; |
| 12068 | while (j >= i) { |
| 12069 | Py_UCS4 ch = PyUnicode_READ(kind, data, j); |
| 12070 | if (!Py_UNICODE_ISSPACE(ch)) |
| 12071 | break; |
| 12072 | j--; |
| 12073 | } |
| 12074 | j++; |
| 12075 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12076 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12077 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12078 | return PyUnicode_Substring(self, i, j); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12079 | } |
| 12080 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12081 | |
| 12082 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12083 | do_argstrip(PyObject *self, int striptype, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12084 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12085 | PyObject *sep = NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12086 | |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 12087 | if (!PyArg_ParseTuple(args, stripformat[striptype], &sep)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12088 | return NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12089 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12090 | if (sep != NULL && sep != Py_None) { |
| 12091 | if (PyUnicode_Check(sep)) |
| 12092 | return _PyUnicode_XStrip(self, striptype, sep); |
| 12093 | else { |
| 12094 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12095 | "%s arg must be None or str", |
| 12096 | STRIPNAME(striptype)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12097 | return NULL; |
| 12098 | } |
| 12099 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12100 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12101 | return do_strip(self, striptype); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12102 | } |
| 12103 | |
| 12104 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12105 | PyDoc_STRVAR(strip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12106 | "S.strip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12107 | \n\ |
| 12108 | Return a copy of the string S with leading and trailing\n\ |
| 12109 | whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12110 | 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] | 12111 | |
| 12112 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12113 | unicode_strip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12114 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12115 | if (PyTuple_GET_SIZE(args) == 0) |
| 12116 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 12117 | else |
| 12118 | return do_argstrip(self, BOTHSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12119 | } |
| 12120 | |
| 12121 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12122 | PyDoc_STRVAR(lstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12123 | "S.lstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12124 | \n\ |
| 12125 | Return a copy of the string S with leading whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12126 | 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] | 12127 | |
| 12128 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12129 | unicode_lstrip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12130 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12131 | if (PyTuple_GET_SIZE(args) == 0) |
| 12132 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 12133 | else |
| 12134 | return do_argstrip(self, LEFTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12135 | } |
| 12136 | |
| 12137 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12138 | PyDoc_STRVAR(rstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12139 | "S.rstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12140 | \n\ |
| 12141 | Return a copy of the string S with trailing whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12142 | If chars is given and not None, remove characters in chars instead."); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12143 | |
| 12144 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12145 | unicode_rstrip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12146 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12147 | if (PyTuple_GET_SIZE(args) == 0) |
| 12148 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 12149 | else |
| 12150 | return do_argstrip(self, RIGHTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12151 | } |
| 12152 | |
| 12153 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12154 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12155 | unicode_repeat(PyObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12156 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12157 | PyObject *u; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12158 | Py_ssize_t nchars, n; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12159 | |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 12160 | if (len < 1) |
| 12161 | _Py_RETURN_UNICODE_EMPTY(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12162 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12163 | /* no repeat, return original string */ |
| 12164 | if (len == 1) |
| 12165 | return unicode_result_unchanged(str); |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 12166 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 12167 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12168 | return NULL; |
| 12169 | |
Victor Stinner | c759f3e | 2011-10-01 03:09:58 +0200 | [diff] [blame] | 12170 | if (PyUnicode_GET_LENGTH(str) > PY_SSIZE_T_MAX / len) { |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12171 | PyErr_SetString(PyExc_OverflowError, |
| 12172 | "repeated string is too long"); |
| 12173 | return NULL; |
| 12174 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12175 | nchars = len * PyUnicode_GET_LENGTH(str); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12176 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12177 | u = PyUnicode_New(nchars, PyUnicode_MAX_CHAR_VALUE(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12178 | if (!u) |
| 12179 | return NULL; |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12180 | assert(PyUnicode_KIND(u) == PyUnicode_KIND(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12181 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12182 | if (PyUnicode_GET_LENGTH(str) == 1) { |
| 12183 | const int kind = PyUnicode_KIND(str); |
| 12184 | const Py_UCS4 fill_char = PyUnicode_READ(kind, PyUnicode_DATA(str), 0); |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 12185 | if (kind == PyUnicode_1BYTE_KIND) { |
| 12186 | void *to = PyUnicode_DATA(u); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12187 | memset(to, (unsigned char)fill_char, len); |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 12188 | } |
| 12189 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 12190 | Py_UCS2 *ucs2 = PyUnicode_2BYTE_DATA(u); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12191 | for (n = 0; n < len; ++n) |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 12192 | ucs2[n] = fill_char; |
| 12193 | } else { |
| 12194 | Py_UCS4 *ucs4 = PyUnicode_4BYTE_DATA(u); |
| 12195 | assert(kind == PyUnicode_4BYTE_KIND); |
| 12196 | for (n = 0; n < len; ++n) |
| 12197 | ucs4[n] = fill_char; |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12198 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12199 | } |
| 12200 | else { |
| 12201 | /* number of characters copied this far */ |
| 12202 | Py_ssize_t done = PyUnicode_GET_LENGTH(str); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 12203 | const Py_ssize_t char_size = PyUnicode_KIND(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12204 | char *to = (char *) PyUnicode_DATA(u); |
| 12205 | Py_MEMCPY(to, PyUnicode_DATA(str), |
| 12206 | PyUnicode_GET_LENGTH(str) * char_size); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12207 | while (done < nchars) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12208 | n = (done <= nchars-done) ? done : nchars-done; |
| 12209 | Py_MEMCPY(to + (done * char_size), to, n * char_size); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12210 | done += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12211 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12212 | } |
| 12213 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12214 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12215 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12216 | } |
| 12217 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12218 | PyObject * |
| 12219 | PyUnicode_Replace(PyObject *obj, |
| 12220 | PyObject *subobj, |
| 12221 | PyObject *replobj, |
| 12222 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12223 | { |
| 12224 | PyObject *self; |
| 12225 | PyObject *str1; |
| 12226 | PyObject *str2; |
| 12227 | PyObject *result; |
| 12228 | |
| 12229 | self = PyUnicode_FromObject(obj); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12230 | if (self == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12231 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12232 | str1 = PyUnicode_FromObject(subobj); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12233 | if (str1 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12234 | Py_DECREF(self); |
| 12235 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12236 | } |
| 12237 | str2 = PyUnicode_FromObject(replobj); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12238 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12239 | Py_DECREF(self); |
| 12240 | Py_DECREF(str1); |
| 12241 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12242 | } |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12243 | if (PyUnicode_READY(self) == -1 || |
| 12244 | PyUnicode_READY(str1) == -1 || |
| 12245 | PyUnicode_READY(str2) == -1) |
| 12246 | result = NULL; |
| 12247 | else |
| 12248 | result = replace(self, str1, str2, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12249 | Py_DECREF(self); |
| 12250 | Py_DECREF(str1); |
| 12251 | Py_DECREF(str2); |
| 12252 | return result; |
| 12253 | } |
| 12254 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12255 | PyDoc_STRVAR(replace__doc__, |
Ezio Melotti | c1897e7 | 2010-06-26 18:50:39 +0000 | [diff] [blame] | 12256 | "S.replace(old, new[, count]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12257 | \n\ |
| 12258 | Return a copy of S with all occurrences of substring\n\ |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 12259 | old replaced by new. If the optional argument count is\n\ |
| 12260 | given, only the first count occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12261 | |
| 12262 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12263 | unicode_replace(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12264 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12265 | PyObject *str1; |
| 12266 | PyObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12267 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12268 | PyObject *result; |
| 12269 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12270 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12271 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12272 | if (PyUnicode_READY(self) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12273 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12274 | str1 = PyUnicode_FromObject(str1); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12275 | if (str1 == NULL) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12276 | return NULL; |
| 12277 | str2 = PyUnicode_FromObject(str2); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12278 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12279 | Py_DECREF(str1); |
| 12280 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 12281 | } |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12282 | if (PyUnicode_READY(str1) == -1 || PyUnicode_READY(str2) == -1) |
| 12283 | result = NULL; |
| 12284 | else |
| 12285 | result = replace(self, str1, str2, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12286 | |
| 12287 | Py_DECREF(str1); |
| 12288 | Py_DECREF(str2); |
| 12289 | return result; |
| 12290 | } |
| 12291 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12292 | static PyObject * |
| 12293 | unicode_repr(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12294 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12295 | PyObject *repr; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12296 | Py_ssize_t isize; |
| 12297 | Py_ssize_t osize, squote, dquote, i, o; |
| 12298 | Py_UCS4 max, quote; |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12299 | int ikind, okind, unchanged; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12300 | void *idata, *odata; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12301 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12302 | if (PyUnicode_READY(unicode) == -1) |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12303 | return NULL; |
| 12304 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12305 | isize = PyUnicode_GET_LENGTH(unicode); |
| 12306 | idata = PyUnicode_DATA(unicode); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12307 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12308 | /* Compute length of output, quote characters, and |
| 12309 | maximum character */ |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12310 | osize = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12311 | max = 127; |
| 12312 | squote = dquote = 0; |
| 12313 | ikind = PyUnicode_KIND(unicode); |
| 12314 | for (i = 0; i < isize; i++) { |
| 12315 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
| 12316 | switch (ch) { |
| 12317 | case '\'': squote++; osize++; break; |
| 12318 | case '"': dquote++; osize++; break; |
| 12319 | case '\\': case '\t': case '\r': case '\n': |
| 12320 | osize += 2; break; |
| 12321 | default: |
| 12322 | /* Fast-path ASCII */ |
| 12323 | if (ch < ' ' || ch == 0x7f) |
| 12324 | osize += 4; /* \xHH */ |
| 12325 | else if (ch < 0x7f) |
| 12326 | osize++; |
| 12327 | else if (Py_UNICODE_ISPRINTABLE(ch)) { |
| 12328 | osize++; |
| 12329 | max = ch > max ? ch : max; |
| 12330 | } |
| 12331 | else if (ch < 0x100) |
| 12332 | osize += 4; /* \xHH */ |
| 12333 | else if (ch < 0x10000) |
| 12334 | osize += 6; /* \uHHHH */ |
| 12335 | else |
| 12336 | osize += 10; /* \uHHHHHHHH */ |
| 12337 | } |
| 12338 | } |
| 12339 | |
| 12340 | quote = '\''; |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12341 | unchanged = (osize == isize); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12342 | if (squote) { |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12343 | unchanged = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12344 | if (dquote) |
| 12345 | /* Both squote and dquote present. Use squote, |
| 12346 | and escape them */ |
| 12347 | osize += squote; |
| 12348 | else |
| 12349 | quote = '"'; |
| 12350 | } |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12351 | osize += 2; /* quotes */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12352 | |
| 12353 | repr = PyUnicode_New(osize, max); |
| 12354 | if (repr == NULL) |
| 12355 | return NULL; |
| 12356 | okind = PyUnicode_KIND(repr); |
| 12357 | odata = PyUnicode_DATA(repr); |
| 12358 | |
| 12359 | PyUnicode_WRITE(okind, odata, 0, quote); |
| 12360 | PyUnicode_WRITE(okind, odata, osize-1, quote); |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12361 | if (unchanged) { |
| 12362 | _PyUnicode_FastCopyCharacters(repr, 1, |
| 12363 | unicode, 0, |
| 12364 | isize); |
| 12365 | } |
| 12366 | else { |
| 12367 | for (i = 0, o = 1; i < isize; i++) { |
| 12368 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12369 | |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12370 | /* Escape quotes and backslashes */ |
| 12371 | if ((ch == quote) || (ch == '\\')) { |
Kristján Valur Jónsson | 55e5dc8 | 2012-06-06 21:58:08 +0000 | [diff] [blame] | 12372 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12373 | PyUnicode_WRITE(okind, odata, o++, ch); |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12374 | continue; |
| 12375 | } |
| 12376 | |
| 12377 | /* Map special whitespace to '\t', \n', '\r' */ |
| 12378 | if (ch == '\t') { |
| 12379 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12380 | PyUnicode_WRITE(okind, odata, o++, 't'); |
| 12381 | } |
| 12382 | else if (ch == '\n') { |
| 12383 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12384 | PyUnicode_WRITE(okind, odata, o++, 'n'); |
| 12385 | } |
| 12386 | else if (ch == '\r') { |
| 12387 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12388 | PyUnicode_WRITE(okind, odata, o++, 'r'); |
| 12389 | } |
| 12390 | |
| 12391 | /* Map non-printable US ASCII to '\xhh' */ |
| 12392 | else if (ch < ' ' || ch == 0x7F) { |
| 12393 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12394 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
| 12395 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0x000F]); |
| 12396 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0x000F]); |
| 12397 | } |
| 12398 | |
| 12399 | /* Copy ASCII characters as-is */ |
| 12400 | else if (ch < 0x7F) { |
| 12401 | PyUnicode_WRITE(okind, odata, o++, ch); |
| 12402 | } |
| 12403 | |
| 12404 | /* Non-ASCII characters */ |
| 12405 | else { |
| 12406 | /* Map Unicode whitespace and control characters |
| 12407 | (categories Z* and C* except ASCII space) |
| 12408 | */ |
| 12409 | if (!Py_UNICODE_ISPRINTABLE(ch)) { |
| 12410 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12411 | /* Map 8-bit characters to '\xhh' */ |
| 12412 | if (ch <= 0xff) { |
| 12413 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
| 12414 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0x000F]); |
| 12415 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0x000F]); |
| 12416 | } |
| 12417 | /* Map 16-bit characters to '\uxxxx' */ |
| 12418 | else if (ch <= 0xffff) { |
| 12419 | PyUnicode_WRITE(okind, odata, o++, 'u'); |
| 12420 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 12) & 0xF]); |
| 12421 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 8) & 0xF]); |
| 12422 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0xF]); |
| 12423 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0xF]); |
| 12424 | } |
| 12425 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 12426 | else { |
| 12427 | PyUnicode_WRITE(okind, odata, o++, 'U'); |
| 12428 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 28) & 0xF]); |
| 12429 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 24) & 0xF]); |
| 12430 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 20) & 0xF]); |
| 12431 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 16) & 0xF]); |
| 12432 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 12) & 0xF]); |
| 12433 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 8) & 0xF]); |
| 12434 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0xF]); |
| 12435 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0xF]); |
| 12436 | } |
| 12437 | } |
| 12438 | /* Copy characters as-is */ |
| 12439 | else { |
| 12440 | PyUnicode_WRITE(okind, odata, o++, ch); |
| 12441 | } |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12442 | } |
| 12443 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12444 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12445 | /* Closing quote already added at the beginning */ |
Victor Stinner | 05d1189 | 2011-10-06 01:13:58 +0200 | [diff] [blame] | 12446 | assert(_PyUnicode_CheckConsistency(repr, 1)); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12447 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12448 | } |
| 12449 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12450 | PyDoc_STRVAR(rfind__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12451 | "S.rfind(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12452 | \n\ |
| 12453 | Return the highest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 12454 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12455 | arguments start and end are interpreted as in slice notation.\n\ |
| 12456 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12457 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12458 | |
| 12459 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12460 | unicode_rfind(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12461 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12462 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 12463 | Py_ssize_t start; |
| 12464 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12465 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12466 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12467 | if (!stringlib_parse_args_finds_unicode("rfind", args, &substring, |
| 12468 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12469 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12470 | |
Christian Heimes | ea71a52 | 2013-06-29 21:17:34 +0200 | [diff] [blame] | 12471 | if (PyUnicode_READY(self) == -1) { |
| 12472 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12473 | return NULL; |
Christian Heimes | ea71a52 | 2013-06-29 21:17:34 +0200 | [diff] [blame] | 12474 | } |
| 12475 | if (PyUnicode_READY(substring) == -1) { |
| 12476 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12477 | return NULL; |
Christian Heimes | ea71a52 | 2013-06-29 21:17:34 +0200 | [diff] [blame] | 12478 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12479 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12480 | result = any_find_slice(-1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12481 | |
| 12482 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12483 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12484 | if (result == -2) |
| 12485 | return NULL; |
| 12486 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12487 | return PyLong_FromSsize_t(result); |
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(rindex__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12491 | "S.rindex(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12492 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12493 | 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] | 12494 | |
| 12495 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12496 | unicode_rindex(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12497 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12498 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 12499 | Py_ssize_t start; |
| 12500 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12501 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12502 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12503 | if (!stringlib_parse_args_finds_unicode("rindex", args, &substring, |
| 12504 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12505 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12506 | |
Christian Heimes | ea71a52 | 2013-06-29 21:17:34 +0200 | [diff] [blame] | 12507 | if (PyUnicode_READY(self) == -1) { |
| 12508 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12509 | return NULL; |
Christian Heimes | ea71a52 | 2013-06-29 21:17:34 +0200 | [diff] [blame] | 12510 | } |
| 12511 | if (PyUnicode_READY(substring) == -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 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12515 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12516 | result = any_find_slice(-1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12517 | |
| 12518 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12519 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12520 | if (result == -2) |
| 12521 | return NULL; |
| 12522 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12523 | if (result < 0) { |
| 12524 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 12525 | return NULL; |
| 12526 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12527 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12528 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12529 | } |
| 12530 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12531 | PyDoc_STRVAR(rjust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12532 | "S.rjust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12533 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 12534 | 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] | 12535 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12536 | |
| 12537 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12538 | unicode_rjust(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12539 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12540 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12541 | Py_UCS4 fillchar = ' '; |
| 12542 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 12543 | 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] | 12544 | return NULL; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 12545 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 12546 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12547 | return NULL; |
| 12548 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12549 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 12550 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12551 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12552 | return pad(self, width - PyUnicode_GET_LENGTH(self), 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12553 | } |
| 12554 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12555 | PyObject * |
| 12556 | PyUnicode_Split(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12557 | { |
| 12558 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 12559 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12560 | s = PyUnicode_FromObject(s); |
| 12561 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12562 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12563 | if (sep != NULL) { |
| 12564 | sep = PyUnicode_FromObject(sep); |
| 12565 | if (sep == NULL) { |
| 12566 | Py_DECREF(s); |
| 12567 | return NULL; |
| 12568 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12569 | } |
| 12570 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12571 | result = split(s, sep, maxsplit); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12572 | |
| 12573 | Py_DECREF(s); |
| 12574 | Py_XDECREF(sep); |
| 12575 | return result; |
| 12576 | } |
| 12577 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12578 | PyDoc_STRVAR(split__doc__, |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12579 | "S.split(sep=None, maxsplit=-1) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12580 | \n\ |
| 12581 | Return a list of the words in S, using sep as the\n\ |
| 12582 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 12583 | 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] | 12584 | whitespace string is a separator and empty strings are\n\ |
| 12585 | removed from the result."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12586 | |
| 12587 | static PyObject* |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12588 | unicode_split(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12589 | { |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12590 | static char *kwlist[] = {"sep", "maxsplit", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12591 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12592 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12593 | |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12594 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|On:split", |
| 12595 | kwlist, &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12596 | return NULL; |
| 12597 | |
| 12598 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12599 | return split(self, NULL, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12600 | else if (PyUnicode_Check(substring)) |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12601 | return split(self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12602 | else |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12603 | return PyUnicode_Split(self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12604 | } |
| 12605 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12606 | PyObject * |
| 12607 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 12608 | { |
| 12609 | PyObject* str_obj; |
| 12610 | PyObject* sep_obj; |
| 12611 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12612 | int kind1, kind2, kind; |
| 12613 | void *buf1 = NULL, *buf2 = NULL; |
| 12614 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12615 | |
| 12616 | str_obj = PyUnicode_FromObject(str_in); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12617 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12618 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12619 | sep_obj = PyUnicode_FromObject(sep_in); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12620 | if (!sep_obj) { |
| 12621 | Py_DECREF(str_obj); |
| 12622 | return NULL; |
| 12623 | } |
| 12624 | if (PyUnicode_READY(sep_obj) == -1 || PyUnicode_READY(str_obj) == -1) { |
| 12625 | Py_DECREF(sep_obj); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12626 | Py_DECREF(str_obj); |
| 12627 | return NULL; |
| 12628 | } |
| 12629 | |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12630 | kind1 = PyUnicode_KIND(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12631 | kind2 = PyUnicode_KIND(sep_obj); |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12632 | kind = Py_MAX(kind1, kind2); |
| 12633 | buf1 = PyUnicode_DATA(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12634 | if (kind1 != kind) |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12635 | buf1 = _PyUnicode_AsKind(str_obj, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12636 | if (!buf1) |
| 12637 | goto onError; |
| 12638 | buf2 = PyUnicode_DATA(sep_obj); |
| 12639 | if (kind2 != kind) |
| 12640 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 12641 | if (!buf2) |
| 12642 | goto onError; |
| 12643 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 12644 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 12645 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 12646 | switch (PyUnicode_KIND(str_obj)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12647 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 12648 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) |
| 12649 | out = asciilib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12650 | else |
| 12651 | 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] | 12652 | break; |
| 12653 | case PyUnicode_2BYTE_KIND: |
| 12654 | out = ucs2lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12655 | break; |
| 12656 | case PyUnicode_4BYTE_KIND: |
| 12657 | out = ucs4lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12658 | break; |
| 12659 | default: |
| 12660 | assert(0); |
| 12661 | out = 0; |
| 12662 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12663 | |
| 12664 | Py_DECREF(sep_obj); |
| 12665 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12666 | if (kind1 != kind) |
| 12667 | PyMem_Free(buf1); |
| 12668 | if (kind2 != kind) |
| 12669 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12670 | |
| 12671 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12672 | onError: |
| 12673 | Py_DECREF(sep_obj); |
| 12674 | Py_DECREF(str_obj); |
| 12675 | if (kind1 != kind && buf1) |
| 12676 | PyMem_Free(buf1); |
| 12677 | if (kind2 != kind && buf2) |
| 12678 | PyMem_Free(buf2); |
| 12679 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12680 | } |
| 12681 | |
| 12682 | |
| 12683 | PyObject * |
| 12684 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 12685 | { |
| 12686 | PyObject* str_obj; |
| 12687 | PyObject* sep_obj; |
| 12688 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12689 | int kind1, kind2, kind; |
| 12690 | void *buf1 = NULL, *buf2 = NULL; |
| 12691 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12692 | |
| 12693 | str_obj = PyUnicode_FromObject(str_in); |
| 12694 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12695 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12696 | sep_obj = PyUnicode_FromObject(sep_in); |
| 12697 | if (!sep_obj) { |
| 12698 | Py_DECREF(str_obj); |
| 12699 | return NULL; |
| 12700 | } |
| 12701 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12702 | kind1 = PyUnicode_KIND(str_in); |
| 12703 | kind2 = PyUnicode_KIND(sep_obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 12704 | kind = Py_MAX(kind1, kind2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12705 | buf1 = PyUnicode_DATA(str_in); |
| 12706 | if (kind1 != kind) |
| 12707 | buf1 = _PyUnicode_AsKind(str_in, kind); |
| 12708 | if (!buf1) |
| 12709 | goto onError; |
| 12710 | buf2 = PyUnicode_DATA(sep_obj); |
| 12711 | if (kind2 != kind) |
| 12712 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 12713 | if (!buf2) |
| 12714 | goto onError; |
| 12715 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 12716 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 12717 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 12718 | switch (PyUnicode_KIND(str_in)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12719 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 12720 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) |
| 12721 | out = asciilib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12722 | else |
| 12723 | 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] | 12724 | break; |
| 12725 | case PyUnicode_2BYTE_KIND: |
| 12726 | out = ucs2lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12727 | break; |
| 12728 | case PyUnicode_4BYTE_KIND: |
| 12729 | out = ucs4lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12730 | break; |
| 12731 | default: |
| 12732 | assert(0); |
| 12733 | out = 0; |
| 12734 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12735 | |
| 12736 | Py_DECREF(sep_obj); |
| 12737 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12738 | if (kind1 != kind) |
| 12739 | PyMem_Free(buf1); |
| 12740 | if (kind2 != kind) |
| 12741 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12742 | |
| 12743 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12744 | onError: |
| 12745 | Py_DECREF(sep_obj); |
| 12746 | Py_DECREF(str_obj); |
| 12747 | if (kind1 != kind && buf1) |
| 12748 | PyMem_Free(buf1); |
| 12749 | if (kind2 != kind && buf2) |
| 12750 | PyMem_Free(buf2); |
| 12751 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12752 | } |
| 12753 | |
| 12754 | PyDoc_STRVAR(partition__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12755 | "S.partition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12756 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 12757 | 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] | 12758 | 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] | 12759 | found, return S and two empty strings."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12760 | |
| 12761 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12762 | unicode_partition(PyObject *self, PyObject *separator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12763 | { |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12764 | return PyUnicode_Partition(self, separator); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12765 | } |
| 12766 | |
| 12767 | PyDoc_STRVAR(rpartition__doc__, |
Ezio Melotti | 5b2b242 | 2010-01-25 11:58:28 +0000 | [diff] [blame] | 12768 | "S.rpartition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12769 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 12770 | 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] | 12771 | 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] | 12772 | separator is not found, return two empty strings and S."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12773 | |
| 12774 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12775 | unicode_rpartition(PyObject *self, PyObject *separator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12776 | { |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12777 | return PyUnicode_RPartition(self, separator); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12778 | } |
| 12779 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12780 | PyObject * |
| 12781 | PyUnicode_RSplit(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12782 | { |
| 12783 | PyObject *result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12784 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12785 | s = PyUnicode_FromObject(s); |
| 12786 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12787 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12788 | if (sep != NULL) { |
| 12789 | sep = PyUnicode_FromObject(sep); |
| 12790 | if (sep == NULL) { |
| 12791 | Py_DECREF(s); |
| 12792 | return NULL; |
| 12793 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12794 | } |
| 12795 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12796 | result = rsplit(s, sep, maxsplit); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12797 | |
| 12798 | Py_DECREF(s); |
| 12799 | Py_XDECREF(sep); |
| 12800 | return result; |
| 12801 | } |
| 12802 | |
| 12803 | PyDoc_STRVAR(rsplit__doc__, |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12804 | "S.rsplit(sep=None, maxsplit=-1) -> list of strings\n\ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12805 | \n\ |
| 12806 | Return a list of the words in S, using sep as the\n\ |
| 12807 | delimiter string, starting at the end of the string and\n\ |
| 12808 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 12809 | splits are done. If sep is not specified, any whitespace string\n\ |
| 12810 | is a separator."); |
| 12811 | |
| 12812 | static PyObject* |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12813 | unicode_rsplit(PyObject *self, PyObject *args, PyObject *kwds) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12814 | { |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12815 | static char *kwlist[] = {"sep", "maxsplit", 0}; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12816 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12817 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12818 | |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12819 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|On:rsplit", |
| 12820 | kwlist, &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12821 | return NULL; |
| 12822 | |
| 12823 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12824 | return rsplit(self, NULL, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12825 | else if (PyUnicode_Check(substring)) |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12826 | return rsplit(self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12827 | else |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12828 | return PyUnicode_RSplit(self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12829 | } |
| 12830 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12831 | PyDoc_STRVAR(splitlines__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12832 | "S.splitlines([keepends]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12833 | \n\ |
| 12834 | 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] | 12835 | 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] | 12836 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12837 | |
| 12838 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12839 | unicode_splitlines(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12840 | { |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12841 | static char *kwlist[] = {"keepends", 0}; |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 12842 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12843 | |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12844 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:splitlines", |
| 12845 | kwlist, &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12846 | return NULL; |
| 12847 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12848 | return PyUnicode_Splitlines(self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12849 | } |
| 12850 | |
| 12851 | static |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 12852 | PyObject *unicode_str(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12853 | { |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12854 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12855 | } |
| 12856 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12857 | PyDoc_STRVAR(swapcase__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12858 | "S.swapcase() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12859 | \n\ |
| 12860 | 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] | 12861 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12862 | |
| 12863 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12864 | unicode_swapcase(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12865 | { |
Benjamin Peterson | eea4846 | 2012-01-16 14:28:50 -0500 | [diff] [blame] | 12866 | if (PyUnicode_READY(self) == -1) |
| 12867 | return NULL; |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 12868 | return case_operation(self, do_swapcase); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12869 | } |
| 12870 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 12871 | /*[clinic input] |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12872 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 12873 | @staticmethod |
| 12874 | str.maketrans as unicode_maketrans |
| 12875 | |
| 12876 | x: object |
| 12877 | |
| 12878 | y: unicode=NULL |
| 12879 | |
| 12880 | z: unicode=NULL |
| 12881 | |
| 12882 | / |
| 12883 | |
| 12884 | Return a translation table usable for str.translate(). |
| 12885 | |
| 12886 | If there is only one argument, it must be a dictionary mapping Unicode |
| 12887 | ordinals (integers) or characters to Unicode ordinals, strings or None. |
| 12888 | Character keys will be then converted to ordinals. |
| 12889 | If there are two arguments, they must be strings of equal length, and |
| 12890 | in the resulting dictionary, each character in x will be mapped to the |
| 12891 | character at the same position in y. If there is a third argument, it |
| 12892 | 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] | 12893 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 12894 | |
| 12895 | PyDoc_STRVAR(unicode_maketrans__doc__, |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame^] | 12896 | "maketrans(x, y=None, z=None, /)\n" |
| 12897 | "--\n" |
| 12898 | "\n" |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 12899 | "Return a translation table usable for str.translate().\n" |
| 12900 | "\n" |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 12901 | "If there is only one argument, it must be a dictionary mapping Unicode\n" |
| 12902 | "ordinals (integers) or characters to Unicode ordinals, strings or None.\n" |
| 12903 | "Character keys will be then converted to ordinals.\n" |
| 12904 | "If there are two arguments, they must be strings of equal length, and\n" |
| 12905 | "in the resulting dictionary, each character in x will be mapped to the\n" |
| 12906 | "character at the same position in y. If there is a third argument, it\n" |
| 12907 | "must be a string, whose characters will be mapped to None in the result."); |
| 12908 | |
| 12909 | #define UNICODE_MAKETRANS_METHODDEF \ |
| 12910 | {"maketrans", (PyCFunction)unicode_maketrans, METH_VARARGS|METH_STATIC, unicode_maketrans__doc__}, |
| 12911 | |
| 12912 | static PyObject * |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 12913 | unicode_maketrans_impl(PyObject *x, PyObject *y, PyObject *z); |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 12914 | |
| 12915 | static PyObject * |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 12916 | unicode_maketrans(void *null, PyObject *args) |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12917 | { |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 12918 | PyObject *return_value = NULL; |
| 12919 | PyObject *x; |
| 12920 | PyObject *y = NULL; |
| 12921 | PyObject *z = NULL; |
| 12922 | |
| 12923 | if (!PyArg_ParseTuple(args, |
| 12924 | "O|UU:maketrans", |
| 12925 | &x, &y, &z)) |
| 12926 | goto exit; |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 12927 | return_value = unicode_maketrans_impl(x, y, z); |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 12928 | |
| 12929 | exit: |
| 12930 | return return_value; |
| 12931 | } |
| 12932 | |
| 12933 | static PyObject * |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 12934 | unicode_maketrans_impl(PyObject *x, PyObject *y, PyObject *z) |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame^] | 12935 | /*[clinic end generated code: output=566edf630f77436a input=7bfbf529a293c6c5]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 12936 | { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12937 | PyObject *new = NULL, *key, *value; |
| 12938 | Py_ssize_t i = 0; |
| 12939 | int res; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12940 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12941 | new = PyDict_New(); |
| 12942 | if (!new) |
| 12943 | return NULL; |
| 12944 | if (y != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12945 | int x_kind, y_kind, z_kind; |
| 12946 | void *x_data, *y_data, *z_data; |
| 12947 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12948 | /* x must be a string too, of equal length */ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12949 | if (!PyUnicode_Check(x)) { |
| 12950 | PyErr_SetString(PyExc_TypeError, "first maketrans argument must " |
| 12951 | "be a string if there is a second argument"); |
| 12952 | goto err; |
| 12953 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12954 | if (PyUnicode_GET_LENGTH(x) != PyUnicode_GET_LENGTH(y)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12955 | PyErr_SetString(PyExc_ValueError, "the first two maketrans " |
| 12956 | "arguments must have equal length"); |
| 12957 | goto err; |
| 12958 | } |
| 12959 | /* 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] | 12960 | x_kind = PyUnicode_KIND(x); |
| 12961 | y_kind = PyUnicode_KIND(y); |
| 12962 | x_data = PyUnicode_DATA(x); |
| 12963 | y_data = PyUnicode_DATA(y); |
| 12964 | for (i = 0; i < PyUnicode_GET_LENGTH(x); i++) { |
| 12965 | key = PyLong_FromLong(PyUnicode_READ(x_kind, x_data, i)); |
Benjamin Peterson | 53aa1d7 | 2011-12-20 13:29:45 -0600 | [diff] [blame] | 12966 | if (!key) |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12967 | goto err; |
Benjamin Peterson | 822c790 | 2011-12-20 13:32:50 -0600 | [diff] [blame] | 12968 | value = PyLong_FromLong(PyUnicode_READ(y_kind, y_data, i)); |
Benjamin Peterson | 53aa1d7 | 2011-12-20 13:29:45 -0600 | [diff] [blame] | 12969 | if (!value) { |
| 12970 | Py_DECREF(key); |
| 12971 | goto err; |
| 12972 | } |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12973 | res = PyDict_SetItem(new, key, value); |
| 12974 | Py_DECREF(key); |
| 12975 | Py_DECREF(value); |
| 12976 | if (res < 0) |
| 12977 | goto err; |
| 12978 | } |
| 12979 | /* create entries for deleting chars in z */ |
| 12980 | if (z != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12981 | z_kind = PyUnicode_KIND(z); |
| 12982 | z_data = PyUnicode_DATA(z); |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 12983 | for (i = 0; i < PyUnicode_GET_LENGTH(z); i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12984 | key = PyLong_FromLong(PyUnicode_READ(z_kind, z_data, i)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12985 | if (!key) |
| 12986 | goto err; |
| 12987 | res = PyDict_SetItem(new, key, Py_None); |
| 12988 | Py_DECREF(key); |
| 12989 | if (res < 0) |
| 12990 | goto err; |
| 12991 | } |
| 12992 | } |
| 12993 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12994 | int kind; |
| 12995 | void *data; |
| 12996 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12997 | /* x must be a dict */ |
Raymond Hettinger | 3ad0576 | 2009-05-29 22:11:22 +0000 | [diff] [blame] | 12998 | if (!PyDict_CheckExact(x)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12999 | PyErr_SetString(PyExc_TypeError, "if you give only one argument " |
| 13000 | "to maketrans it must be a dict"); |
| 13001 | goto err; |
| 13002 | } |
| 13003 | /* copy entries into the new dict, converting string keys to int keys */ |
| 13004 | while (PyDict_Next(x, &i, &key, &value)) { |
| 13005 | if (PyUnicode_Check(key)) { |
| 13006 | /* convert string keys to integer keys */ |
| 13007 | PyObject *newkey; |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 13008 | if (PyUnicode_GET_LENGTH(key) != 1) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 13009 | PyErr_SetString(PyExc_ValueError, "string keys in translate " |
| 13010 | "table must be of length 1"); |
| 13011 | goto err; |
| 13012 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13013 | kind = PyUnicode_KIND(key); |
| 13014 | data = PyUnicode_DATA(key); |
| 13015 | newkey = PyLong_FromLong(PyUnicode_READ(kind, data, 0)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 13016 | if (!newkey) |
| 13017 | goto err; |
| 13018 | res = PyDict_SetItem(new, newkey, value); |
| 13019 | Py_DECREF(newkey); |
| 13020 | if (res < 0) |
| 13021 | goto err; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 13022 | } else if (PyLong_Check(key)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 13023 | /* just keep integer keys */ |
| 13024 | if (PyDict_SetItem(new, key, value) < 0) |
| 13025 | goto err; |
| 13026 | } else { |
| 13027 | PyErr_SetString(PyExc_TypeError, "keys in translate table must " |
| 13028 | "be strings or integers"); |
| 13029 | goto err; |
| 13030 | } |
| 13031 | } |
| 13032 | } |
| 13033 | return new; |
| 13034 | err: |
| 13035 | Py_DECREF(new); |
| 13036 | return NULL; |
| 13037 | } |
| 13038 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13039 | PyDoc_STRVAR(translate__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13040 | "S.translate(table) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13041 | \n\ |
| 13042 | Return a copy of the string S, where all characters have been mapped\n\ |
| 13043 | through the given translation table, which must be a mapping of\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 13044 | Unicode ordinals to Unicode ordinals, strings, or None.\n\ |
Walter Dörwald | 5c1ee17 | 2002-09-04 20:31:32 +0000 | [diff] [blame] | 13045 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 13046 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13047 | |
| 13048 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13049 | unicode_translate(PyObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13050 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13051 | return _PyUnicode_TranslateCharmap(self, table, "ignore"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13052 | } |
| 13053 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13054 | PyDoc_STRVAR(upper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13055 | "S.upper() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13056 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13057 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13058 | |
| 13059 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 13060 | unicode_upper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13061 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 13062 | if (PyUnicode_READY(self) == -1) |
| 13063 | return NULL; |
| 13064 | if (PyUnicode_IS_ASCII(self)) |
| 13065 | return ascii_upper_or_lower(self, 0); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 13066 | return case_operation(self, do_upper); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13067 | } |
| 13068 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13069 | PyDoc_STRVAR(zfill__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13070 | "S.zfill(width) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13071 | \n\ |
Benjamin Peterson | 9aa4299 | 2008-09-10 21:57:34 +0000 | [diff] [blame] | 13072 | Pad a numeric string S with zeros on the left, to fill a field\n\ |
| 13073 | of the specified width. The string S is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13074 | |
| 13075 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 13076 | unicode_zfill(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13077 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13078 | Py_ssize_t fill; |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 13079 | PyObject *u; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13080 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13081 | int kind; |
| 13082 | void *data; |
| 13083 | Py_UCS4 chr; |
| 13084 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13085 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13086 | return NULL; |
| 13087 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 13088 | if (PyUnicode_READY(self) == -1) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 13089 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13090 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 13091 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 13092 | return unicode_result_unchanged(self); |
| 13093 | |
| 13094 | fill = width - PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13095 | |
| 13096 | u = pad(self, fill, 0, '0'); |
| 13097 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 13098 | if (u == NULL) |
| 13099 | return NULL; |
| 13100 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13101 | kind = PyUnicode_KIND(u); |
| 13102 | data = PyUnicode_DATA(u); |
| 13103 | chr = PyUnicode_READ(kind, data, fill); |
| 13104 | |
| 13105 | if (chr == '+' || chr == '-') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13106 | /* move sign to beginning of string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13107 | PyUnicode_WRITE(kind, data, 0, chr); |
| 13108 | PyUnicode_WRITE(kind, data, fill, '0'); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13109 | } |
| 13110 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13111 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 13112 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13113 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13114 | |
| 13115 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 13116 | static PyObject * |
| 13117 | unicode__decimal2ascii(PyObject *self) |
| 13118 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13119 | return PyUnicode_TransformDecimalAndSpaceToASCII(self); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 13120 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13121 | #endif |
| 13122 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13123 | PyDoc_STRVAR(startswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13124 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13125 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 13126 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 13127 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13128 | With optional end, stop comparing S at that position.\n\ |
| 13129 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13130 | |
| 13131 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13132 | unicode_startswith(PyObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13133 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13134 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13135 | PyObject *subobj; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13136 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13137 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 13138 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13139 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13140 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 13141 | if (!stringlib_parse_args_finds("startswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13142 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13143 | if (PyTuple_Check(subobj)) { |
| 13144 | Py_ssize_t i; |
| 13145 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13146 | substring = PyUnicode_FromObject(PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13147 | if (substring == NULL) |
| 13148 | return NULL; |
| 13149 | result = tailmatch(self, substring, start, end, -1); |
| 13150 | Py_DECREF(substring); |
Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame] | 13151 | if (result == -1) |
| 13152 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13153 | if (result) { |
| 13154 | Py_RETURN_TRUE; |
| 13155 | } |
| 13156 | } |
| 13157 | /* nothing matched */ |
| 13158 | Py_RETURN_FALSE; |
| 13159 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13160 | substring = PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 13161 | if (substring == NULL) { |
| 13162 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 13163 | PyErr_Format(PyExc_TypeError, "startswith first arg must be str or " |
| 13164 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13165 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 13166 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13167 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13168 | Py_DECREF(substring); |
Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame] | 13169 | if (result == -1) |
| 13170 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13171 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13172 | } |
| 13173 | |
| 13174 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13175 | PyDoc_STRVAR(endswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13176 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13177 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 13178 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 13179 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13180 | With optional end, stop comparing S at that position.\n\ |
| 13181 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13182 | |
| 13183 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13184 | unicode_endswith(PyObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13185 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13186 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13187 | PyObject *subobj; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13188 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13189 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 13190 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13191 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13192 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 13193 | if (!stringlib_parse_args_finds("endswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13194 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13195 | if (PyTuple_Check(subobj)) { |
| 13196 | Py_ssize_t i; |
| 13197 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13198 | substring = PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13199 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13200 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13201 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13202 | result = tailmatch(self, substring, start, end, +1); |
| 13203 | Py_DECREF(substring); |
Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame] | 13204 | if (result == -1) |
| 13205 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13206 | if (result) { |
| 13207 | Py_RETURN_TRUE; |
| 13208 | } |
| 13209 | } |
| 13210 | Py_RETURN_FALSE; |
| 13211 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13212 | substring = PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 13213 | if (substring == NULL) { |
| 13214 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 13215 | PyErr_Format(PyExc_TypeError, "endswith first arg must be str or " |
| 13216 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13217 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 13218 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13219 | result = tailmatch(self, substring, start, end, +1); |
Christian Heimes | 305e49e | 2013-06-29 20:41:06 +0200 | [diff] [blame] | 13220 | Py_DECREF(substring); |
Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame] | 13221 | if (result == -1) |
| 13222 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13223 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13224 | } |
| 13225 | |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13226 | Py_LOCAL_INLINE(void) |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 13227 | _PyUnicodeWriter_Update(_PyUnicodeWriter *writer) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13228 | { |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13229 | if (!writer->readonly) |
| 13230 | writer->size = PyUnicode_GET_LENGTH(writer->buffer); |
| 13231 | else { |
| 13232 | /* Copy-on-write mode: set buffer size to 0 so |
| 13233 | * _PyUnicodeWriter_Prepare() will copy (and enlarge) the buffer on |
| 13234 | * next write. */ |
| 13235 | writer->size = 0; |
| 13236 | } |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13237 | writer->maxchar = PyUnicode_MAX_CHAR_VALUE(writer->buffer); |
| 13238 | writer->data = PyUnicode_DATA(writer->buffer); |
| 13239 | writer->kind = PyUnicode_KIND(writer->buffer); |
| 13240 | } |
| 13241 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13242 | void |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13243 | _PyUnicodeWriter_Init(_PyUnicodeWriter *writer) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13244 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13245 | memset(writer, 0, sizeof(*writer)); |
| 13246 | #ifdef Py_DEBUG |
| 13247 | writer->kind = 5; /* invalid kind */ |
| 13248 | #endif |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13249 | writer->min_char = 127; |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13250 | } |
| 13251 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13252 | int |
| 13253 | _PyUnicodeWriter_PrepareInternal(_PyUnicodeWriter *writer, |
| 13254 | Py_ssize_t length, Py_UCS4 maxchar) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13255 | { |
Victor Stinner | 6989ba0 | 2013-11-18 21:08:39 +0100 | [diff] [blame] | 13256 | #ifdef MS_WINDOWS |
| 13257 | /* On Windows, overallocate by 50% is the best factor */ |
| 13258 | # define OVERALLOCATE_FACTOR 2 |
| 13259 | #else |
| 13260 | /* On Linux, overallocate by 25% is the best factor */ |
| 13261 | # define OVERALLOCATE_FACTOR 4 |
| 13262 | #endif |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13263 | Py_ssize_t newlen; |
| 13264 | PyObject *newbuffer; |
| 13265 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13266 | assert(length > 0); |
| 13267 | |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13268 | if (length > PY_SSIZE_T_MAX - writer->pos) { |
| 13269 | PyErr_NoMemory(); |
| 13270 | return -1; |
| 13271 | } |
| 13272 | newlen = writer->pos + length; |
| 13273 | |
Benjamin Peterson | 3164f5d | 2013-06-10 09:24:01 -0700 | [diff] [blame] | 13274 | maxchar = Py_MAX(maxchar, writer->min_char); |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13275 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13276 | if (writer->buffer == NULL) { |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13277 | assert(!writer->readonly); |
Victor Stinner | 6989ba0 | 2013-11-18 21:08:39 +0100 | [diff] [blame] | 13278 | if (writer->overallocate |
| 13279 | && newlen <= (PY_SSIZE_T_MAX - newlen / OVERALLOCATE_FACTOR)) { |
| 13280 | /* overallocate to limit the number of realloc() */ |
| 13281 | newlen += newlen / OVERALLOCATE_FACTOR; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13282 | } |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13283 | if (newlen < writer->min_length) |
| 13284 | newlen = writer->min_length; |
| 13285 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13286 | writer->buffer = PyUnicode_New(newlen, maxchar); |
| 13287 | if (writer->buffer == NULL) |
| 13288 | return -1; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13289 | } |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13290 | else if (newlen > writer->size) { |
Victor Stinner | 6989ba0 | 2013-11-18 21:08:39 +0100 | [diff] [blame] | 13291 | if (writer->overallocate |
| 13292 | && newlen <= (PY_SSIZE_T_MAX - newlen / OVERALLOCATE_FACTOR)) { |
| 13293 | /* overallocate to limit the number of realloc() */ |
| 13294 | newlen += newlen / OVERALLOCATE_FACTOR; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13295 | } |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13296 | if (newlen < writer->min_length) |
| 13297 | newlen = writer->min_length; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13298 | |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 13299 | if (maxchar > writer->maxchar || writer->readonly) { |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13300 | /* resize + widen */ |
| 13301 | newbuffer = PyUnicode_New(newlen, maxchar); |
| 13302 | if (newbuffer == NULL) |
| 13303 | return -1; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13304 | _PyUnicode_FastCopyCharacters(newbuffer, 0, |
| 13305 | writer->buffer, 0, writer->pos); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13306 | Py_DECREF(writer->buffer); |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 13307 | writer->readonly = 0; |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13308 | } |
| 13309 | else { |
| 13310 | newbuffer = resize_compact(writer->buffer, newlen); |
| 13311 | if (newbuffer == NULL) |
| 13312 | return -1; |
| 13313 | } |
| 13314 | writer->buffer = newbuffer; |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13315 | } |
| 13316 | else if (maxchar > writer->maxchar) { |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 13317 | assert(!writer->readonly); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13318 | newbuffer = PyUnicode_New(writer->size, maxchar); |
| 13319 | if (newbuffer == NULL) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13320 | return -1; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13321 | _PyUnicode_FastCopyCharacters(newbuffer, 0, |
| 13322 | writer->buffer, 0, writer->pos); |
| 13323 | Py_DECREF(writer->buffer); |
| 13324 | writer->buffer = newbuffer; |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13325 | } |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13326 | _PyUnicodeWriter_Update(writer); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13327 | return 0; |
Victor Stinner | 6989ba0 | 2013-11-18 21:08:39 +0100 | [diff] [blame] | 13328 | |
| 13329 | #undef OVERALLOCATE_FACTOR |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13330 | } |
| 13331 | |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 13332 | Py_LOCAL_INLINE(int) |
| 13333 | _PyUnicodeWriter_WriteCharInline(_PyUnicodeWriter *writer, Py_UCS4 ch) |
Victor Stinner | a0dd021 | 2013-04-11 22:09:04 +0200 | [diff] [blame] | 13334 | { |
| 13335 | if (_PyUnicodeWriter_Prepare(writer, 1, ch) < 0) |
| 13336 | return -1; |
| 13337 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, ch); |
| 13338 | writer->pos++; |
| 13339 | return 0; |
| 13340 | } |
| 13341 | |
| 13342 | int |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 13343 | _PyUnicodeWriter_WriteChar(_PyUnicodeWriter *writer, Py_UCS4 ch) |
| 13344 | { |
| 13345 | return _PyUnicodeWriter_WriteCharInline(writer, ch); |
| 13346 | } |
| 13347 | |
| 13348 | int |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13349 | _PyUnicodeWriter_WriteStr(_PyUnicodeWriter *writer, PyObject *str) |
| 13350 | { |
| 13351 | Py_UCS4 maxchar; |
| 13352 | Py_ssize_t len; |
| 13353 | |
| 13354 | if (PyUnicode_READY(str) == -1) |
| 13355 | return -1; |
| 13356 | len = PyUnicode_GET_LENGTH(str); |
| 13357 | if (len == 0) |
| 13358 | return 0; |
| 13359 | maxchar = PyUnicode_MAX_CHAR_VALUE(str); |
| 13360 | if (maxchar > writer->maxchar || len > writer->size - writer->pos) { |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 13361 | if (writer->buffer == NULL && !writer->overallocate) { |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13362 | writer->readonly = 1; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13363 | Py_INCREF(str); |
| 13364 | writer->buffer = str; |
| 13365 | _PyUnicodeWriter_Update(writer); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13366 | writer->pos += len; |
| 13367 | return 0; |
| 13368 | } |
| 13369 | if (_PyUnicodeWriter_PrepareInternal(writer, len, maxchar) == -1) |
| 13370 | return -1; |
| 13371 | } |
| 13372 | _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos, |
| 13373 | str, 0, len); |
| 13374 | writer->pos += len; |
| 13375 | return 0; |
| 13376 | } |
| 13377 | |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 13378 | int |
Victor Stinner | cfc4c13 | 2013-04-03 01:48:39 +0200 | [diff] [blame] | 13379 | _PyUnicodeWriter_WriteSubstring(_PyUnicodeWriter *writer, PyObject *str, |
| 13380 | Py_ssize_t start, Py_ssize_t end) |
| 13381 | { |
| 13382 | Py_UCS4 maxchar; |
| 13383 | Py_ssize_t len; |
| 13384 | |
| 13385 | if (PyUnicode_READY(str) == -1) |
| 13386 | return -1; |
| 13387 | |
| 13388 | assert(0 <= start); |
| 13389 | assert(end <= PyUnicode_GET_LENGTH(str)); |
| 13390 | assert(start <= end); |
| 13391 | |
| 13392 | if (end == 0) |
| 13393 | return 0; |
| 13394 | |
| 13395 | if (start == 0 && end == PyUnicode_GET_LENGTH(str)) |
| 13396 | return _PyUnicodeWriter_WriteStr(writer, str); |
| 13397 | |
| 13398 | if (PyUnicode_MAX_CHAR_VALUE(str) > writer->maxchar) |
| 13399 | maxchar = _PyUnicode_FindMaxChar(str, start, end); |
| 13400 | else |
| 13401 | maxchar = writer->maxchar; |
| 13402 | len = end - start; |
| 13403 | |
| 13404 | if (_PyUnicodeWriter_Prepare(writer, len, maxchar) < 0) |
| 13405 | return -1; |
| 13406 | |
| 13407 | _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos, |
| 13408 | str, start, len); |
| 13409 | writer->pos += len; |
| 13410 | return 0; |
| 13411 | } |
| 13412 | |
| 13413 | int |
Victor Stinner | 4a58707 | 2013-11-19 12:54:53 +0100 | [diff] [blame] | 13414 | _PyUnicodeWriter_WriteASCIIString(_PyUnicodeWriter *writer, |
| 13415 | const char *ascii, Py_ssize_t len) |
| 13416 | { |
| 13417 | if (len == -1) |
| 13418 | len = strlen(ascii); |
| 13419 | |
| 13420 | assert(ucs1lib_find_max_char((Py_UCS1*)ascii, (Py_UCS1*)ascii + len) < 128); |
| 13421 | |
| 13422 | if (writer->buffer == NULL && !writer->overallocate) { |
| 13423 | PyObject *str; |
| 13424 | |
| 13425 | str = _PyUnicode_FromASCII(ascii, len); |
| 13426 | if (str == NULL) |
| 13427 | return -1; |
| 13428 | |
| 13429 | writer->readonly = 1; |
| 13430 | writer->buffer = str; |
| 13431 | _PyUnicodeWriter_Update(writer); |
| 13432 | writer->pos += len; |
| 13433 | return 0; |
| 13434 | } |
| 13435 | |
| 13436 | if (_PyUnicodeWriter_Prepare(writer, len, 127) == -1) |
| 13437 | return -1; |
| 13438 | |
| 13439 | switch (writer->kind) |
| 13440 | { |
| 13441 | case PyUnicode_1BYTE_KIND: |
| 13442 | { |
| 13443 | const Py_UCS1 *str = (const Py_UCS1 *)ascii; |
| 13444 | Py_UCS1 *data = writer->data; |
| 13445 | |
| 13446 | Py_MEMCPY(data + writer->pos, str, len); |
| 13447 | break; |
| 13448 | } |
| 13449 | case PyUnicode_2BYTE_KIND: |
| 13450 | { |
| 13451 | _PyUnicode_CONVERT_BYTES( |
| 13452 | Py_UCS1, Py_UCS2, |
| 13453 | ascii, ascii + len, |
| 13454 | (Py_UCS2 *)writer->data + writer->pos); |
| 13455 | break; |
| 13456 | } |
| 13457 | case PyUnicode_4BYTE_KIND: |
| 13458 | { |
| 13459 | _PyUnicode_CONVERT_BYTES( |
| 13460 | Py_UCS1, Py_UCS4, |
| 13461 | ascii, ascii + len, |
| 13462 | (Py_UCS4 *)writer->data + writer->pos); |
| 13463 | break; |
| 13464 | } |
| 13465 | default: |
| 13466 | assert(0); |
| 13467 | } |
| 13468 | |
| 13469 | writer->pos += len; |
| 13470 | return 0; |
| 13471 | } |
| 13472 | |
| 13473 | int |
| 13474 | _PyUnicodeWriter_WriteLatin1String(_PyUnicodeWriter *writer, |
| 13475 | const char *str, Py_ssize_t len) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 13476 | { |
| 13477 | Py_UCS4 maxchar; |
| 13478 | |
| 13479 | maxchar = ucs1lib_find_max_char((Py_UCS1*)str, (Py_UCS1*)str + len); |
| 13480 | if (_PyUnicodeWriter_Prepare(writer, len, maxchar) == -1) |
| 13481 | return -1; |
| 13482 | unicode_write_cstr(writer->buffer, writer->pos, str, len); |
| 13483 | writer->pos += len; |
| 13484 | return 0; |
| 13485 | } |
| 13486 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13487 | PyObject * |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 13488 | _PyUnicodeWriter_Finish(_PyUnicodeWriter *writer) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13489 | { |
Victor Stinner | 15a0bd3 | 2013-07-08 22:29:55 +0200 | [diff] [blame] | 13490 | PyObject *str; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13491 | if (writer->pos == 0) { |
Victor Stinner | 9e6b4d7 | 2013-07-09 00:37:24 +0200 | [diff] [blame] | 13492 | Py_CLEAR(writer->buffer); |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 13493 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13494 | } |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 13495 | if (writer->readonly) { |
Victor Stinner | 9e6b4d7 | 2013-07-09 00:37:24 +0200 | [diff] [blame] | 13496 | str = writer->buffer; |
| 13497 | writer->buffer = NULL; |
| 13498 | assert(PyUnicode_GET_LENGTH(str) == writer->pos); |
| 13499 | return str; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13500 | } |
| 13501 | if (PyUnicode_GET_LENGTH(writer->buffer) != writer->pos) { |
| 13502 | PyObject *newbuffer; |
| 13503 | newbuffer = resize_compact(writer->buffer, writer->pos); |
| 13504 | if (newbuffer == NULL) { |
| 13505 | Py_DECREF(writer->buffer); |
Victor Stinner | 9e6b4d7 | 2013-07-09 00:37:24 +0200 | [diff] [blame] | 13506 | writer->buffer = NULL; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13507 | return NULL; |
| 13508 | } |
| 13509 | writer->buffer = newbuffer; |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13510 | } |
Victor Stinner | 15a0bd3 | 2013-07-08 22:29:55 +0200 | [diff] [blame] | 13511 | str = writer->buffer; |
| 13512 | writer->buffer = NULL; |
| 13513 | assert(_PyUnicode_CheckConsistency(str, 1)); |
| 13514 | return unicode_result_ready(str); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13515 | } |
| 13516 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13517 | void |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 13518 | _PyUnicodeWriter_Dealloc(_PyUnicodeWriter *writer) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13519 | { |
| 13520 | Py_CLEAR(writer->buffer); |
| 13521 | } |
| 13522 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13523 | #include "stringlib/unicode_format.h" |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13524 | |
| 13525 | PyDoc_STRVAR(format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13526 | "S.format(*args, **kwargs) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13527 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 13528 | Return a formatted version of S, using substitutions from args and kwargs.\n\ |
| 13529 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13530 | |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 13531 | PyDoc_STRVAR(format_map__doc__, |
| 13532 | "S.format_map(mapping) -> str\n\ |
| 13533 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 13534 | Return a formatted version of S, using substitutions from mapping.\n\ |
| 13535 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 13536 | |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13537 | static PyObject * |
| 13538 | unicode__format__(PyObject* self, PyObject* args) |
| 13539 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13540 | PyObject *format_spec; |
| 13541 | _PyUnicodeWriter writer; |
| 13542 | int ret; |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13543 | |
| 13544 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
| 13545 | return NULL; |
| 13546 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13547 | if (PyUnicode_READY(self) == -1) |
| 13548 | return NULL; |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13549 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13550 | ret = _PyUnicode_FormatAdvancedWriter(&writer, |
| 13551 | self, format_spec, 0, |
| 13552 | PyUnicode_GET_LENGTH(format_spec)); |
| 13553 | if (ret == -1) { |
| 13554 | _PyUnicodeWriter_Dealloc(&writer); |
| 13555 | return NULL; |
| 13556 | } |
| 13557 | return _PyUnicodeWriter_Finish(&writer); |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13558 | } |
| 13559 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13560 | PyDoc_STRVAR(p_format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13561 | "S.__format__(format_spec) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13562 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 13563 | Return a formatted version of S as described by format_spec."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13564 | |
| 13565 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13566 | unicode__sizeof__(PyObject *v) |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13567 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13568 | Py_ssize_t size; |
| 13569 | |
| 13570 | /* If it's a compact object, account for base structure + |
| 13571 | character data. */ |
| 13572 | if (PyUnicode_IS_COMPACT_ASCII(v)) |
| 13573 | size = sizeof(PyASCIIObject) + PyUnicode_GET_LENGTH(v) + 1; |
| 13574 | else if (PyUnicode_IS_COMPACT(v)) |
| 13575 | size = sizeof(PyCompactUnicodeObject) + |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 13576 | (PyUnicode_GET_LENGTH(v) + 1) * PyUnicode_KIND(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13577 | else { |
| 13578 | /* If it is a two-block object, account for base object, and |
| 13579 | for character block if present. */ |
| 13580 | size = sizeof(PyUnicodeObject); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 13581 | if (_PyUnicode_DATA_ANY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13582 | size += (PyUnicode_GET_LENGTH(v) + 1) * |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 13583 | PyUnicode_KIND(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13584 | } |
| 13585 | /* 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] | 13586 | with the data pointer. Check if the data is not shared. */ |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 13587 | if (_PyUnicode_HAS_WSTR_MEMORY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13588 | size += (PyUnicode_WSTR_LENGTH(v) + 1) * sizeof(wchar_t); |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 13589 | if (_PyUnicode_HAS_UTF8_MEMORY(v)) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 13590 | size += PyUnicode_UTF8_LENGTH(v) + 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13591 | |
| 13592 | return PyLong_FromSsize_t(size); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13593 | } |
| 13594 | |
| 13595 | PyDoc_STRVAR(sizeof__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13596 | "S.__sizeof__() -> size of S in memory, in bytes"); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13597 | |
| 13598 | static PyObject * |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 13599 | unicode_getnewargs(PyObject *v) |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 13600 | { |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 13601 | PyObject *copy = _PyUnicode_Copy(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13602 | if (!copy) |
| 13603 | return NULL; |
| 13604 | return Py_BuildValue("(N)", copy); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 13605 | } |
| 13606 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13607 | static PyMethodDef unicode_methods[] = { |
Benjamin Peterson | 28a4dce | 2010-12-12 01:33:04 +0000 | [diff] [blame] | 13608 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS | METH_KEYWORDS, encode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13609 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 13610 | {"split", (PyCFunction) unicode_split, METH_VARARGS | METH_KEYWORDS, split__doc__}, |
| 13611 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS | METH_KEYWORDS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13612 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 13613 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 13614 | {"casefold", (PyCFunction) unicode_casefold, METH_NOARGS, casefold__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13615 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 13616 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 13617 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
Ezio Melotti | 745d54d | 2013-11-16 19:10:57 +0200 | [diff] [blame] | 13618 | {"expandtabs", (PyCFunction) unicode_expandtabs, |
| 13619 | METH_VARARGS | METH_KEYWORDS, expandtabs__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13620 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13621 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13622 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 13623 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 13624 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 13625 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13626 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 13627 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 13628 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 13629 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13630 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Ezio Melotti | 745d54d | 2013-11-16 19:10:57 +0200 | [diff] [blame] | 13631 | {"splitlines", (PyCFunction) unicode_splitlines, |
| 13632 | METH_VARARGS | METH_KEYWORDS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 13633 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13634 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 13635 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 13636 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 13637 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 13638 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 13639 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 13640 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 13641 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 13642 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 13643 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 13644 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 13645 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 13646 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 13647 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 13648 | {"isidentifier", (PyCFunction) unicode_isidentifier, METH_NOARGS, isidentifier__doc__}, |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 13649 | {"isprintable", (PyCFunction) unicode_isprintable, METH_NOARGS, isprintable__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13650 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Eric Smith | 9cd1e09 | 2007-08-31 18:39:38 +0000 | [diff] [blame] | 13651 | {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 13652 | {"format_map", (PyCFunction) do_string_format_map, METH_O, format_map__doc__}, |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13653 | {"__format__", (PyCFunction) unicode__format__, METH_VARARGS, p_format__doc__}, |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 13654 | UNICODE_MAKETRANS_METHODDEF |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13655 | {"__sizeof__", (PyCFunction) unicode__sizeof__, METH_NOARGS, sizeof__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 13656 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 13657 | /* These methods are just used for debugging the implementation. */ |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 13658 | {"_decimal2ascii", (PyCFunction) unicode__decimal2ascii, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13659 | #endif |
| 13660 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13661 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13662 | {NULL, NULL} |
| 13663 | }; |
| 13664 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13665 | static PyObject * |
| 13666 | unicode_mod(PyObject *v, PyObject *w) |
| 13667 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 13668 | if (!PyUnicode_Check(v)) |
| 13669 | Py_RETURN_NOTIMPLEMENTED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13670 | return PyUnicode_Format(v, w); |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13671 | } |
| 13672 | |
| 13673 | static PyNumberMethods unicode_as_number = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13674 | 0, /*nb_add*/ |
| 13675 | 0, /*nb_subtract*/ |
| 13676 | 0, /*nb_multiply*/ |
| 13677 | unicode_mod, /*nb_remainder*/ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13678 | }; |
| 13679 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13680 | static PySequenceMethods unicode_as_sequence = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13681 | (lenfunc) unicode_length, /* sq_length */ |
| 13682 | PyUnicode_Concat, /* sq_concat */ |
| 13683 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 13684 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
| 13685 | 0, /* sq_slice */ |
| 13686 | 0, /* sq_ass_item */ |
| 13687 | 0, /* sq_ass_slice */ |
| 13688 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13689 | }; |
| 13690 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13691 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13692 | unicode_subscript(PyObject* self, PyObject* item) |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13693 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13694 | if (PyUnicode_READY(self) == -1) |
| 13695 | return NULL; |
| 13696 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 13697 | if (PyIndex_Check(item)) { |
| 13698 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13699 | if (i == -1 && PyErr_Occurred()) |
| 13700 | return NULL; |
| 13701 | if (i < 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13702 | i += PyUnicode_GET_LENGTH(self); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13703 | return unicode_getitem(self, i); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13704 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13705 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13706 | PyObject *result; |
| 13707 | void *src_data, *dest_data; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13708 | int src_kind, dest_kind; |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 13709 | Py_UCS4 ch, max_char, kind_limit; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13710 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13711 | if (PySlice_GetIndicesEx(item, PyUnicode_GET_LENGTH(self), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13712 | &start, &stop, &step, &slicelength) < 0) { |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13713 | return NULL; |
| 13714 | } |
| 13715 | |
| 13716 | if (slicelength <= 0) { |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 13717 | _Py_RETURN_UNICODE_EMPTY(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13718 | } else if (start == 0 && step == 1 && |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 13719 | slicelength == PyUnicode_GET_LENGTH(self)) { |
| 13720 | return unicode_result_unchanged(self); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 13721 | } else if (step == 1) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13722 | return PyUnicode_Substring(self, |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 13723 | start, start + slicelength); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13724 | } |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13725 | /* General case */ |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13726 | src_kind = PyUnicode_KIND(self); |
| 13727 | src_data = PyUnicode_DATA(self); |
Victor Stinner | 55c9911 | 2011-10-13 01:17:06 +0200 | [diff] [blame] | 13728 | if (!PyUnicode_IS_ASCII(self)) { |
| 13729 | kind_limit = kind_maxchar_limit(src_kind); |
| 13730 | max_char = 0; |
| 13731 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 13732 | ch = PyUnicode_READ(src_kind, src_data, cur); |
| 13733 | if (ch > max_char) { |
| 13734 | max_char = ch; |
| 13735 | if (max_char >= kind_limit) |
| 13736 | break; |
| 13737 | } |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 13738 | } |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13739 | } |
Victor Stinner | 55c9911 | 2011-10-13 01:17:06 +0200 | [diff] [blame] | 13740 | else |
| 13741 | max_char = 127; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13742 | result = PyUnicode_New(slicelength, max_char); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13743 | if (result == NULL) |
| 13744 | return NULL; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13745 | dest_kind = PyUnicode_KIND(result); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13746 | dest_data = PyUnicode_DATA(result); |
| 13747 | |
| 13748 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13749 | Py_UCS4 ch = PyUnicode_READ(src_kind, src_data, cur); |
| 13750 | PyUnicode_WRITE(dest_kind, dest_data, i, ch); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13751 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13752 | assert(_PyUnicode_CheckConsistency(result, 1)); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13753 | return result; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13754 | } else { |
| 13755 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 13756 | return NULL; |
| 13757 | } |
| 13758 | } |
| 13759 | |
| 13760 | static PyMappingMethods unicode_as_mapping = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13761 | (lenfunc)unicode_length, /* mp_length */ |
| 13762 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 13763 | (objobjargproc)0, /* mp_ass_subscript */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13764 | }; |
| 13765 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13766 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13767 | /* Helpers for PyUnicode_Format() */ |
| 13768 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13769 | struct unicode_formatter_t { |
| 13770 | PyObject *args; |
| 13771 | int args_owned; |
| 13772 | Py_ssize_t arglen, argidx; |
| 13773 | PyObject *dict; |
| 13774 | |
| 13775 | enum PyUnicode_Kind fmtkind; |
| 13776 | Py_ssize_t fmtcnt, fmtpos; |
| 13777 | void *fmtdata; |
| 13778 | PyObject *fmtstr; |
| 13779 | |
| 13780 | _PyUnicodeWriter writer; |
| 13781 | }; |
| 13782 | |
| 13783 | struct unicode_format_arg_t { |
| 13784 | Py_UCS4 ch; |
| 13785 | int flags; |
| 13786 | Py_ssize_t width; |
| 13787 | int prec; |
| 13788 | int sign; |
| 13789 | }; |
| 13790 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13791 | static PyObject * |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13792 | unicode_format_getnextarg(struct unicode_formatter_t *ctx) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13793 | { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13794 | Py_ssize_t argidx = ctx->argidx; |
| 13795 | |
| 13796 | if (argidx < ctx->arglen) { |
| 13797 | ctx->argidx++; |
| 13798 | if (ctx->arglen < 0) |
| 13799 | return ctx->args; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13800 | else |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13801 | return PyTuple_GetItem(ctx->args, argidx); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13802 | } |
| 13803 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13804 | "not enough arguments for format string"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13805 | return NULL; |
| 13806 | } |
| 13807 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13808 | /* 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] | 13809 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13810 | /* Format a float into the writer if the writer is not NULL, or into *p_output |
| 13811 | otherwise. |
| 13812 | |
| 13813 | Return 0 on success, raise an exception and return -1 on error. */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13814 | static int |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13815 | formatfloat(PyObject *v, struct unicode_format_arg_t *arg, |
| 13816 | PyObject **p_output, |
| 13817 | _PyUnicodeWriter *writer) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13818 | { |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13819 | char *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13820 | double x; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13821 | Py_ssize_t len; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13822 | int prec; |
| 13823 | int dtoa_flags; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 13824 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13825 | x = PyFloat_AsDouble(v); |
| 13826 | if (x == -1.0 && PyErr_Occurred()) |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13827 | return -1; |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13828 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13829 | prec = arg->prec; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13830 | if (prec < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13831 | prec = 6; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13832 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13833 | if (arg->flags & F_ALT) |
| 13834 | dtoa_flags = Py_DTSF_ALT; |
| 13835 | else |
| 13836 | dtoa_flags = 0; |
| 13837 | p = PyOS_double_to_string(x, arg->ch, prec, dtoa_flags, NULL); |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13838 | if (p == NULL) |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13839 | return -1; |
| 13840 | len = strlen(p); |
| 13841 | if (writer) { |
Victor Stinner | 4a58707 | 2013-11-19 12:54:53 +0100 | [diff] [blame] | 13842 | if (_PyUnicodeWriter_WriteASCIIString(writer, p, len) < 0) { |
Christian Heimes | f4f9939 | 2012-09-10 11:48:41 +0200 | [diff] [blame] | 13843 | PyMem_Free(p); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13844 | return -1; |
Christian Heimes | f4f9939 | 2012-09-10 11:48:41 +0200 | [diff] [blame] | 13845 | } |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13846 | } |
| 13847 | else |
| 13848 | *p_output = _PyUnicode_FromASCII(p, len); |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13849 | PyMem_Free(p); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13850 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13851 | } |
| 13852 | |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13853 | /* formatlong() emulates the format codes d, u, o, x and X, and |
| 13854 | * the F_ALT flag, for Python's long (unbounded) ints. It's not used for |
| 13855 | * Python's regular ints. |
| 13856 | * Return value: a new PyUnicodeObject*, or NULL if error. |
| 13857 | * The output string is of the form |
| 13858 | * "-"? ("0x" | "0X")? digit+ |
| 13859 | * "0x"/"0X" are present only for x and X conversions, with F_ALT |
| 13860 | * set in flags. The case of hex digits will be correct, |
| 13861 | * There will be at least prec digits, zero-filled on the left if |
| 13862 | * necessary to get that many. |
| 13863 | * val object to be converted |
| 13864 | * flags bitmask of format flags; only F_ALT is looked at |
| 13865 | * prec minimum number of digits; 0-fill on left if needed |
| 13866 | * type a character in [duoxX]; u acts the same as d |
| 13867 | * |
| 13868 | * CAUTION: o, x and X conversions on regular ints can never |
| 13869 | * produce a '-' sign, but can for Python's unbounded ints. |
| 13870 | */ |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13871 | static PyObject* |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13872 | formatlong(PyObject *val, struct unicode_format_arg_t *arg) |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13873 | { |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13874 | PyObject *result = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13875 | char *buf; |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13876 | Py_ssize_t i; |
| 13877 | int sign; /* 1 if '-', else 0 */ |
| 13878 | int len; /* number of characters */ |
| 13879 | Py_ssize_t llen; |
| 13880 | int numdigits; /* len == numnondigits + numdigits */ |
| 13881 | int numnondigits = 0; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13882 | int prec = arg->prec; |
| 13883 | int type = arg->ch; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13884 | |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13885 | /* Avoid exceeding SSIZE_T_MAX */ |
| 13886 | if (prec > INT_MAX-3) { |
| 13887 | PyErr_SetString(PyExc_OverflowError, |
| 13888 | "precision too large"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13889 | return NULL; |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13890 | } |
| 13891 | |
| 13892 | assert(PyLong_Check(val)); |
| 13893 | |
| 13894 | switch (type) { |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13895 | default: |
| 13896 | assert(!"'type' not in [diuoxX]"); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13897 | case 'd': |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13898 | case 'i': |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13899 | case 'u': |
Ethan Furman | fb13721 | 2013-08-31 10:18:55 -0700 | [diff] [blame] | 13900 | /* int and int subclasses should print numerically when a numeric */ |
| 13901 | /* format code is used (see issue18780) */ |
| 13902 | result = PyNumber_ToBase(val, 10); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13903 | break; |
| 13904 | case 'o': |
| 13905 | numnondigits = 2; |
| 13906 | result = PyNumber_ToBase(val, 8); |
| 13907 | break; |
| 13908 | case 'x': |
| 13909 | case 'X': |
| 13910 | numnondigits = 2; |
| 13911 | result = PyNumber_ToBase(val, 16); |
| 13912 | break; |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13913 | } |
| 13914 | if (!result) |
| 13915 | return NULL; |
| 13916 | |
| 13917 | assert(unicode_modifiable(result)); |
| 13918 | assert(PyUnicode_IS_READY(result)); |
| 13919 | assert(PyUnicode_IS_ASCII(result)); |
| 13920 | |
| 13921 | /* To modify the string in-place, there can only be one reference. */ |
| 13922 | if (Py_REFCNT(result) != 1) { |
Christian Heimes | d47802e | 2013-06-29 21:33:36 +0200 | [diff] [blame] | 13923 | Py_DECREF(result); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13924 | PyErr_BadInternalCall(); |
| 13925 | return NULL; |
| 13926 | } |
| 13927 | buf = PyUnicode_DATA(result); |
| 13928 | llen = PyUnicode_GET_LENGTH(result); |
| 13929 | if (llen > INT_MAX) { |
Christian Heimes | d47802e | 2013-06-29 21:33:36 +0200 | [diff] [blame] | 13930 | Py_DECREF(result); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13931 | PyErr_SetString(PyExc_ValueError, |
| 13932 | "string too large in _PyBytes_FormatLong"); |
| 13933 | return NULL; |
| 13934 | } |
| 13935 | len = (int)llen; |
| 13936 | sign = buf[0] == '-'; |
| 13937 | numnondigits += sign; |
| 13938 | numdigits = len - numnondigits; |
| 13939 | assert(numdigits > 0); |
| 13940 | |
| 13941 | /* Get rid of base marker unless F_ALT */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13942 | if (((arg->flags & F_ALT) == 0 && |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13943 | (type == 'o' || type == 'x' || type == 'X'))) { |
| 13944 | assert(buf[sign] == '0'); |
| 13945 | assert(buf[sign+1] == 'x' || buf[sign+1] == 'X' || |
| 13946 | buf[sign+1] == 'o'); |
| 13947 | numnondigits -= 2; |
| 13948 | buf += 2; |
| 13949 | len -= 2; |
| 13950 | if (sign) |
| 13951 | buf[0] = '-'; |
| 13952 | assert(len == numnondigits + numdigits); |
| 13953 | assert(numdigits > 0); |
| 13954 | } |
| 13955 | |
| 13956 | /* Fill with leading zeroes to meet minimum width. */ |
| 13957 | if (prec > numdigits) { |
| 13958 | PyObject *r1 = PyBytes_FromStringAndSize(NULL, |
| 13959 | numnondigits + prec); |
| 13960 | char *b1; |
| 13961 | if (!r1) { |
| 13962 | Py_DECREF(result); |
| 13963 | return NULL; |
| 13964 | } |
| 13965 | b1 = PyBytes_AS_STRING(r1); |
| 13966 | for (i = 0; i < numnondigits; ++i) |
| 13967 | *b1++ = *buf++; |
| 13968 | for (i = 0; i < prec - numdigits; i++) |
| 13969 | *b1++ = '0'; |
| 13970 | for (i = 0; i < numdigits; i++) |
| 13971 | *b1++ = *buf++; |
| 13972 | *b1 = '\0'; |
| 13973 | Py_DECREF(result); |
| 13974 | result = r1; |
| 13975 | buf = PyBytes_AS_STRING(result); |
| 13976 | len = numnondigits + prec; |
| 13977 | } |
| 13978 | |
| 13979 | /* Fix up case for hex conversions. */ |
| 13980 | if (type == 'X') { |
| 13981 | /* Need to convert all lower case letters to upper case. |
| 13982 | and need to convert 0x to 0X (and -0x to -0X). */ |
| 13983 | for (i = 0; i < len; i++) |
| 13984 | if (buf[i] >= 'a' && buf[i] <= 'x') |
| 13985 | buf[i] -= 'a'-'A'; |
| 13986 | } |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13987 | if (!PyUnicode_Check(result) |
| 13988 | || buf != PyUnicode_DATA(result)) { |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13989 | PyObject *unicode; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13990 | unicode = _PyUnicode_FromASCII(buf, len); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13991 | Py_DECREF(result); |
| 13992 | result = unicode; |
| 13993 | } |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13994 | else if (len != PyUnicode_GET_LENGTH(result)) { |
| 13995 | if (PyUnicode_Resize(&result, len) < 0) |
| 13996 | Py_CLEAR(result); |
| 13997 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13998 | return result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13999 | } |
| 14000 | |
Ethan Furman | df3ed24 | 2014-01-05 06:50:30 -0800 | [diff] [blame] | 14001 | /* Format an integer or a float as an integer. |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14002 | * Return 1 if the number has been formatted into the writer, |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14003 | * 0 if the number has been formatted into *p_output |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14004 | * -1 and raise an exception on error */ |
| 14005 | static int |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14006 | mainformatlong(PyObject *v, |
| 14007 | struct unicode_format_arg_t *arg, |
| 14008 | PyObject **p_output, |
| 14009 | _PyUnicodeWriter *writer) |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14010 | { |
| 14011 | PyObject *iobj, *res; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14012 | char type = (char)arg->ch; |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14013 | |
| 14014 | if (!PyNumber_Check(v)) |
| 14015 | goto wrongtype; |
| 14016 | |
Ethan Furman | f9bba9c | 2014-01-11 23:20:58 -0800 | [diff] [blame] | 14017 | /* make sure number is a type of integer */ |
Ethan Furman | a70805e | 2014-01-12 08:42:35 -0800 | [diff] [blame] | 14018 | /* if not, issue deprecation warning for now */ |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14019 | if (!PyLong_Check(v)) { |
Ethan Furman | df3ed24 | 2014-01-05 06:50:30 -0800 | [diff] [blame] | 14020 | if (type == 'o' || type == 'x' || type == 'X') { |
| 14021 | iobj = PyNumber_Index(v); |
| 14022 | if (iobj == NULL) { |
Ethan Furman | f9bba9c | 2014-01-11 23:20:58 -0800 | [diff] [blame] | 14023 | PyErr_Clear(); |
| 14024 | if (PyErr_WarnEx(PyExc_DeprecationWarning, |
| 14025 | "automatic int conversions have been deprecated", |
| 14026 | 1)) { |
| 14027 | return -1; |
| 14028 | } |
| 14029 | iobj = PyNumber_Long(v); |
| 14030 | if (iobj == NULL ) { |
| 14031 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 14032 | goto wrongtype; |
| 14033 | return -1; |
| 14034 | } |
Ethan Furman | df3ed24 | 2014-01-05 06:50:30 -0800 | [diff] [blame] | 14035 | } |
| 14036 | } |
| 14037 | else { |
| 14038 | iobj = PyNumber_Long(v); |
| 14039 | if (iobj == NULL ) { |
| 14040 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 14041 | goto wrongtype; |
| 14042 | return -1; |
| 14043 | } |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14044 | } |
| 14045 | assert(PyLong_Check(iobj)); |
| 14046 | } |
| 14047 | else { |
| 14048 | iobj = v; |
| 14049 | Py_INCREF(iobj); |
| 14050 | } |
| 14051 | |
| 14052 | if (PyLong_CheckExact(v) |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14053 | && arg->width == -1 && arg->prec == -1 |
| 14054 | && !(arg->flags & (F_SIGN | F_BLANK)) |
| 14055 | && type != 'X') |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14056 | { |
| 14057 | /* Fast path */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14058 | int alternate = arg->flags & F_ALT; |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14059 | int base; |
| 14060 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14061 | switch(type) |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14062 | { |
| 14063 | default: |
| 14064 | assert(0 && "'type' not in [diuoxX]"); |
| 14065 | case 'd': |
| 14066 | case 'i': |
| 14067 | case 'u': |
| 14068 | base = 10; |
| 14069 | break; |
| 14070 | case 'o': |
| 14071 | base = 8; |
| 14072 | break; |
| 14073 | case 'x': |
| 14074 | case 'X': |
| 14075 | base = 16; |
| 14076 | break; |
| 14077 | } |
| 14078 | |
Victor Stinner | c89d28f | 2012-10-02 12:54:07 +0200 | [diff] [blame] | 14079 | if (_PyLong_FormatWriter(writer, v, base, alternate) == -1) { |
| 14080 | Py_DECREF(iobj); |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14081 | return -1; |
Victor Stinner | c89d28f | 2012-10-02 12:54:07 +0200 | [diff] [blame] | 14082 | } |
| 14083 | Py_DECREF(iobj); |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14084 | return 1; |
| 14085 | } |
| 14086 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14087 | res = formatlong(iobj, arg); |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14088 | Py_DECREF(iobj); |
| 14089 | if (res == NULL) |
| 14090 | return -1; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14091 | *p_output = res; |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14092 | return 0; |
| 14093 | |
| 14094 | wrongtype: |
| 14095 | PyErr_Format(PyExc_TypeError, |
| 14096 | "%%%c format: a number is required, " |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14097 | "not %.200s", |
| 14098 | type, Py_TYPE(v)->tp_name); |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14099 | return -1; |
| 14100 | } |
| 14101 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14102 | static Py_UCS4 |
| 14103 | formatchar(PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14104 | { |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 14105 | /* presume that the buffer is at least 3 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 14106 | if (PyUnicode_Check(v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14107 | if (PyUnicode_GET_LENGTH(v) == 1) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14108 | return PyUnicode_READ_CHAR(v, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14109 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14110 | goto onError; |
| 14111 | } |
| 14112 | else { |
Ethan Furman | df3ed24 | 2014-01-05 06:50:30 -0800 | [diff] [blame] | 14113 | PyObject *iobj; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14114 | long x; |
Ethan Furman | df3ed24 | 2014-01-05 06:50:30 -0800 | [diff] [blame] | 14115 | /* make sure number is a type of integer */ |
Ethan Furman | a70805e | 2014-01-12 08:42:35 -0800 | [diff] [blame] | 14116 | /* if not, issue deprecation warning for now */ |
Ethan Furman | df3ed24 | 2014-01-05 06:50:30 -0800 | [diff] [blame] | 14117 | if (!PyLong_Check(v)) { |
| 14118 | iobj = PyNumber_Index(v); |
| 14119 | if (iobj == NULL) { |
Ethan Furman | f9bba9c | 2014-01-11 23:20:58 -0800 | [diff] [blame] | 14120 | PyErr_Clear(); |
| 14121 | if (PyErr_WarnEx(PyExc_DeprecationWarning, |
| 14122 | "automatic int conversions have been deprecated", |
| 14123 | 1)) { |
| 14124 | return -1; |
| 14125 | } |
| 14126 | iobj = PyNumber_Long(v); |
| 14127 | if (iobj == NULL ) { |
| 14128 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 14129 | goto onError; |
| 14130 | return -1; |
| 14131 | } |
Ethan Furman | df3ed24 | 2014-01-05 06:50:30 -0800 | [diff] [blame] | 14132 | } |
| 14133 | v = iobj; |
| 14134 | Py_DECREF(iobj); |
| 14135 | } |
| 14136 | /* Integer input truncated to a character */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14137 | x = PyLong_AsLong(v); |
| 14138 | if (x == -1 && PyErr_Occurred()) |
| 14139 | goto onError; |
| 14140 | |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 14141 | if (x < 0 || x > MAX_UNICODE) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14142 | PyErr_SetString(PyExc_OverflowError, |
| 14143 | "%c arg not in range(0x110000)"); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14144 | return (Py_UCS4) -1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14145 | } |
| 14146 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14147 | return (Py_UCS4) x; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14148 | } |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 14149 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14150 | onError: |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 14151 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14152 | "%c requires int or char"); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14153 | return (Py_UCS4) -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14154 | } |
| 14155 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14156 | /* Parse options of an argument: flags, width, precision. |
| 14157 | Handle also "%(name)" syntax. |
| 14158 | |
| 14159 | Return 0 if the argument has been formatted into arg->str. |
| 14160 | Return 1 if the argument has been written into ctx->writer, |
| 14161 | Raise an exception and return -1 on error. */ |
| 14162 | static int |
| 14163 | unicode_format_arg_parse(struct unicode_formatter_t *ctx, |
| 14164 | struct unicode_format_arg_t *arg) |
| 14165 | { |
| 14166 | #define FORMAT_READ(ctx) \ |
| 14167 | PyUnicode_READ((ctx)->fmtkind, (ctx)->fmtdata, (ctx)->fmtpos) |
| 14168 | |
| 14169 | PyObject *v; |
| 14170 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14171 | if (arg->ch == '(') { |
| 14172 | /* Get argument value from a dictionary. Example: "%(name)s". */ |
| 14173 | Py_ssize_t keystart; |
| 14174 | Py_ssize_t keylen; |
| 14175 | PyObject *key; |
| 14176 | int pcount = 1; |
| 14177 | |
| 14178 | if (ctx->dict == NULL) { |
| 14179 | PyErr_SetString(PyExc_TypeError, |
| 14180 | "format requires a mapping"); |
| 14181 | return -1; |
| 14182 | } |
| 14183 | ++ctx->fmtpos; |
| 14184 | --ctx->fmtcnt; |
| 14185 | keystart = ctx->fmtpos; |
| 14186 | /* Skip over balanced parentheses */ |
| 14187 | while (pcount > 0 && --ctx->fmtcnt >= 0) { |
| 14188 | arg->ch = FORMAT_READ(ctx); |
| 14189 | if (arg->ch == ')') |
| 14190 | --pcount; |
| 14191 | else if (arg->ch == '(') |
| 14192 | ++pcount; |
| 14193 | ctx->fmtpos++; |
| 14194 | } |
| 14195 | keylen = ctx->fmtpos - keystart - 1; |
| 14196 | if (ctx->fmtcnt < 0 || pcount > 0) { |
| 14197 | PyErr_SetString(PyExc_ValueError, |
| 14198 | "incomplete format key"); |
| 14199 | return -1; |
| 14200 | } |
| 14201 | key = PyUnicode_Substring(ctx->fmtstr, |
| 14202 | keystart, keystart + keylen); |
| 14203 | if (key == NULL) |
| 14204 | return -1; |
| 14205 | if (ctx->args_owned) { |
| 14206 | Py_DECREF(ctx->args); |
| 14207 | ctx->args_owned = 0; |
| 14208 | } |
| 14209 | ctx->args = PyObject_GetItem(ctx->dict, key); |
| 14210 | Py_DECREF(key); |
| 14211 | if (ctx->args == NULL) |
| 14212 | return -1; |
| 14213 | ctx->args_owned = 1; |
| 14214 | ctx->arglen = -1; |
| 14215 | ctx->argidx = -2; |
| 14216 | } |
| 14217 | |
| 14218 | /* Parse flags. Example: "%+i" => flags=F_SIGN. */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14219 | while (--ctx->fmtcnt >= 0) { |
| 14220 | arg->ch = FORMAT_READ(ctx); |
| 14221 | ctx->fmtpos++; |
| 14222 | switch (arg->ch) { |
| 14223 | case '-': arg->flags |= F_LJUST; continue; |
| 14224 | case '+': arg->flags |= F_SIGN; continue; |
| 14225 | case ' ': arg->flags |= F_BLANK; continue; |
| 14226 | case '#': arg->flags |= F_ALT; continue; |
| 14227 | case '0': arg->flags |= F_ZERO; continue; |
| 14228 | } |
| 14229 | break; |
| 14230 | } |
| 14231 | |
| 14232 | /* Parse width. Example: "%10s" => width=10 */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14233 | if (arg->ch == '*') { |
| 14234 | v = unicode_format_getnextarg(ctx); |
| 14235 | if (v == NULL) |
| 14236 | return -1; |
| 14237 | if (!PyLong_Check(v)) { |
| 14238 | PyErr_SetString(PyExc_TypeError, |
| 14239 | "* wants int"); |
| 14240 | return -1; |
| 14241 | } |
Serhiy Storchaka | 7898043 | 2013-01-15 01:12:17 +0200 | [diff] [blame] | 14242 | arg->width = PyLong_AsSsize_t(v); |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14243 | if (arg->width == -1 && PyErr_Occurred()) |
| 14244 | return -1; |
| 14245 | if (arg->width < 0) { |
| 14246 | arg->flags |= F_LJUST; |
| 14247 | arg->width = -arg->width; |
| 14248 | } |
| 14249 | if (--ctx->fmtcnt >= 0) { |
| 14250 | arg->ch = FORMAT_READ(ctx); |
| 14251 | ctx->fmtpos++; |
| 14252 | } |
| 14253 | } |
| 14254 | else if (arg->ch >= '0' && arg->ch <= '9') { |
| 14255 | arg->width = arg->ch - '0'; |
| 14256 | while (--ctx->fmtcnt >= 0) { |
| 14257 | arg->ch = FORMAT_READ(ctx); |
| 14258 | ctx->fmtpos++; |
| 14259 | if (arg->ch < '0' || arg->ch > '9') |
| 14260 | break; |
| 14261 | /* Since arg->ch is unsigned, the RHS would end up as unsigned, |
| 14262 | mixing signed and unsigned comparison. Since arg->ch is between |
| 14263 | '0' and '9', casting to int is safe. */ |
| 14264 | if (arg->width > (PY_SSIZE_T_MAX - ((int)arg->ch - '0')) / 10) { |
| 14265 | PyErr_SetString(PyExc_ValueError, |
| 14266 | "width too big"); |
| 14267 | return -1; |
| 14268 | } |
| 14269 | arg->width = arg->width*10 + (arg->ch - '0'); |
| 14270 | } |
| 14271 | } |
| 14272 | |
| 14273 | /* Parse precision. Example: "%.3f" => prec=3 */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14274 | if (arg->ch == '.') { |
| 14275 | arg->prec = 0; |
| 14276 | if (--ctx->fmtcnt >= 0) { |
| 14277 | arg->ch = FORMAT_READ(ctx); |
| 14278 | ctx->fmtpos++; |
| 14279 | } |
| 14280 | if (arg->ch == '*') { |
| 14281 | v = unicode_format_getnextarg(ctx); |
| 14282 | if (v == NULL) |
| 14283 | return -1; |
| 14284 | if (!PyLong_Check(v)) { |
| 14285 | PyErr_SetString(PyExc_TypeError, |
| 14286 | "* wants int"); |
| 14287 | return -1; |
| 14288 | } |
Serhiy Storchaka | 7898043 | 2013-01-15 01:12:17 +0200 | [diff] [blame] | 14289 | arg->prec = _PyLong_AsInt(v); |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14290 | if (arg->prec == -1 && PyErr_Occurred()) |
| 14291 | return -1; |
| 14292 | if (arg->prec < 0) |
| 14293 | arg->prec = 0; |
| 14294 | if (--ctx->fmtcnt >= 0) { |
| 14295 | arg->ch = FORMAT_READ(ctx); |
| 14296 | ctx->fmtpos++; |
| 14297 | } |
| 14298 | } |
| 14299 | else if (arg->ch >= '0' && arg->ch <= '9') { |
| 14300 | arg->prec = arg->ch - '0'; |
| 14301 | while (--ctx->fmtcnt >= 0) { |
| 14302 | arg->ch = FORMAT_READ(ctx); |
| 14303 | ctx->fmtpos++; |
| 14304 | if (arg->ch < '0' || arg->ch > '9') |
| 14305 | break; |
| 14306 | if (arg->prec > (INT_MAX - ((int)arg->ch - '0')) / 10) { |
| 14307 | PyErr_SetString(PyExc_ValueError, |
Victor Stinner | 3921e90 | 2012-10-06 23:05:00 +0200 | [diff] [blame] | 14308 | "precision too big"); |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14309 | return -1; |
| 14310 | } |
| 14311 | arg->prec = arg->prec*10 + (arg->ch - '0'); |
| 14312 | } |
| 14313 | } |
| 14314 | } |
| 14315 | |
| 14316 | /* Ignore "h", "l" and "L" format prefix (ex: "%hi" or "%ls") */ |
| 14317 | if (ctx->fmtcnt >= 0) { |
| 14318 | if (arg->ch == 'h' || arg->ch == 'l' || arg->ch == 'L') { |
| 14319 | if (--ctx->fmtcnt >= 0) { |
| 14320 | arg->ch = FORMAT_READ(ctx); |
| 14321 | ctx->fmtpos++; |
| 14322 | } |
| 14323 | } |
| 14324 | } |
| 14325 | if (ctx->fmtcnt < 0) { |
| 14326 | PyErr_SetString(PyExc_ValueError, |
| 14327 | "incomplete format"); |
| 14328 | return -1; |
| 14329 | } |
| 14330 | return 0; |
| 14331 | |
| 14332 | #undef FORMAT_READ |
| 14333 | } |
| 14334 | |
| 14335 | /* Format one argument. Supported conversion specifiers: |
| 14336 | |
| 14337 | - "s", "r", "a": any type |
Ethan Furman | df3ed24 | 2014-01-05 06:50:30 -0800 | [diff] [blame] | 14338 | - "i", "d", "u": int or float |
| 14339 | - "o", "x", "X": int |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14340 | - "e", "E", "f", "F", "g", "G": float |
| 14341 | - "c": int or str (1 character) |
| 14342 | |
Victor Stinner | 8dbd421 | 2012-12-04 09:30:24 +0100 | [diff] [blame] | 14343 | When possible, the output is written directly into the Unicode writer |
| 14344 | (ctx->writer). A string is created when padding is required. |
| 14345 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14346 | Return 0 if the argument has been formatted into *p_str, |
| 14347 | 1 if the argument has been written into ctx->writer, |
Victor Stinner | 8dbd421 | 2012-12-04 09:30:24 +0100 | [diff] [blame] | 14348 | -1 on error. */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14349 | static int |
| 14350 | unicode_format_arg_format(struct unicode_formatter_t *ctx, |
| 14351 | struct unicode_format_arg_t *arg, |
| 14352 | PyObject **p_str) |
| 14353 | { |
| 14354 | PyObject *v; |
| 14355 | _PyUnicodeWriter *writer = &ctx->writer; |
| 14356 | |
| 14357 | if (ctx->fmtcnt == 0) |
| 14358 | ctx->writer.overallocate = 0; |
| 14359 | |
| 14360 | if (arg->ch == '%') { |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 14361 | if (_PyUnicodeWriter_WriteCharInline(writer, '%') < 0) |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14362 | return -1; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14363 | return 1; |
| 14364 | } |
| 14365 | |
| 14366 | v = unicode_format_getnextarg(ctx); |
| 14367 | if (v == NULL) |
| 14368 | return -1; |
| 14369 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14370 | |
| 14371 | switch (arg->ch) { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14372 | case 's': |
| 14373 | case 'r': |
| 14374 | case 'a': |
| 14375 | if (PyLong_CheckExact(v) && arg->width == -1 && arg->prec == -1) { |
| 14376 | /* Fast path */ |
| 14377 | if (_PyLong_FormatWriter(writer, v, 10, arg->flags & F_ALT) == -1) |
| 14378 | return -1; |
| 14379 | return 1; |
| 14380 | } |
| 14381 | |
| 14382 | if (PyUnicode_CheckExact(v) && arg->ch == 's') { |
| 14383 | *p_str = v; |
| 14384 | Py_INCREF(*p_str); |
| 14385 | } |
| 14386 | else { |
| 14387 | if (arg->ch == 's') |
| 14388 | *p_str = PyObject_Str(v); |
| 14389 | else if (arg->ch == 'r') |
| 14390 | *p_str = PyObject_Repr(v); |
| 14391 | else |
| 14392 | *p_str = PyObject_ASCII(v); |
| 14393 | } |
| 14394 | break; |
| 14395 | |
| 14396 | case 'i': |
| 14397 | case 'd': |
| 14398 | case 'u': |
| 14399 | case 'o': |
| 14400 | case 'x': |
| 14401 | case 'X': |
| 14402 | { |
| 14403 | int ret = mainformatlong(v, arg, p_str, writer); |
| 14404 | if (ret != 0) |
| 14405 | return ret; |
| 14406 | arg->sign = 1; |
| 14407 | break; |
| 14408 | } |
| 14409 | |
| 14410 | case 'e': |
| 14411 | case 'E': |
| 14412 | case 'f': |
| 14413 | case 'F': |
| 14414 | case 'g': |
| 14415 | case 'G': |
| 14416 | if (arg->width == -1 && arg->prec == -1 |
| 14417 | && !(arg->flags & (F_SIGN | F_BLANK))) |
| 14418 | { |
| 14419 | /* Fast path */ |
| 14420 | if (formatfloat(v, arg, NULL, writer) == -1) |
| 14421 | return -1; |
| 14422 | return 1; |
| 14423 | } |
| 14424 | |
| 14425 | arg->sign = 1; |
| 14426 | if (formatfloat(v, arg, p_str, NULL) == -1) |
| 14427 | return -1; |
| 14428 | break; |
| 14429 | |
| 14430 | case 'c': |
| 14431 | { |
| 14432 | Py_UCS4 ch = formatchar(v); |
| 14433 | if (ch == (Py_UCS4) -1) |
| 14434 | return -1; |
| 14435 | if (arg->width == -1 && arg->prec == -1) { |
| 14436 | /* Fast path */ |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 14437 | if (_PyUnicodeWriter_WriteCharInline(writer, ch) < 0) |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14438 | return -1; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14439 | return 1; |
| 14440 | } |
| 14441 | *p_str = PyUnicode_FromOrdinal(ch); |
| 14442 | break; |
| 14443 | } |
| 14444 | |
| 14445 | default: |
| 14446 | PyErr_Format(PyExc_ValueError, |
| 14447 | "unsupported format character '%c' (0x%x) " |
| 14448 | "at index %zd", |
| 14449 | (31<=arg->ch && arg->ch<=126) ? (char)arg->ch : '?', |
| 14450 | (int)arg->ch, |
| 14451 | ctx->fmtpos - 1); |
| 14452 | return -1; |
| 14453 | } |
| 14454 | if (*p_str == NULL) |
| 14455 | return -1; |
| 14456 | assert (PyUnicode_Check(*p_str)); |
| 14457 | return 0; |
| 14458 | } |
| 14459 | |
| 14460 | static int |
| 14461 | unicode_format_arg_output(struct unicode_formatter_t *ctx, |
| 14462 | struct unicode_format_arg_t *arg, |
| 14463 | PyObject *str) |
| 14464 | { |
| 14465 | Py_ssize_t len; |
| 14466 | enum PyUnicode_Kind kind; |
| 14467 | void *pbuf; |
| 14468 | Py_ssize_t pindex; |
| 14469 | Py_UCS4 signchar; |
| 14470 | Py_ssize_t buflen; |
Victor Stinner | eb4b5ac | 2013-04-03 02:02:33 +0200 | [diff] [blame] | 14471 | Py_UCS4 maxchar; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14472 | Py_ssize_t sublen; |
| 14473 | _PyUnicodeWriter *writer = &ctx->writer; |
| 14474 | Py_UCS4 fill; |
| 14475 | |
| 14476 | fill = ' '; |
| 14477 | if (arg->sign && arg->flags & F_ZERO) |
| 14478 | fill = '0'; |
| 14479 | |
| 14480 | if (PyUnicode_READY(str) == -1) |
| 14481 | return -1; |
| 14482 | |
| 14483 | len = PyUnicode_GET_LENGTH(str); |
| 14484 | if ((arg->width == -1 || arg->width <= len) |
| 14485 | && (arg->prec == -1 || arg->prec >= len) |
| 14486 | && !(arg->flags & (F_SIGN | F_BLANK))) |
| 14487 | { |
| 14488 | /* Fast path */ |
| 14489 | if (_PyUnicodeWriter_WriteStr(writer, str) == -1) |
| 14490 | return -1; |
| 14491 | return 0; |
| 14492 | } |
| 14493 | |
| 14494 | /* Truncate the string for "s", "r" and "a" formats |
| 14495 | if the precision is set */ |
| 14496 | if (arg->ch == 's' || arg->ch == 'r' || arg->ch == 'a') { |
| 14497 | if (arg->prec >= 0 && len > arg->prec) |
| 14498 | len = arg->prec; |
| 14499 | } |
| 14500 | |
| 14501 | /* Adjust sign and width */ |
| 14502 | kind = PyUnicode_KIND(str); |
| 14503 | pbuf = PyUnicode_DATA(str); |
| 14504 | pindex = 0; |
| 14505 | signchar = '\0'; |
| 14506 | if (arg->sign) { |
| 14507 | Py_UCS4 ch = PyUnicode_READ(kind, pbuf, pindex); |
| 14508 | if (ch == '-' || ch == '+') { |
| 14509 | signchar = ch; |
| 14510 | len--; |
| 14511 | pindex++; |
| 14512 | } |
| 14513 | else if (arg->flags & F_SIGN) |
| 14514 | signchar = '+'; |
| 14515 | else if (arg->flags & F_BLANK) |
| 14516 | signchar = ' '; |
| 14517 | else |
| 14518 | arg->sign = 0; |
| 14519 | } |
| 14520 | if (arg->width < len) |
| 14521 | arg->width = len; |
| 14522 | |
| 14523 | /* Prepare the writer */ |
Victor Stinner | eb4b5ac | 2013-04-03 02:02:33 +0200 | [diff] [blame] | 14524 | maxchar = writer->maxchar; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14525 | if (!(arg->flags & F_LJUST)) { |
| 14526 | if (arg->sign) { |
| 14527 | if ((arg->width-1) > len) |
Benjamin Peterson | 3164f5d | 2013-06-10 09:24:01 -0700 | [diff] [blame] | 14528 | maxchar = Py_MAX(maxchar, fill); |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14529 | } |
| 14530 | else { |
| 14531 | if (arg->width > len) |
Benjamin Peterson | 3164f5d | 2013-06-10 09:24:01 -0700 | [diff] [blame] | 14532 | maxchar = Py_MAX(maxchar, fill); |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14533 | } |
| 14534 | } |
Victor Stinner | eb4b5ac | 2013-04-03 02:02:33 +0200 | [diff] [blame] | 14535 | if (PyUnicode_MAX_CHAR_VALUE(str) > maxchar) { |
| 14536 | Py_UCS4 strmaxchar = _PyUnicode_FindMaxChar(str, 0, pindex+len); |
Benjamin Peterson | 3164f5d | 2013-06-10 09:24:01 -0700 | [diff] [blame] | 14537 | maxchar = Py_MAX(maxchar, strmaxchar); |
Victor Stinner | eb4b5ac | 2013-04-03 02:02:33 +0200 | [diff] [blame] | 14538 | } |
| 14539 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14540 | buflen = arg->width; |
| 14541 | if (arg->sign && len == arg->width) |
| 14542 | buflen++; |
Victor Stinner | eb4b5ac | 2013-04-03 02:02:33 +0200 | [diff] [blame] | 14543 | if (_PyUnicodeWriter_Prepare(writer, buflen, maxchar) == -1) |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14544 | return -1; |
| 14545 | |
| 14546 | /* Write the sign if needed */ |
| 14547 | if (arg->sign) { |
| 14548 | if (fill != ' ') { |
| 14549 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, signchar); |
| 14550 | writer->pos += 1; |
| 14551 | } |
| 14552 | if (arg->width > len) |
| 14553 | arg->width--; |
| 14554 | } |
| 14555 | |
| 14556 | /* Write the numeric prefix for "x", "X" and "o" formats |
| 14557 | if the alternate form is used. |
| 14558 | For example, write "0x" for the "%#x" format. */ |
| 14559 | if ((arg->flags & F_ALT) && (arg->ch == 'x' || arg->ch == 'X' || arg->ch == 'o')) { |
| 14560 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
| 14561 | assert(PyUnicode_READ(kind, pbuf, pindex + 1) == arg->ch); |
| 14562 | if (fill != ' ') { |
| 14563 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, '0'); |
| 14564 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos+1, arg->ch); |
| 14565 | writer->pos += 2; |
| 14566 | pindex += 2; |
| 14567 | } |
| 14568 | arg->width -= 2; |
| 14569 | if (arg->width < 0) |
| 14570 | arg->width = 0; |
| 14571 | len -= 2; |
| 14572 | } |
| 14573 | |
| 14574 | /* Pad left with the fill character if needed */ |
| 14575 | if (arg->width > len && !(arg->flags & F_LJUST)) { |
| 14576 | sublen = arg->width - len; |
| 14577 | FILL(writer->kind, writer->data, fill, writer->pos, sublen); |
| 14578 | writer->pos += sublen; |
| 14579 | arg->width = len; |
| 14580 | } |
| 14581 | |
| 14582 | /* If padding with spaces: write sign if needed and/or numeric prefix if |
| 14583 | the alternate form is used */ |
| 14584 | if (fill == ' ') { |
| 14585 | if (arg->sign) { |
| 14586 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, signchar); |
| 14587 | writer->pos += 1; |
| 14588 | } |
| 14589 | if ((arg->flags & F_ALT) && (arg->ch == 'x' || arg->ch == 'X' || arg->ch == 'o')) { |
| 14590 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
| 14591 | assert(PyUnicode_READ(kind, pbuf, pindex+1) == arg->ch); |
| 14592 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, '0'); |
| 14593 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos+1, arg->ch); |
| 14594 | writer->pos += 2; |
| 14595 | pindex += 2; |
| 14596 | } |
| 14597 | } |
| 14598 | |
| 14599 | /* Write characters */ |
| 14600 | if (len) { |
| 14601 | _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos, |
| 14602 | str, pindex, len); |
| 14603 | writer->pos += len; |
| 14604 | } |
| 14605 | |
| 14606 | /* Pad right with the fill character if needed */ |
| 14607 | if (arg->width > len) { |
| 14608 | sublen = arg->width - len; |
| 14609 | FILL(writer->kind, writer->data, ' ', writer->pos, sublen); |
| 14610 | writer->pos += sublen; |
| 14611 | } |
| 14612 | return 0; |
| 14613 | } |
| 14614 | |
| 14615 | /* Helper of PyUnicode_Format(): format one arg. |
| 14616 | Return 0 on success, raise an exception and return -1 on error. */ |
| 14617 | static int |
| 14618 | unicode_format_arg(struct unicode_formatter_t *ctx) |
| 14619 | { |
| 14620 | struct unicode_format_arg_t arg; |
| 14621 | PyObject *str; |
| 14622 | int ret; |
| 14623 | |
Victor Stinner | 8dbd421 | 2012-12-04 09:30:24 +0100 | [diff] [blame] | 14624 | arg.ch = PyUnicode_READ(ctx->fmtkind, ctx->fmtdata, ctx->fmtpos); |
| 14625 | arg.flags = 0; |
| 14626 | arg.width = -1; |
| 14627 | arg.prec = -1; |
| 14628 | arg.sign = 0; |
| 14629 | str = NULL; |
| 14630 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14631 | ret = unicode_format_arg_parse(ctx, &arg); |
| 14632 | if (ret == -1) |
| 14633 | return -1; |
| 14634 | |
| 14635 | ret = unicode_format_arg_format(ctx, &arg, &str); |
| 14636 | if (ret == -1) |
| 14637 | return -1; |
| 14638 | |
| 14639 | if (ret != 1) { |
| 14640 | ret = unicode_format_arg_output(ctx, &arg, str); |
| 14641 | Py_DECREF(str); |
| 14642 | if (ret == -1) |
| 14643 | return -1; |
| 14644 | } |
| 14645 | |
| 14646 | if (ctx->dict && (ctx->argidx < ctx->arglen) && arg.ch != '%') { |
| 14647 | PyErr_SetString(PyExc_TypeError, |
| 14648 | "not all arguments converted during string formatting"); |
| 14649 | return -1; |
| 14650 | } |
| 14651 | return 0; |
| 14652 | } |
| 14653 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 14654 | PyObject * |
| 14655 | PyUnicode_Format(PyObject *format, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14656 | { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14657 | struct unicode_formatter_t ctx; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 14658 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14659 | if (format == NULL || args == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14660 | PyErr_BadInternalCall(); |
| 14661 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14662 | } |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14663 | |
| 14664 | ctx.fmtstr = PyUnicode_FromObject(format); |
| 14665 | if (ctx.fmtstr == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14666 | return NULL; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14667 | if (PyUnicode_READY(ctx.fmtstr) == -1) { |
| 14668 | Py_DECREF(ctx.fmtstr); |
| 14669 | return NULL; |
| 14670 | } |
| 14671 | ctx.fmtdata = PyUnicode_DATA(ctx.fmtstr); |
| 14672 | ctx.fmtkind = PyUnicode_KIND(ctx.fmtstr); |
| 14673 | ctx.fmtcnt = PyUnicode_GET_LENGTH(ctx.fmtstr); |
| 14674 | ctx.fmtpos = 0; |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 14675 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 14676 | _PyUnicodeWriter_Init(&ctx.writer); |
| 14677 | ctx.writer.min_length = ctx.fmtcnt + 100; |
| 14678 | ctx.writer.overallocate = 1; |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 14679 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14680 | if (PyTuple_Check(args)) { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14681 | ctx.arglen = PyTuple_Size(args); |
| 14682 | ctx.argidx = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14683 | } |
| 14684 | else { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14685 | ctx.arglen = -1; |
| 14686 | ctx.argidx = -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14687 | } |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14688 | ctx.args_owned = 0; |
Benjamin Peterson | 28a6cfa | 2012-08-28 17:55:35 -0400 | [diff] [blame] | 14689 | if (PyMapping_Check(args) && !PyTuple_Check(args) && !PyUnicode_Check(args)) |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14690 | ctx.dict = args; |
| 14691 | else |
| 14692 | ctx.dict = NULL; |
| 14693 | ctx.args = args; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14694 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14695 | while (--ctx.fmtcnt >= 0) { |
| 14696 | if (PyUnicode_READ(ctx.fmtkind, ctx.fmtdata, ctx.fmtpos) != '%') { |
Victor Stinner | cfc4c13 | 2013-04-03 01:48:39 +0200 | [diff] [blame] | 14697 | Py_ssize_t nonfmtpos; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14698 | |
| 14699 | nonfmtpos = ctx.fmtpos++; |
| 14700 | while (ctx.fmtcnt >= 0 && |
| 14701 | PyUnicode_READ(ctx.fmtkind, ctx.fmtdata, ctx.fmtpos) != '%') { |
| 14702 | ctx.fmtpos++; |
| 14703 | ctx.fmtcnt--; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14704 | } |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14705 | if (ctx.fmtcnt < 0) { |
| 14706 | ctx.fmtpos--; |
| 14707 | ctx.writer.overallocate = 0; |
Victor Stinner | a049443 | 2012-10-03 23:03:46 +0200 | [diff] [blame] | 14708 | } |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 14709 | |
Victor Stinner | cfc4c13 | 2013-04-03 01:48:39 +0200 | [diff] [blame] | 14710 | if (_PyUnicodeWriter_WriteSubstring(&ctx.writer, ctx.fmtstr, |
| 14711 | nonfmtpos, ctx.fmtpos) < 0) |
| 14712 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14713 | } |
| 14714 | else { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14715 | ctx.fmtpos++; |
| 14716 | if (unicode_format_arg(&ctx) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14717 | goto onError; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14718 | } |
| 14719 | } |
Victor Stinner | aff3cc6 | 2012-04-30 05:19:21 +0200 | [diff] [blame] | 14720 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14721 | if (ctx.argidx < ctx.arglen && !ctx.dict) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14722 | PyErr_SetString(PyExc_TypeError, |
| 14723 | "not all arguments converted during string formatting"); |
| 14724 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14725 | } |
| 14726 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14727 | if (ctx.args_owned) { |
| 14728 | Py_DECREF(ctx.args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14729 | } |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14730 | Py_DECREF(ctx.fmtstr); |
| 14731 | return _PyUnicodeWriter_Finish(&ctx.writer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14732 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14733 | onError: |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14734 | Py_DECREF(ctx.fmtstr); |
| 14735 | _PyUnicodeWriter_Dealloc(&ctx.writer); |
| 14736 | if (ctx.args_owned) { |
| 14737 | Py_DECREF(ctx.args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14738 | } |
| 14739 | return NULL; |
| 14740 | } |
| 14741 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 14742 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14743 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 14744 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14745 | static PyObject * |
| 14746 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 14747 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14748 | PyObject *x = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14749 | static char *kwlist[] = {"object", "encoding", "errors", 0}; |
| 14750 | char *encoding = NULL; |
| 14751 | char *errors = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14752 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14753 | if (type != &PyUnicode_Type) |
| 14754 | return unicode_subtype_new(type, args, kwds); |
| 14755 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:str", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14756 | kwlist, &x, &encoding, &errors)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14757 | return NULL; |
| 14758 | if (x == NULL) |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 14759 | _Py_RETURN_UNICODE_EMPTY(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14760 | if (encoding == NULL && errors == NULL) |
| 14761 | return PyObject_Str(x); |
| 14762 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14763 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14764 | } |
| 14765 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14766 | static PyObject * |
| 14767 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 14768 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14769 | PyObject *unicode, *self; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14770 | Py_ssize_t length, char_size; |
| 14771 | int share_wstr, share_utf8; |
| 14772 | unsigned int kind; |
| 14773 | void *data; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14774 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14775 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14776 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14777 | unicode = unicode_new(&PyUnicode_Type, args, kwds); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14778 | if (unicode == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14779 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 14780 | assert(_PyUnicode_CHECK(unicode)); |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 14781 | if (PyUnicode_READY(unicode) == -1) { |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 14782 | Py_DECREF(unicode); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14783 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 14784 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14785 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14786 | self = type->tp_alloc(type, 0); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14787 | if (self == NULL) { |
| 14788 | Py_DECREF(unicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14789 | return NULL; |
| 14790 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14791 | kind = PyUnicode_KIND(unicode); |
| 14792 | length = PyUnicode_GET_LENGTH(unicode); |
| 14793 | |
| 14794 | _PyUnicode_LENGTH(self) = length; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 14795 | #ifdef Py_DEBUG |
| 14796 | _PyUnicode_HASH(self) = -1; |
| 14797 | #else |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14798 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 14799 | #endif |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14800 | _PyUnicode_STATE(self).interned = 0; |
| 14801 | _PyUnicode_STATE(self).kind = kind; |
| 14802 | _PyUnicode_STATE(self).compact = 0; |
Victor Stinner | 3cf4637 | 2011-10-03 14:42:15 +0200 | [diff] [blame] | 14803 | _PyUnicode_STATE(self).ascii = _PyUnicode_STATE(unicode).ascii; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14804 | _PyUnicode_STATE(self).ready = 1; |
| 14805 | _PyUnicode_WSTR(self) = NULL; |
| 14806 | _PyUnicode_UTF8_LENGTH(self) = 0; |
| 14807 | _PyUnicode_UTF8(self) = NULL; |
| 14808 | _PyUnicode_WSTR_LENGTH(self) = 0; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 14809 | _PyUnicode_DATA_ANY(self) = NULL; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14810 | |
| 14811 | share_utf8 = 0; |
| 14812 | share_wstr = 0; |
| 14813 | if (kind == PyUnicode_1BYTE_KIND) { |
| 14814 | char_size = 1; |
| 14815 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) |
| 14816 | share_utf8 = 1; |
| 14817 | } |
| 14818 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 14819 | char_size = 2; |
| 14820 | if (sizeof(wchar_t) == 2) |
| 14821 | share_wstr = 1; |
| 14822 | } |
| 14823 | else { |
| 14824 | assert(kind == PyUnicode_4BYTE_KIND); |
| 14825 | char_size = 4; |
| 14826 | if (sizeof(wchar_t) == 4) |
| 14827 | share_wstr = 1; |
| 14828 | } |
| 14829 | |
| 14830 | /* Ensure we won't overflow the length. */ |
| 14831 | if (length > (PY_SSIZE_T_MAX / char_size - 1)) { |
| 14832 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14833 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14834 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14835 | data = PyObject_MALLOC((length + 1) * char_size); |
| 14836 | if (data == NULL) { |
| 14837 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14838 | goto onError; |
| 14839 | } |
| 14840 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 14841 | _PyUnicode_DATA_ANY(self) = data; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14842 | if (share_utf8) { |
| 14843 | _PyUnicode_UTF8_LENGTH(self) = length; |
| 14844 | _PyUnicode_UTF8(self) = data; |
| 14845 | } |
| 14846 | if (share_wstr) { |
| 14847 | _PyUnicode_WSTR_LENGTH(self) = length; |
| 14848 | _PyUnicode_WSTR(self) = (wchar_t *)data; |
| 14849 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14850 | |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14851 | Py_MEMCPY(data, PyUnicode_DATA(unicode), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 14852 | kind * (length + 1)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 14853 | assert(_PyUnicode_CheckConsistency(self, 1)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 14854 | #ifdef Py_DEBUG |
| 14855 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
| 14856 | #endif |
Victor Stinner | dd18d3a | 2011-10-22 11:08:10 +0200 | [diff] [blame] | 14857 | Py_DECREF(unicode); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 14858 | return self; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14859 | |
| 14860 | onError: |
| 14861 | Py_DECREF(unicode); |
| 14862 | Py_DECREF(self); |
| 14863 | return NULL; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14864 | } |
| 14865 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 14866 | PyDoc_STRVAR(unicode_doc, |
Chris Jerdonek | 83fe2e1 | 2012-10-07 14:48:36 -0700 | [diff] [blame] | 14867 | "str(object='') -> str\n\ |
| 14868 | str(bytes_or_buffer[, encoding[, errors]]) -> str\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14869 | \n\ |
Nick Coghlan | 573b1fd | 2012-08-16 14:13:07 +1000 | [diff] [blame] | 14870 | Create a new string object from the given object. If encoding or\n\ |
| 14871 | errors is specified, then the object must expose a data buffer\n\ |
| 14872 | that will be decoded using the given encoding and error handler.\n\ |
| 14873 | Otherwise, returns the result of object.__str__() (if defined)\n\ |
| 14874 | or repr(object).\n\ |
| 14875 | encoding defaults to sys.getdefaultencoding().\n\ |
| 14876 | errors defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14877 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14878 | static PyObject *unicode_iter(PyObject *seq); |
| 14879 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14880 | PyTypeObject PyUnicode_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 14881 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14882 | "str", /* tp_name */ |
| 14883 | sizeof(PyUnicodeObject), /* tp_size */ |
| 14884 | 0, /* tp_itemsize */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14885 | /* Slots */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14886 | (destructor)unicode_dealloc, /* tp_dealloc */ |
| 14887 | 0, /* tp_print */ |
| 14888 | 0, /* tp_getattr */ |
| 14889 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 14890 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14891 | unicode_repr, /* tp_repr */ |
| 14892 | &unicode_as_number, /* tp_as_number */ |
| 14893 | &unicode_as_sequence, /* tp_as_sequence */ |
| 14894 | &unicode_as_mapping, /* tp_as_mapping */ |
| 14895 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 14896 | 0, /* tp_call*/ |
| 14897 | (reprfunc) unicode_str, /* tp_str */ |
| 14898 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 14899 | 0, /* tp_setattro */ |
| 14900 | 0, /* tp_as_buffer */ |
| 14901 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14902 | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14903 | unicode_doc, /* tp_doc */ |
| 14904 | 0, /* tp_traverse */ |
| 14905 | 0, /* tp_clear */ |
| 14906 | PyUnicode_RichCompare, /* tp_richcompare */ |
| 14907 | 0, /* tp_weaklistoffset */ |
| 14908 | unicode_iter, /* tp_iter */ |
| 14909 | 0, /* tp_iternext */ |
| 14910 | unicode_methods, /* tp_methods */ |
| 14911 | 0, /* tp_members */ |
| 14912 | 0, /* tp_getset */ |
| 14913 | &PyBaseObject_Type, /* tp_base */ |
| 14914 | 0, /* tp_dict */ |
| 14915 | 0, /* tp_descr_get */ |
| 14916 | 0, /* tp_descr_set */ |
| 14917 | 0, /* tp_dictoffset */ |
| 14918 | 0, /* tp_init */ |
| 14919 | 0, /* tp_alloc */ |
| 14920 | unicode_new, /* tp_new */ |
| 14921 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14922 | }; |
| 14923 | |
| 14924 | /* Initialize the Unicode implementation */ |
| 14925 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 14926 | int _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14927 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14928 | /* XXX - move this array to unicodectype.c ? */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14929 | Py_UCS2 linebreak[] = { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14930 | 0x000A, /* LINE FEED */ |
| 14931 | 0x000D, /* CARRIAGE RETURN */ |
| 14932 | 0x001C, /* FILE SEPARATOR */ |
| 14933 | 0x001D, /* GROUP SEPARATOR */ |
| 14934 | 0x001E, /* RECORD SEPARATOR */ |
| 14935 | 0x0085, /* NEXT LINE */ |
| 14936 | 0x2028, /* LINE SEPARATOR */ |
| 14937 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 14938 | }; |
| 14939 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 14940 | /* Init the implementation */ |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 14941 | _Py_INCREF_UNICODE_EMPTY(); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 14942 | if (!unicode_empty) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14943 | Py_FatalError("Can't create empty string"); |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 14944 | Py_DECREF(unicode_empty); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 14945 | |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 14946 | if (PyType_Ready(&PyUnicode_Type) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14947 | Py_FatalError("Can't initialize 'unicode'"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14948 | |
| 14949 | /* initialize the linebreak bloom filter */ |
| 14950 | bloom_linebreak = make_bloom_mask( |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14951 | PyUnicode_2BYTE_KIND, linebreak, |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 14952 | Py_ARRAY_LENGTH(linebreak)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 14953 | |
Christian Heimes | 26532f7 | 2013-07-20 14:57:16 +0200 | [diff] [blame] | 14954 | if (PyType_Ready(&EncodingMapType) < 0) |
| 14955 | Py_FatalError("Can't initialize encoding map type"); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 14956 | |
Benjamin Peterson | c431128 | 2012-10-30 23:21:10 -0400 | [diff] [blame] | 14957 | if (PyType_Ready(&PyFieldNameIter_Type) < 0) |
| 14958 | Py_FatalError("Can't initialize field name iterator type"); |
| 14959 | |
| 14960 | if (PyType_Ready(&PyFormatterIter_Type) < 0) |
| 14961 | Py_FatalError("Can't initialize formatter iter type"); |
Benjamin Peterson | e8ea97f | 2012-10-30 23:27:52 -0400 | [diff] [blame] | 14962 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 14963 | #ifdef HAVE_MBCS |
| 14964 | winver.dwOSVersionInfoSize = sizeof(winver); |
| 14965 | if (!GetVersionEx((OSVERSIONINFO*)&winver)) { |
| 14966 | PyErr_SetFromWindowsErr(0); |
| 14967 | return -1; |
| 14968 | } |
| 14969 | #endif |
| 14970 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14971 | } |
| 14972 | |
| 14973 | /* Finalize the Unicode implementation */ |
| 14974 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 14975 | int |
| 14976 | PyUnicode_ClearFreeList(void) |
| 14977 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14978 | return 0; |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 14979 | } |
| 14980 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14981 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 14982 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14983 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 14984 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14985 | |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 14986 | Py_CLEAR(unicode_empty); |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 14987 | |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 14988 | for (i = 0; i < 256; i++) |
| 14989 | Py_CLEAR(unicode_latin1[i]); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 14990 | _PyUnicode_ClearStaticStrings(); |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 14991 | (void)PyUnicode_ClearFreeList(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14992 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 14993 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14994 | void |
| 14995 | PyUnicode_InternInPlace(PyObject **p) |
| 14996 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 14997 | PyObject *s = *p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14998 | PyObject *t; |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 14999 | #ifdef Py_DEBUG |
| 15000 | assert(s != NULL); |
| 15001 | assert(_PyUnicode_CHECK(s)); |
| 15002 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15003 | if (s == NULL || !PyUnicode_Check(s)) |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 15004 | return; |
| 15005 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15006 | /* If it's a subclass, we don't really know what putting |
| 15007 | it in the interned dict might do. */ |
| 15008 | if (!PyUnicode_CheckExact(s)) |
| 15009 | return; |
| 15010 | if (PyUnicode_CHECK_INTERNED(s)) |
| 15011 | return; |
| 15012 | if (interned == NULL) { |
| 15013 | interned = PyDict_New(); |
| 15014 | if (interned == NULL) { |
| 15015 | PyErr_Clear(); /* Don't leave an exception */ |
| 15016 | return; |
| 15017 | } |
| 15018 | } |
| 15019 | /* It might be that the GetItem call fails even |
| 15020 | though the key is present in the dictionary, |
| 15021 | namely when this happens during a stack overflow. */ |
| 15022 | Py_ALLOW_RECURSION |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 15023 | t = PyDict_GetItem(interned, s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15024 | Py_END_ALLOW_RECURSION |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 15025 | |
Victor Stinner | f033510 | 2013-04-14 19:13:03 +0200 | [diff] [blame] | 15026 | if (t) { |
| 15027 | Py_INCREF(t); |
| 15028 | Py_DECREF(*p); |
| 15029 | *p = t; |
| 15030 | return; |
| 15031 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 15032 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15033 | PyThreadState_GET()->recursion_critical = 1; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 15034 | if (PyDict_SetItem(interned, s, s) < 0) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15035 | PyErr_Clear(); |
| 15036 | PyThreadState_GET()->recursion_critical = 0; |
| 15037 | return; |
| 15038 | } |
| 15039 | PyThreadState_GET()->recursion_critical = 0; |
| 15040 | /* The two references in interned are not counted by refcnt. |
| 15041 | The deallocator will take care of this */ |
| 15042 | Py_REFCNT(s) -= 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15043 | _PyUnicode_STATE(s).interned = SSTATE_INTERNED_MORTAL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 15044 | } |
| 15045 | |
| 15046 | void |
| 15047 | PyUnicode_InternImmortal(PyObject **p) |
| 15048 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15049 | PyUnicode_InternInPlace(p); |
| 15050 | if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { |
Victor Stinner | af9e4b8 | 2011-10-23 20:07:00 +0200 | [diff] [blame] | 15051 | _PyUnicode_STATE(*p).interned = SSTATE_INTERNED_IMMORTAL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15052 | Py_INCREF(*p); |
| 15053 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 15054 | } |
| 15055 | |
| 15056 | PyObject * |
| 15057 | PyUnicode_InternFromString(const char *cp) |
| 15058 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15059 | PyObject *s = PyUnicode_FromString(cp); |
| 15060 | if (s == NULL) |
| 15061 | return NULL; |
| 15062 | PyUnicode_InternInPlace(&s); |
| 15063 | return s; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 15064 | } |
| 15065 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 15066 | void |
| 15067 | _Py_ReleaseInternedUnicodeStrings(void) |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 15068 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15069 | PyObject *keys; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 15070 | PyObject *s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15071 | Py_ssize_t i, n; |
| 15072 | Py_ssize_t immortal_size = 0, mortal_size = 0; |
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 | if (interned == NULL || !PyDict_Check(interned)) |
| 15075 | return; |
| 15076 | keys = PyDict_Keys(interned); |
| 15077 | if (keys == NULL || !PyList_Check(keys)) { |
| 15078 | PyErr_Clear(); |
| 15079 | return; |
| 15080 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 15081 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15082 | /* Since _Py_ReleaseInternedUnicodeStrings() is intended to help a leak |
| 15083 | detector, interned unicode strings are not forcibly deallocated; |
| 15084 | rather, we give them their stolen references back, and then clear |
| 15085 | and DECREF the interned dict. */ |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 15086 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15087 | n = PyList_GET_SIZE(keys); |
| 15088 | fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 15089 | n); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15090 | for (i = 0; i < n; i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 15091 | s = PyList_GET_ITEM(keys, i); |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 15092 | if (PyUnicode_READY(s) == -1) { |
| 15093 | assert(0 && "could not ready string"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15094 | fprintf(stderr, "could not ready string\n"); |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 15095 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15096 | switch (PyUnicode_CHECK_INTERNED(s)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15097 | case SSTATE_NOT_INTERNED: |
| 15098 | /* XXX Shouldn't happen */ |
| 15099 | break; |
| 15100 | case SSTATE_INTERNED_IMMORTAL: |
| 15101 | Py_REFCNT(s) += 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15102 | immortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15103 | break; |
| 15104 | case SSTATE_INTERNED_MORTAL: |
| 15105 | Py_REFCNT(s) += 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15106 | mortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15107 | break; |
| 15108 | default: |
| 15109 | Py_FatalError("Inconsistent interned string state."); |
| 15110 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15111 | _PyUnicode_STATE(s).interned = SSTATE_NOT_INTERNED; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15112 | } |
| 15113 | fprintf(stderr, "total size of all interned strings: " |
| 15114 | "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " |
| 15115 | "mortal/immortal\n", mortal_size, immortal_size); |
| 15116 | Py_DECREF(keys); |
| 15117 | PyDict_Clear(interned); |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 15118 | Py_CLEAR(interned); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 15119 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15120 | |
| 15121 | |
| 15122 | /********************* Unicode Iterator **************************/ |
| 15123 | |
| 15124 | typedef struct { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15125 | PyObject_HEAD |
| 15126 | Py_ssize_t it_index; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 15127 | PyObject *it_seq; /* Set to NULL when iterator is exhausted */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15128 | } unicodeiterobject; |
| 15129 | |
| 15130 | static void |
| 15131 | unicodeiter_dealloc(unicodeiterobject *it) |
| 15132 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15133 | _PyObject_GC_UNTRACK(it); |
| 15134 | Py_XDECREF(it->it_seq); |
| 15135 | PyObject_GC_Del(it); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15136 | } |
| 15137 | |
| 15138 | static int |
| 15139 | unicodeiter_traverse(unicodeiterobject *it, visitproc visit, void *arg) |
| 15140 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15141 | Py_VISIT(it->it_seq); |
| 15142 | return 0; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15143 | } |
| 15144 | |
| 15145 | static PyObject * |
| 15146 | unicodeiter_next(unicodeiterobject *it) |
| 15147 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 15148 | PyObject *seq, *item; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15149 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15150 | assert(it != NULL); |
| 15151 | seq = it->it_seq; |
| 15152 | if (seq == NULL) |
| 15153 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 15154 | assert(_PyUnicode_CHECK(seq)); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15155 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15156 | if (it->it_index < PyUnicode_GET_LENGTH(seq)) { |
| 15157 | int kind = PyUnicode_KIND(seq); |
| 15158 | void *data = PyUnicode_DATA(seq); |
| 15159 | Py_UCS4 chr = PyUnicode_READ(kind, data, it->it_index); |
| 15160 | item = PyUnicode_FromOrdinal(chr); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15161 | if (item != NULL) |
| 15162 | ++it->it_index; |
| 15163 | return item; |
| 15164 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15165 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15166 | Py_DECREF(seq); |
| 15167 | it->it_seq = NULL; |
| 15168 | return NULL; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15169 | } |
| 15170 | |
| 15171 | static PyObject * |
| 15172 | unicodeiter_len(unicodeiterobject *it) |
| 15173 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15174 | Py_ssize_t len = 0; |
| 15175 | if (it->it_seq) |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 15176 | len = PyUnicode_GET_LENGTH(it->it_seq) - it->it_index; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15177 | return PyLong_FromSsize_t(len); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15178 | } |
| 15179 | |
| 15180 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
| 15181 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 15182 | static PyObject * |
| 15183 | unicodeiter_reduce(unicodeiterobject *it) |
| 15184 | { |
| 15185 | if (it->it_seq != NULL) { |
Antoine Pitrou | a701388 | 2012-04-05 00:04:20 +0200 | [diff] [blame] | 15186 | return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("iter"), |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 15187 | it->it_seq, it->it_index); |
| 15188 | } else { |
| 15189 | PyObject *u = PyUnicode_FromUnicode(NULL, 0); |
| 15190 | if (u == NULL) |
| 15191 | return NULL; |
Antoine Pitrou | a701388 | 2012-04-05 00:04:20 +0200 | [diff] [blame] | 15192 | return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), u); |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 15193 | } |
| 15194 | } |
| 15195 | |
| 15196 | PyDoc_STRVAR(reduce_doc, "Return state information for pickling."); |
| 15197 | |
| 15198 | static PyObject * |
| 15199 | unicodeiter_setstate(unicodeiterobject *it, PyObject *state) |
| 15200 | { |
| 15201 | Py_ssize_t index = PyLong_AsSsize_t(state); |
| 15202 | if (index == -1 && PyErr_Occurred()) |
| 15203 | return NULL; |
| 15204 | if (index < 0) |
| 15205 | index = 0; |
| 15206 | it->it_index = index; |
| 15207 | Py_RETURN_NONE; |
| 15208 | } |
| 15209 | |
| 15210 | PyDoc_STRVAR(setstate_doc, "Set state information for unpickling."); |
| 15211 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15212 | static PyMethodDef unicodeiter_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15213 | {"__length_hint__", (PyCFunction)unicodeiter_len, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 15214 | length_hint_doc}, |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 15215 | {"__reduce__", (PyCFunction)unicodeiter_reduce, METH_NOARGS, |
| 15216 | reduce_doc}, |
| 15217 | {"__setstate__", (PyCFunction)unicodeiter_setstate, METH_O, |
| 15218 | setstate_doc}, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15219 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15220 | }; |
| 15221 | |
| 15222 | PyTypeObject PyUnicodeIter_Type = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15223 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 15224 | "str_iterator", /* tp_name */ |
| 15225 | sizeof(unicodeiterobject), /* tp_basicsize */ |
| 15226 | 0, /* tp_itemsize */ |
| 15227 | /* methods */ |
| 15228 | (destructor)unicodeiter_dealloc, /* tp_dealloc */ |
| 15229 | 0, /* tp_print */ |
| 15230 | 0, /* tp_getattr */ |
| 15231 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 15232 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15233 | 0, /* tp_repr */ |
| 15234 | 0, /* tp_as_number */ |
| 15235 | 0, /* tp_as_sequence */ |
| 15236 | 0, /* tp_as_mapping */ |
| 15237 | 0, /* tp_hash */ |
| 15238 | 0, /* tp_call */ |
| 15239 | 0, /* tp_str */ |
| 15240 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 15241 | 0, /* tp_setattro */ |
| 15242 | 0, /* tp_as_buffer */ |
| 15243 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 15244 | 0, /* tp_doc */ |
| 15245 | (traverseproc)unicodeiter_traverse, /* tp_traverse */ |
| 15246 | 0, /* tp_clear */ |
| 15247 | 0, /* tp_richcompare */ |
| 15248 | 0, /* tp_weaklistoffset */ |
| 15249 | PyObject_SelfIter, /* tp_iter */ |
| 15250 | (iternextfunc)unicodeiter_next, /* tp_iternext */ |
| 15251 | unicodeiter_methods, /* tp_methods */ |
| 15252 | 0, |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15253 | }; |
| 15254 | |
| 15255 | static PyObject * |
| 15256 | unicode_iter(PyObject *seq) |
| 15257 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15258 | unicodeiterobject *it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15259 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15260 | if (!PyUnicode_Check(seq)) { |
| 15261 | PyErr_BadInternalCall(); |
| 15262 | return NULL; |
| 15263 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15264 | if (PyUnicode_READY(seq) == -1) |
| 15265 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15266 | it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type); |
| 15267 | if (it == NULL) |
| 15268 | return NULL; |
| 15269 | it->it_index = 0; |
| 15270 | Py_INCREF(seq); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 15271 | it->it_seq = seq; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15272 | _PyObject_GC_TRACK(it); |
| 15273 | return (PyObject *)it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15274 | } |
| 15275 | |
Martin v. Löwis | 0d3072e | 2011-10-31 08:40:56 +0100 | [diff] [blame] | 15276 | |
| 15277 | size_t |
| 15278 | Py_UNICODE_strlen(const Py_UNICODE *u) |
| 15279 | { |
| 15280 | int res = 0; |
| 15281 | while(*u++) |
| 15282 | res++; |
| 15283 | return res; |
| 15284 | } |
| 15285 | |
| 15286 | Py_UNICODE* |
| 15287 | Py_UNICODE_strcpy(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 15288 | { |
| 15289 | Py_UNICODE *u = s1; |
| 15290 | while ((*u++ = *s2++)); |
| 15291 | return s1; |
| 15292 | } |
| 15293 | |
| 15294 | Py_UNICODE* |
| 15295 | Py_UNICODE_strncpy(Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 15296 | { |
| 15297 | Py_UNICODE *u = s1; |
| 15298 | while ((*u++ = *s2++)) |
| 15299 | if (n-- == 0) |
| 15300 | break; |
| 15301 | return s1; |
| 15302 | } |
| 15303 | |
| 15304 | Py_UNICODE* |
| 15305 | Py_UNICODE_strcat(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 15306 | { |
| 15307 | Py_UNICODE *u1 = s1; |
| 15308 | u1 += Py_UNICODE_strlen(u1); |
| 15309 | Py_UNICODE_strcpy(u1, s2); |
| 15310 | return s1; |
| 15311 | } |
| 15312 | |
| 15313 | int |
| 15314 | Py_UNICODE_strcmp(const Py_UNICODE *s1, const Py_UNICODE *s2) |
| 15315 | { |
| 15316 | while (*s1 && *s2 && *s1 == *s2) |
| 15317 | s1++, s2++; |
| 15318 | if (*s1 && *s2) |
| 15319 | return (*s1 < *s2) ? -1 : +1; |
| 15320 | if (*s1) |
| 15321 | return 1; |
| 15322 | if (*s2) |
| 15323 | return -1; |
| 15324 | return 0; |
| 15325 | } |
| 15326 | |
| 15327 | int |
| 15328 | Py_UNICODE_strncmp(const Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 15329 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 15330 | Py_UNICODE u1, u2; |
Martin v. Löwis | 0d3072e | 2011-10-31 08:40:56 +0100 | [diff] [blame] | 15331 | for (; n != 0; n--) { |
| 15332 | u1 = *s1; |
| 15333 | u2 = *s2; |
| 15334 | if (u1 != u2) |
| 15335 | return (u1 < u2) ? -1 : +1; |
| 15336 | if (u1 == '\0') |
| 15337 | return 0; |
| 15338 | s1++; |
| 15339 | s2++; |
| 15340 | } |
| 15341 | return 0; |
| 15342 | } |
| 15343 | |
| 15344 | Py_UNICODE* |
| 15345 | Py_UNICODE_strchr(const Py_UNICODE *s, Py_UNICODE c) |
| 15346 | { |
| 15347 | const Py_UNICODE *p; |
| 15348 | for (p = s; *p; p++) |
| 15349 | if (*p == c) |
| 15350 | return (Py_UNICODE*)p; |
| 15351 | return NULL; |
| 15352 | } |
| 15353 | |
| 15354 | Py_UNICODE* |
| 15355 | Py_UNICODE_strrchr(const Py_UNICODE *s, Py_UNICODE c) |
| 15356 | { |
| 15357 | const Py_UNICODE *p; |
| 15358 | p = s + Py_UNICODE_strlen(s); |
| 15359 | while (p != s) { |
| 15360 | p--; |
| 15361 | if (*p == c) |
| 15362 | return (Py_UNICODE*)p; |
| 15363 | } |
| 15364 | return NULL; |
| 15365 | } |
Victor Stinner | 331ea92 | 2010-08-10 16:37:20 +0000 | [diff] [blame] | 15366 | |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 15367 | Py_UNICODE* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 15368 | PyUnicode_AsUnicodeCopy(PyObject *unicode) |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 15369 | { |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 15370 | Py_UNICODE *u, *copy; |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 15371 | Py_ssize_t len, size; |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 15372 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15373 | if (!PyUnicode_Check(unicode)) { |
| 15374 | PyErr_BadArgument(); |
| 15375 | return NULL; |
| 15376 | } |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 15377 | u = PyUnicode_AsUnicodeAndSize(unicode, &len); |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 15378 | if (u == NULL) |
| 15379 | return NULL; |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 15380 | /* Ensure we won't overflow the size. */ |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 15381 | if (len > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 15382 | PyErr_NoMemory(); |
| 15383 | return NULL; |
| 15384 | } |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 15385 | size = len + 1; /* copy the null character */ |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 15386 | size *= sizeof(Py_UNICODE); |
| 15387 | copy = PyMem_Malloc(size); |
| 15388 | if (copy == NULL) { |
| 15389 | PyErr_NoMemory(); |
| 15390 | return NULL; |
| 15391 | } |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 15392 | memcpy(copy, u, size); |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 15393 | return copy; |
| 15394 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 15395 | |
Georg Brandl | 66c221e | 2010-10-14 07:04:07 +0000 | [diff] [blame] | 15396 | /* A _string module, to export formatter_parser and formatter_field_name_split |
| 15397 | to the string.Formatter class implemented in Python. */ |
| 15398 | |
| 15399 | static PyMethodDef _string_methods[] = { |
| 15400 | {"formatter_field_name_split", (PyCFunction) formatter_field_name_split, |
| 15401 | METH_O, PyDoc_STR("split the argument as a field name")}, |
| 15402 | {"formatter_parser", (PyCFunction) formatter_parser, |
| 15403 | METH_O, PyDoc_STR("parse the argument as a format string")}, |
| 15404 | {NULL, NULL} |
| 15405 | }; |
| 15406 | |
| 15407 | static struct PyModuleDef _string_module = { |
| 15408 | PyModuleDef_HEAD_INIT, |
| 15409 | "_string", |
| 15410 | PyDoc_STR("string helper module"), |
| 15411 | 0, |
| 15412 | _string_methods, |
| 15413 | NULL, |
| 15414 | NULL, |
| 15415 | NULL, |
| 15416 | NULL |
| 15417 | }; |
| 15418 | |
| 15419 | PyMODINIT_FUNC |
| 15420 | PyInit__string(void) |
| 15421 | { |
| 15422 | return PyModule_Create(&_string_module); |
| 15423 | } |
| 15424 | |
| 15425 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 15426 | #ifdef __cplusplus |
| 15427 | } |
| 15428 | #endif |