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 | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 50 | /*[clinic] |
| 51 | class str |
| 52 | [clinic]*/ |
| 53 | /*[clinic checksum: da39a3ee5e6b4b0d3255bfef95601890afd80709]*/ |
| 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) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4462 | writer.pos = shiftOutStart; /* back off output */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4463 | *consumed = startinpos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4464 | } |
| 4465 | else { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4466 | *consumed = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4467 | } |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4468 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4469 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4470 | Py_XDECREF(errorHandler); |
| 4471 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4472 | return _PyUnicodeWriter_Finish(&writer); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4473 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4474 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4475 | Py_XDECREF(errorHandler); |
| 4476 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4477 | _PyUnicodeWriter_Dealloc(&writer); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4478 | return NULL; |
| 4479 | } |
| 4480 | |
| 4481 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4482 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4483 | _PyUnicode_EncodeUTF7(PyObject *str, |
| 4484 | int base64SetO, |
| 4485 | int base64WhiteSpace, |
| 4486 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4487 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4488 | int kind; |
| 4489 | void *data; |
| 4490 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4491 | PyObject *v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4492 | int inShift = 0; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4493 | Py_ssize_t i; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4494 | unsigned int base64bits = 0; |
| 4495 | unsigned long base64buffer = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4496 | char * out; |
| 4497 | char * start; |
| 4498 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 4499 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4500 | return NULL; |
| 4501 | kind = PyUnicode_KIND(str); |
| 4502 | data = PyUnicode_DATA(str); |
| 4503 | len = PyUnicode_GET_LENGTH(str); |
| 4504 | |
| 4505 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4506 | return PyBytes_FromStringAndSize(NULL, 0); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4507 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4508 | /* It might be possible to tighten this worst case */ |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 4509 | if (len > PY_SSIZE_T_MAX / 8) |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4510 | return PyErr_NoMemory(); |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 4511 | v = PyBytes_FromStringAndSize(NULL, len * 8); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4512 | if (v == NULL) |
| 4513 | return NULL; |
| 4514 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4515 | start = out = PyBytes_AS_STRING(v); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4516 | for (i = 0; i < len; ++i) { |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 4517 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4518 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4519 | if (inShift) { |
| 4520 | if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 4521 | /* shifting out */ |
| 4522 | if (base64bits) { /* output remaining bits */ |
| 4523 | *out++ = TO_BASE64(base64buffer << (6-base64bits)); |
| 4524 | base64buffer = 0; |
| 4525 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4526 | } |
| 4527 | inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4528 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 4529 | so no '-' is required, except if the character is itself a '-' */ |
| 4530 | if (IS_BASE64(ch) || ch == '-') { |
| 4531 | *out++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4532 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4533 | *out++ = (char) ch; |
| 4534 | } |
| 4535 | else { |
| 4536 | goto encode_char; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4537 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4538 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4539 | else { /* not in a shift sequence */ |
| 4540 | if (ch == '+') { |
| 4541 | *out++ = '+'; |
| 4542 | *out++ = '-'; |
| 4543 | } |
| 4544 | else if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 4545 | *out++ = (char) ch; |
| 4546 | } |
| 4547 | else { |
| 4548 | *out++ = '+'; |
| 4549 | inShift = 1; |
| 4550 | goto encode_char; |
| 4551 | } |
| 4552 | } |
| 4553 | continue; |
| 4554 | encode_char: |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4555 | if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 4556 | assert(ch <= MAX_UNICODE); |
Victor Stinner | 0d3721d | 2011-11-22 03:27:53 +0100 | [diff] [blame] | 4557 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4558 | /* code first surrogate */ |
| 4559 | base64bits += 16; |
Victor Stinner | 76df43d | 2012-10-30 01:42:39 +0100 | [diff] [blame] | 4560 | base64buffer = (base64buffer << 16) | Py_UNICODE_HIGH_SURROGATE(ch); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4561 | while (base64bits >= 6) { |
| 4562 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 4563 | base64bits -= 6; |
| 4564 | } |
| 4565 | /* prepare second surrogate */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4566 | ch = Py_UNICODE_LOW_SURROGATE(ch); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4567 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4568 | base64bits += 16; |
| 4569 | base64buffer = (base64buffer << 16) | ch; |
| 4570 | while (base64bits >= 6) { |
| 4571 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 4572 | base64bits -= 6; |
| 4573 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 4574 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4575 | if (base64bits) |
| 4576 | *out++= TO_BASE64(base64buffer << (6-base64bits) ); |
| 4577 | if (inShift) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4578 | *out++ = '-'; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4579 | if (_PyBytes_Resize(&v, out - start) < 0) |
| 4580 | return NULL; |
| 4581 | return v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4582 | } |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4583 | PyObject * |
| 4584 | PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
| 4585 | Py_ssize_t size, |
| 4586 | int base64SetO, |
| 4587 | int base64WhiteSpace, |
| 4588 | const char *errors) |
| 4589 | { |
| 4590 | PyObject *result; |
| 4591 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 4592 | if (tmp == NULL) |
| 4593 | return NULL; |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 4594 | result = _PyUnicode_EncodeUTF7(tmp, base64SetO, |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4595 | base64WhiteSpace, errors); |
| 4596 | Py_DECREF(tmp); |
| 4597 | return result; |
| 4598 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4599 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4600 | #undef IS_BASE64 |
| 4601 | #undef FROM_BASE64 |
| 4602 | #undef TO_BASE64 |
| 4603 | #undef DECODE_DIRECT |
| 4604 | #undef ENCODE_DIRECT |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4605 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4606 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 4607 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4608 | PyObject * |
| 4609 | PyUnicode_DecodeUTF8(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4610 | Py_ssize_t size, |
| 4611 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4612 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4613 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 4614 | } |
| 4615 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4616 | #include "stringlib/asciilib.h" |
| 4617 | #include "stringlib/codecs.h" |
| 4618 | #include "stringlib/undef.h" |
| 4619 | |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4620 | #include "stringlib/ucs1lib.h" |
| 4621 | #include "stringlib/codecs.h" |
| 4622 | #include "stringlib/undef.h" |
| 4623 | |
| 4624 | #include "stringlib/ucs2lib.h" |
| 4625 | #include "stringlib/codecs.h" |
| 4626 | #include "stringlib/undef.h" |
| 4627 | |
| 4628 | #include "stringlib/ucs4lib.h" |
| 4629 | #include "stringlib/codecs.h" |
| 4630 | #include "stringlib/undef.h" |
| 4631 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4632 | /* Mask to quickly check whether a C 'long' contains a |
| 4633 | non-ASCII, UTF8-encoded char. */ |
| 4634 | #if (SIZEOF_LONG == 8) |
Mark Dickinson | 01ac8b6 | 2012-07-07 14:08:48 +0200 | [diff] [blame] | 4635 | # define ASCII_CHAR_MASK 0x8080808080808080UL |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4636 | #elif (SIZEOF_LONG == 4) |
Mark Dickinson | 01ac8b6 | 2012-07-07 14:08:48 +0200 | [diff] [blame] | 4637 | # define ASCII_CHAR_MASK 0x80808080UL |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4638 | #else |
| 4639 | # error C 'long' size should be either 4 or 8! |
| 4640 | #endif |
| 4641 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4642 | static Py_ssize_t |
| 4643 | 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] | 4644 | { |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4645 | const char *p = start; |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 4646 | 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] | 4647 | |
Antoine Pitrou | 8b0e984 | 2013-05-11 15:58:34 +0200 | [diff] [blame] | 4648 | /* |
| 4649 | * Issue #17237: m68k is a bit different from most architectures in |
| 4650 | * that objects do not use "natural alignment" - for example, int and |
| 4651 | * long are only aligned at 2-byte boundaries. Therefore the assert() |
| 4652 | * won't work; also, tests have shown that skipping the "optimised |
| 4653 | * version" will even speed up m68k. |
| 4654 | */ |
| 4655 | #if !defined(__m68k__) |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4656 | #if SIZEOF_LONG <= SIZEOF_VOID_P |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 4657 | assert(_Py_IS_ALIGNED(dest, SIZEOF_LONG)); |
| 4658 | if (_Py_IS_ALIGNED(p, SIZEOF_LONG)) { |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4659 | /* Fast path, see in STRINGLIB(utf8_decode) for |
| 4660 | an explanation. */ |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 4661 | /* Help allocation */ |
| 4662 | const char *_p = p; |
| 4663 | Py_UCS1 * q = dest; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4664 | while (_p < aligned_end) { |
| 4665 | unsigned long value = *(const unsigned long *) _p; |
| 4666 | if (value & ASCII_CHAR_MASK) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4667 | break; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4668 | *((unsigned long *)q) = value; |
| 4669 | _p += SIZEOF_LONG; |
| 4670 | q += SIZEOF_LONG; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4671 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4672 | p = _p; |
| 4673 | while (p < end) { |
| 4674 | if ((unsigned char)*p & 0x80) |
| 4675 | break; |
| 4676 | *q++ = *p++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4677 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4678 | return p - start; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4679 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4680 | #endif |
Antoine Pitrou | 8b0e984 | 2013-05-11 15:58:34 +0200 | [diff] [blame] | 4681 | #endif |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4682 | while (p < end) { |
| 4683 | /* Fast path, see in STRINGLIB(utf8_decode) in stringlib/codecs.h |
| 4684 | for an explanation. */ |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 4685 | if (_Py_IS_ALIGNED(p, SIZEOF_LONG)) { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 4686 | /* Help allocation */ |
| 4687 | const char *_p = p; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4688 | while (_p < aligned_end) { |
| 4689 | unsigned long value = *(unsigned long *) _p; |
| 4690 | if (value & ASCII_CHAR_MASK) |
| 4691 | break; |
| 4692 | _p += SIZEOF_LONG; |
| 4693 | } |
| 4694 | p = _p; |
| 4695 | if (_p == end) |
| 4696 | break; |
| 4697 | } |
| 4698 | if ((unsigned char)*p & 0x80) |
| 4699 | break; |
| 4700 | ++p; |
| 4701 | } |
| 4702 | memcpy(dest, start, p - start); |
| 4703 | return p - start; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4704 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4705 | |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4706 | PyObject * |
| 4707 | PyUnicode_DecodeUTF8Stateful(const char *s, |
| 4708 | Py_ssize_t size, |
| 4709 | const char *errors, |
| 4710 | Py_ssize_t *consumed) |
| 4711 | { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4712 | _PyUnicodeWriter writer; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4713 | const char *starts = s; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4714 | const char *end = s + size; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4715 | |
| 4716 | Py_ssize_t startinpos; |
| 4717 | Py_ssize_t endinpos; |
| 4718 | const char *errmsg = ""; |
| 4719 | PyObject *errorHandler = NULL; |
| 4720 | PyObject *exc = NULL; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4721 | |
| 4722 | if (size == 0) { |
| 4723 | if (consumed) |
| 4724 | *consumed = 0; |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 4725 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4726 | } |
| 4727 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4728 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
| 4729 | if (size == 1 && (unsigned char)s[0] < 128) { |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4730 | if (consumed) |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4731 | *consumed = 1; |
| 4732 | return get_latin1_char((unsigned char)s[0]); |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4733 | } |
| 4734 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 4735 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 4736 | writer.min_length = size; |
| 4737 | if (_PyUnicodeWriter_Prepare(&writer, writer.min_length, 127) == -1) |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4738 | goto onError; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4739 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4740 | writer.pos = ascii_decode(s, end, writer.data); |
| 4741 | s += writer.pos; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4742 | while (s < end) { |
| 4743 | Py_UCS4 ch; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4744 | int kind = writer.kind; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4745 | if (kind == PyUnicode_1BYTE_KIND) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4746 | if (PyUnicode_IS_ASCII(writer.buffer)) |
| 4747 | ch = asciilib_utf8_decode(&s, end, writer.data, &writer.pos); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4748 | else |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4749 | ch = ucs1lib_utf8_decode(&s, end, writer.data, &writer.pos); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4750 | } else if (kind == PyUnicode_2BYTE_KIND) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4751 | ch = ucs2lib_utf8_decode(&s, end, writer.data, &writer.pos); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4752 | } else { |
| 4753 | assert(kind == PyUnicode_4BYTE_KIND); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4754 | ch = ucs4lib_utf8_decode(&s, end, writer.data, &writer.pos); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4755 | } |
| 4756 | |
| 4757 | switch (ch) { |
| 4758 | case 0: |
| 4759 | if (s == end || consumed) |
| 4760 | goto End; |
| 4761 | errmsg = "unexpected end of data"; |
| 4762 | startinpos = s - starts; |
Ezio Melotti | f7ed5d1 | 2012-11-04 23:21:38 +0200 | [diff] [blame] | 4763 | endinpos = end - starts; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4764 | break; |
| 4765 | case 1: |
| 4766 | errmsg = "invalid start byte"; |
| 4767 | startinpos = s - starts; |
| 4768 | endinpos = startinpos + 1; |
| 4769 | break; |
| 4770 | case 2: |
Ezio Melotti | f7ed5d1 | 2012-11-04 23:21:38 +0200 | [diff] [blame] | 4771 | case 3: |
| 4772 | case 4: |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4773 | errmsg = "invalid continuation byte"; |
| 4774 | startinpos = s - starts; |
Ezio Melotti | f7ed5d1 | 2012-11-04 23:21:38 +0200 | [diff] [blame] | 4775 | endinpos = startinpos + ch - 1; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4776 | break; |
| 4777 | default: |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4778 | if (_PyUnicodeWriter_WriteCharInline(&writer, ch) < 0) |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4779 | goto onError; |
| 4780 | continue; |
| 4781 | } |
| 4782 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4783 | if (unicode_decode_call_errorhandler_writer( |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4784 | errors, &errorHandler, |
| 4785 | "utf-8", errmsg, |
| 4786 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4787 | &writer)) |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4788 | goto onError; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4789 | } |
| 4790 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4791 | End: |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4792 | if (consumed) |
| 4793 | *consumed = s - starts; |
| 4794 | |
| 4795 | Py_XDECREF(errorHandler); |
| 4796 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4797 | return _PyUnicodeWriter_Finish(&writer); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4798 | |
| 4799 | onError: |
| 4800 | Py_XDECREF(errorHandler); |
| 4801 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4802 | _PyUnicodeWriter_Dealloc(&writer); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4803 | return NULL; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4804 | } |
| 4805 | |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4806 | #ifdef __APPLE__ |
| 4807 | |
| 4808 | /* Simplified UTF-8 decoder using surrogateescape error handler, |
Victor Stinner | 0d92c4f | 2012-11-12 23:32:21 +0100 | [diff] [blame] | 4809 | used to decode the command line arguments on Mac OS X. |
| 4810 | |
| 4811 | Return a pointer to a newly allocated wide character string (use |
Victor Stinner | 6f8eeee | 2013-07-07 22:57:45 +0200 | [diff] [blame] | 4812 | PyMem_RawFree() to free the memory), or NULL on memory allocation error. */ |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4813 | |
| 4814 | wchar_t* |
| 4815 | _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size) |
| 4816 | { |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4817 | const char *e; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4818 | wchar_t *unicode; |
| 4819 | Py_ssize_t outpos; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4820 | |
| 4821 | /* Note: size will always be longer than the resulting Unicode |
| 4822 | character count */ |
Victor Stinner | 0d92c4f | 2012-11-12 23:32:21 +0100 | [diff] [blame] | 4823 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < (size + 1)) |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4824 | return NULL; |
Victor Stinner | 6f8eeee | 2013-07-07 22:57:45 +0200 | [diff] [blame] | 4825 | unicode = PyMem_RawMalloc((size + 1) * sizeof(wchar_t)); |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4826 | if (!unicode) |
| 4827 | return NULL; |
| 4828 | |
| 4829 | /* Unpack UTF-8 encoded data */ |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4830 | e = s + size; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4831 | outpos = 0; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4832 | while (s < e) { |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4833 | Py_UCS4 ch; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4834 | #if SIZEOF_WCHAR_T == 4 |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4835 | ch = ucs4lib_utf8_decode(&s, e, (Py_UCS4 *)unicode, &outpos); |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4836 | #else |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4837 | ch = ucs2lib_utf8_decode(&s, e, (Py_UCS2 *)unicode, &outpos); |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4838 | #endif |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4839 | if (ch > 0xFF) { |
| 4840 | #if SIZEOF_WCHAR_T == 4 |
| 4841 | assert(0); |
| 4842 | #else |
| 4843 | assert(Py_UNICODE_IS_SURROGATE(ch)); |
| 4844 | /* compute and append the two surrogates: */ |
| 4845 | unicode[outpos++] = (wchar_t)Py_UNICODE_HIGH_SURROGATE(ch); |
| 4846 | unicode[outpos++] = (wchar_t)Py_UNICODE_LOW_SURROGATE(ch); |
| 4847 | #endif |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4848 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4849 | else { |
| 4850 | if (!ch && s == e) |
| 4851 | break; |
| 4852 | /* surrogateescape */ |
| 4853 | unicode[outpos++] = 0xDC00 + (unsigned char)*s++; |
| 4854 | } |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4855 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4856 | unicode[outpos] = L'\0'; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4857 | return unicode; |
| 4858 | } |
| 4859 | |
| 4860 | #endif /* __APPLE__ */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4861 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4862 | /* Primary internal function which creates utf8 encoded bytes objects. |
| 4863 | |
| 4864 | Allocation strategy: if the string is short, convert into a stack buffer |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4865 | and allocate exactly as much space needed at the end. Else allocate the |
| 4866 | maximum possible needed (4 result bytes per Unicode character), and return |
| 4867 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4868 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 4869 | PyObject * |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4870 | _PyUnicode_AsUTF8String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4871 | { |
Victor Stinner | 6099a03 | 2011-12-18 14:22:26 +0100 | [diff] [blame] | 4872 | enum PyUnicode_Kind kind; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4873 | void *data; |
| 4874 | Py_ssize_t size; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 4875 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4876 | if (!PyUnicode_Check(unicode)) { |
| 4877 | PyErr_BadArgument(); |
| 4878 | return NULL; |
| 4879 | } |
| 4880 | |
| 4881 | if (PyUnicode_READY(unicode) == -1) |
| 4882 | return NULL; |
| 4883 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 4884 | if (PyUnicode_UTF8(unicode)) |
| 4885 | return PyBytes_FromStringAndSize(PyUnicode_UTF8(unicode), |
| 4886 | PyUnicode_UTF8_LENGTH(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4887 | |
| 4888 | kind = PyUnicode_KIND(unicode); |
| 4889 | data = PyUnicode_DATA(unicode); |
| 4890 | size = PyUnicode_GET_LENGTH(unicode); |
| 4891 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 4892 | switch (kind) { |
Victor Stinner | 6099a03 | 2011-12-18 14:22:26 +0100 | [diff] [blame] | 4893 | default: |
| 4894 | assert(0); |
| 4895 | case PyUnicode_1BYTE_KIND: |
| 4896 | /* the string cannot be ASCII, or PyUnicode_UTF8() would be set */ |
| 4897 | assert(!PyUnicode_IS_ASCII(unicode)); |
| 4898 | return ucs1lib_utf8_encoder(unicode, data, size, errors); |
| 4899 | case PyUnicode_2BYTE_KIND: |
| 4900 | return ucs2lib_utf8_encoder(unicode, data, size, errors); |
| 4901 | case PyUnicode_4BYTE_KIND: |
| 4902 | return ucs4lib_utf8_encoder(unicode, data, size, errors); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4903 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4904 | } |
| 4905 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4906 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4907 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
| 4908 | Py_ssize_t size, |
| 4909 | const char *errors) |
| 4910 | { |
| 4911 | PyObject *v, *unicode; |
| 4912 | |
| 4913 | unicode = PyUnicode_FromUnicode(s, size); |
| 4914 | if (unicode == NULL) |
| 4915 | return NULL; |
| 4916 | v = _PyUnicode_AsUTF8String(unicode, errors); |
| 4917 | Py_DECREF(unicode); |
| 4918 | return v; |
| 4919 | } |
| 4920 | |
| 4921 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4922 | PyUnicode_AsUTF8String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4923 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4924 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4925 | } |
| 4926 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4927 | /* --- UTF-32 Codec ------------------------------------------------------- */ |
| 4928 | |
| 4929 | PyObject * |
| 4930 | PyUnicode_DecodeUTF32(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4931 | Py_ssize_t size, |
| 4932 | const char *errors, |
| 4933 | int *byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4934 | { |
| 4935 | return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); |
| 4936 | } |
| 4937 | |
| 4938 | PyObject * |
| 4939 | PyUnicode_DecodeUTF32Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4940 | Py_ssize_t size, |
| 4941 | const char *errors, |
| 4942 | int *byteorder, |
| 4943 | Py_ssize_t *consumed) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4944 | { |
| 4945 | const char *starts = s; |
| 4946 | Py_ssize_t startinpos; |
| 4947 | Py_ssize_t endinpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4948 | _PyUnicodeWriter writer; |
Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 4949 | const unsigned char *q, *e; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4950 | int le, bo = 0; /* assume native ordering by default */ |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 4951 | const char *encoding; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4952 | const char *errmsg = ""; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4953 | PyObject *errorHandler = NULL; |
| 4954 | PyObject *exc = NULL; |
Victor Stinner | 313a120 | 2010-06-11 23:56:51 +0000 | [diff] [blame] | 4955 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4956 | q = (unsigned char *)s; |
| 4957 | e = q + size; |
| 4958 | |
| 4959 | if (byteorder) |
| 4960 | bo = *byteorder; |
| 4961 | |
| 4962 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 4963 | byte order setting accordingly. In native mode, the leading BOM |
| 4964 | mark is skipped, in all other modes, it is copied to the output |
| 4965 | stream as-is (giving a ZWNBSP character). */ |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4966 | if (bo == 0 && size >= 4) { |
| 4967 | Py_UCS4 bom = (q[3] << 24) | (q[2] << 16) | (q[1] << 8) | q[0]; |
| 4968 | if (bom == 0x0000FEFF) { |
| 4969 | bo = -1; |
| 4970 | q += 4; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4971 | } |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4972 | else if (bom == 0xFFFE0000) { |
| 4973 | bo = 1; |
| 4974 | q += 4; |
| 4975 | } |
| 4976 | if (byteorder) |
| 4977 | *byteorder = bo; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4978 | } |
| 4979 | |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4980 | if (q == e) { |
| 4981 | if (consumed) |
| 4982 | *consumed = size; |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 4983 | _Py_RETURN_UNICODE_EMPTY(); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4984 | } |
| 4985 | |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4986 | #ifdef WORDS_BIGENDIAN |
| 4987 | le = bo < 0; |
| 4988 | #else |
| 4989 | le = bo <= 0; |
| 4990 | #endif |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 4991 | encoding = le ? "utf-32-le" : "utf-32-be"; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4992 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 4993 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 4994 | writer.min_length = (e - q + 3) / 4; |
| 4995 | if (_PyUnicodeWriter_Prepare(&writer, writer.min_length, 127) == -1) |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4996 | goto onError; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4997 | |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4998 | while (1) { |
| 4999 | Py_UCS4 ch = 0; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5000 | Py_UCS4 maxch = PyUnicode_MAX_CHAR_VALUE(writer.buffer); |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 5001 | |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5002 | if (e - q >= 4) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5003 | enum PyUnicode_Kind kind = writer.kind; |
| 5004 | void *data = writer.data; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5005 | const unsigned char *last = e - 4; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5006 | Py_ssize_t pos = writer.pos; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5007 | if (le) { |
| 5008 | do { |
| 5009 | ch = (q[3] << 24) | (q[2] << 16) | (q[1] << 8) | q[0]; |
| 5010 | if (ch > maxch) |
| 5011 | break; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5012 | if (kind != PyUnicode_1BYTE_KIND && |
| 5013 | Py_UNICODE_IS_SURROGATE(ch)) |
| 5014 | break; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5015 | PyUnicode_WRITE(kind, data, pos++, ch); |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5016 | q += 4; |
| 5017 | } while (q <= last); |
| 5018 | } |
| 5019 | else { |
| 5020 | do { |
| 5021 | ch = (q[0] << 24) | (q[1] << 16) | (q[2] << 8) | q[3]; |
| 5022 | if (ch > maxch) |
| 5023 | break; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5024 | if (kind != PyUnicode_1BYTE_KIND && |
| 5025 | Py_UNICODE_IS_SURROGATE(ch)) |
| 5026 | break; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5027 | PyUnicode_WRITE(kind, data, pos++, ch); |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5028 | q += 4; |
| 5029 | } while (q <= last); |
| 5030 | } |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5031 | writer.pos = pos; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5032 | } |
| 5033 | |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5034 | if (Py_UNICODE_IS_SURROGATE(ch)) { |
| 5035 | errmsg = "codepoint in surrogate code point range(0xd800, 0xe000)"; |
| 5036 | startinpos = ((const char *)q) - starts; |
| 5037 | endinpos = startinpos + 4; |
| 5038 | } |
| 5039 | else if (ch <= maxch) { |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5040 | if (q == e || consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5041 | break; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5042 | /* remaining bytes at the end? (size should be divisible by 4) */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5043 | errmsg = "truncated data"; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5044 | startinpos = ((const char *)q) - starts; |
| 5045 | endinpos = ((const char *)e) - starts; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5046 | } |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5047 | else { |
| 5048 | if (ch < 0x110000) { |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 5049 | if (_PyUnicodeWriter_WriteCharInline(&writer, ch) < 0) |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5050 | goto onError; |
| 5051 | q += 4; |
| 5052 | continue; |
| 5053 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5054 | errmsg = "codepoint not in range(0x110000)"; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5055 | startinpos = ((const char *)q) - starts; |
| 5056 | endinpos = startinpos + 4; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5057 | } |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5058 | |
| 5059 | /* The remaining input chars are ignored if the callback |
| 5060 | chooses to skip the input */ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5061 | if (unicode_decode_call_errorhandler_writer( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5062 | errors, &errorHandler, |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5063 | encoding, errmsg, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5064 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5065 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5066 | goto onError; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5067 | } |
| 5068 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5069 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5070 | *consumed = (const char *)q-starts; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5071 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5072 | Py_XDECREF(errorHandler); |
| 5073 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5074 | return _PyUnicodeWriter_Finish(&writer); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5075 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5076 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5077 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5078 | Py_XDECREF(errorHandler); |
| 5079 | Py_XDECREF(exc); |
| 5080 | return NULL; |
| 5081 | } |
| 5082 | |
| 5083 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5084 | _PyUnicode_EncodeUTF32(PyObject *str, |
| 5085 | const char *errors, |
| 5086 | int byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5087 | { |
Serhiy Storchaka | 3079328 | 2014-01-04 22:44:01 +0200 | [diff] [blame^] | 5088 | int kind; |
| 5089 | void *data; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5090 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5091 | PyObject *v; |
Serhiy Storchaka | 3079328 | 2014-01-04 22:44:01 +0200 | [diff] [blame^] | 5092 | unsigned char *p; |
| 5093 | Py_ssize_t nsize, i; |
| 5094 | /* Offsets from p for storing byte pairs in the right order. */ |
Christian Heimes | 743e0cd | 2012-10-17 23:52:17 +0200 | [diff] [blame] | 5095 | #if PY_LITTLE_ENDIAN |
Serhiy Storchaka | 3079328 | 2014-01-04 22:44:01 +0200 | [diff] [blame^] | 5096 | int iorder[] = {0, 1, 2, 3}; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5097 | #else |
Serhiy Storchaka | 3079328 | 2014-01-04 22:44:01 +0200 | [diff] [blame^] | 5098 | int iorder[] = {3, 2, 1, 0}; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5099 | #endif |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5100 | const char *encoding; |
| 5101 | PyObject *errorHandler = NULL; |
| 5102 | PyObject *exc = NULL; |
| 5103 | PyObject *rep = NULL; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5104 | |
Serhiy Storchaka | 3079328 | 2014-01-04 22:44:01 +0200 | [diff] [blame^] | 5105 | #define STORECHAR(CH) \ |
| 5106 | do { \ |
| 5107 | p[iorder[3]] = ((CH) >> 24) & 0xff; \ |
| 5108 | p[iorder[2]] = ((CH) >> 16) & 0xff; \ |
| 5109 | p[iorder[1]] = ((CH) >> 8) & 0xff; \ |
| 5110 | p[iorder[0]] = (CH) & 0xff; \ |
| 5111 | p += 4; \ |
| 5112 | } while(0) |
| 5113 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5114 | if (!PyUnicode_Check(str)) { |
| 5115 | PyErr_BadArgument(); |
| 5116 | return NULL; |
| 5117 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 5118 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5119 | return NULL; |
| 5120 | kind = PyUnicode_KIND(str); |
| 5121 | data = PyUnicode_DATA(str); |
| 5122 | len = PyUnicode_GET_LENGTH(str); |
| 5123 | |
Serhiy Storchaka | 583a939 | 2014-01-04 19:25:37 +0200 | [diff] [blame] | 5124 | nsize = len + (byteorder == 0); |
Serhiy Storchaka | 3079328 | 2014-01-04 22:44:01 +0200 | [diff] [blame^] | 5125 | if (nsize > PY_SSIZE_T_MAX / 4) |
| 5126 | return PyErr_NoMemory(); |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 5127 | v = PyBytes_FromStringAndSize(NULL, nsize * 4); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5128 | if (v == NULL) |
| 5129 | return NULL; |
| 5130 | |
Serhiy Storchaka | 3079328 | 2014-01-04 22:44:01 +0200 | [diff] [blame^] | 5131 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5132 | if (byteorder == 0) |
Serhiy Storchaka | 3079328 | 2014-01-04 22:44:01 +0200 | [diff] [blame^] | 5133 | STORECHAR(0xFEFF); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5134 | if (len == 0) |
Serhiy Storchaka | 3079328 | 2014-01-04 22:44:01 +0200 | [diff] [blame^] | 5135 | return v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5136 | |
Serhiy Storchaka | 3079328 | 2014-01-04 22:44:01 +0200 | [diff] [blame^] | 5137 | if (byteorder == -1) { |
| 5138 | /* force LE */ |
| 5139 | iorder[0] = 0; |
| 5140 | iorder[1] = 1; |
| 5141 | iorder[2] = 2; |
| 5142 | iorder[3] = 3; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5143 | encoding = "utf-32-le"; |
Serhiy Storchaka | 3079328 | 2014-01-04 22:44:01 +0200 | [diff] [blame^] | 5144 | } |
| 5145 | else if (byteorder == 1) { |
| 5146 | /* force BE */ |
| 5147 | iorder[0] = 3; |
| 5148 | iorder[1] = 2; |
| 5149 | iorder[2] = 1; |
| 5150 | iorder[3] = 0; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5151 | encoding = "utf-32-be"; |
Serhiy Storchaka | 3079328 | 2014-01-04 22:44:01 +0200 | [diff] [blame^] | 5152 | } |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5153 | else |
| 5154 | encoding = "utf-32"; |
| 5155 | |
| 5156 | if (kind == PyUnicode_1BYTE_KIND) { |
Serhiy Storchaka | 3079328 | 2014-01-04 22:44:01 +0200 | [diff] [blame^] | 5157 | for (i = 0; i < len; i++) |
| 5158 | STORECHAR(PyUnicode_READ(kind, data, i)); |
| 5159 | return v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5160 | } |
| 5161 | |
Serhiy Storchaka | 3079328 | 2014-01-04 22:44:01 +0200 | [diff] [blame^] | 5162 | for (i = 0; i < len;) { |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5163 | Py_ssize_t repsize, moreunits; |
Serhiy Storchaka | 3079328 | 2014-01-04 22:44:01 +0200 | [diff] [blame^] | 5164 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 5165 | i++; |
| 5166 | assert(ch <= MAX_UNICODE); |
| 5167 | if (!Py_UNICODE_IS_SURROGATE(ch)) { |
| 5168 | STORECHAR(ch); |
| 5169 | continue; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5170 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5171 | |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5172 | rep = unicode_encode_call_errorhandler( |
| 5173 | errors, &errorHandler, |
| 5174 | encoding, "surrogates not allowed", |
Serhiy Storchaka | 3079328 | 2014-01-04 22:44:01 +0200 | [diff] [blame^] | 5175 | str, &exc, i-1, i, &i); |
| 5176 | |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5177 | if (!rep) |
| 5178 | goto error; |
| 5179 | |
| 5180 | if (PyBytes_Check(rep)) { |
| 5181 | repsize = PyBytes_GET_SIZE(rep); |
| 5182 | if (repsize & 3) { |
| 5183 | raise_encode_exception(&exc, encoding, |
Serhiy Storchaka | 3079328 | 2014-01-04 22:44:01 +0200 | [diff] [blame^] | 5184 | str, i - 1, i, |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5185 | "surrogates not allowed"); |
| 5186 | goto error; |
| 5187 | } |
| 5188 | moreunits = repsize / 4; |
| 5189 | } |
| 5190 | else { |
| 5191 | assert(PyUnicode_Check(rep)); |
| 5192 | if (PyUnicode_READY(rep) < 0) |
| 5193 | goto error; |
| 5194 | moreunits = repsize = PyUnicode_GET_LENGTH(rep); |
| 5195 | if (!PyUnicode_IS_ASCII(rep)) { |
| 5196 | raise_encode_exception(&exc, encoding, |
Serhiy Storchaka | 3079328 | 2014-01-04 22:44:01 +0200 | [diff] [blame^] | 5197 | str, i - 1, i, |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5198 | "surrogates not allowed"); |
| 5199 | goto error; |
| 5200 | } |
| 5201 | } |
| 5202 | |
| 5203 | /* four bytes are reserved for each surrogate */ |
| 5204 | if (moreunits > 1) { |
Serhiy Storchaka | 3079328 | 2014-01-04 22:44:01 +0200 | [diff] [blame^] | 5205 | Py_ssize_t outpos = p - (unsigned char*) PyBytes_AS_STRING(v); |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5206 | Py_ssize_t morebytes = 4 * (moreunits - 1); |
| 5207 | if (PyBytes_GET_SIZE(v) > PY_SSIZE_T_MAX - morebytes) { |
| 5208 | /* integer overflow */ |
| 5209 | PyErr_NoMemory(); |
| 5210 | goto error; |
| 5211 | } |
| 5212 | if (_PyBytes_Resize(&v, PyBytes_GET_SIZE(v) + morebytes) < 0) |
| 5213 | goto error; |
Serhiy Storchaka | 3079328 | 2014-01-04 22:44:01 +0200 | [diff] [blame^] | 5214 | p = (unsigned char*) PyBytes_AS_STRING(v) + outpos; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5215 | } |
| 5216 | |
| 5217 | if (PyBytes_Check(rep)) { |
Serhiy Storchaka | 3079328 | 2014-01-04 22:44:01 +0200 | [diff] [blame^] | 5218 | Py_MEMCPY(p, PyBytes_AS_STRING(rep), repsize); |
| 5219 | p += repsize; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5220 | } else /* rep is unicode */ { |
Serhiy Storchaka | 3079328 | 2014-01-04 22:44:01 +0200 | [diff] [blame^] | 5221 | const Py_UCS1 *repdata; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5222 | assert(PyUnicode_KIND(rep) == PyUnicode_1BYTE_KIND); |
Serhiy Storchaka | 3079328 | 2014-01-04 22:44:01 +0200 | [diff] [blame^] | 5223 | repdata = PyUnicode_1BYTE_DATA(rep); |
| 5224 | while (repsize--) { |
| 5225 | Py_UCS4 ch = *repdata++; |
| 5226 | STORECHAR(ch); |
| 5227 | } |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5228 | } |
| 5229 | |
| 5230 | Py_CLEAR(rep); |
| 5231 | } |
| 5232 | |
| 5233 | /* Cut back to size actually needed. This is necessary for, for example, |
| 5234 | encoding of a string containing isolated surrogates and the 'ignore' |
| 5235 | handler is used. */ |
Serhiy Storchaka | 3079328 | 2014-01-04 22:44:01 +0200 | [diff] [blame^] | 5236 | nsize = p - (unsigned char*) PyBytes_AS_STRING(v); |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5237 | if (nsize != PyBytes_GET_SIZE(v)) |
| 5238 | _PyBytes_Resize(&v, nsize); |
| 5239 | Py_XDECREF(errorHandler); |
| 5240 | Py_XDECREF(exc); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5241 | return v; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5242 | error: |
| 5243 | Py_XDECREF(rep); |
| 5244 | Py_XDECREF(errorHandler); |
| 5245 | Py_XDECREF(exc); |
| 5246 | Py_XDECREF(v); |
| 5247 | return NULL; |
Serhiy Storchaka | 3079328 | 2014-01-04 22:44:01 +0200 | [diff] [blame^] | 5248 | #undef STORECHAR |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5249 | } |
| 5250 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5251 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5252 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
| 5253 | Py_ssize_t size, |
| 5254 | const char *errors, |
| 5255 | int byteorder) |
| 5256 | { |
| 5257 | PyObject *result; |
| 5258 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5259 | if (tmp == NULL) |
| 5260 | return NULL; |
| 5261 | result = _PyUnicode_EncodeUTF32(tmp, errors, byteorder); |
| 5262 | Py_DECREF(tmp); |
| 5263 | return result; |
| 5264 | } |
| 5265 | |
| 5266 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5267 | PyUnicode_AsUTF32String(PyObject *unicode) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5268 | { |
Victor Stinner | b960b34 | 2011-11-20 19:12:52 +0100 | [diff] [blame] | 5269 | return _PyUnicode_EncodeUTF32(unicode, NULL, 0); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5270 | } |
| 5271 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5272 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 5273 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5274 | PyObject * |
| 5275 | PyUnicode_DecodeUTF16(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5276 | Py_ssize_t size, |
| 5277 | const char *errors, |
| 5278 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5279 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5280 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 5281 | } |
| 5282 | |
| 5283 | PyObject * |
| 5284 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5285 | Py_ssize_t size, |
| 5286 | const char *errors, |
| 5287 | int *byteorder, |
| 5288 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5289 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5290 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5291 | Py_ssize_t startinpos; |
| 5292 | Py_ssize_t endinpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5293 | _PyUnicodeWriter writer; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5294 | const unsigned char *q, *e; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5295 | int bo = 0; /* assume native ordering by default */ |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5296 | int native_ordering; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 5297 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5298 | PyObject *errorHandler = NULL; |
| 5299 | PyObject *exc = NULL; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5300 | const char *encoding; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5301 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5302 | q = (unsigned char *)s; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5303 | e = q + size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5304 | |
| 5305 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5306 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5307 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5308 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 5309 | byte order setting accordingly. In native mode, the leading BOM |
| 5310 | mark is skipped, in all other modes, it is copied to the output |
| 5311 | stream as-is (giving a ZWNBSP character). */ |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5312 | if (bo == 0 && size >= 2) { |
| 5313 | const Py_UCS4 bom = (q[1] << 8) | q[0]; |
| 5314 | if (bom == 0xFEFF) { |
| 5315 | q += 2; |
| 5316 | bo = -1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5317 | } |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5318 | else if (bom == 0xFFFE) { |
| 5319 | q += 2; |
| 5320 | bo = 1; |
| 5321 | } |
| 5322 | if (byteorder) |
| 5323 | *byteorder = bo; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5324 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5325 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5326 | if (q == e) { |
| 5327 | if (consumed) |
| 5328 | *consumed = size; |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 5329 | _Py_RETURN_UNICODE_EMPTY(); |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5330 | } |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5331 | |
Christian Heimes | 743e0cd | 2012-10-17 23:52:17 +0200 | [diff] [blame] | 5332 | #if PY_LITTLE_ENDIAN |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5333 | native_ordering = bo <= 0; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5334 | encoding = bo <= 0 ? "utf-16-le" : "utf-16-be"; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5335 | #else |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5336 | native_ordering = bo >= 0; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5337 | encoding = bo >= 0 ? "utf-16-be" : "utf-16-le"; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5338 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5339 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5340 | /* Note: size will always be longer than the resulting Unicode |
| 5341 | character count */ |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 5342 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 5343 | writer.min_length = (e - q + 1) / 2; |
| 5344 | if (_PyUnicodeWriter_Prepare(&writer, writer.min_length, 127) == -1) |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5345 | goto onError; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5346 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5347 | while (1) { |
| 5348 | Py_UCS4 ch = 0; |
| 5349 | if (e - q >= 2) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5350 | int kind = writer.kind; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5351 | if (kind == PyUnicode_1BYTE_KIND) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5352 | if (PyUnicode_IS_ASCII(writer.buffer)) |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5353 | ch = asciilib_utf16_decode(&q, e, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5354 | (Py_UCS1*)writer.data, &writer.pos, |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5355 | native_ordering); |
| 5356 | else |
| 5357 | ch = ucs1lib_utf16_decode(&q, e, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5358 | (Py_UCS1*)writer.data, &writer.pos, |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5359 | native_ordering); |
| 5360 | } else if (kind == PyUnicode_2BYTE_KIND) { |
| 5361 | ch = ucs2lib_utf16_decode(&q, e, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5362 | (Py_UCS2*)writer.data, &writer.pos, |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5363 | native_ordering); |
| 5364 | } else { |
| 5365 | assert(kind == PyUnicode_4BYTE_KIND); |
| 5366 | ch = ucs4lib_utf16_decode(&q, e, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5367 | (Py_UCS4*)writer.data, &writer.pos, |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5368 | native_ordering); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5369 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5370 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5371 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5372 | switch (ch) |
| 5373 | { |
| 5374 | case 0: |
| 5375 | /* remaining byte at the end? (size should be even) */ |
| 5376 | if (q == e || consumed) |
| 5377 | goto End; |
| 5378 | errmsg = "truncated data"; |
| 5379 | startinpos = ((const char *)q) - starts; |
| 5380 | endinpos = ((const char *)e) - starts; |
| 5381 | break; |
| 5382 | /* The remaining input chars are ignored if the callback |
| 5383 | chooses to skip the input */ |
| 5384 | case 1: |
Serhiy Storchaka | 48e188e | 2013-01-08 23:14:24 +0200 | [diff] [blame] | 5385 | q -= 2; |
| 5386 | if (consumed) |
Serhiy Storchaka | ae3b32a | 2013-01-08 23:40:52 +0200 | [diff] [blame] | 5387 | goto End; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5388 | errmsg = "unexpected end of data"; |
Serhiy Storchaka | 48e188e | 2013-01-08 23:14:24 +0200 | [diff] [blame] | 5389 | startinpos = ((const char *)q) - starts; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5390 | endinpos = ((const char *)e) - starts; |
| 5391 | break; |
| 5392 | case 2: |
| 5393 | errmsg = "illegal encoding"; |
| 5394 | startinpos = ((const char *)q) - 2 - starts; |
| 5395 | endinpos = startinpos + 2; |
| 5396 | break; |
| 5397 | case 3: |
| 5398 | errmsg = "illegal UTF-16 surrogate"; |
| 5399 | startinpos = ((const char *)q) - 4 - starts; |
| 5400 | endinpos = startinpos + 2; |
| 5401 | break; |
| 5402 | default: |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 5403 | if (_PyUnicodeWriter_WriteCharInline(&writer, ch) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5404 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5405 | continue; |
| 5406 | } |
| 5407 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5408 | if (unicode_decode_call_errorhandler_writer( |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5409 | errors, |
| 5410 | &errorHandler, |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5411 | encoding, errmsg, |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5412 | &starts, |
| 5413 | (const char **)&e, |
| 5414 | &startinpos, |
| 5415 | &endinpos, |
| 5416 | &exc, |
| 5417 | (const char **)&q, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5418 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5419 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5420 | } |
| 5421 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5422 | End: |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5423 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5424 | *consumed = (const char *)q-starts; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5425 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5426 | Py_XDECREF(errorHandler); |
| 5427 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5428 | return _PyUnicodeWriter_Finish(&writer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5429 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5430 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5431 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5432 | Py_XDECREF(errorHandler); |
| 5433 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5434 | return NULL; |
| 5435 | } |
| 5436 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5437 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5438 | _PyUnicode_EncodeUTF16(PyObject *str, |
| 5439 | const char *errors, |
| 5440 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5441 | { |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5442 | enum PyUnicode_Kind kind; |
| 5443 | const void *data; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5444 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5445 | PyObject *v; |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5446 | unsigned short *out; |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5447 | Py_ssize_t pairs; |
Christian Heimes | 743e0cd | 2012-10-17 23:52:17 +0200 | [diff] [blame] | 5448 | #if PY_BIG_ENDIAN |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5449 | int native_ordering = byteorder >= 0; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5450 | #else |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5451 | int native_ordering = byteorder <= 0; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5452 | #endif |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5453 | const char *encoding; |
| 5454 | Py_ssize_t nsize, pos; |
| 5455 | PyObject *errorHandler = NULL; |
| 5456 | PyObject *exc = NULL; |
| 5457 | PyObject *rep = NULL; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5458 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5459 | if (!PyUnicode_Check(str)) { |
| 5460 | PyErr_BadArgument(); |
| 5461 | return NULL; |
| 5462 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 5463 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5464 | return NULL; |
| 5465 | kind = PyUnicode_KIND(str); |
| 5466 | data = PyUnicode_DATA(str); |
| 5467 | len = PyUnicode_GET_LENGTH(str); |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 5468 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5469 | pairs = 0; |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5470 | if (kind == PyUnicode_4BYTE_KIND) { |
| 5471 | const Py_UCS4 *in = (const Py_UCS4 *)data; |
| 5472 | const Py_UCS4 *end = in + len; |
| 5473 | while (in < end) |
| 5474 | if (*in++ >= 0x10000) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5475 | pairs++; |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5476 | } |
| 5477 | if (len > PY_SSIZE_T_MAX / 2 - pairs - (byteorder == 0)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5478 | return PyErr_NoMemory(); |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5479 | nsize = len + pairs + (byteorder == 0); |
| 5480 | v = PyBytes_FromStringAndSize(NULL, nsize * 2); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5481 | if (v == NULL) |
| 5482 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5483 | |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5484 | /* output buffer is 2-bytes aligned */ |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 5485 | assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(v), 2)); |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5486 | out = (unsigned short *)PyBytes_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5487 | if (byteorder == 0) |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5488 | *out++ = 0xFEFF; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5489 | if (len == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5490 | goto done; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5491 | |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5492 | if (kind == PyUnicode_1BYTE_KIND) { |
| 5493 | ucs1lib_utf16_encode((const Py_UCS1 *)data, len, &out, native_ordering); |
| 5494 | goto done; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5495 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5496 | |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5497 | if (byteorder < 0) |
| 5498 | encoding = "utf-16-le"; |
| 5499 | else if (byteorder > 0) |
| 5500 | encoding = "utf-16-be"; |
| 5501 | else |
| 5502 | encoding = "utf-16"; |
| 5503 | |
| 5504 | pos = 0; |
| 5505 | while (pos < len) { |
| 5506 | Py_ssize_t repsize, moreunits; |
| 5507 | |
| 5508 | if (kind == PyUnicode_2BYTE_KIND) { |
| 5509 | pos += ucs2lib_utf16_encode((const Py_UCS2 *)data + pos, len - pos, |
| 5510 | &out, native_ordering); |
| 5511 | } |
| 5512 | else { |
| 5513 | assert(kind == PyUnicode_4BYTE_KIND); |
| 5514 | pos += ucs4lib_utf16_encode((const Py_UCS4 *)data + pos, len - pos, |
| 5515 | &out, native_ordering); |
| 5516 | } |
| 5517 | if (pos == len) |
| 5518 | break; |
| 5519 | |
| 5520 | rep = unicode_encode_call_errorhandler( |
| 5521 | errors, &errorHandler, |
| 5522 | encoding, "surrogates not allowed", |
| 5523 | str, &exc, pos, pos + 1, &pos); |
| 5524 | if (!rep) |
| 5525 | goto error; |
| 5526 | |
| 5527 | if (PyBytes_Check(rep)) { |
| 5528 | repsize = PyBytes_GET_SIZE(rep); |
| 5529 | if (repsize & 1) { |
| 5530 | raise_encode_exception(&exc, encoding, |
| 5531 | str, pos - 1, pos, |
| 5532 | "surrogates not allowed"); |
| 5533 | goto error; |
| 5534 | } |
| 5535 | moreunits = repsize / 2; |
| 5536 | } |
| 5537 | else { |
| 5538 | assert(PyUnicode_Check(rep)); |
| 5539 | if (PyUnicode_READY(rep) < 0) |
| 5540 | goto error; |
| 5541 | moreunits = repsize = PyUnicode_GET_LENGTH(rep); |
| 5542 | if (!PyUnicode_IS_ASCII(rep)) { |
| 5543 | raise_encode_exception(&exc, encoding, |
| 5544 | str, pos - 1, pos, |
| 5545 | "surrogates not allowed"); |
| 5546 | goto error; |
| 5547 | } |
| 5548 | } |
| 5549 | |
| 5550 | /* two bytes are reserved for each surrogate */ |
| 5551 | if (moreunits > 1) { |
| 5552 | Py_ssize_t outpos = out - (unsigned short*) PyBytes_AS_STRING(v); |
| 5553 | Py_ssize_t morebytes = 2 * (moreunits - 1); |
| 5554 | if (PyBytes_GET_SIZE(v) > PY_SSIZE_T_MAX - morebytes) { |
| 5555 | /* integer overflow */ |
| 5556 | PyErr_NoMemory(); |
| 5557 | goto error; |
| 5558 | } |
| 5559 | if (_PyBytes_Resize(&v, PyBytes_GET_SIZE(v) + morebytes) < 0) |
| 5560 | goto error; |
| 5561 | out = (unsigned short*) PyBytes_AS_STRING(v) + outpos; |
| 5562 | } |
| 5563 | |
| 5564 | if (PyBytes_Check(rep)) { |
| 5565 | Py_MEMCPY(out, PyBytes_AS_STRING(rep), repsize); |
| 5566 | out += moreunits; |
| 5567 | } else /* rep is unicode */ { |
| 5568 | assert(PyUnicode_KIND(rep) == PyUnicode_1BYTE_KIND); |
| 5569 | ucs1lib_utf16_encode(PyUnicode_1BYTE_DATA(rep), repsize, |
| 5570 | &out, native_ordering); |
| 5571 | } |
| 5572 | |
| 5573 | Py_CLEAR(rep); |
| 5574 | } |
| 5575 | |
| 5576 | /* Cut back to size actually needed. This is necessary for, for example, |
| 5577 | encoding of a string containing isolated surrogates and the 'ignore' handler |
| 5578 | is used. */ |
| 5579 | nsize = (unsigned char*) out - (unsigned char*) PyBytes_AS_STRING(v); |
| 5580 | if (nsize != PyBytes_GET_SIZE(v)) |
| 5581 | _PyBytes_Resize(&v, nsize); |
| 5582 | Py_XDECREF(errorHandler); |
| 5583 | Py_XDECREF(exc); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5584 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5585 | return v; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5586 | error: |
| 5587 | Py_XDECREF(rep); |
| 5588 | Py_XDECREF(errorHandler); |
| 5589 | Py_XDECREF(exc); |
| 5590 | Py_XDECREF(v); |
| 5591 | return NULL; |
| 5592 | #undef STORECHAR |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5593 | } |
| 5594 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5595 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5596 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
| 5597 | Py_ssize_t size, |
| 5598 | const char *errors, |
| 5599 | int byteorder) |
| 5600 | { |
| 5601 | PyObject *result; |
| 5602 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5603 | if (tmp == NULL) |
| 5604 | return NULL; |
| 5605 | result = _PyUnicode_EncodeUTF16(tmp, errors, byteorder); |
| 5606 | Py_DECREF(tmp); |
| 5607 | return result; |
| 5608 | } |
| 5609 | |
| 5610 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5611 | PyUnicode_AsUTF16String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5612 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5613 | return _PyUnicode_EncodeUTF16(unicode, NULL, 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5614 | } |
| 5615 | |
| 5616 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 5617 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5618 | /* Helper function for PyUnicode_DecodeUnicodeEscape, determines |
| 5619 | if all the escapes in the string make it still a valid ASCII string. |
| 5620 | Returns -1 if any escapes were found which cause the string to |
| 5621 | pop out of ASCII range. Otherwise returns the length of the |
| 5622 | required buffer to hold the string. |
| 5623 | */ |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 5624 | static Py_ssize_t |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5625 | length_of_escaped_ascii_string(const char *s, Py_ssize_t size) |
| 5626 | { |
| 5627 | const unsigned char *p = (const unsigned char *)s; |
| 5628 | const unsigned char *end = p + size; |
| 5629 | Py_ssize_t length = 0; |
| 5630 | |
| 5631 | if (size < 0) |
| 5632 | return -1; |
| 5633 | |
| 5634 | for (; p < end; ++p) { |
| 5635 | if (*p > 127) { |
| 5636 | /* Non-ASCII */ |
| 5637 | return -1; |
| 5638 | } |
| 5639 | else if (*p != '\\') { |
| 5640 | /* Normal character */ |
| 5641 | ++length; |
| 5642 | } |
| 5643 | else { |
| 5644 | /* Backslash-escape, check next char */ |
| 5645 | ++p; |
| 5646 | /* Escape sequence reaches till end of string or |
| 5647 | non-ASCII follow-up. */ |
| 5648 | if (p >= end || *p > 127) |
| 5649 | return -1; |
| 5650 | switch (*p) { |
| 5651 | case '\n': |
| 5652 | /* backslash + \n result in zero characters */ |
| 5653 | break; |
| 5654 | case '\\': case '\'': case '\"': |
| 5655 | case 'b': case 'f': case 't': |
| 5656 | case 'n': case 'r': case 'v': case 'a': |
| 5657 | ++length; |
| 5658 | break; |
| 5659 | case '0': case '1': case '2': case '3': |
| 5660 | case '4': case '5': case '6': case '7': |
| 5661 | case 'x': case 'u': case 'U': case 'N': |
| 5662 | /* these do not guarantee ASCII characters */ |
| 5663 | return -1; |
| 5664 | default: |
| 5665 | /* count the backslash + the other character */ |
| 5666 | length += 2; |
| 5667 | } |
| 5668 | } |
| 5669 | } |
| 5670 | return length; |
| 5671 | } |
| 5672 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5673 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 5674 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5675 | PyObject * |
| 5676 | PyUnicode_DecodeUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5677 | Py_ssize_t size, |
Victor Stinner | c17f540 | 2011-09-29 00:16:58 +0200 | [diff] [blame] | 5678 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5679 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5680 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5681 | Py_ssize_t startinpos; |
| 5682 | Py_ssize_t endinpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5683 | _PyUnicodeWriter writer; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5684 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5685 | char* message; |
| 5686 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5687 | PyObject *errorHandler = NULL; |
| 5688 | PyObject *exc = NULL; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5689 | Py_ssize_t len; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5690 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5691 | len = length_of_escaped_ascii_string(s, size); |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 5692 | if (len == 0) |
| 5693 | _Py_RETURN_UNICODE_EMPTY(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5694 | |
| 5695 | /* After length_of_escaped_ascii_string() there are two alternatives, |
| 5696 | either the string is pure ASCII with named escapes like \n, etc. |
| 5697 | and we determined it's exact size (common case) |
| 5698 | or it contains \x, \u, ... escape sequences. then we create a |
| 5699 | legacy wchar string and resize it at the end of this function. */ |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 5700 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5701 | if (len > 0) { |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 5702 | writer.min_length = len; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5703 | } |
| 5704 | else { |
| 5705 | /* Escaped strings will always be longer than the resulting |
| 5706 | Unicode string, so we start with size here and then reduce the |
| 5707 | length after conversion to the true value. |
| 5708 | (but if the error callback returns a long replacement string |
| 5709 | we'll have to allocate more space) */ |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 5710 | writer.min_length = size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5711 | } |
| 5712 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5713 | if (size == 0) |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5714 | return _PyUnicodeWriter_Finish(&writer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5715 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5716 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5717 | while (s < end) { |
| 5718 | unsigned char c; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 5719 | Py_UCS4 x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5720 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5721 | |
| 5722 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 5723 | if (*s != '\\') { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5724 | x = (unsigned char)*s; |
| 5725 | s++; |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 5726 | if (_PyUnicodeWriter_WriteCharInline(&writer, x) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5727 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5728 | continue; |
| 5729 | } |
| 5730 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5731 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5732 | /* \ - Escapes */ |
| 5733 | s++; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5734 | c = *s++; |
| 5735 | if (s > end) |
| 5736 | c = '\0'; /* Invalid after \ */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5737 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5738 | switch (c) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5739 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5740 | /* \x escapes */ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5741 | #define WRITECHAR(ch) \ |
| 5742 | do { \ |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 5743 | if (_PyUnicodeWriter_WriteCharInline(&writer, (ch)) < 0) \ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5744 | goto onError; \ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5745 | } while(0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5746 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5747 | case '\n': break; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5748 | case '\\': WRITECHAR('\\'); break; |
| 5749 | case '\'': WRITECHAR('\''); break; |
| 5750 | case '\"': WRITECHAR('\"'); break; |
| 5751 | case 'b': WRITECHAR('\b'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5752 | /* FF */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5753 | case 'f': WRITECHAR('\014'); break; |
| 5754 | case 't': WRITECHAR('\t'); break; |
| 5755 | case 'n': WRITECHAR('\n'); break; |
| 5756 | case 'r': WRITECHAR('\r'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5757 | /* VT */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5758 | case 'v': WRITECHAR('\013'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5759 | /* BEL, not classic C */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5760 | case 'a': WRITECHAR('\007'); break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5761 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5762 | /* \OOO (octal) escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5763 | case '0': case '1': case '2': case '3': |
| 5764 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5765 | x = s[-1] - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5766 | if (s < end && '0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5767 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5768 | if (s < end && '0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5769 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5770 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5771 | WRITECHAR(x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5772 | break; |
| 5773 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5774 | /* hex escapes */ |
| 5775 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5776 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5777 | digits = 2; |
| 5778 | message = "truncated \\xXX escape"; |
| 5779 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5780 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5781 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5782 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5783 | digits = 4; |
| 5784 | message = "truncated \\uXXXX escape"; |
| 5785 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5786 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5787 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5788 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5789 | digits = 8; |
| 5790 | message = "truncated \\UXXXXXXXX escape"; |
| 5791 | hexescape: |
| 5792 | chr = 0; |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5793 | if (end - s < digits) { |
| 5794 | /* count only hex digits */ |
| 5795 | for (; s < end; ++s) { |
| 5796 | c = (unsigned char)*s; |
| 5797 | if (!Py_ISXDIGIT(c)) |
| 5798 | goto error; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5799 | } |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5800 | goto error; |
| 5801 | } |
| 5802 | for (; digits--; ++s) { |
| 5803 | c = (unsigned char)*s; |
| 5804 | if (!Py_ISXDIGIT(c)) |
| 5805 | goto error; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5806 | chr = (chr<<4) & ~0xF; |
| 5807 | if (c >= '0' && c <= '9') |
| 5808 | chr += c - '0'; |
| 5809 | else if (c >= 'a' && c <= 'f') |
| 5810 | chr += 10 + c - 'a'; |
| 5811 | else |
| 5812 | chr += 10 + c - 'A'; |
| 5813 | } |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 5814 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5815 | /* _decoding_error will have already written into the |
| 5816 | target buffer. */ |
| 5817 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5818 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5819 | /* when we get here, chr is a 32-bit unicode character */ |
Serhiy Storchaka | 24193de | 2013-01-29 10:28:07 +0200 | [diff] [blame] | 5820 | message = "illegal Unicode character"; |
| 5821 | if (chr > MAX_UNICODE) |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5822 | goto error; |
Serhiy Storchaka | 24193de | 2013-01-29 10:28:07 +0200 | [diff] [blame] | 5823 | WRITECHAR(chr); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5824 | break; |
| 5825 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5826 | /* \N{name} */ |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5827 | case 'N': |
| 5828 | message = "malformed \\N character escape"; |
| 5829 | if (ucnhash_CAPI == NULL) { |
| 5830 | /* load the unicode data module */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5831 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCapsule_Import( |
| 5832 | PyUnicodeData_CAPSULE_NAME, 1); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5833 | if (ucnhash_CAPI == NULL) |
| 5834 | goto ucnhashError; |
| 5835 | } |
| 5836 | if (*s == '{') { |
| 5837 | const char *start = s+1; |
| 5838 | /* look for the closing brace */ |
| 5839 | while (*s != '}' && s < end) |
| 5840 | s++; |
| 5841 | if (s > start && s < end && *s == '}') { |
| 5842 | /* found a name. look it up in the unicode database */ |
| 5843 | message = "unknown Unicode character name"; |
| 5844 | s++; |
Serhiy Storchaka | 4f5f0e5 | 2013-01-21 11:38:00 +0200 | [diff] [blame] | 5845 | if (s - start - 1 <= INT_MAX && |
Serhiy Storchaka | c35f3a9 | 2013-01-21 11:42:57 +0200 | [diff] [blame] | 5846 | ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), |
Ezio Melotti | 931b8aa | 2011-10-21 21:57:36 +0300 | [diff] [blame] | 5847 | &chr, 0)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5848 | goto store; |
| 5849 | } |
| 5850 | } |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5851 | goto error; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5852 | |
| 5853 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5854 | if (s > end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5855 | message = "\\ at end of string"; |
| 5856 | s--; |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5857 | goto error; |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5858 | } |
| 5859 | else { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5860 | WRITECHAR('\\'); |
Serhiy Storchaka | 73e3880 | 2013-01-25 23:52:21 +0200 | [diff] [blame] | 5861 | WRITECHAR((unsigned char)s[-1]); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5862 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5863 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5864 | } |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5865 | continue; |
| 5866 | |
| 5867 | error: |
| 5868 | endinpos = s-starts; |
Serhiy Storchaka | 8fe5a9f | 2013-01-29 10:37:39 +0200 | [diff] [blame] | 5869 | if (unicode_decode_call_errorhandler_writer( |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5870 | errors, &errorHandler, |
| 5871 | "unicodeescape", message, |
| 5872 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Serhiy Storchaka | 8fe5a9f | 2013-01-29 10:37:39 +0200 | [diff] [blame] | 5873 | &writer)) |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5874 | goto onError; |
| 5875 | continue; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5876 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5877 | #undef WRITECHAR |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5878 | |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 5879 | Py_XDECREF(errorHandler); |
| 5880 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5881 | return _PyUnicodeWriter_Finish(&writer); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5882 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5883 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5884 | PyErr_SetString( |
| 5885 | PyExc_UnicodeError, |
| 5886 | "\\N escapes not supported (can't load unicodedata module)" |
| 5887 | ); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5888 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5889 | Py_XDECREF(errorHandler); |
| 5890 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 5891 | return NULL; |
| 5892 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5893 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5894 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5895 | Py_XDECREF(errorHandler); |
| 5896 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5897 | return NULL; |
| 5898 | } |
| 5899 | |
| 5900 | /* Return a Unicode-Escape string version of the Unicode object. |
| 5901 | |
| 5902 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 5903 | appropriate. |
| 5904 | |
| 5905 | */ |
| 5906 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5907 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5908 | PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5909 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5910 | Py_ssize_t i, len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5911 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5912 | char *p; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5913 | int kind; |
| 5914 | void *data; |
| 5915 | Py_ssize_t expandsize = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5916 | |
Ezio Melotti | e7f9037 | 2012-10-05 03:33:31 +0300 | [diff] [blame] | 5917 | /* Initial allocation is based on the longest-possible character |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5918 | escape. |
| 5919 | |
Ezio Melotti | e7f9037 | 2012-10-05 03:33:31 +0300 | [diff] [blame] | 5920 | For UCS1 strings it's '\xxx', 4 bytes per source character. |
| 5921 | For UCS2 strings it's '\uxxxx', 6 bytes per source character. |
| 5922 | For UCS4 strings it's '\U00xxxxxx', 10 bytes per source character. |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5923 | */ |
| 5924 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5925 | if (!PyUnicode_Check(unicode)) { |
| 5926 | PyErr_BadArgument(); |
| 5927 | return NULL; |
| 5928 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 5929 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5930 | return NULL; |
| 5931 | len = PyUnicode_GET_LENGTH(unicode); |
| 5932 | kind = PyUnicode_KIND(unicode); |
| 5933 | data = PyUnicode_DATA(unicode); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 5934 | switch (kind) { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5935 | case PyUnicode_1BYTE_KIND: expandsize = 4; break; |
| 5936 | case PyUnicode_2BYTE_KIND: expandsize = 6; break; |
| 5937 | case PyUnicode_4BYTE_KIND: expandsize = 10; break; |
| 5938 | } |
| 5939 | |
| 5940 | if (len == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5941 | return PyBytes_FromStringAndSize(NULL, 0); |
| 5942 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5943 | if (len > (PY_SSIZE_T_MAX - 2 - 1) / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5944 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5945 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5946 | repr = PyBytes_FromStringAndSize(NULL, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5947 | 2 |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5948 | + expandsize*len |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5949 | + 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5950 | if (repr == NULL) |
| 5951 | return NULL; |
| 5952 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5953 | p = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5954 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5955 | for (i = 0; i < len; i++) { |
Victor Stinner | 3326cb6 | 2011-11-10 20:15:25 +0100 | [diff] [blame] | 5956 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5957 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5958 | /* Escape backslashes */ |
| 5959 | if (ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5960 | *p++ = '\\'; |
| 5961 | *p++ = (char) ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5962 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5963 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5964 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5965 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 5966 | else if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 5967 | assert(ch <= MAX_UNICODE); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5968 | *p++ = '\\'; |
| 5969 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 5970 | *p++ = Py_hexdigits[(ch >> 28) & 0x0000000F]; |
| 5971 | *p++ = Py_hexdigits[(ch >> 24) & 0x0000000F]; |
| 5972 | *p++ = Py_hexdigits[(ch >> 20) & 0x0000000F]; |
| 5973 | *p++ = Py_hexdigits[(ch >> 16) & 0x0000000F]; |
| 5974 | *p++ = Py_hexdigits[(ch >> 12) & 0x0000000F]; |
| 5975 | *p++ = Py_hexdigits[(ch >> 8) & 0x0000000F]; |
| 5976 | *p++ = Py_hexdigits[(ch >> 4) & 0x0000000F]; |
| 5977 | *p++ = Py_hexdigits[ch & 0x0000000F]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5978 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5979 | } |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5980 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5981 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5982 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5983 | *p++ = '\\'; |
| 5984 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 5985 | *p++ = Py_hexdigits[(ch >> 12) & 0x000F]; |
| 5986 | *p++ = Py_hexdigits[(ch >> 8) & 0x000F]; |
| 5987 | *p++ = Py_hexdigits[(ch >> 4) & 0x000F]; |
| 5988 | *p++ = Py_hexdigits[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5989 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5990 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5991 | /* Map special whitespace to '\t', \n', '\r' */ |
| 5992 | else if (ch == '\t') { |
| 5993 | *p++ = '\\'; |
| 5994 | *p++ = 't'; |
| 5995 | } |
| 5996 | else if (ch == '\n') { |
| 5997 | *p++ = '\\'; |
| 5998 | *p++ = 'n'; |
| 5999 | } |
| 6000 | else if (ch == '\r') { |
| 6001 | *p++ = '\\'; |
| 6002 | *p++ = 'r'; |
| 6003 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 6004 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 6005 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 6006 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6007 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 6008 | *p++ = 'x'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6009 | *p++ = Py_hexdigits[(ch >> 4) & 0x000F]; |
| 6010 | *p++ = Py_hexdigits[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6011 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 6012 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6013 | /* Copy everything else as-is */ |
| 6014 | else |
| 6015 | *p++ = (char) ch; |
| 6016 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6017 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6018 | assert(p - PyBytes_AS_STRING(repr) > 0); |
| 6019 | if (_PyBytes_Resize(&repr, p - PyBytes_AS_STRING(repr)) < 0) |
| 6020 | return NULL; |
| 6021 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6022 | } |
| 6023 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6024 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6025 | PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
| 6026 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6027 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6028 | PyObject *result; |
| 6029 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 6030 | if (tmp == NULL) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6031 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6032 | result = PyUnicode_AsUnicodeEscapeString(tmp); |
| 6033 | Py_DECREF(tmp); |
| 6034 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6035 | } |
| 6036 | |
| 6037 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 6038 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6039 | PyObject * |
| 6040 | PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6041 | Py_ssize_t size, |
| 6042 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6043 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6044 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6045 | Py_ssize_t startinpos; |
| 6046 | Py_ssize_t endinpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6047 | _PyUnicodeWriter writer; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6048 | const char *end; |
| 6049 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6050 | PyObject *errorHandler = NULL; |
| 6051 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6052 | |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 6053 | if (size == 0) |
| 6054 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6055 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6056 | /* Escaped strings will always be longer than the resulting |
| 6057 | 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] | 6058 | length after conversion to the true value. (But decoding error |
| 6059 | handler might have to resize the string) */ |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 6060 | _PyUnicodeWriter_Init(&writer); |
| 6061 | writer.min_length = size; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6062 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6063 | end = s + size; |
| 6064 | while (s < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6065 | unsigned char c; |
| 6066 | Py_UCS4 x; |
| 6067 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6068 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6069 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6070 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 6071 | if (*s != '\\') { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6072 | x = (unsigned char)*s++; |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 6073 | if (_PyUnicodeWriter_WriteCharInline(&writer, x) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6074 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6075 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6076 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6077 | startinpos = s-starts; |
| 6078 | |
| 6079 | /* \u-escapes are only interpreted iff the number of leading |
| 6080 | backslashes if odd */ |
| 6081 | bs = s; |
| 6082 | for (;s < end;) { |
| 6083 | if (*s != '\\') |
| 6084 | break; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6085 | x = (unsigned char)*s++; |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 6086 | if (_PyUnicodeWriter_WriteCharInline(&writer, x) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6087 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6088 | } |
| 6089 | if (((s - bs) & 1) == 0 || |
| 6090 | s >= end || |
| 6091 | (*s != 'u' && *s != 'U')) { |
| 6092 | continue; |
| 6093 | } |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6094 | writer.pos--; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6095 | count = *s=='u' ? 4 : 8; |
| 6096 | s++; |
| 6097 | |
| 6098 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6099 | for (x = 0, i = 0; i < count; ++i, ++s) { |
| 6100 | c = (unsigned char)*s; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 6101 | if (!Py_ISXDIGIT(c)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6102 | endinpos = s-starts; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6103 | if (unicode_decode_call_errorhandler_writer( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6104 | errors, &errorHandler, |
| 6105 | "rawunicodeescape", "truncated \\uXXXX", |
| 6106 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6107 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6108 | goto onError; |
| 6109 | goto nextByte; |
| 6110 | } |
| 6111 | x = (x<<4) & ~0xF; |
| 6112 | if (c >= '0' && c <= '9') |
| 6113 | x += c - '0'; |
| 6114 | else if (c >= 'a' && c <= 'f') |
| 6115 | x += 10 + c - 'a'; |
| 6116 | else |
| 6117 | x += 10 + c - 'A'; |
| 6118 | } |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 6119 | if (x <= MAX_UNICODE) { |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 6120 | if (_PyUnicodeWriter_WriteCharInline(&writer, x) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6121 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6122 | } |
| 6123 | else { |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 6124 | endinpos = s-starts; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6125 | if (unicode_decode_call_errorhandler_writer( |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6126 | errors, &errorHandler, |
| 6127 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6128 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6129 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6130 | goto onError; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6131 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6132 | nextByte: |
| 6133 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6134 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6135 | Py_XDECREF(errorHandler); |
| 6136 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6137 | return _PyUnicodeWriter_Finish(&writer); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6138 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6139 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6140 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6141 | Py_XDECREF(errorHandler); |
| 6142 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6143 | return NULL; |
| 6144 | } |
| 6145 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6146 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6147 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6148 | PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6149 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6150 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6151 | char *p; |
| 6152 | char *q; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6153 | Py_ssize_t expandsize, pos; |
| 6154 | int kind; |
| 6155 | void *data; |
| 6156 | Py_ssize_t len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6157 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6158 | if (!PyUnicode_Check(unicode)) { |
| 6159 | PyErr_BadArgument(); |
| 6160 | return NULL; |
| 6161 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6162 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6163 | return NULL; |
| 6164 | kind = PyUnicode_KIND(unicode); |
| 6165 | data = PyUnicode_DATA(unicode); |
| 6166 | len = PyUnicode_GET_LENGTH(unicode); |
Benjamin Peterson | 1518e87 | 2011-11-23 10:44:52 -0600 | [diff] [blame] | 6167 | /* 4 byte characters can take up 10 bytes, 2 byte characters can take up 6 |
| 6168 | bytes, and 1 byte characters 4. */ |
| 6169 | expandsize = kind * 2 + 2; |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 6170 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6171 | if (len > PY_SSIZE_T_MAX / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6172 | return PyErr_NoMemory(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6173 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6174 | repr = PyBytes_FromStringAndSize(NULL, expandsize * len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6175 | if (repr == NULL) |
| 6176 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6177 | if (len == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6178 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6179 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6180 | p = q = PyBytes_AS_STRING(repr); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6181 | for (pos = 0; pos < len; pos++) { |
| 6182 | Py_UCS4 ch = PyUnicode_READ(kind, data, pos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6183 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 6184 | if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 6185 | assert(ch <= MAX_UNICODE); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6186 | *p++ = '\\'; |
| 6187 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6188 | *p++ = Py_hexdigits[(ch >> 28) & 0xf]; |
| 6189 | *p++ = Py_hexdigits[(ch >> 24) & 0xf]; |
| 6190 | *p++ = Py_hexdigits[(ch >> 20) & 0xf]; |
| 6191 | *p++ = Py_hexdigits[(ch >> 16) & 0xf]; |
| 6192 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 6193 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 6194 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 6195 | *p++ = Py_hexdigits[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6196 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6197 | /* Map 16-bit characters to '\uxxxx' */ |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6198 | else if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6199 | *p++ = '\\'; |
| 6200 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6201 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 6202 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 6203 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 6204 | *p++ = Py_hexdigits[ch & 15]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6205 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6206 | /* Copy everything else as-is */ |
| 6207 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6208 | *p++ = (char) ch; |
| 6209 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6210 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6211 | assert(p > q); |
| 6212 | if (_PyBytes_Resize(&repr, p - q) < 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6213 | return NULL; |
| 6214 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6215 | } |
| 6216 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6217 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6218 | PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
| 6219 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6220 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6221 | PyObject *result; |
| 6222 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 6223 | if (tmp == NULL) |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 6224 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6225 | result = PyUnicode_AsRawUnicodeEscapeString(tmp); |
| 6226 | Py_DECREF(tmp); |
| 6227 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6228 | } |
| 6229 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6230 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 6231 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6232 | PyObject * |
| 6233 | _PyUnicode_DecodeUnicodeInternal(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6234 | Py_ssize_t size, |
| 6235 | const char *errors) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6236 | { |
| 6237 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6238 | Py_ssize_t startinpos; |
| 6239 | Py_ssize_t endinpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6240 | _PyUnicodeWriter writer; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6241 | const char *end; |
| 6242 | const char *reason; |
| 6243 | PyObject *errorHandler = NULL; |
| 6244 | PyObject *exc = NULL; |
| 6245 | |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6246 | if (PyErr_WarnEx(PyExc_DeprecationWarning, |
Ezio Melotti | 11060a4 | 2011-11-16 09:39:10 +0200 | [diff] [blame] | 6247 | "unicode_internal codec has been deprecated", |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6248 | 1)) |
| 6249 | return NULL; |
| 6250 | |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 6251 | if (size == 0) |
| 6252 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6253 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 6254 | _PyUnicodeWriter_Init(&writer); |
| 6255 | if (size / Py_UNICODE_SIZE > PY_SSIZE_T_MAX - 1) { |
| 6256 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6257 | goto onError; |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 6258 | } |
| 6259 | writer.min_length = (size + (Py_UNICODE_SIZE - 1)) / Py_UNICODE_SIZE; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6260 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 6261 | end = s + size; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6262 | while (s < end) { |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6263 | Py_UNICODE uch; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6264 | Py_UCS4 ch; |
Serhiy Storchaka | 03ee12e | 2013-02-07 16:25:25 +0200 | [diff] [blame] | 6265 | if (end - s < Py_UNICODE_SIZE) { |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6266 | endinpos = end-starts; |
| 6267 | reason = "truncated input"; |
| 6268 | goto error; |
| 6269 | } |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6270 | /* We copy the raw representation one byte at a time because the |
| 6271 | pointer may be unaligned (see test_codeccallbacks). */ |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6272 | ((char *) &uch)[0] = s[0]; |
| 6273 | ((char *) &uch)[1] = s[1]; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6274 | #ifdef Py_UNICODE_WIDE |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6275 | ((char *) &uch)[2] = s[2]; |
| 6276 | ((char *) &uch)[3] = s[3]; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6277 | #endif |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6278 | ch = uch; |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6279 | #ifdef Py_UNICODE_WIDE |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6280 | /* We have to sanity check the raw data, otherwise doom looms for |
| 6281 | some malformed UCS-4 data. */ |
Serhiy Storchaka | 03ee12e | 2013-02-07 16:25:25 +0200 | [diff] [blame] | 6282 | if (ch > 0x10ffff) { |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6283 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 6284 | reason = "illegal code point (> 0x10FFFF)"; |
| 6285 | goto error; |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6286 | } |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6287 | #endif |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6288 | s += Py_UNICODE_SIZE; |
| 6289 | #ifndef Py_UNICODE_WIDE |
Serhiy Storchaka | 03ee12e | 2013-02-07 16:25:25 +0200 | [diff] [blame] | 6290 | if (Py_UNICODE_IS_HIGH_SURROGATE(ch) && end - s >= Py_UNICODE_SIZE) |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6291 | { |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6292 | Py_UNICODE uch2; |
| 6293 | ((char *) &uch2)[0] = s[0]; |
| 6294 | ((char *) &uch2)[1] = s[1]; |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 6295 | if (Py_UNICODE_IS_LOW_SURROGATE(uch2)) |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6296 | { |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 6297 | ch = Py_UNICODE_JOIN_SURROGATES(uch, uch2); |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6298 | s += Py_UNICODE_SIZE; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6299 | } |
| 6300 | } |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6301 | #endif |
| 6302 | |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 6303 | if (_PyUnicodeWriter_WriteCharInline(&writer, ch) < 0) |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6304 | goto onError; |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6305 | continue; |
| 6306 | |
| 6307 | error: |
| 6308 | startinpos = s - starts; |
Serhiy Storchaka | d0c79dc | 2013-02-07 16:26:55 +0200 | [diff] [blame] | 6309 | if (unicode_decode_call_errorhandler_writer( |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6310 | errors, &errorHandler, |
| 6311 | "unicode_internal", reason, |
| 6312 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Serhiy Storchaka | d0c79dc | 2013-02-07 16:26:55 +0200 | [diff] [blame] | 6313 | &writer)) |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6314 | goto onError; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6315 | } |
| 6316 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6317 | Py_XDECREF(errorHandler); |
| 6318 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6319 | return _PyUnicodeWriter_Finish(&writer); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6320 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6321 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6322 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6323 | Py_XDECREF(errorHandler); |
| 6324 | Py_XDECREF(exc); |
| 6325 | return NULL; |
| 6326 | } |
| 6327 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6328 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 6329 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6330 | PyObject * |
| 6331 | PyUnicode_DecodeLatin1(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6332 | Py_ssize_t size, |
| 6333 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6334 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6335 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 6336 | return _PyUnicode_FromUCS1((unsigned char*)s, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6337 | } |
| 6338 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6339 | /* create or adjust a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6340 | static void |
| 6341 | make_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6342 | const char *encoding, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6343 | PyObject *unicode, |
| 6344 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6345 | const char *reason) |
| 6346 | { |
| 6347 | if (*exceptionObject == NULL) { |
| 6348 | *exceptionObject = PyObject_CallFunction( |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6349 | PyExc_UnicodeEncodeError, "sOnns", |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6350 | encoding, unicode, startpos, endpos, reason); |
| 6351 | } |
| 6352 | else { |
| 6353 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 6354 | goto onError; |
| 6355 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 6356 | goto onError; |
| 6357 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 6358 | goto onError; |
| 6359 | return; |
| 6360 | onError: |
| 6361 | Py_DECREF(*exceptionObject); |
| 6362 | *exceptionObject = NULL; |
| 6363 | } |
| 6364 | } |
| 6365 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6366 | /* raises a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6367 | static void |
| 6368 | raise_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6369 | const char *encoding, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6370 | PyObject *unicode, |
| 6371 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6372 | const char *reason) |
| 6373 | { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6374 | make_encode_exception(exceptionObject, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6375 | encoding, unicode, startpos, endpos, reason); |
| 6376 | if (*exceptionObject != NULL) |
| 6377 | PyCodec_StrictErrors(*exceptionObject); |
| 6378 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6379 | |
| 6380 | /* error handling callback helper: |
| 6381 | build arguments, call the callback and check the arguments, |
| 6382 | put the result into newpos and return the replacement string, which |
| 6383 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6384 | static PyObject * |
| 6385 | unicode_encode_call_errorhandler(const char *errors, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6386 | PyObject **errorHandler, |
| 6387 | const char *encoding, const char *reason, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6388 | PyObject *unicode, PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6389 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6390 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6391 | { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6392 | 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] | 6393 | Py_ssize_t len; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6394 | PyObject *restuple; |
| 6395 | PyObject *resunicode; |
| 6396 | |
| 6397 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6398 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6399 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6400 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6401 | } |
| 6402 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6403 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6404 | return NULL; |
| 6405 | len = PyUnicode_GET_LENGTH(unicode); |
| 6406 | |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6407 | make_encode_exception(exceptionObject, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6408 | encoding, unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6409 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6410 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6411 | |
| 6412 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6413 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6414 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6415 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6416 | if (!PyTuple_Check(restuple)) { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6417 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6418 | Py_DECREF(restuple); |
| 6419 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6420 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6421 | if (!PyArg_ParseTuple(restuple, argparse, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6422 | &resunicode, newpos)) { |
| 6423 | Py_DECREF(restuple); |
| 6424 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6425 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6426 | if (!PyUnicode_Check(resunicode) && !PyBytes_Check(resunicode)) { |
| 6427 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
| 6428 | Py_DECREF(restuple); |
| 6429 | return NULL; |
| 6430 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6431 | if (*newpos<0) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6432 | *newpos = len + *newpos; |
| 6433 | if (*newpos<0 || *newpos>len) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6434 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 6435 | Py_DECREF(restuple); |
| 6436 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 6437 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6438 | Py_INCREF(resunicode); |
| 6439 | Py_DECREF(restuple); |
| 6440 | return resunicode; |
| 6441 | } |
| 6442 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6443 | static PyObject * |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6444 | unicode_encode_ucs1(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6445 | const char *errors, |
Victor Stinner | fcd9653 | 2011-11-04 00:28:50 +0100 | [diff] [blame] | 6446 | unsigned int limit) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6447 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6448 | /* input state */ |
| 6449 | Py_ssize_t pos=0, size; |
| 6450 | int kind; |
| 6451 | void *data; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6452 | /* output object */ |
| 6453 | PyObject *res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6454 | /* pointer into the output */ |
| 6455 | char *str; |
| 6456 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6457 | Py_ssize_t ressize; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6458 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 6459 | 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] | 6460 | PyObject *errorHandler = NULL; |
| 6461 | PyObject *exc = NULL; |
| 6462 | /* the following variable is used for caching string comparisons |
| 6463 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 6464 | int known_errorHandler = -1; |
| 6465 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6466 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6467 | return NULL; |
| 6468 | size = PyUnicode_GET_LENGTH(unicode); |
| 6469 | kind = PyUnicode_KIND(unicode); |
| 6470 | data = PyUnicode_DATA(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6471 | /* allocate enough for a simple encoding without |
| 6472 | replacements, if we need more, we'll resize */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6473 | if (size == 0) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6474 | return PyBytes_FromStringAndSize(NULL, 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6475 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6476 | if (res == NULL) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6477 | return NULL; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6478 | str = PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6479 | ressize = size; |
| 6480 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6481 | while (pos < size) { |
| 6482 | Py_UCS4 c = PyUnicode_READ(kind, data, pos); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6483 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6484 | /* can we encode this? */ |
| 6485 | if (c<limit) { |
| 6486 | /* no overflow check, because we know that the space is enough */ |
| 6487 | *str++ = (char)c; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6488 | ++pos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6489 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6490 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6491 | Py_ssize_t requiredsize; |
| 6492 | PyObject *repunicode; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6493 | Py_ssize_t repsize, newpos, respos, i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6494 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6495 | Py_ssize_t collstart = pos; |
| 6496 | Py_ssize_t collend = pos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6497 | /* find all unecodable characters */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6498 | while ((collend < size) && (PyUnicode_READ(kind, data, collend)>=limit)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6499 | ++collend; |
| 6500 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 6501 | if (known_errorHandler==-1) { |
| 6502 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 6503 | known_errorHandler = 1; |
| 6504 | else if (!strcmp(errors, "replace")) |
| 6505 | known_errorHandler = 2; |
| 6506 | else if (!strcmp(errors, "ignore")) |
| 6507 | known_errorHandler = 3; |
| 6508 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 6509 | known_errorHandler = 4; |
| 6510 | else |
| 6511 | known_errorHandler = 0; |
| 6512 | } |
| 6513 | switch (known_errorHandler) { |
| 6514 | case 1: /* strict */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6515 | raise_encode_exception(&exc, encoding, unicode, collstart, collend, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6516 | goto onError; |
| 6517 | case 2: /* replace */ |
| 6518 | while (collstart++<collend) |
| 6519 | *str++ = '?'; /* fall through */ |
| 6520 | case 3: /* ignore */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6521 | pos = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6522 | break; |
| 6523 | case 4: /* xmlcharrefreplace */ |
| 6524 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6525 | /* determine replacement size */ |
| 6526 | for (i = collstart, repsize = 0; i < collend; ++i) { |
| 6527 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 6528 | if (ch < 10) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6529 | repsize += 2+1+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6530 | else if (ch < 100) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6531 | repsize += 2+2+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6532 | else if (ch < 1000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6533 | repsize += 2+3+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6534 | else if (ch < 10000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6535 | repsize += 2+4+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6536 | else if (ch < 100000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6537 | repsize += 2+5+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6538 | else if (ch < 1000000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6539 | repsize += 2+6+1; |
Victor Stinner | 0d3721d | 2011-11-22 03:27:53 +0100 | [diff] [blame] | 6540 | else { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 6541 | assert(ch <= MAX_UNICODE); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6542 | repsize += 2+7+1; |
Victor Stinner | 0d3721d | 2011-11-22 03:27:53 +0100 | [diff] [blame] | 6543 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6544 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6545 | requiredsize = respos+repsize+(size-collend); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6546 | if (requiredsize > ressize) { |
| 6547 | if (requiredsize<2*ressize) |
| 6548 | requiredsize = 2*ressize; |
| 6549 | if (_PyBytes_Resize(&res, requiredsize)) |
| 6550 | goto onError; |
| 6551 | str = PyBytes_AS_STRING(res) + respos; |
| 6552 | ressize = requiredsize; |
| 6553 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6554 | /* generate replacement */ |
| 6555 | for (i = collstart; i < collend; ++i) { |
| 6556 | str += sprintf(str, "&#%d;", PyUnicode_READ(kind, data, i)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6557 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6558 | pos = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6559 | break; |
| 6560 | default: |
| 6561 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6562 | encoding, reason, unicode, &exc, |
| 6563 | collstart, collend, &newpos); |
| 6564 | if (repunicode == NULL || (PyUnicode_Check(repunicode) && |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6565 | PyUnicode_READY(repunicode) == -1)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6566 | goto onError; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6567 | if (PyBytes_Check(repunicode)) { |
| 6568 | /* Directly copy bytes result to output. */ |
| 6569 | repsize = PyBytes_Size(repunicode); |
| 6570 | if (repsize > 1) { |
| 6571 | /* Make room for all additional bytes. */ |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6572 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6573 | if (_PyBytes_Resize(&res, ressize+repsize-1)) { |
| 6574 | Py_DECREF(repunicode); |
| 6575 | goto onError; |
| 6576 | } |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6577 | str = PyBytes_AS_STRING(res) + respos; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6578 | ressize += repsize-1; |
| 6579 | } |
| 6580 | memcpy(str, PyBytes_AsString(repunicode), repsize); |
| 6581 | str += repsize; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6582 | pos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6583 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6584 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6585 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6586 | /* need more space? (at least enough for what we |
| 6587 | have+the replacement+the rest of the string, so |
| 6588 | we won't have to check space for encodable characters) */ |
| 6589 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6590 | repsize = PyUnicode_GET_LENGTH(repunicode); |
| 6591 | requiredsize = respos+repsize+(size-collend); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6592 | if (requiredsize > ressize) { |
| 6593 | if (requiredsize<2*ressize) |
| 6594 | requiredsize = 2*ressize; |
| 6595 | if (_PyBytes_Resize(&res, requiredsize)) { |
| 6596 | Py_DECREF(repunicode); |
| 6597 | goto onError; |
| 6598 | } |
| 6599 | str = PyBytes_AS_STRING(res) + respos; |
| 6600 | ressize = requiredsize; |
| 6601 | } |
| 6602 | /* check if there is anything unencodable in the replacement |
| 6603 | and copy it to the output */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6604 | for (i = 0; repsize-->0; ++i, ++str) { |
| 6605 | c = PyUnicode_READ_CHAR(repunicode, i); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6606 | if (c >= limit) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6607 | raise_encode_exception(&exc, encoding, unicode, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6608 | pos, pos+1, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6609 | Py_DECREF(repunicode); |
| 6610 | goto onError; |
| 6611 | } |
| 6612 | *str = (char)c; |
| 6613 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6614 | pos = newpos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6615 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6616 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6617 | } |
| 6618 | } |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6619 | /* Resize if we allocated to much */ |
| 6620 | size = str - PyBytes_AS_STRING(res); |
| 6621 | if (size < ressize) { /* If this falls res will be NULL */ |
Alexandre Vassalotti | bad1b92 | 2008-12-27 09:49:09 +0000 | [diff] [blame] | 6622 | assert(size >= 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6623 | if (_PyBytes_Resize(&res, size) < 0) |
| 6624 | goto onError; |
| 6625 | } |
| 6626 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6627 | Py_XDECREF(errorHandler); |
| 6628 | Py_XDECREF(exc); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6629 | return res; |
| 6630 | |
| 6631 | onError: |
| 6632 | Py_XDECREF(res); |
| 6633 | Py_XDECREF(errorHandler); |
| 6634 | Py_XDECREF(exc); |
| 6635 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6636 | } |
| 6637 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6638 | /* Deprecated */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6639 | PyObject * |
| 6640 | PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6641 | Py_ssize_t size, |
| 6642 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6643 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6644 | PyObject *result; |
| 6645 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 6646 | if (unicode == NULL) |
| 6647 | return NULL; |
| 6648 | result = unicode_encode_ucs1(unicode, errors, 256); |
| 6649 | Py_DECREF(unicode); |
| 6650 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6651 | } |
| 6652 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6653 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6654 | _PyUnicode_AsLatin1String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6655 | { |
| 6656 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6657 | PyErr_BadArgument(); |
| 6658 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6659 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6660 | if (PyUnicode_READY(unicode) == -1) |
| 6661 | return NULL; |
| 6662 | /* Fast path: if it is a one-byte string, construct |
| 6663 | bytes object directly. */ |
| 6664 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) |
| 6665 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6666 | PyUnicode_GET_LENGTH(unicode)); |
| 6667 | /* Non-Latin-1 characters present. Defer to above function to |
| 6668 | raise the exception. */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6669 | return unicode_encode_ucs1(unicode, errors, 256); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6670 | } |
| 6671 | |
| 6672 | PyObject* |
| 6673 | PyUnicode_AsLatin1String(PyObject *unicode) |
| 6674 | { |
| 6675 | return _PyUnicode_AsLatin1String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6676 | } |
| 6677 | |
| 6678 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 6679 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6680 | PyObject * |
| 6681 | PyUnicode_DecodeASCII(const char *s, |
| 6682 | Py_ssize_t size, |
| 6683 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6684 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6685 | const char *starts = s; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6686 | _PyUnicodeWriter writer; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6687 | int kind; |
| 6688 | void *data; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6689 | Py_ssize_t startinpos; |
| 6690 | Py_ssize_t endinpos; |
| 6691 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6692 | const char *e; |
| 6693 | PyObject *errorHandler = NULL; |
| 6694 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6695 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6696 | if (size == 0) |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 6697 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 6698 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6699 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6700 | if (size == 1 && (unsigned char)s[0] < 128) |
| 6701 | return get_latin1_char((unsigned char)s[0]); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6702 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 6703 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 6704 | writer.min_length = size; |
| 6705 | if (_PyUnicodeWriter_Prepare(&writer, writer.min_length, 127) < 0) |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 6706 | return NULL; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6707 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6708 | e = s + size; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6709 | data = writer.data; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6710 | outpos = ascii_decode(s, e, (Py_UCS1 *)data); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6711 | writer.pos = outpos; |
| 6712 | if (writer.pos == size) |
| 6713 | return _PyUnicodeWriter_Finish(&writer); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6714 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6715 | s += writer.pos; |
| 6716 | kind = writer.kind; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6717 | while (s < e) { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 6718 | unsigned char c = (unsigned char)*s; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6719 | if (c < 128) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6720 | PyUnicode_WRITE(kind, data, writer.pos, c); |
| 6721 | writer.pos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6722 | ++s; |
| 6723 | } |
| 6724 | else { |
| 6725 | startinpos = s-starts; |
| 6726 | endinpos = startinpos + 1; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6727 | if (unicode_decode_call_errorhandler_writer( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6728 | errors, &errorHandler, |
| 6729 | "ascii", "ordinal not in range(128)", |
| 6730 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6731 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6732 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6733 | kind = writer.kind; |
| 6734 | data = writer.data; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6735 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6736 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6737 | Py_XDECREF(errorHandler); |
| 6738 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6739 | return _PyUnicodeWriter_Finish(&writer); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6740 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6741 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6742 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6743 | Py_XDECREF(errorHandler); |
| 6744 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6745 | return NULL; |
| 6746 | } |
| 6747 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6748 | /* Deprecated */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6749 | PyObject * |
| 6750 | PyUnicode_EncodeASCII(const Py_UNICODE *p, |
| 6751 | Py_ssize_t size, |
| 6752 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6753 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6754 | PyObject *result; |
| 6755 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 6756 | if (unicode == NULL) |
| 6757 | return NULL; |
| 6758 | result = unicode_encode_ucs1(unicode, errors, 128); |
| 6759 | Py_DECREF(unicode); |
| 6760 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6761 | } |
| 6762 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6763 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6764 | _PyUnicode_AsASCIIString(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6765 | { |
| 6766 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6767 | PyErr_BadArgument(); |
| 6768 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6769 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6770 | if (PyUnicode_READY(unicode) == -1) |
| 6771 | return NULL; |
| 6772 | /* Fast path: if it is an ASCII-only string, construct bytes object |
| 6773 | directly. Else defer to above function to raise the exception. */ |
Victor Stinner | af03757 | 2013-04-14 18:44:10 +0200 | [diff] [blame] | 6774 | if (PyUnicode_IS_ASCII(unicode)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6775 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6776 | PyUnicode_GET_LENGTH(unicode)); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6777 | return unicode_encode_ucs1(unicode, errors, 128); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6778 | } |
| 6779 | |
| 6780 | PyObject * |
| 6781 | PyUnicode_AsASCIIString(PyObject *unicode) |
| 6782 | { |
| 6783 | return _PyUnicode_AsASCIIString(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6784 | } |
| 6785 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 6786 | #ifdef HAVE_MBCS |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6787 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6788 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6789 | |
Hirokazu Yamamoto | 3530246 | 2009-03-21 13:23:27 +0000 | [diff] [blame] | 6790 | #if SIZEOF_INT < SIZEOF_SIZE_T |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6791 | #define NEED_RETRY |
| 6792 | #endif |
| 6793 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6794 | #ifndef WC_ERR_INVALID_CHARS |
| 6795 | # define WC_ERR_INVALID_CHARS 0x0080 |
| 6796 | #endif |
| 6797 | |
| 6798 | static char* |
| 6799 | code_page_name(UINT code_page, PyObject **obj) |
| 6800 | { |
| 6801 | *obj = NULL; |
| 6802 | if (code_page == CP_ACP) |
| 6803 | return "mbcs"; |
| 6804 | if (code_page == CP_UTF7) |
| 6805 | return "CP_UTF7"; |
| 6806 | if (code_page == CP_UTF8) |
| 6807 | return "CP_UTF8"; |
| 6808 | |
| 6809 | *obj = PyBytes_FromFormat("cp%u", code_page); |
| 6810 | if (*obj == NULL) |
| 6811 | return NULL; |
| 6812 | return PyBytes_AS_STRING(*obj); |
| 6813 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6814 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6815 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6816 | is_dbcs_lead_byte(UINT code_page, const char *s, int offset) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6817 | { |
| 6818 | const char *curr = s + offset; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6819 | const char *prev; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6820 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6821 | if (!IsDBCSLeadByteEx(code_page, *curr)) |
| 6822 | return 0; |
| 6823 | |
| 6824 | prev = CharPrevExA(code_page, s, curr, 0); |
| 6825 | if (prev == curr) |
| 6826 | return 1; |
| 6827 | /* FIXME: This code is limited to "true" double-byte encodings, |
| 6828 | as it assumes an incomplete character consists of a single |
| 6829 | byte. */ |
| 6830 | if (curr - prev == 2) |
| 6831 | return 1; |
| 6832 | if (!IsDBCSLeadByteEx(code_page, *prev)) |
| 6833 | return 1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6834 | return 0; |
| 6835 | } |
| 6836 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6837 | static DWORD |
| 6838 | decode_code_page_flags(UINT code_page) |
| 6839 | { |
| 6840 | if (code_page == CP_UTF7) { |
| 6841 | /* The CP_UTF7 decoder only supports flags=0 */ |
| 6842 | return 0; |
| 6843 | } |
| 6844 | else |
| 6845 | return MB_ERR_INVALID_CHARS; |
| 6846 | } |
| 6847 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6848 | /* |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6849 | * Decode a byte string from a Windows code page into unicode object in strict |
| 6850 | * mode. |
| 6851 | * |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 6852 | * Returns consumed size if succeed, returns -2 on decode error, or raise an |
| 6853 | * OSError and returns -1 on other error. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6854 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6855 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6856 | decode_code_page_strict(UINT code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6857 | PyObject **v, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6858 | const char *in, |
| 6859 | int insize) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6860 | { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6861 | const DWORD flags = decode_code_page_flags(code_page); |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 6862 | wchar_t *out; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6863 | DWORD outsize; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6864 | |
| 6865 | /* First get the size of the result */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6866 | assert(insize > 0); |
| 6867 | outsize = MultiByteToWideChar(code_page, flags, in, insize, NULL, 0); |
| 6868 | if (outsize <= 0) |
| 6869 | goto error; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6870 | |
| 6871 | if (*v == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6872 | /* Create unicode object */ |
Victor Stinner | ab59594 | 2011-12-17 04:59:06 +0100 | [diff] [blame] | 6873 | /* FIXME: don't use _PyUnicode_New(), but allocate a wchar_t* buffer */ |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6874 | *v = (PyObject*)_PyUnicode_New(outsize); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6875 | if (*v == NULL) |
| 6876 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6877 | out = PyUnicode_AS_UNICODE(*v); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6878 | } |
| 6879 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6880 | /* Extend unicode object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6881 | Py_ssize_t n = PyUnicode_GET_SIZE(*v); |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 6882 | if (unicode_resize(v, n + outsize) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6883 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6884 | out = PyUnicode_AS_UNICODE(*v) + n; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6885 | } |
| 6886 | |
| 6887 | /* Do the conversion */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6888 | outsize = MultiByteToWideChar(code_page, flags, in, insize, out, outsize); |
| 6889 | if (outsize <= 0) |
| 6890 | goto error; |
| 6891 | return insize; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6892 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6893 | error: |
| 6894 | if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION) |
| 6895 | return -2; |
| 6896 | PyErr_SetFromWindowsErr(0); |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6897 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6898 | } |
| 6899 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6900 | /* |
| 6901 | * Decode a byte string from a code page into unicode object with an error |
| 6902 | * handler. |
| 6903 | * |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 6904 | * Returns consumed size if succeed, or raise an OSError or |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6905 | * UnicodeDecodeError exception and returns -1 on error. |
| 6906 | */ |
| 6907 | static int |
| 6908 | decode_code_page_errors(UINT code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6909 | PyObject **v, |
| 6910 | const char *in, const int size, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6911 | const char *errors) |
| 6912 | { |
| 6913 | const char *startin = in; |
| 6914 | const char *endin = in + size; |
| 6915 | const DWORD flags = decode_code_page_flags(code_page); |
| 6916 | /* Ideally, we should get reason from FormatMessage. This is the Windows |
| 6917 | 2000 English version of the message. */ |
| 6918 | const char *reason = "No mapping for the Unicode character exists " |
| 6919 | "in the target code page."; |
| 6920 | /* each step cannot decode more than 1 character, but a character can be |
| 6921 | represented as a surrogate pair */ |
| 6922 | wchar_t buffer[2], *startout, *out; |
Victor Stinner | 9f067f4 | 2013-06-05 00:21:31 +0200 | [diff] [blame] | 6923 | int insize; |
| 6924 | Py_ssize_t outsize; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6925 | PyObject *errorHandler = NULL; |
| 6926 | PyObject *exc = NULL; |
| 6927 | PyObject *encoding_obj = NULL; |
| 6928 | char *encoding; |
| 6929 | DWORD err; |
| 6930 | int ret = -1; |
| 6931 | |
| 6932 | assert(size > 0); |
| 6933 | |
| 6934 | encoding = code_page_name(code_page, &encoding_obj); |
| 6935 | if (encoding == NULL) |
| 6936 | return -1; |
| 6937 | |
| 6938 | if (errors == NULL || strcmp(errors, "strict") == 0) { |
| 6939 | /* The last error was ERROR_NO_UNICODE_TRANSLATION, then we raise a |
| 6940 | UnicodeDecodeError. */ |
| 6941 | make_decode_exception(&exc, encoding, in, size, 0, 0, reason); |
| 6942 | if (exc != NULL) { |
| 6943 | PyCodec_StrictErrors(exc); |
| 6944 | Py_CLEAR(exc); |
| 6945 | } |
| 6946 | goto error; |
| 6947 | } |
| 6948 | |
| 6949 | if (*v == NULL) { |
| 6950 | /* Create unicode object */ |
| 6951 | if (size > PY_SSIZE_T_MAX / (Py_ssize_t)Py_ARRAY_LENGTH(buffer)) { |
| 6952 | PyErr_NoMemory(); |
| 6953 | goto error; |
| 6954 | } |
Victor Stinner | ab59594 | 2011-12-17 04:59:06 +0100 | [diff] [blame] | 6955 | /* FIXME: don't use _PyUnicode_New(), but allocate a wchar_t* buffer */ |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6956 | *v = (PyObject*)_PyUnicode_New(size * Py_ARRAY_LENGTH(buffer)); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6957 | if (*v == NULL) |
| 6958 | goto error; |
| 6959 | startout = PyUnicode_AS_UNICODE(*v); |
| 6960 | } |
| 6961 | else { |
| 6962 | /* Extend unicode object */ |
| 6963 | Py_ssize_t n = PyUnicode_GET_SIZE(*v); |
| 6964 | if (size > (PY_SSIZE_T_MAX - n) / (Py_ssize_t)Py_ARRAY_LENGTH(buffer)) { |
| 6965 | PyErr_NoMemory(); |
| 6966 | goto error; |
| 6967 | } |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 6968 | if (unicode_resize(v, n + size * Py_ARRAY_LENGTH(buffer)) < 0) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6969 | goto error; |
| 6970 | startout = PyUnicode_AS_UNICODE(*v) + n; |
| 6971 | } |
| 6972 | |
| 6973 | /* Decode the byte string character per character */ |
| 6974 | out = startout; |
| 6975 | while (in < endin) |
| 6976 | { |
| 6977 | /* Decode a character */ |
| 6978 | insize = 1; |
| 6979 | do |
| 6980 | { |
| 6981 | outsize = MultiByteToWideChar(code_page, flags, |
| 6982 | in, insize, |
| 6983 | buffer, Py_ARRAY_LENGTH(buffer)); |
| 6984 | if (outsize > 0) |
| 6985 | break; |
| 6986 | err = GetLastError(); |
| 6987 | if (err != ERROR_NO_UNICODE_TRANSLATION |
| 6988 | && err != ERROR_INSUFFICIENT_BUFFER) |
| 6989 | { |
| 6990 | PyErr_SetFromWindowsErr(0); |
| 6991 | goto error; |
| 6992 | } |
| 6993 | insize++; |
| 6994 | } |
| 6995 | /* 4=maximum length of a UTF-8 sequence */ |
| 6996 | while (insize <= 4 && (in + insize) <= endin); |
| 6997 | |
| 6998 | if (outsize <= 0) { |
| 6999 | Py_ssize_t startinpos, endinpos, outpos; |
| 7000 | |
| 7001 | startinpos = in - startin; |
| 7002 | endinpos = startinpos + 1; |
| 7003 | outpos = out - PyUnicode_AS_UNICODE(*v); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7004 | if (unicode_decode_call_errorhandler_wchar( |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7005 | errors, &errorHandler, |
| 7006 | encoding, reason, |
| 7007 | &startin, &endin, &startinpos, &endinpos, &exc, &in, |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 7008 | v, &outpos)) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7009 | { |
| 7010 | goto error; |
| 7011 | } |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 7012 | out = PyUnicode_AS_UNICODE(*v) + outpos; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7013 | } |
| 7014 | else { |
| 7015 | in += insize; |
| 7016 | memcpy(out, buffer, outsize * sizeof(wchar_t)); |
| 7017 | out += outsize; |
| 7018 | } |
| 7019 | } |
| 7020 | |
| 7021 | /* write a NUL character at the end */ |
| 7022 | *out = 0; |
| 7023 | |
| 7024 | /* Extend unicode object */ |
| 7025 | outsize = out - startout; |
| 7026 | assert(outsize <= PyUnicode_WSTR_LENGTH(*v)); |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 7027 | if (unicode_resize(v, outsize) < 0) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7028 | goto error; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7029 | ret = size; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7030 | |
| 7031 | error: |
| 7032 | Py_XDECREF(encoding_obj); |
| 7033 | Py_XDECREF(errorHandler); |
| 7034 | Py_XDECREF(exc); |
| 7035 | return ret; |
| 7036 | } |
| 7037 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7038 | static PyObject * |
| 7039 | decode_code_page_stateful(int code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7040 | const char *s, Py_ssize_t size, |
| 7041 | const char *errors, Py_ssize_t *consumed) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7042 | { |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7043 | PyObject *v = NULL; |
| 7044 | int chunk_size, final, converted, done; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7045 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7046 | if (code_page < 0) { |
| 7047 | PyErr_SetString(PyExc_ValueError, "invalid code page number"); |
| 7048 | return NULL; |
| 7049 | } |
| 7050 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7051 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7052 | *consumed = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7053 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7054 | do |
| 7055 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7056 | #ifdef NEED_RETRY |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7057 | if (size > INT_MAX) { |
| 7058 | chunk_size = INT_MAX; |
| 7059 | final = 0; |
| 7060 | done = 0; |
| 7061 | } |
| 7062 | else |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7063 | #endif |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7064 | { |
| 7065 | chunk_size = (int)size; |
| 7066 | final = (consumed == NULL); |
| 7067 | done = 1; |
| 7068 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7069 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7070 | /* Skip trailing lead-byte unless 'final' is set */ |
| 7071 | if (!final && is_dbcs_lead_byte(code_page, s, chunk_size - 1)) |
| 7072 | --chunk_size; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7073 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7074 | if (chunk_size == 0 && done) { |
| 7075 | if (v != NULL) |
| 7076 | break; |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 7077 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7078 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7079 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7080 | |
| 7081 | converted = decode_code_page_strict(code_page, &v, |
| 7082 | s, chunk_size); |
| 7083 | if (converted == -2) |
| 7084 | converted = decode_code_page_errors(code_page, &v, |
| 7085 | s, chunk_size, |
| 7086 | errors); |
| 7087 | assert(converted != 0); |
| 7088 | |
| 7089 | if (converted < 0) { |
| 7090 | Py_XDECREF(v); |
| 7091 | return NULL; |
| 7092 | } |
| 7093 | |
| 7094 | if (consumed) |
| 7095 | *consumed += converted; |
| 7096 | |
| 7097 | s += converted; |
| 7098 | size -= converted; |
| 7099 | } while (!done); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7100 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 7101 | return unicode_result(v); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7102 | } |
| 7103 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7104 | PyObject * |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7105 | PyUnicode_DecodeCodePageStateful(int code_page, |
| 7106 | const char *s, |
| 7107 | Py_ssize_t size, |
| 7108 | const char *errors, |
| 7109 | Py_ssize_t *consumed) |
| 7110 | { |
| 7111 | return decode_code_page_stateful(code_page, s, size, errors, consumed); |
| 7112 | } |
| 7113 | |
| 7114 | PyObject * |
| 7115 | PyUnicode_DecodeMBCSStateful(const char *s, |
| 7116 | Py_ssize_t size, |
| 7117 | const char *errors, |
| 7118 | Py_ssize_t *consumed) |
| 7119 | { |
| 7120 | return decode_code_page_stateful(CP_ACP, s, size, errors, consumed); |
| 7121 | } |
| 7122 | |
| 7123 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7124 | PyUnicode_DecodeMBCS(const char *s, |
| 7125 | Py_ssize_t size, |
| 7126 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7127 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7128 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 7129 | } |
| 7130 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7131 | static DWORD |
| 7132 | encode_code_page_flags(UINT code_page, const char *errors) |
| 7133 | { |
| 7134 | if (code_page == CP_UTF8) { |
| 7135 | if (winver.dwMajorVersion >= 6) |
| 7136 | /* CP_UTF8 supports WC_ERR_INVALID_CHARS on Windows Vista |
| 7137 | and later */ |
| 7138 | return WC_ERR_INVALID_CHARS; |
| 7139 | else |
| 7140 | /* CP_UTF8 only supports flags=0 on Windows older than Vista */ |
| 7141 | return 0; |
| 7142 | } |
| 7143 | else if (code_page == CP_UTF7) { |
| 7144 | /* CP_UTF7 only supports flags=0 */ |
| 7145 | return 0; |
| 7146 | } |
| 7147 | else { |
| 7148 | if (errors != NULL && strcmp(errors, "replace") == 0) |
| 7149 | return 0; |
| 7150 | else |
| 7151 | return WC_NO_BEST_FIT_CHARS; |
| 7152 | } |
| 7153 | } |
| 7154 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7155 | /* |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7156 | * Encode a Unicode string to a Windows code page into a byte string in strict |
| 7157 | * mode. |
| 7158 | * |
| 7159 | * Returns consumed characters if succeed, returns -2 on encode error, or raise |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 7160 | * an OSError and returns -1 on other error. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7161 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7162 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7163 | encode_code_page_strict(UINT code_page, PyObject **outbytes, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7164 | PyObject *unicode, Py_ssize_t offset, int len, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7165 | const char* errors) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7166 | { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7167 | BOOL usedDefaultChar = FALSE; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7168 | BOOL *pusedDefaultChar = &usedDefaultChar; |
| 7169 | int outsize; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7170 | PyObject *exc = NULL; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 7171 | wchar_t *p; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7172 | Py_ssize_t size; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7173 | const DWORD flags = encode_code_page_flags(code_page, NULL); |
| 7174 | char *out; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7175 | /* Create a substring so that we can get the UTF-16 representation |
| 7176 | of just the slice under consideration. */ |
| 7177 | PyObject *substring; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7178 | |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7179 | assert(len > 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7180 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7181 | if (code_page != CP_UTF8 && code_page != CP_UTF7) |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7182 | pusedDefaultChar = &usedDefaultChar; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7183 | else |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7184 | pusedDefaultChar = NULL; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7185 | |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7186 | substring = PyUnicode_Substring(unicode, offset, offset+len); |
| 7187 | if (substring == NULL) |
| 7188 | return -1; |
| 7189 | p = PyUnicode_AsUnicodeAndSize(substring, &size); |
| 7190 | if (p == NULL) { |
| 7191 | Py_DECREF(substring); |
| 7192 | return -1; |
| 7193 | } |
Victor Stinner | 9f067f4 | 2013-06-05 00:21:31 +0200 | [diff] [blame] | 7194 | assert(size <= INT_MAX); |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7195 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7196 | /* First get the size of the result */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7197 | outsize = WideCharToMultiByte(code_page, flags, |
Victor Stinner | 9f067f4 | 2013-06-05 00:21:31 +0200 | [diff] [blame] | 7198 | p, (int)size, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7199 | NULL, 0, |
| 7200 | NULL, pusedDefaultChar); |
| 7201 | if (outsize <= 0) |
| 7202 | goto error; |
| 7203 | /* If we used a default char, then we failed! */ |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7204 | if (pusedDefaultChar && *pusedDefaultChar) { |
| 7205 | Py_DECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7206 | return -2; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7207 | } |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7208 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7209 | if (*outbytes == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7210 | /* Create string object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7211 | *outbytes = PyBytes_FromStringAndSize(NULL, outsize); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7212 | if (*outbytes == NULL) { |
| 7213 | Py_DECREF(substring); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7214 | return -1; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7215 | } |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7216 | out = PyBytes_AS_STRING(*outbytes); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7217 | } |
| 7218 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7219 | /* Extend string object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7220 | const Py_ssize_t n = PyBytes_Size(*outbytes); |
| 7221 | if (outsize > PY_SSIZE_T_MAX - n) { |
| 7222 | PyErr_NoMemory(); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7223 | Py_DECREF(substring); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7224 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7225 | } |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7226 | if (_PyBytes_Resize(outbytes, n + outsize) < 0) { |
| 7227 | Py_DECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7228 | return -1; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7229 | } |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7230 | out = PyBytes_AS_STRING(*outbytes) + n; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7231 | } |
| 7232 | |
| 7233 | /* Do the conversion */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7234 | outsize = WideCharToMultiByte(code_page, flags, |
Victor Stinner | 9f067f4 | 2013-06-05 00:21:31 +0200 | [diff] [blame] | 7235 | p, (int)size, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7236 | out, outsize, |
| 7237 | NULL, pusedDefaultChar); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7238 | Py_CLEAR(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7239 | if (outsize <= 0) |
| 7240 | goto error; |
| 7241 | if (pusedDefaultChar && *pusedDefaultChar) |
| 7242 | return -2; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7243 | return 0; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7244 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7245 | error: |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7246 | Py_XDECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7247 | if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION) |
| 7248 | return -2; |
| 7249 | PyErr_SetFromWindowsErr(0); |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7250 | return -1; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7251 | } |
| 7252 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7253 | /* |
| 7254 | * Encode a Unicode string to a Windows code page into a byte string using a |
| 7255 | * error handler. |
| 7256 | * |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 7257 | * Returns consumed characters if succeed, or raise an OSError and returns |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7258 | * -1 on other error. |
| 7259 | */ |
| 7260 | static int |
| 7261 | encode_code_page_errors(UINT code_page, PyObject **outbytes, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7262 | PyObject *unicode, Py_ssize_t unicode_offset, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7263 | Py_ssize_t insize, const char* errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7264 | { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7265 | const DWORD flags = encode_code_page_flags(code_page, errors); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7266 | Py_ssize_t pos = unicode_offset; |
| 7267 | Py_ssize_t endin = unicode_offset + insize; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7268 | /* Ideally, we should get reason from FormatMessage. This is the Windows |
| 7269 | 2000 English version of the message. */ |
| 7270 | const char *reason = "invalid character"; |
| 7271 | /* 4=maximum length of a UTF-8 sequence */ |
| 7272 | char buffer[4]; |
| 7273 | BOOL usedDefaultChar = FALSE, *pusedDefaultChar; |
| 7274 | Py_ssize_t outsize; |
| 7275 | char *out; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7276 | PyObject *errorHandler = NULL; |
| 7277 | PyObject *exc = NULL; |
| 7278 | PyObject *encoding_obj = NULL; |
| 7279 | char *encoding; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7280 | Py_ssize_t newpos, newoutsize; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7281 | PyObject *rep; |
| 7282 | int ret = -1; |
| 7283 | |
| 7284 | assert(insize > 0); |
| 7285 | |
| 7286 | encoding = code_page_name(code_page, &encoding_obj); |
| 7287 | if (encoding == NULL) |
| 7288 | return -1; |
| 7289 | |
| 7290 | if (errors == NULL || strcmp(errors, "strict") == 0) { |
| 7291 | /* The last error was ERROR_NO_UNICODE_TRANSLATION, |
| 7292 | then we raise a UnicodeEncodeError. */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7293 | make_encode_exception(&exc, encoding, unicode, 0, 0, reason); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7294 | if (exc != NULL) { |
| 7295 | PyCodec_StrictErrors(exc); |
| 7296 | Py_DECREF(exc); |
| 7297 | } |
| 7298 | Py_XDECREF(encoding_obj); |
| 7299 | return -1; |
| 7300 | } |
| 7301 | |
| 7302 | if (code_page != CP_UTF8 && code_page != CP_UTF7) |
| 7303 | pusedDefaultChar = &usedDefaultChar; |
| 7304 | else |
| 7305 | pusedDefaultChar = NULL; |
| 7306 | |
| 7307 | if (Py_ARRAY_LENGTH(buffer) > PY_SSIZE_T_MAX / insize) { |
| 7308 | PyErr_NoMemory(); |
| 7309 | goto error; |
| 7310 | } |
| 7311 | outsize = insize * Py_ARRAY_LENGTH(buffer); |
| 7312 | |
| 7313 | if (*outbytes == NULL) { |
| 7314 | /* Create string object */ |
| 7315 | *outbytes = PyBytes_FromStringAndSize(NULL, outsize); |
| 7316 | if (*outbytes == NULL) |
| 7317 | goto error; |
| 7318 | out = PyBytes_AS_STRING(*outbytes); |
| 7319 | } |
| 7320 | else { |
| 7321 | /* Extend string object */ |
| 7322 | Py_ssize_t n = PyBytes_Size(*outbytes); |
| 7323 | if (n > PY_SSIZE_T_MAX - outsize) { |
| 7324 | PyErr_NoMemory(); |
| 7325 | goto error; |
| 7326 | } |
| 7327 | if (_PyBytes_Resize(outbytes, n + outsize) < 0) |
| 7328 | goto error; |
| 7329 | out = PyBytes_AS_STRING(*outbytes) + n; |
| 7330 | } |
| 7331 | |
| 7332 | /* Encode the string character per character */ |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7333 | while (pos < endin) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7334 | { |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7335 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, pos); |
| 7336 | wchar_t chars[2]; |
| 7337 | int charsize; |
| 7338 | if (ch < 0x10000) { |
| 7339 | chars[0] = (wchar_t)ch; |
| 7340 | charsize = 1; |
| 7341 | } |
| 7342 | else { |
Victor Stinner | 76df43d | 2012-10-30 01:42:39 +0100 | [diff] [blame] | 7343 | chars[0] = Py_UNICODE_HIGH_SURROGATE(ch); |
| 7344 | chars[1] = Py_UNICODE_LOW_SURROGATE(ch); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7345 | charsize = 2; |
| 7346 | } |
| 7347 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7348 | outsize = WideCharToMultiByte(code_page, flags, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7349 | chars, charsize, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7350 | buffer, Py_ARRAY_LENGTH(buffer), |
| 7351 | NULL, pusedDefaultChar); |
| 7352 | if (outsize > 0) { |
| 7353 | if (pusedDefaultChar == NULL || !(*pusedDefaultChar)) |
| 7354 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7355 | pos++; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7356 | memcpy(out, buffer, outsize); |
| 7357 | out += outsize; |
| 7358 | continue; |
| 7359 | } |
| 7360 | } |
| 7361 | else if (GetLastError() != ERROR_NO_UNICODE_TRANSLATION) { |
| 7362 | PyErr_SetFromWindowsErr(0); |
| 7363 | goto error; |
| 7364 | } |
| 7365 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7366 | rep = unicode_encode_call_errorhandler( |
| 7367 | errors, &errorHandler, encoding, reason, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7368 | unicode, &exc, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7369 | pos, pos + 1, &newpos); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7370 | if (rep == NULL) |
| 7371 | goto error; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7372 | pos = newpos; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7373 | |
| 7374 | if (PyBytes_Check(rep)) { |
| 7375 | outsize = PyBytes_GET_SIZE(rep); |
| 7376 | if (outsize != 1) { |
| 7377 | Py_ssize_t offset = out - PyBytes_AS_STRING(*outbytes); |
| 7378 | newoutsize = PyBytes_GET_SIZE(*outbytes) + (outsize - 1); |
| 7379 | if (_PyBytes_Resize(outbytes, newoutsize) < 0) { |
| 7380 | Py_DECREF(rep); |
| 7381 | goto error; |
| 7382 | } |
| 7383 | out = PyBytes_AS_STRING(*outbytes) + offset; |
| 7384 | } |
| 7385 | memcpy(out, PyBytes_AS_STRING(rep), outsize); |
| 7386 | out += outsize; |
| 7387 | } |
| 7388 | else { |
| 7389 | Py_ssize_t i; |
| 7390 | enum PyUnicode_Kind kind; |
| 7391 | void *data; |
| 7392 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7393 | if (PyUnicode_READY(rep) == -1) { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7394 | Py_DECREF(rep); |
| 7395 | goto error; |
| 7396 | } |
| 7397 | |
| 7398 | outsize = PyUnicode_GET_LENGTH(rep); |
| 7399 | if (outsize != 1) { |
| 7400 | Py_ssize_t offset = out - PyBytes_AS_STRING(*outbytes); |
| 7401 | newoutsize = PyBytes_GET_SIZE(*outbytes) + (outsize - 1); |
| 7402 | if (_PyBytes_Resize(outbytes, newoutsize) < 0) { |
| 7403 | Py_DECREF(rep); |
| 7404 | goto error; |
| 7405 | } |
| 7406 | out = PyBytes_AS_STRING(*outbytes) + offset; |
| 7407 | } |
| 7408 | kind = PyUnicode_KIND(rep); |
| 7409 | data = PyUnicode_DATA(rep); |
| 7410 | for (i=0; i < outsize; i++) { |
| 7411 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 7412 | if (ch > 127) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7413 | raise_encode_exception(&exc, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7414 | encoding, unicode, |
| 7415 | pos, pos + 1, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7416 | "unable to encode error handler result to ASCII"); |
| 7417 | Py_DECREF(rep); |
| 7418 | goto error; |
| 7419 | } |
| 7420 | *out = (unsigned char)ch; |
| 7421 | out++; |
| 7422 | } |
| 7423 | } |
| 7424 | Py_DECREF(rep); |
| 7425 | } |
| 7426 | /* write a NUL byte */ |
| 7427 | *out = 0; |
| 7428 | outsize = out - PyBytes_AS_STRING(*outbytes); |
| 7429 | assert(outsize <= PyBytes_GET_SIZE(*outbytes)); |
| 7430 | if (_PyBytes_Resize(outbytes, outsize) < 0) |
| 7431 | goto error; |
| 7432 | ret = 0; |
| 7433 | |
| 7434 | error: |
| 7435 | Py_XDECREF(encoding_obj); |
| 7436 | Py_XDECREF(errorHandler); |
| 7437 | Py_XDECREF(exc); |
| 7438 | return ret; |
| 7439 | } |
| 7440 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7441 | static PyObject * |
| 7442 | encode_code_page(int code_page, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7443 | PyObject *unicode, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7444 | const char *errors) |
| 7445 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7446 | Py_ssize_t len; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7447 | PyObject *outbytes = NULL; |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7448 | Py_ssize_t offset; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7449 | int chunk_len, ret, done; |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7450 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7451 | if (PyUnicode_READY(unicode) == -1) |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7452 | return NULL; |
| 7453 | len = PyUnicode_GET_LENGTH(unicode); |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 7454 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7455 | if (code_page < 0) { |
| 7456 | PyErr_SetString(PyExc_ValueError, "invalid code page number"); |
| 7457 | return NULL; |
| 7458 | } |
| 7459 | |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7460 | if (len == 0) |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7461 | return PyBytes_FromStringAndSize(NULL, 0); |
| 7462 | |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7463 | offset = 0; |
| 7464 | do |
| 7465 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7466 | #ifdef NEED_RETRY |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7467 | /* UTF-16 encoding may double the size, so use only INT_MAX/2 |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7468 | chunks. */ |
| 7469 | if (len > INT_MAX/2) { |
| 7470 | chunk_len = INT_MAX/2; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7471 | done = 0; |
| 7472 | } |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7473 | else |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7474 | #endif |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7475 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7476 | chunk_len = (int)len; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7477 | done = 1; |
| 7478 | } |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7479 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7480 | ret = encode_code_page_strict(code_page, &outbytes, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7481 | unicode, offset, chunk_len, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7482 | errors); |
| 7483 | if (ret == -2) |
| 7484 | ret = encode_code_page_errors(code_page, &outbytes, |
| 7485 | unicode, offset, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7486 | chunk_len, errors); |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7487 | if (ret < 0) { |
| 7488 | Py_XDECREF(outbytes); |
| 7489 | return NULL; |
| 7490 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7491 | |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7492 | offset += chunk_len; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7493 | len -= chunk_len; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7494 | } while (!done); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7495 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7496 | return outbytes; |
| 7497 | } |
| 7498 | |
| 7499 | PyObject * |
| 7500 | PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
| 7501 | Py_ssize_t size, |
| 7502 | const char *errors) |
| 7503 | { |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7504 | PyObject *unicode, *res; |
| 7505 | unicode = PyUnicode_FromUnicode(p, size); |
| 7506 | if (unicode == NULL) |
| 7507 | return NULL; |
| 7508 | res = encode_code_page(CP_ACP, unicode, errors); |
| 7509 | Py_DECREF(unicode); |
| 7510 | return res; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7511 | } |
| 7512 | |
| 7513 | PyObject * |
| 7514 | PyUnicode_EncodeCodePage(int code_page, |
| 7515 | PyObject *unicode, |
| 7516 | const char *errors) |
| 7517 | { |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7518 | return encode_code_page(code_page, unicode, errors); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7519 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 7520 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7521 | PyObject * |
| 7522 | PyUnicode_AsMBCSString(PyObject *unicode) |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7523 | { |
| 7524 | if (!PyUnicode_Check(unicode)) { |
| 7525 | PyErr_BadArgument(); |
| 7526 | return NULL; |
| 7527 | } |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7528 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, NULL); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7529 | } |
| 7530 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7531 | #undef NEED_RETRY |
| 7532 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 7533 | #endif /* HAVE_MBCS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7534 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7535 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 7536 | |
Victor Stinner | fb161b1 | 2013-04-18 01:44:27 +0200 | [diff] [blame] | 7537 | static int |
| 7538 | charmap_decode_string(const char *s, |
| 7539 | Py_ssize_t size, |
| 7540 | PyObject *mapping, |
| 7541 | const char *errors, |
| 7542 | _PyUnicodeWriter *writer) |
| 7543 | { |
| 7544 | const char *starts = s; |
| 7545 | const char *e; |
| 7546 | Py_ssize_t startinpos, endinpos; |
| 7547 | PyObject *errorHandler = NULL, *exc = NULL; |
| 7548 | Py_ssize_t maplen; |
| 7549 | enum PyUnicode_Kind mapkind; |
| 7550 | void *mapdata; |
| 7551 | Py_UCS4 x; |
| 7552 | unsigned char ch; |
| 7553 | |
| 7554 | if (PyUnicode_READY(mapping) == -1) |
| 7555 | return -1; |
| 7556 | |
| 7557 | maplen = PyUnicode_GET_LENGTH(mapping); |
| 7558 | mapdata = PyUnicode_DATA(mapping); |
| 7559 | mapkind = PyUnicode_KIND(mapping); |
| 7560 | |
| 7561 | e = s + size; |
| 7562 | |
| 7563 | if (mapkind == PyUnicode_1BYTE_KIND && maplen >= 256) { |
| 7564 | /* fast-path for cp037, cp500 and iso8859_1 encodings. iso8859_1 |
| 7565 | * is disabled in encoding aliases, latin1 is preferred because |
| 7566 | * its implementation is faster. */ |
| 7567 | Py_UCS1 *mapdata_ucs1 = (Py_UCS1 *)mapdata; |
| 7568 | Py_UCS1 *outdata = (Py_UCS1 *)writer->data; |
| 7569 | Py_UCS4 maxchar = writer->maxchar; |
| 7570 | |
| 7571 | assert (writer->kind == PyUnicode_1BYTE_KIND); |
| 7572 | while (s < e) { |
| 7573 | ch = *s; |
| 7574 | x = mapdata_ucs1[ch]; |
| 7575 | if (x > maxchar) { |
| 7576 | if (_PyUnicodeWriter_Prepare(writer, 1, 0xff) == -1) |
| 7577 | goto onError; |
| 7578 | maxchar = writer->maxchar; |
| 7579 | outdata = (Py_UCS1 *)writer->data; |
| 7580 | } |
| 7581 | outdata[writer->pos] = x; |
| 7582 | writer->pos++; |
| 7583 | ++s; |
| 7584 | } |
| 7585 | return 0; |
| 7586 | } |
| 7587 | |
| 7588 | while (s < e) { |
| 7589 | if (mapkind == PyUnicode_2BYTE_KIND && maplen >= 256) { |
| 7590 | enum PyUnicode_Kind outkind = writer->kind; |
| 7591 | Py_UCS2 *mapdata_ucs2 = (Py_UCS2 *)mapdata; |
| 7592 | if (outkind == PyUnicode_1BYTE_KIND) { |
| 7593 | Py_UCS1 *outdata = (Py_UCS1 *)writer->data; |
| 7594 | Py_UCS4 maxchar = writer->maxchar; |
| 7595 | while (s < e) { |
| 7596 | ch = *s; |
| 7597 | x = mapdata_ucs2[ch]; |
| 7598 | if (x > maxchar) |
| 7599 | goto Error; |
| 7600 | outdata[writer->pos] = x; |
| 7601 | writer->pos++; |
| 7602 | ++s; |
| 7603 | } |
| 7604 | break; |
| 7605 | } |
| 7606 | else if (outkind == PyUnicode_2BYTE_KIND) { |
| 7607 | Py_UCS2 *outdata = (Py_UCS2 *)writer->data; |
| 7608 | while (s < e) { |
| 7609 | ch = *s; |
| 7610 | x = mapdata_ucs2[ch]; |
| 7611 | if (x == 0xFFFE) |
| 7612 | goto Error; |
| 7613 | outdata[writer->pos] = x; |
| 7614 | writer->pos++; |
| 7615 | ++s; |
| 7616 | } |
| 7617 | break; |
| 7618 | } |
| 7619 | } |
| 7620 | ch = *s; |
| 7621 | |
| 7622 | if (ch < maplen) |
| 7623 | x = PyUnicode_READ(mapkind, mapdata, ch); |
| 7624 | else |
| 7625 | x = 0xfffe; /* invalid value */ |
| 7626 | Error: |
| 7627 | if (x == 0xfffe) |
| 7628 | { |
| 7629 | /* undefined mapping */ |
| 7630 | startinpos = s-starts; |
| 7631 | endinpos = startinpos+1; |
| 7632 | if (unicode_decode_call_errorhandler_writer( |
| 7633 | errors, &errorHandler, |
| 7634 | "charmap", "character maps to <undefined>", |
| 7635 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 7636 | writer)) { |
| 7637 | goto onError; |
| 7638 | } |
| 7639 | continue; |
| 7640 | } |
| 7641 | |
| 7642 | if (_PyUnicodeWriter_WriteCharInline(writer, x) < 0) |
| 7643 | goto onError; |
| 7644 | ++s; |
| 7645 | } |
| 7646 | Py_XDECREF(errorHandler); |
| 7647 | Py_XDECREF(exc); |
| 7648 | return 0; |
| 7649 | |
| 7650 | onError: |
| 7651 | Py_XDECREF(errorHandler); |
| 7652 | Py_XDECREF(exc); |
| 7653 | return -1; |
| 7654 | } |
| 7655 | |
| 7656 | static int |
| 7657 | charmap_decode_mapping(const char *s, |
| 7658 | Py_ssize_t size, |
| 7659 | PyObject *mapping, |
| 7660 | const char *errors, |
| 7661 | _PyUnicodeWriter *writer) |
| 7662 | { |
| 7663 | const char *starts = s; |
| 7664 | const char *e; |
| 7665 | Py_ssize_t startinpos, endinpos; |
| 7666 | PyObject *errorHandler = NULL, *exc = NULL; |
| 7667 | unsigned char ch; |
Victor Stinner | f4f2424 | 2013-05-07 01:01:31 +0200 | [diff] [blame] | 7668 | PyObject *key, *item = NULL; |
Victor Stinner | fb161b1 | 2013-04-18 01:44:27 +0200 | [diff] [blame] | 7669 | |
| 7670 | e = s + size; |
| 7671 | |
| 7672 | while (s < e) { |
| 7673 | ch = *s; |
| 7674 | |
| 7675 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 7676 | key = PyLong_FromLong((long)ch); |
| 7677 | if (key == NULL) |
| 7678 | goto onError; |
| 7679 | |
| 7680 | item = PyObject_GetItem(mapping, key); |
| 7681 | Py_DECREF(key); |
| 7682 | if (item == NULL) { |
| 7683 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 7684 | /* No mapping found means: mapping is undefined. */ |
| 7685 | PyErr_Clear(); |
| 7686 | goto Undefined; |
| 7687 | } else |
| 7688 | goto onError; |
| 7689 | } |
| 7690 | |
| 7691 | /* Apply mapping */ |
| 7692 | if (item == Py_None) |
| 7693 | goto Undefined; |
| 7694 | if (PyLong_Check(item)) { |
| 7695 | long value = PyLong_AS_LONG(item); |
| 7696 | if (value == 0xFFFE) |
| 7697 | goto Undefined; |
| 7698 | if (value < 0 || value > MAX_UNICODE) { |
| 7699 | PyErr_Format(PyExc_TypeError, |
| 7700 | "character mapping must be in range(0x%lx)", |
| 7701 | (unsigned long)MAX_UNICODE + 1); |
| 7702 | goto onError; |
| 7703 | } |
| 7704 | |
| 7705 | if (_PyUnicodeWriter_WriteCharInline(writer, value) < 0) |
| 7706 | goto onError; |
| 7707 | } |
| 7708 | else if (PyUnicode_Check(item)) { |
| 7709 | if (PyUnicode_READY(item) == -1) |
| 7710 | goto onError; |
| 7711 | if (PyUnicode_GET_LENGTH(item) == 1) { |
| 7712 | Py_UCS4 value = PyUnicode_READ_CHAR(item, 0); |
| 7713 | if (value == 0xFFFE) |
| 7714 | goto Undefined; |
| 7715 | if (_PyUnicodeWriter_WriteCharInline(writer, value) < 0) |
| 7716 | goto onError; |
| 7717 | } |
| 7718 | else { |
| 7719 | writer->overallocate = 1; |
| 7720 | if (_PyUnicodeWriter_WriteStr(writer, item) == -1) |
| 7721 | goto onError; |
| 7722 | } |
| 7723 | } |
| 7724 | else { |
| 7725 | /* wrong return value */ |
| 7726 | PyErr_SetString(PyExc_TypeError, |
| 7727 | "character mapping must return integer, None or str"); |
| 7728 | goto onError; |
| 7729 | } |
| 7730 | Py_CLEAR(item); |
| 7731 | ++s; |
| 7732 | continue; |
| 7733 | |
| 7734 | Undefined: |
| 7735 | /* undefined mapping */ |
| 7736 | Py_CLEAR(item); |
| 7737 | startinpos = s-starts; |
| 7738 | endinpos = startinpos+1; |
| 7739 | if (unicode_decode_call_errorhandler_writer( |
| 7740 | errors, &errorHandler, |
| 7741 | "charmap", "character maps to <undefined>", |
| 7742 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 7743 | writer)) { |
| 7744 | goto onError; |
| 7745 | } |
| 7746 | } |
| 7747 | Py_XDECREF(errorHandler); |
| 7748 | Py_XDECREF(exc); |
| 7749 | return 0; |
| 7750 | |
| 7751 | onError: |
| 7752 | Py_XDECREF(item); |
| 7753 | Py_XDECREF(errorHandler); |
| 7754 | Py_XDECREF(exc); |
| 7755 | return -1; |
| 7756 | } |
| 7757 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7758 | PyObject * |
| 7759 | PyUnicode_DecodeCharmap(const char *s, |
| 7760 | Py_ssize_t size, |
| 7761 | PyObject *mapping, |
| 7762 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7763 | { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7764 | _PyUnicodeWriter writer; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7765 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7766 | /* Default to Latin-1 */ |
| 7767 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7768 | return PyUnicode_DecodeLatin1(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7769 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7770 | if (size == 0) |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 7771 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 7772 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 7773 | writer.min_length = size; |
| 7774 | if (_PyUnicodeWriter_Prepare(&writer, writer.min_length, 127) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7775 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7776 | |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7777 | if (PyUnicode_CheckExact(mapping)) { |
Victor Stinner | fb161b1 | 2013-04-18 01:44:27 +0200 | [diff] [blame] | 7778 | if (charmap_decode_string(s, size, mapping, errors, &writer) < 0) |
| 7779 | goto onError; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7780 | } |
| 7781 | else { |
Victor Stinner | fb161b1 | 2013-04-18 01:44:27 +0200 | [diff] [blame] | 7782 | if (charmap_decode_mapping(s, size, mapping, errors, &writer) < 0) |
| 7783 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7784 | } |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7785 | return _PyUnicodeWriter_Finish(&writer); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7786 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7787 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7788 | _PyUnicodeWriter_Dealloc(&writer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7789 | return NULL; |
| 7790 | } |
| 7791 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7792 | /* Charmap encoding: the lookup table */ |
| 7793 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7794 | struct encoding_map { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7795 | PyObject_HEAD |
| 7796 | unsigned char level1[32]; |
| 7797 | int count2, count3; |
| 7798 | unsigned char level23[1]; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7799 | }; |
| 7800 | |
| 7801 | static PyObject* |
| 7802 | encoding_map_size(PyObject *obj, PyObject* args) |
| 7803 | { |
| 7804 | struct encoding_map *map = (struct encoding_map*)obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7805 | return PyLong_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7806 | 128*map->count3); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7807 | } |
| 7808 | |
| 7809 | static PyMethodDef encoding_map_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7810 | {"size", encoding_map_size, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7811 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 7812 | { 0 } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7813 | }; |
| 7814 | |
| 7815 | static void |
| 7816 | encoding_map_dealloc(PyObject* o) |
| 7817 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7818 | PyObject_FREE(o); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7819 | } |
| 7820 | |
| 7821 | static PyTypeObject EncodingMapType = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7822 | PyVarObject_HEAD_INIT(NULL, 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7823 | "EncodingMap", /*tp_name*/ |
| 7824 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 7825 | 0, /*tp_itemsize*/ |
| 7826 | /* methods */ |
| 7827 | encoding_map_dealloc, /*tp_dealloc*/ |
| 7828 | 0, /*tp_print*/ |
| 7829 | 0, /*tp_getattr*/ |
| 7830 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 7831 | 0, /*tp_reserved*/ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7832 | 0, /*tp_repr*/ |
| 7833 | 0, /*tp_as_number*/ |
| 7834 | 0, /*tp_as_sequence*/ |
| 7835 | 0, /*tp_as_mapping*/ |
| 7836 | 0, /*tp_hash*/ |
| 7837 | 0, /*tp_call*/ |
| 7838 | 0, /*tp_str*/ |
| 7839 | 0, /*tp_getattro*/ |
| 7840 | 0, /*tp_setattro*/ |
| 7841 | 0, /*tp_as_buffer*/ |
| 7842 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 7843 | 0, /*tp_doc*/ |
| 7844 | 0, /*tp_traverse*/ |
| 7845 | 0, /*tp_clear*/ |
| 7846 | 0, /*tp_richcompare*/ |
| 7847 | 0, /*tp_weaklistoffset*/ |
| 7848 | 0, /*tp_iter*/ |
| 7849 | 0, /*tp_iternext*/ |
| 7850 | encoding_map_methods, /*tp_methods*/ |
| 7851 | 0, /*tp_members*/ |
| 7852 | 0, /*tp_getset*/ |
| 7853 | 0, /*tp_base*/ |
| 7854 | 0, /*tp_dict*/ |
| 7855 | 0, /*tp_descr_get*/ |
| 7856 | 0, /*tp_descr_set*/ |
| 7857 | 0, /*tp_dictoffset*/ |
| 7858 | 0, /*tp_init*/ |
| 7859 | 0, /*tp_alloc*/ |
| 7860 | 0, /*tp_new*/ |
| 7861 | 0, /*tp_free*/ |
| 7862 | 0, /*tp_is_gc*/ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7863 | }; |
| 7864 | |
| 7865 | PyObject* |
| 7866 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 7867 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7868 | PyObject *result; |
| 7869 | struct encoding_map *mresult; |
| 7870 | int i; |
| 7871 | int need_dict = 0; |
| 7872 | unsigned char level1[32]; |
| 7873 | unsigned char level2[512]; |
| 7874 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 7875 | int count2 = 0, count3 = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7876 | int kind; |
| 7877 | void *data; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7878 | Py_ssize_t length; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7879 | Py_UCS4 ch; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7880 | |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7881 | if (!PyUnicode_Check(string) || !PyUnicode_GET_LENGTH(string)) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7882 | PyErr_BadArgument(); |
| 7883 | return NULL; |
| 7884 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7885 | kind = PyUnicode_KIND(string); |
| 7886 | data = PyUnicode_DATA(string); |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7887 | length = PyUnicode_GET_LENGTH(string); |
| 7888 | length = Py_MIN(length, 256); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7889 | memset(level1, 0xFF, sizeof level1); |
| 7890 | memset(level2, 0xFF, sizeof level2); |
| 7891 | |
| 7892 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 7893 | or if there are non-BMP characters, we need to use |
| 7894 | a mapping dictionary. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7895 | if (PyUnicode_READ(kind, data, 0) != 0) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7896 | need_dict = 1; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7897 | for (i = 1; i < length; i++) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7898 | int l1, l2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7899 | ch = PyUnicode_READ(kind, data, i); |
| 7900 | if (ch == 0 || ch > 0xFFFF) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7901 | need_dict = 1; |
| 7902 | break; |
| 7903 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7904 | if (ch == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7905 | /* unmapped character */ |
| 7906 | continue; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7907 | l1 = ch >> 11; |
| 7908 | l2 = ch >> 7; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7909 | if (level1[l1] == 0xFF) |
| 7910 | level1[l1] = count2++; |
| 7911 | if (level2[l2] == 0xFF) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7912 | level2[l2] = count3++; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7913 | } |
| 7914 | |
| 7915 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 7916 | need_dict = 1; |
| 7917 | |
| 7918 | if (need_dict) { |
| 7919 | PyObject *result = PyDict_New(); |
| 7920 | PyObject *key, *value; |
| 7921 | if (!result) |
| 7922 | return NULL; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7923 | for (i = 0; i < length; i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7924 | key = PyLong_FromLong(PyUnicode_READ(kind, data, i)); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7925 | value = PyLong_FromLong(i); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7926 | if (!key || !value) |
| 7927 | goto failed1; |
| 7928 | if (PyDict_SetItem(result, key, value) == -1) |
| 7929 | goto failed1; |
| 7930 | Py_DECREF(key); |
| 7931 | Py_DECREF(value); |
| 7932 | } |
| 7933 | return result; |
| 7934 | failed1: |
| 7935 | Py_XDECREF(key); |
| 7936 | Py_XDECREF(value); |
| 7937 | Py_DECREF(result); |
| 7938 | return NULL; |
| 7939 | } |
| 7940 | |
| 7941 | /* Create a three-level trie */ |
| 7942 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 7943 | 16*count2 + 128*count3 - 1); |
| 7944 | if (!result) |
| 7945 | return PyErr_NoMemory(); |
| 7946 | PyObject_Init(result, &EncodingMapType); |
| 7947 | mresult = (struct encoding_map*)result; |
| 7948 | mresult->count2 = count2; |
| 7949 | mresult->count3 = count3; |
| 7950 | mlevel1 = mresult->level1; |
| 7951 | mlevel2 = mresult->level23; |
| 7952 | mlevel3 = mresult->level23 + 16*count2; |
| 7953 | memcpy(mlevel1, level1, 32); |
| 7954 | memset(mlevel2, 0xFF, 16*count2); |
| 7955 | memset(mlevel3, 0, 128*count3); |
| 7956 | count3 = 0; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7957 | for (i = 1; i < length; i++) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7958 | int o1, o2, o3, i2, i3; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7959 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 7960 | if (ch == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7961 | /* unmapped character */ |
| 7962 | continue; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7963 | o1 = ch>>11; |
| 7964 | o2 = (ch>>7) & 0xF; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7965 | i2 = 16*mlevel1[o1] + o2; |
| 7966 | if (mlevel2[i2] == 0xFF) |
| 7967 | mlevel2[i2] = count3++; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7968 | o3 = ch & 0x7F; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7969 | i3 = 128*mlevel2[i2] + o3; |
| 7970 | mlevel3[i3] = i; |
| 7971 | } |
| 7972 | return result; |
| 7973 | } |
| 7974 | |
| 7975 | static int |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 7976 | encoding_map_lookup(Py_UCS4 c, PyObject *mapping) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7977 | { |
| 7978 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 7979 | int l1 = c>>11; |
| 7980 | int l2 = (c>>7) & 0xF; |
| 7981 | int l3 = c & 0x7F; |
| 7982 | int i; |
| 7983 | |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 7984 | if (c > 0xFFFF) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7985 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7986 | if (c == 0) |
| 7987 | return 0; |
| 7988 | /* level 1*/ |
| 7989 | i = map->level1[l1]; |
| 7990 | if (i == 0xFF) { |
| 7991 | return -1; |
| 7992 | } |
| 7993 | /* level 2*/ |
| 7994 | i = map->level23[16*i+l2]; |
| 7995 | if (i == 0xFF) { |
| 7996 | return -1; |
| 7997 | } |
| 7998 | /* level 3 */ |
| 7999 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 8000 | if (i == 0) { |
| 8001 | return -1; |
| 8002 | } |
| 8003 | return i; |
| 8004 | } |
| 8005 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8006 | /* Lookup the character ch in the mapping. If the character |
| 8007 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 8008 | error occurred). */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8009 | static PyObject * |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 8010 | charmapencode_lookup(Py_UCS4 c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8011 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8012 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8013 | PyObject *x; |
| 8014 | |
| 8015 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8016 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8017 | x = PyObject_GetItem(mapping, w); |
| 8018 | Py_DECREF(w); |
| 8019 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8020 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 8021 | /* No mapping found means: mapping is undefined. */ |
| 8022 | PyErr_Clear(); |
| 8023 | x = Py_None; |
| 8024 | Py_INCREF(x); |
| 8025 | return x; |
| 8026 | } else |
| 8027 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8028 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 8029 | else if (x == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8030 | return x; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8031 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8032 | long value = PyLong_AS_LONG(x); |
| 8033 | if (value < 0 || value > 255) { |
| 8034 | PyErr_SetString(PyExc_TypeError, |
| 8035 | "character mapping must be in range(256)"); |
| 8036 | Py_DECREF(x); |
| 8037 | return NULL; |
| 8038 | } |
| 8039 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8040 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8041 | else if (PyBytes_Check(x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8042 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8043 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8044 | /* wrong return value */ |
| 8045 | PyErr_Format(PyExc_TypeError, |
| 8046 | "character mapping must return integer, bytes or None, not %.400s", |
| 8047 | x->ob_type->tp_name); |
| 8048 | Py_DECREF(x); |
| 8049 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8050 | } |
| 8051 | } |
| 8052 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8053 | static int |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8054 | charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8055 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8056 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
| 8057 | /* exponentially overallocate to minimize reallocations */ |
| 8058 | if (requiredsize < 2*outsize) |
| 8059 | requiredsize = 2*outsize; |
| 8060 | if (_PyBytes_Resize(outobj, requiredsize)) |
| 8061 | return -1; |
| 8062 | return 0; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8063 | } |
| 8064 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8065 | typedef enum charmapencode_result { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8066 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8067 | } charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8068 | /* 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] | 8069 | various state variables. Resize the output bytes object if not enough |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8070 | space is available. Return a new reference to the object that |
| 8071 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 8072 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 8073 | reallocation error occurred. The caller must decref the result */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8074 | static charmapencode_result |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 8075 | charmapencode_output(Py_UCS4 c, PyObject *mapping, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8076 | PyObject **outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8077 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8078 | PyObject *rep; |
| 8079 | char *outstart; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8080 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8081 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 8082 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8083 | int res = encoding_map_lookup(c, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8084 | Py_ssize_t requiredsize = *outpos+1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8085 | if (res == -1) |
| 8086 | return enc_FAILED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8087 | if (outsize<requiredsize) |
| 8088 | if (charmapencode_resize(outobj, outpos, requiredsize)) |
| 8089 | return enc_EXCEPTION; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8090 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8091 | outstart[(*outpos)++] = (char)res; |
| 8092 | return enc_SUCCESS; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8093 | } |
| 8094 | |
| 8095 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8096 | if (rep==NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8097 | return enc_EXCEPTION; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8098 | else if (rep==Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8099 | Py_DECREF(rep); |
| 8100 | return enc_FAILED; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8101 | } else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8102 | if (PyLong_Check(rep)) { |
| 8103 | Py_ssize_t requiredsize = *outpos+1; |
| 8104 | if (outsize<requiredsize) |
| 8105 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 8106 | Py_DECREF(rep); |
| 8107 | return enc_EXCEPTION; |
| 8108 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8109 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8110 | outstart[(*outpos)++] = (char)PyLong_AS_LONG(rep); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8111 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8112 | else { |
| 8113 | const char *repchars = PyBytes_AS_STRING(rep); |
| 8114 | Py_ssize_t repsize = PyBytes_GET_SIZE(rep); |
| 8115 | Py_ssize_t requiredsize = *outpos+repsize; |
| 8116 | if (outsize<requiredsize) |
| 8117 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 8118 | Py_DECREF(rep); |
| 8119 | return enc_EXCEPTION; |
| 8120 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8121 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8122 | memcpy(outstart + *outpos, repchars, repsize); |
| 8123 | *outpos += repsize; |
| 8124 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8125 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8126 | Py_DECREF(rep); |
| 8127 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8128 | } |
| 8129 | |
| 8130 | /* handle an error in PyUnicode_EncodeCharmap |
| 8131 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8132 | static int |
| 8133 | charmap_encoding_error( |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8134 | PyObject *unicode, Py_ssize_t *inpos, PyObject *mapping, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8135 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 8136 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8137 | PyObject **res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8138 | { |
| 8139 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8140 | Py_ssize_t size, repsize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8141 | Py_ssize_t newpos; |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8142 | enum PyUnicode_Kind kind; |
| 8143 | void *data; |
| 8144 | Py_ssize_t index; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8145 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8146 | Py_ssize_t collstartpos = *inpos; |
| 8147 | Py_ssize_t collendpos = *inpos+1; |
| 8148 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8149 | char *encoding = "charmap"; |
| 8150 | char *reason = "character maps to <undefined>"; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8151 | charmapencode_result x; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8152 | Py_UCS4 ch; |
Brian Curtin | 2787ea4 | 2011-11-02 15:09:37 -0500 | [diff] [blame] | 8153 | int val; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8154 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 8155 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8156 | return -1; |
| 8157 | size = PyUnicode_GET_LENGTH(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8158 | /* find all unencodable characters */ |
| 8159 | while (collendpos < size) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8160 | PyObject *rep; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 8161 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8162 | ch = PyUnicode_READ_CHAR(unicode, collendpos); |
Brian Curtin | 2787ea4 | 2011-11-02 15:09:37 -0500 | [diff] [blame] | 8163 | val = encoding_map_lookup(ch, mapping); |
| 8164 | if (val != -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8165 | break; |
| 8166 | ++collendpos; |
| 8167 | continue; |
| 8168 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8169 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8170 | ch = PyUnicode_READ_CHAR(unicode, collendpos); |
| 8171 | rep = charmapencode_lookup(ch, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8172 | if (rep==NULL) |
| 8173 | return -1; |
| 8174 | else if (rep!=Py_None) { |
| 8175 | Py_DECREF(rep); |
| 8176 | break; |
| 8177 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8178 | Py_DECREF(rep); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8179 | ++collendpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8180 | } |
| 8181 | /* cache callback name lookup |
| 8182 | * (if not done yet, i.e. it's the first error) */ |
| 8183 | if (*known_errorHandler==-1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8184 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 8185 | *known_errorHandler = 1; |
| 8186 | else if (!strcmp(errors, "replace")) |
| 8187 | *known_errorHandler = 2; |
| 8188 | else if (!strcmp(errors, "ignore")) |
| 8189 | *known_errorHandler = 3; |
| 8190 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 8191 | *known_errorHandler = 4; |
| 8192 | else |
| 8193 | *known_errorHandler = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8194 | } |
| 8195 | switch (*known_errorHandler) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8196 | case 1: /* strict */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8197 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8198 | return -1; |
| 8199 | case 2: /* replace */ |
| 8200 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8201 | x = charmapencode_output('?', mapping, res, respos); |
| 8202 | if (x==enc_EXCEPTION) { |
| 8203 | return -1; |
| 8204 | } |
| 8205 | else if (x==enc_FAILED) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8206 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8207 | return -1; |
| 8208 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8209 | } |
| 8210 | /* fall through */ |
| 8211 | case 3: /* ignore */ |
| 8212 | *inpos = collendpos; |
| 8213 | break; |
| 8214 | case 4: /* xmlcharrefreplace */ |
| 8215 | /* generate replacement (temporarily (mis)uses p) */ |
| 8216 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8217 | char buffer[2+29+1+1]; |
| 8218 | char *cp; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8219 | sprintf(buffer, "&#%d;", (int)PyUnicode_READ_CHAR(unicode, collpos)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8220 | for (cp = buffer; *cp; ++cp) { |
| 8221 | x = charmapencode_output(*cp, mapping, res, respos); |
| 8222 | if (x==enc_EXCEPTION) |
| 8223 | return -1; |
| 8224 | else if (x==enc_FAILED) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8225 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8226 | return -1; |
| 8227 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8228 | } |
| 8229 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8230 | *inpos = collendpos; |
| 8231 | break; |
| 8232 | default: |
| 8233 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8234 | encoding, reason, unicode, exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8235 | collstartpos, collendpos, &newpos); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8236 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8237 | return -1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8238 | if (PyBytes_Check(repunicode)) { |
| 8239 | /* Directly copy bytes result to output. */ |
| 8240 | Py_ssize_t outsize = PyBytes_Size(*res); |
| 8241 | Py_ssize_t requiredsize; |
| 8242 | repsize = PyBytes_Size(repunicode); |
| 8243 | requiredsize = *respos + repsize; |
| 8244 | if (requiredsize > outsize) |
| 8245 | /* Make room for all additional bytes. */ |
| 8246 | if (charmapencode_resize(res, respos, requiredsize)) { |
| 8247 | Py_DECREF(repunicode); |
| 8248 | return -1; |
| 8249 | } |
| 8250 | memcpy(PyBytes_AsString(*res) + *respos, |
| 8251 | PyBytes_AsString(repunicode), repsize); |
| 8252 | *respos += repsize; |
| 8253 | *inpos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8254 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8255 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8256 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8257 | /* generate replacement */ |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 8258 | if (PyUnicode_READY(repunicode) == -1) { |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8259 | Py_DECREF(repunicode); |
| 8260 | return -1; |
| 8261 | } |
Victor Stinner | 9e30aa5 | 2011-11-21 02:49:52 +0100 | [diff] [blame] | 8262 | repsize = PyUnicode_GET_LENGTH(repunicode); |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8263 | data = PyUnicode_DATA(repunicode); |
| 8264 | kind = PyUnicode_KIND(repunicode); |
| 8265 | for (index = 0; index < repsize; index++) { |
| 8266 | Py_UCS4 repch = PyUnicode_READ(kind, data, index); |
| 8267 | x = charmapencode_output(repch, mapping, res, respos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8268 | if (x==enc_EXCEPTION) { |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8269 | Py_DECREF(repunicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8270 | return -1; |
| 8271 | } |
| 8272 | else if (x==enc_FAILED) { |
| 8273 | Py_DECREF(repunicode); |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8274 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8275 | return -1; |
| 8276 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8277 | } |
| 8278 | *inpos = newpos; |
| 8279 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8280 | } |
| 8281 | return 0; |
| 8282 | } |
| 8283 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8284 | PyObject * |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8285 | _PyUnicode_EncodeCharmap(PyObject *unicode, |
| 8286 | PyObject *mapping, |
| 8287 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8288 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8289 | /* output object */ |
| 8290 | PyObject *res = NULL; |
| 8291 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8292 | Py_ssize_t inpos = 0; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8293 | Py_ssize_t size; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8294 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8295 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8296 | PyObject *errorHandler = NULL; |
| 8297 | PyObject *exc = NULL; |
| 8298 | /* the following variable is used for caching string comparisons |
| 8299 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 8300 | * 3=ignore, 4=xmlcharrefreplace */ |
| 8301 | int known_errorHandler = -1; |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 8302 | void *data; |
| 8303 | int kind; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8304 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 8305 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8306 | return NULL; |
| 8307 | size = PyUnicode_GET_LENGTH(unicode); |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 8308 | data = PyUnicode_DATA(unicode); |
| 8309 | kind = PyUnicode_KIND(unicode); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8310 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8311 | /* Default to Latin-1 */ |
| 8312 | if (mapping == NULL) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8313 | return unicode_encode_ucs1(unicode, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8314 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8315 | /* allocate enough for a simple encoding without |
| 8316 | replacements, if we need more, we'll resize */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8317 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8318 | if (res == NULL) |
| 8319 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 8320 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8321 | return res; |
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 | while (inpos<size) { |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 8324 | Py_UCS4 ch = PyUnicode_READ(kind, data, inpos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8325 | /* try to encode it */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8326 | charmapencode_result x = charmapencode_output(ch, mapping, &res, &respos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8327 | if (x==enc_EXCEPTION) /* error */ |
| 8328 | goto onError; |
| 8329 | if (x==enc_FAILED) { /* unencodable character */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8330 | if (charmap_encoding_error(unicode, &inpos, mapping, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8331 | &exc, |
| 8332 | &known_errorHandler, &errorHandler, errors, |
| 8333 | &res, &respos)) { |
| 8334 | goto onError; |
| 8335 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8336 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8337 | else |
| 8338 | /* done with this character => adjust input position */ |
| 8339 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8340 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8341 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8342 | /* Resize if we allocated to much */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8343 | if (respos<PyBytes_GET_SIZE(res)) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 8344 | if (_PyBytes_Resize(&res, respos) < 0) |
| 8345 | goto onError; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8346 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8347 | Py_XDECREF(exc); |
| 8348 | Py_XDECREF(errorHandler); |
| 8349 | return res; |
| 8350 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8351 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8352 | Py_XDECREF(res); |
| 8353 | Py_XDECREF(exc); |
| 8354 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8355 | return NULL; |
| 8356 | } |
| 8357 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8358 | /* Deprecated */ |
| 8359 | PyObject * |
| 8360 | PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
| 8361 | Py_ssize_t size, |
| 8362 | PyObject *mapping, |
| 8363 | const char *errors) |
| 8364 | { |
| 8365 | PyObject *result; |
| 8366 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 8367 | if (unicode == NULL) |
| 8368 | return NULL; |
| 8369 | result = _PyUnicode_EncodeCharmap(unicode, mapping, errors); |
| 8370 | Py_DECREF(unicode); |
Victor Stinner | fc026c9 | 2011-11-04 00:24:51 +0100 | [diff] [blame] | 8371 | return result; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8372 | } |
| 8373 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8374 | PyObject * |
| 8375 | PyUnicode_AsCharmapString(PyObject *unicode, |
| 8376 | PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8377 | { |
| 8378 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8379 | PyErr_BadArgument(); |
| 8380 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8381 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8382 | return _PyUnicode_EncodeCharmap(unicode, mapping, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8383 | } |
| 8384 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8385 | /* create or adjust a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8386 | static void |
| 8387 | make_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8388 | PyObject *unicode, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8389 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8390 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8391 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8392 | if (*exceptionObject == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8393 | *exceptionObject = _PyUnicodeTranslateError_Create( |
| 8394 | unicode, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8395 | } |
| 8396 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8397 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 8398 | goto onError; |
| 8399 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 8400 | goto onError; |
| 8401 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 8402 | goto onError; |
| 8403 | return; |
| 8404 | onError: |
| 8405 | Py_DECREF(*exceptionObject); |
| 8406 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8407 | } |
| 8408 | } |
| 8409 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8410 | /* error handling callback helper: |
| 8411 | build arguments, call the callback and check the arguments, |
| 8412 | put the result into newpos and return the replacement string, which |
| 8413 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8414 | static PyObject * |
| 8415 | unicode_translate_call_errorhandler(const char *errors, |
| 8416 | PyObject **errorHandler, |
| 8417 | const char *reason, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8418 | PyObject *unicode, PyObject **exceptionObject, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8419 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8420 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8421 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8422 | 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] | 8423 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8424 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8425 | PyObject *restuple; |
| 8426 | PyObject *resunicode; |
| 8427 | |
| 8428 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8429 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8430 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8431 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8432 | } |
| 8433 | |
| 8434 | make_translate_exception(exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8435 | unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8436 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8437 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8438 | |
| 8439 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8440 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8441 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8442 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8443 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 8444 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8445 | Py_DECREF(restuple); |
| 8446 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8447 | } |
| 8448 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8449 | &resunicode, &i_newpos)) { |
| 8450 | Py_DECREF(restuple); |
| 8451 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8452 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8453 | if (i_newpos<0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8454 | *newpos = PyUnicode_GET_LENGTH(unicode)+i_newpos; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8455 | else |
| 8456 | *newpos = i_newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8457 | if (*newpos<0 || *newpos>PyUnicode_GET_LENGTH(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8458 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 8459 | Py_DECREF(restuple); |
| 8460 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 8461 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8462 | Py_INCREF(resunicode); |
| 8463 | Py_DECREF(restuple); |
| 8464 | return resunicode; |
| 8465 | } |
| 8466 | |
| 8467 | /* Lookup the character ch in the mapping and put the result in result, |
| 8468 | which must be decrefed by the caller. |
| 8469 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8470 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8471 | charmaptranslate_lookup(Py_UCS4 c, PyObject *mapping, PyObject **result) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8472 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8473 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8474 | PyObject *x; |
| 8475 | |
| 8476 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8477 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8478 | x = PyObject_GetItem(mapping, w); |
| 8479 | Py_DECREF(w); |
| 8480 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8481 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 8482 | /* No mapping found means: use 1:1 mapping. */ |
| 8483 | PyErr_Clear(); |
| 8484 | *result = NULL; |
| 8485 | return 0; |
| 8486 | } else |
| 8487 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8488 | } |
| 8489 | else if (x == Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8490 | *result = x; |
| 8491 | return 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8492 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8493 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8494 | long value = PyLong_AS_LONG(x); |
| 8495 | long max = PyUnicode_GetMax(); |
| 8496 | if (value < 0 || value > max) { |
| 8497 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5a2f7e60 | 2007-10-24 21:13:09 +0000 | [diff] [blame] | 8498 | "character mapping must be in range(0x%x)", max+1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8499 | Py_DECREF(x); |
| 8500 | return -1; |
| 8501 | } |
| 8502 | *result = x; |
| 8503 | return 0; |
| 8504 | } |
| 8505 | else if (PyUnicode_Check(x)) { |
| 8506 | *result = x; |
| 8507 | return 0; |
| 8508 | } |
| 8509 | else { |
| 8510 | /* wrong return value */ |
| 8511 | PyErr_SetString(PyExc_TypeError, |
| 8512 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8513 | Py_DECREF(x); |
| 8514 | return -1; |
| 8515 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8516 | } |
| 8517 | /* ensure that *outobj is at least requiredsize characters long, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8518 | if not reallocate and adjust various state variables. |
| 8519 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8520 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8521 | charmaptranslate_makespace(Py_UCS4 **outobj, Py_ssize_t *psize, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8522 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8523 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8524 | Py_ssize_t oldsize = *psize; |
Kristjan Valur Jonsson | 85634d7 | 2012-05-31 09:37:31 +0000 | [diff] [blame] | 8525 | Py_UCS4 *new_outobj; |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 8526 | if (requiredsize > oldsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8527 | /* exponentially overallocate to minimize reallocations */ |
| 8528 | if (requiredsize < 2 * oldsize) |
| 8529 | requiredsize = 2 * oldsize; |
Kristjan Valur Jonsson | 85634d7 | 2012-05-31 09:37:31 +0000 | [diff] [blame] | 8530 | new_outobj = PyMem_Realloc(*outobj, requiredsize * sizeof(Py_UCS4)); |
| 8531 | if (new_outobj == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8532 | return -1; |
Kristjan Valur Jonsson | 85634d7 | 2012-05-31 09:37:31 +0000 | [diff] [blame] | 8533 | *outobj = new_outobj; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8534 | *psize = requiredsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8535 | } |
| 8536 | return 0; |
| 8537 | } |
| 8538 | /* lookup the character, put the result in the output string and adjust |
| 8539 | various state variables. Return a new reference to the object that |
| 8540 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 8541 | undefined (in which case no character was written). |
| 8542 | The called must decref result. |
| 8543 | Return 0 on success, -1 on error. */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8544 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8545 | charmaptranslate_output(PyObject *input, Py_ssize_t ipos, |
| 8546 | PyObject *mapping, Py_UCS4 **output, |
| 8547 | Py_ssize_t *osize, Py_ssize_t *opos, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8548 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8549 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8550 | Py_UCS4 curinp = PyUnicode_READ_CHAR(input, ipos); |
| 8551 | if (charmaptranslate_lookup(curinp, mapping, res)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8552 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8553 | if (*res==NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8554 | /* not found => default to 1:1 mapping */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8555 | (*output)[(*opos)++] = curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8556 | } |
| 8557 | else if (*res==Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8558 | ; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8559 | else if (PyLong_Check(*res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8560 | /* 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] | 8561 | (*output)[(*opos)++] = (Py_UCS4)PyLong_AS_LONG(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8562 | } |
| 8563 | else if (PyUnicode_Check(*res)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8564 | Py_ssize_t repsize; |
| 8565 | if (PyUnicode_READY(*res) == -1) |
| 8566 | return -1; |
| 8567 | repsize = PyUnicode_GET_LENGTH(*res); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8568 | if (repsize==1) { |
| 8569 | /* 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] | 8570 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8571 | } |
| 8572 | else if (repsize!=0) { |
| 8573 | /* more than one character */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8574 | Py_ssize_t requiredsize = *opos + |
| 8575 | (PyUnicode_GET_LENGTH(input) - ipos) + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8576 | repsize - 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8577 | Py_ssize_t i; |
| 8578 | if (charmaptranslate_makespace(output, osize, requiredsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8579 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8580 | for(i = 0; i < repsize; i++) |
| 8581 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, i); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8582 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8583 | } |
| 8584 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8585 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8586 | return 0; |
| 8587 | } |
| 8588 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8589 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8590 | _PyUnicode_TranslateCharmap(PyObject *input, |
| 8591 | PyObject *mapping, |
| 8592 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8593 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8594 | /* input object */ |
| 8595 | char *idata; |
| 8596 | Py_ssize_t size, i; |
| 8597 | int kind; |
| 8598 | /* output buffer */ |
| 8599 | Py_UCS4 *output = NULL; |
| 8600 | Py_ssize_t osize; |
| 8601 | PyObject *res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8602 | /* current output position */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8603 | Py_ssize_t opos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8604 | char *reason = "character maps to <undefined>"; |
| 8605 | PyObject *errorHandler = NULL; |
| 8606 | PyObject *exc = NULL; |
| 8607 | /* the following variable is used for caching string comparisons |
| 8608 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 8609 | * 3=ignore, 4=xmlcharrefreplace */ |
| 8610 | int known_errorHandler = -1; |
| 8611 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8612 | if (mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8613 | PyErr_BadArgument(); |
| 8614 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8615 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8616 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8617 | if (PyUnicode_READY(input) == -1) |
| 8618 | return NULL; |
| 8619 | idata = (char*)PyUnicode_DATA(input); |
| 8620 | kind = PyUnicode_KIND(input); |
| 8621 | size = PyUnicode_GET_LENGTH(input); |
| 8622 | i = 0; |
| 8623 | |
| 8624 | if (size == 0) { |
| 8625 | Py_INCREF(input); |
| 8626 | return input; |
| 8627 | } |
| 8628 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8629 | /* allocate enough for a simple 1:1 translation without |
| 8630 | replacements, if we need more, we'll resize */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8631 | osize = size; |
| 8632 | output = PyMem_Malloc(osize * sizeof(Py_UCS4)); |
| 8633 | opos = 0; |
| 8634 | if (output == NULL) { |
| 8635 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8636 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8637 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8638 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8639 | while (i<size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8640 | /* try to encode it */ |
| 8641 | PyObject *x = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8642 | if (charmaptranslate_output(input, i, mapping, |
| 8643 | &output, &osize, &opos, &x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8644 | Py_XDECREF(x); |
| 8645 | goto onError; |
| 8646 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8647 | Py_XDECREF(x); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8648 | if (x!=Py_None) /* it worked => adjust input pointer */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8649 | ++i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8650 | else { /* untranslatable character */ |
| 8651 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
| 8652 | Py_ssize_t repsize; |
| 8653 | Py_ssize_t newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8654 | Py_ssize_t uni2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8655 | /* startpos for collecting untranslatable chars */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8656 | Py_ssize_t collstart = i; |
| 8657 | Py_ssize_t collend = i+1; |
| 8658 | Py_ssize_t coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8659 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8660 | /* find all untranslatable characters */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8661 | while (collend < size) { |
| 8662 | if (charmaptranslate_lookup(PyUnicode_READ(kind,idata, collend), mapping, &x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8663 | goto onError; |
| 8664 | Py_XDECREF(x); |
| 8665 | if (x!=Py_None) |
| 8666 | break; |
| 8667 | ++collend; |
| 8668 | } |
| 8669 | /* cache callback name lookup |
| 8670 | * (if not done yet, i.e. it's the first error) */ |
| 8671 | if (known_errorHandler==-1) { |
| 8672 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 8673 | known_errorHandler = 1; |
| 8674 | else if (!strcmp(errors, "replace")) |
| 8675 | known_errorHandler = 2; |
| 8676 | else if (!strcmp(errors, "ignore")) |
| 8677 | known_errorHandler = 3; |
| 8678 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 8679 | known_errorHandler = 4; |
| 8680 | else |
| 8681 | known_errorHandler = 0; |
| 8682 | } |
| 8683 | switch (known_errorHandler) { |
| 8684 | case 1: /* strict */ |
Victor Stinner | 6fa6275 | 2012-10-23 02:51:50 +0200 | [diff] [blame] | 8685 | make_translate_exception(&exc, |
| 8686 | input, collstart, collend, reason); |
| 8687 | if (exc != NULL) |
| 8688 | PyCodec_StrictErrors(exc); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8689 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8690 | case 2: /* replace */ |
| 8691 | /* 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] | 8692 | for (coll = collstart; coll<collend; coll++) |
| 8693 | output[opos++] = '?'; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8694 | /* fall through */ |
| 8695 | case 3: /* ignore */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8696 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8697 | break; |
| 8698 | case 4: /* xmlcharrefreplace */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8699 | /* generate replacement (temporarily (mis)uses i) */ |
| 8700 | for (i = collstart; i < collend; ++i) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8701 | char buffer[2+29+1+1]; |
| 8702 | char *cp; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8703 | sprintf(buffer, "&#%d;", PyUnicode_READ(kind, idata, i)); |
| 8704 | if (charmaptranslate_makespace(&output, &osize, |
| 8705 | opos+strlen(buffer)+(size-collend))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8706 | goto onError; |
| 8707 | for (cp = buffer; *cp; ++cp) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8708 | output[opos++] = *cp; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8709 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8710 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8711 | break; |
| 8712 | default: |
| 8713 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8714 | reason, input, &exc, |
| 8715 | collstart, collend, &newpos); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 8716 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8717 | goto onError; |
Benjamin Peterson | 9ca3ffa | 2012-01-01 16:04:29 -0600 | [diff] [blame] | 8718 | if (PyUnicode_READY(repunicode) == -1) { |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 8719 | Py_DECREF(repunicode); |
| 8720 | goto onError; |
| 8721 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8722 | /* generate replacement */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8723 | repsize = PyUnicode_GET_LENGTH(repunicode); |
| 8724 | if (charmaptranslate_makespace(&output, &osize, |
| 8725 | opos+repsize+(size-collend))) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8726 | Py_DECREF(repunicode); |
| 8727 | goto onError; |
| 8728 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8729 | for (uni2 = 0; repsize-->0; ++uni2) |
| 8730 | output[opos++] = PyUnicode_READ_CHAR(repunicode, uni2); |
| 8731 | i = newpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8732 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8733 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8734 | } |
| 8735 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8736 | res = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, output, opos); |
| 8737 | if (!res) |
| 8738 | goto onError; |
| 8739 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8740 | Py_XDECREF(exc); |
| 8741 | Py_XDECREF(errorHandler); |
| 8742 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8743 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8744 | onError: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8745 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8746 | Py_XDECREF(exc); |
| 8747 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8748 | return NULL; |
| 8749 | } |
| 8750 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8751 | /* Deprecated. Use PyUnicode_Translate instead. */ |
| 8752 | PyObject * |
| 8753 | PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
| 8754 | Py_ssize_t size, |
| 8755 | PyObject *mapping, |
| 8756 | const char *errors) |
| 8757 | { |
Christian Heimes | 5f520f4 | 2012-09-11 14:03:25 +0200 | [diff] [blame] | 8758 | PyObject *result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8759 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 8760 | if (!unicode) |
| 8761 | return NULL; |
Christian Heimes | 5f520f4 | 2012-09-11 14:03:25 +0200 | [diff] [blame] | 8762 | result = _PyUnicode_TranslateCharmap(unicode, mapping, errors); |
| 8763 | Py_DECREF(unicode); |
| 8764 | return result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8765 | } |
| 8766 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8767 | PyObject * |
| 8768 | PyUnicode_Translate(PyObject *str, |
| 8769 | PyObject *mapping, |
| 8770 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8771 | { |
| 8772 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8773 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8774 | str = PyUnicode_FromObject(str); |
| 8775 | if (str == NULL) |
Christian Heimes | 5f520f4 | 2012-09-11 14:03:25 +0200 | [diff] [blame] | 8776 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8777 | result = _PyUnicode_TranslateCharmap(str, mapping, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8778 | Py_DECREF(str); |
| 8779 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8780 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8781 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8782 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8783 | fix_decimal_and_space_to_ascii(PyObject *self) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8784 | { |
| 8785 | /* No need to call PyUnicode_READY(self) because this function is only |
| 8786 | called as a callback from fixup() which does it already. */ |
| 8787 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8788 | const int kind = PyUnicode_KIND(self); |
| 8789 | void *data = PyUnicode_DATA(self); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 8790 | Py_UCS4 maxchar = 127, ch, fixed; |
Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame] | 8791 | int modified = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8792 | Py_ssize_t i; |
| 8793 | |
| 8794 | for (i = 0; i < len; ++i) { |
| 8795 | ch = PyUnicode_READ(kind, data, i); |
| 8796 | fixed = 0; |
| 8797 | if (ch > 127) { |
| 8798 | if (Py_UNICODE_ISSPACE(ch)) |
| 8799 | fixed = ' '; |
| 8800 | else { |
| 8801 | const int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8802 | if (decimal >= 0) |
| 8803 | fixed = '0' + decimal; |
| 8804 | } |
| 8805 | if (fixed != 0) { |
Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame] | 8806 | modified = 1; |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 8807 | maxchar = Py_MAX(maxchar, fixed); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8808 | PyUnicode_WRITE(kind, data, i, fixed); |
| 8809 | } |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 8810 | else |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 8811 | maxchar = Py_MAX(maxchar, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8812 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8813 | } |
| 8814 | |
Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame] | 8815 | return (modified) ? maxchar : 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8816 | } |
| 8817 | |
| 8818 | PyObject * |
| 8819 | _PyUnicode_TransformDecimalAndSpaceToASCII(PyObject *unicode) |
| 8820 | { |
| 8821 | if (!PyUnicode_Check(unicode)) { |
| 8822 | PyErr_BadInternalCall(); |
| 8823 | return NULL; |
| 8824 | } |
| 8825 | if (PyUnicode_READY(unicode) == -1) |
| 8826 | return NULL; |
| 8827 | if (PyUnicode_MAX_CHAR_VALUE(unicode) <= 127) { |
| 8828 | /* If the string is already ASCII, just return the same string */ |
| 8829 | Py_INCREF(unicode); |
| 8830 | return unicode; |
| 8831 | } |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8832 | return fixup(unicode, fix_decimal_and_space_to_ascii); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8833 | } |
| 8834 | |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8835 | PyObject * |
| 8836 | PyUnicode_TransformDecimalToASCII(Py_UNICODE *s, |
| 8837 | Py_ssize_t length) |
| 8838 | { |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8839 | PyObject *decimal; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8840 | Py_ssize_t i; |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8841 | Py_UCS4 maxchar; |
| 8842 | enum PyUnicode_Kind kind; |
| 8843 | void *data; |
| 8844 | |
Victor Stinner | 99d7ad0 | 2012-02-22 13:37:39 +0100 | [diff] [blame] | 8845 | maxchar = 127; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8846 | for (i = 0; i < length; i++) { |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8847 | Py_UNICODE ch = s[i]; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8848 | if (ch > 127) { |
| 8849 | int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8850 | if (decimal >= 0) |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8851 | ch = '0' + decimal; |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 8852 | maxchar = Py_MAX(maxchar, ch); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8853 | } |
| 8854 | } |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8855 | |
| 8856 | /* Copy to a new string */ |
| 8857 | decimal = PyUnicode_New(length, maxchar); |
| 8858 | if (decimal == NULL) |
| 8859 | return decimal; |
| 8860 | kind = PyUnicode_KIND(decimal); |
| 8861 | data = PyUnicode_DATA(decimal); |
| 8862 | /* Iterate over code points */ |
| 8863 | for (i = 0; i < length; i++) { |
| 8864 | Py_UNICODE ch = s[i]; |
| 8865 | if (ch > 127) { |
| 8866 | int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8867 | if (decimal >= 0) |
| 8868 | ch = '0' + decimal; |
| 8869 | } |
| 8870 | PyUnicode_WRITE(kind, data, i, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8871 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 8872 | return unicode_result(decimal); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8873 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8874 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 8875 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8876 | int |
| 8877 | PyUnicode_EncodeDecimal(Py_UNICODE *s, |
| 8878 | Py_ssize_t length, |
| 8879 | char *output, |
| 8880 | const char *errors) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8881 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8882 | PyObject *unicode; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8883 | Py_ssize_t i; |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8884 | enum PyUnicode_Kind kind; |
| 8885 | void *data; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8886 | |
| 8887 | if (output == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8888 | PyErr_BadArgument(); |
| 8889 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8890 | } |
| 8891 | |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8892 | unicode = PyUnicode_FromUnicode(s, length); |
| 8893 | if (unicode == NULL) |
| 8894 | return -1; |
| 8895 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 8896 | if (PyUnicode_READY(unicode) == -1) { |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8897 | Py_DECREF(unicode); |
| 8898 | return -1; |
| 8899 | } |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8900 | kind = PyUnicode_KIND(unicode); |
| 8901 | data = PyUnicode_DATA(unicode); |
| 8902 | |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8903 | for (i=0; i < length; ) { |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8904 | PyObject *exc; |
| 8905 | Py_UCS4 ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8906 | int decimal; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8907 | Py_ssize_t startpos; |
| 8908 | |
| 8909 | ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8910 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8911 | if (Py_UNICODE_ISSPACE(ch)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8912 | *output++ = ' '; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8913 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8914 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8915 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8916 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 8917 | if (decimal >= 0) { |
| 8918 | *output++ = '0' + decimal; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8919 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8920 | continue; |
| 8921 | } |
| 8922 | if (0 < ch && ch < 256) { |
| 8923 | *output++ = (char)ch; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8924 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8925 | continue; |
| 8926 | } |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8927 | |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8928 | startpos = i; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8929 | exc = NULL; |
| 8930 | raise_encode_exception(&exc, "decimal", unicode, |
| 8931 | startpos, startpos+1, |
| 8932 | "invalid decimal Unicode string"); |
| 8933 | Py_XDECREF(exc); |
| 8934 | Py_DECREF(unicode); |
| 8935 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8936 | } |
| 8937 | /* 0-terminate the output string */ |
| 8938 | *output++ = '\0'; |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8939 | Py_DECREF(unicode); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8940 | return 0; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8941 | } |
| 8942 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8943 | /* --- Helpers ------------------------------------------------------------ */ |
| 8944 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8945 | static Py_ssize_t |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 8946 | any_find_slice(int direction, PyObject* s1, PyObject* s2, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8947 | Py_ssize_t start, |
| 8948 | Py_ssize_t end) |
| 8949 | { |
| 8950 | int kind1, kind2, kind; |
| 8951 | void *buf1, *buf2; |
| 8952 | Py_ssize_t len1, len2, result; |
| 8953 | |
| 8954 | kind1 = PyUnicode_KIND(s1); |
| 8955 | kind2 = PyUnicode_KIND(s2); |
| 8956 | kind = kind1 > kind2 ? kind1 : kind2; |
| 8957 | buf1 = PyUnicode_DATA(s1); |
| 8958 | buf2 = PyUnicode_DATA(s2); |
| 8959 | if (kind1 != kind) |
| 8960 | buf1 = _PyUnicode_AsKind(s1, kind); |
| 8961 | if (!buf1) |
| 8962 | return -2; |
| 8963 | if (kind2 != kind) |
| 8964 | buf2 = _PyUnicode_AsKind(s2, kind); |
| 8965 | if (!buf2) { |
| 8966 | if (kind1 != kind) PyMem_Free(buf1); |
| 8967 | return -2; |
| 8968 | } |
| 8969 | len1 = PyUnicode_GET_LENGTH(s1); |
| 8970 | len2 = PyUnicode_GET_LENGTH(s2); |
| 8971 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 8972 | if (direction > 0) { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 8973 | switch (kind) { |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 8974 | case PyUnicode_1BYTE_KIND: |
| 8975 | if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2)) |
| 8976 | result = asciilib_find_slice(buf1, len1, buf2, len2, start, end); |
| 8977 | else |
| 8978 | result = ucs1lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 8979 | break; |
| 8980 | case PyUnicode_2BYTE_KIND: |
| 8981 | result = ucs2lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 8982 | break; |
| 8983 | case PyUnicode_4BYTE_KIND: |
| 8984 | result = ucs4lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 8985 | break; |
| 8986 | default: |
| 8987 | assert(0); result = -2; |
| 8988 | } |
| 8989 | } |
| 8990 | else { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 8991 | switch (kind) { |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 8992 | case PyUnicode_1BYTE_KIND: |
| 8993 | if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2)) |
| 8994 | result = asciilib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 8995 | else |
| 8996 | result = ucs1lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 8997 | break; |
| 8998 | case PyUnicode_2BYTE_KIND: |
| 8999 | result = ucs2lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9000 | break; |
| 9001 | case PyUnicode_4BYTE_KIND: |
| 9002 | result = ucs4lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9003 | break; |
| 9004 | default: |
| 9005 | assert(0); result = -2; |
| 9006 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9007 | } |
| 9008 | |
| 9009 | if (kind1 != kind) |
| 9010 | PyMem_Free(buf1); |
| 9011 | if (kind2 != kind) |
| 9012 | PyMem_Free(buf2); |
| 9013 | |
| 9014 | return result; |
| 9015 | } |
| 9016 | |
| 9017 | Py_ssize_t |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9018 | _PyUnicode_InsertThousandsGrouping( |
| 9019 | PyObject *unicode, Py_ssize_t index, |
| 9020 | Py_ssize_t n_buffer, |
| 9021 | void *digits, Py_ssize_t n_digits, |
| 9022 | Py_ssize_t min_width, |
| 9023 | const char *grouping, PyObject *thousands_sep, |
| 9024 | Py_UCS4 *maxchar) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9025 | { |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9026 | unsigned int kind, thousands_sep_kind; |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9027 | char *data, *thousands_sep_data; |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9028 | Py_ssize_t thousands_sep_len; |
| 9029 | Py_ssize_t len; |
| 9030 | |
| 9031 | if (unicode != NULL) { |
| 9032 | kind = PyUnicode_KIND(unicode); |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9033 | data = (char *) PyUnicode_DATA(unicode) + index * kind; |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9034 | } |
| 9035 | else { |
| 9036 | kind = PyUnicode_1BYTE_KIND; |
| 9037 | data = NULL; |
| 9038 | } |
| 9039 | thousands_sep_kind = PyUnicode_KIND(thousands_sep); |
| 9040 | thousands_sep_data = PyUnicode_DATA(thousands_sep); |
| 9041 | thousands_sep_len = PyUnicode_GET_LENGTH(thousands_sep); |
| 9042 | if (unicode != NULL && thousands_sep_kind != kind) { |
Victor Stinner | 90f50d4 | 2012-02-24 01:44:47 +0100 | [diff] [blame] | 9043 | if (thousands_sep_kind < kind) { |
| 9044 | thousands_sep_data = _PyUnicode_AsKind(thousands_sep, kind); |
| 9045 | if (!thousands_sep_data) |
| 9046 | return -1; |
| 9047 | } |
| 9048 | else { |
| 9049 | data = _PyUnicode_AsKind(unicode, thousands_sep_kind); |
| 9050 | if (!data) |
| 9051 | return -1; |
| 9052 | } |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9053 | } |
| 9054 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9055 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9056 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9057 | if (unicode != NULL && PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9058 | len = asciilib_InsertThousandsGrouping( |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9059 | (Py_UCS1 *) data, n_buffer, (Py_UCS1 *) digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9060 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9061 | (Py_UCS1 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9062 | else |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9063 | len = ucs1lib_InsertThousandsGrouping( |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9064 | (Py_UCS1*)data, n_buffer, (Py_UCS1*)digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9065 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9066 | (Py_UCS1 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9067 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9068 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9069 | len = ucs2lib_InsertThousandsGrouping( |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9070 | (Py_UCS2 *) data, n_buffer, (Py_UCS2 *) digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9071 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9072 | (Py_UCS2 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9073 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9074 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9075 | len = ucs4lib_InsertThousandsGrouping( |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9076 | (Py_UCS4 *) data, n_buffer, (Py_UCS4 *) digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9077 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9078 | (Py_UCS4 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9079 | break; |
| 9080 | default: |
| 9081 | assert(0); |
| 9082 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9083 | } |
Victor Stinner | 90f50d4 | 2012-02-24 01:44:47 +0100 | [diff] [blame] | 9084 | if (unicode != NULL && thousands_sep_kind != kind) { |
| 9085 | if (thousands_sep_kind < kind) |
| 9086 | PyMem_Free(thousands_sep_data); |
| 9087 | else |
| 9088 | PyMem_Free(data); |
| 9089 | } |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9090 | if (unicode == NULL) { |
| 9091 | *maxchar = 127; |
| 9092 | if (len != n_digits) { |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9093 | *maxchar = Py_MAX(*maxchar, |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9094 | PyUnicode_MAX_CHAR_VALUE(thousands_sep)); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9095 | } |
| 9096 | } |
| 9097 | return len; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9098 | } |
| 9099 | |
| 9100 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9101 | /* helper macro to fixup start/end slice values */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9102 | #define ADJUST_INDICES(start, end, len) \ |
| 9103 | if (end > len) \ |
| 9104 | end = len; \ |
| 9105 | else if (end < 0) { \ |
| 9106 | end += len; \ |
| 9107 | if (end < 0) \ |
| 9108 | end = 0; \ |
| 9109 | } \ |
| 9110 | if (start < 0) { \ |
| 9111 | start += len; \ |
| 9112 | if (start < 0) \ |
| 9113 | start = 0; \ |
| 9114 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9115 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9116 | Py_ssize_t |
| 9117 | PyUnicode_Count(PyObject *str, |
| 9118 | PyObject *substr, |
| 9119 | Py_ssize_t start, |
| 9120 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9121 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9122 | Py_ssize_t result; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9123 | PyObject* str_obj; |
| 9124 | PyObject* sub_obj; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9125 | int kind1, kind2, kind; |
| 9126 | void *buf1 = NULL, *buf2 = NULL; |
| 9127 | Py_ssize_t len1, len2; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9128 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9129 | str_obj = PyUnicode_FromObject(str); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9130 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9131 | return -1; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9132 | sub_obj = PyUnicode_FromObject(substr); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9133 | if (!sub_obj) { |
| 9134 | Py_DECREF(str_obj); |
| 9135 | return -1; |
| 9136 | } |
Benjamin Peterson | 4c13a4a | 2012-01-02 09:07:38 -0600 | [diff] [blame] | 9137 | if (PyUnicode_READY(sub_obj) == -1 || PyUnicode_READY(str_obj) == -1) { |
Benjamin Peterson | 5e458f5 | 2012-01-02 10:12:13 -0600 | [diff] [blame] | 9138 | Py_DECREF(sub_obj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9139 | Py_DECREF(str_obj); |
| 9140 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9141 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9142 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9143 | kind1 = PyUnicode_KIND(str_obj); |
| 9144 | kind2 = PyUnicode_KIND(sub_obj); |
Antoine Pitrou | e45c0c5 | 2012-05-12 15:49:07 +0200 | [diff] [blame] | 9145 | kind = kind1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9146 | buf1 = PyUnicode_DATA(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9147 | buf2 = PyUnicode_DATA(sub_obj); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 9148 | if (kind2 != kind) { |
Antoine Pitrou | 758153b | 2012-05-12 15:51:51 +0200 | [diff] [blame] | 9149 | if (kind2 > kind) { |
| 9150 | Py_DECREF(sub_obj); |
| 9151 | Py_DECREF(str_obj); |
Antoine Pitrou | e45c0c5 | 2012-05-12 15:49:07 +0200 | [diff] [blame] | 9152 | return 0; |
Antoine Pitrou | 758153b | 2012-05-12 15:51:51 +0200 | [diff] [blame] | 9153 | } |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9154 | buf2 = _PyUnicode_AsKind(sub_obj, kind); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 9155 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9156 | if (!buf2) |
| 9157 | goto onError; |
| 9158 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 9159 | len2 = PyUnicode_GET_LENGTH(sub_obj); |
| 9160 | |
| 9161 | ADJUST_INDICES(start, end, len1); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9162 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9163 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9164 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sub_obj)) |
| 9165 | result = asciilib_count( |
| 9166 | ((Py_UCS1*)buf1) + start, end - start, |
| 9167 | buf2, len2, PY_SSIZE_T_MAX |
| 9168 | ); |
| 9169 | else |
| 9170 | result = ucs1lib_count( |
| 9171 | ((Py_UCS1*)buf1) + start, end - start, |
| 9172 | buf2, len2, PY_SSIZE_T_MAX |
| 9173 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9174 | break; |
| 9175 | case PyUnicode_2BYTE_KIND: |
| 9176 | result = ucs2lib_count( |
| 9177 | ((Py_UCS2*)buf1) + start, end - start, |
| 9178 | buf2, len2, PY_SSIZE_T_MAX |
| 9179 | ); |
| 9180 | break; |
| 9181 | case PyUnicode_4BYTE_KIND: |
| 9182 | result = ucs4lib_count( |
| 9183 | ((Py_UCS4*)buf1) + start, end - start, |
| 9184 | buf2, len2, PY_SSIZE_T_MAX |
| 9185 | ); |
| 9186 | break; |
| 9187 | default: |
| 9188 | assert(0); result = 0; |
| 9189 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9190 | |
| 9191 | Py_DECREF(sub_obj); |
| 9192 | Py_DECREF(str_obj); |
| 9193 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9194 | if (kind2 != kind) |
| 9195 | PyMem_Free(buf2); |
| 9196 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9197 | return result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9198 | onError: |
| 9199 | Py_DECREF(sub_obj); |
| 9200 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9201 | if (kind2 != kind && buf2) |
| 9202 | PyMem_Free(buf2); |
| 9203 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9204 | } |
| 9205 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9206 | Py_ssize_t |
| 9207 | PyUnicode_Find(PyObject *str, |
| 9208 | PyObject *sub, |
| 9209 | Py_ssize_t start, |
| 9210 | Py_ssize_t end, |
| 9211 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9212 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9213 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9214 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9215 | str = PyUnicode_FromObject(str); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9216 | if (!str) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9217 | return -2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9218 | sub = PyUnicode_FromObject(sub); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9219 | if (!sub) { |
| 9220 | Py_DECREF(str); |
| 9221 | return -2; |
| 9222 | } |
| 9223 | if (PyUnicode_READY(sub) == -1 || PyUnicode_READY(str) == -1) { |
| 9224 | Py_DECREF(sub); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9225 | Py_DECREF(str); |
| 9226 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9227 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9228 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9229 | result = any_find_slice(direction, |
| 9230 | str, sub, start, end |
| 9231 | ); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9232 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9233 | Py_DECREF(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9234 | Py_DECREF(sub); |
| 9235 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9236 | return result; |
| 9237 | } |
| 9238 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9239 | Py_ssize_t |
| 9240 | PyUnicode_FindChar(PyObject *str, Py_UCS4 ch, |
| 9241 | Py_ssize_t start, Py_ssize_t end, |
| 9242 | int direction) |
| 9243 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9244 | int kind; |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9245 | Py_ssize_t result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9246 | if (PyUnicode_READY(str) == -1) |
| 9247 | return -2; |
Victor Stinner | 267aa24 | 2011-10-02 01:08:37 +0200 | [diff] [blame] | 9248 | if (start < 0 || end < 0) { |
| 9249 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 9250 | return -2; |
| 9251 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9252 | if (end > PyUnicode_GET_LENGTH(str)) |
| 9253 | end = PyUnicode_GET_LENGTH(str); |
| 9254 | kind = PyUnicode_KIND(str); |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9255 | result = findchar(PyUnicode_1BYTE_DATA(str) + kind*start, |
| 9256 | kind, end-start, ch, direction); |
| 9257 | if (result == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9258 | return -1; |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9259 | else |
| 9260 | return start + result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9261 | } |
| 9262 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9263 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9264 | tailmatch(PyObject *self, |
| 9265 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9266 | Py_ssize_t start, |
| 9267 | Py_ssize_t end, |
| 9268 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9269 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9270 | int kind_self; |
| 9271 | int kind_sub; |
| 9272 | void *data_self; |
| 9273 | void *data_sub; |
| 9274 | Py_ssize_t offset; |
| 9275 | Py_ssize_t i; |
| 9276 | Py_ssize_t end_sub; |
| 9277 | |
| 9278 | if (PyUnicode_READY(self) == -1 || |
| 9279 | PyUnicode_READY(substring) == -1) |
Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame] | 9280 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9281 | |
| 9282 | if (PyUnicode_GET_LENGTH(substring) == 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9283 | return 1; |
| 9284 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9285 | ADJUST_INDICES(start, end, PyUnicode_GET_LENGTH(self)); |
| 9286 | end -= PyUnicode_GET_LENGTH(substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9287 | if (end < start) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9288 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9289 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9290 | kind_self = PyUnicode_KIND(self); |
| 9291 | data_self = PyUnicode_DATA(self); |
| 9292 | kind_sub = PyUnicode_KIND(substring); |
| 9293 | data_sub = PyUnicode_DATA(substring); |
| 9294 | end_sub = PyUnicode_GET_LENGTH(substring) - 1; |
| 9295 | |
| 9296 | if (direction > 0) |
| 9297 | offset = end; |
| 9298 | else |
| 9299 | offset = start; |
| 9300 | |
| 9301 | if (PyUnicode_READ(kind_self, data_self, offset) == |
| 9302 | PyUnicode_READ(kind_sub, data_sub, 0) && |
| 9303 | PyUnicode_READ(kind_self, data_self, offset + end_sub) == |
| 9304 | PyUnicode_READ(kind_sub, data_sub, end_sub)) { |
| 9305 | /* If both are of the same kind, memcmp is sufficient */ |
| 9306 | if (kind_self == kind_sub) { |
| 9307 | return ! memcmp((char *)data_self + |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9308 | (offset * PyUnicode_KIND(substring)), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9309 | data_sub, |
| 9310 | PyUnicode_GET_LENGTH(substring) * |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9311 | PyUnicode_KIND(substring)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9312 | } |
| 9313 | /* otherwise we have to compare each character by first accesing it */ |
| 9314 | else { |
| 9315 | /* We do not need to compare 0 and len(substring)-1 because |
| 9316 | the if statement above ensured already that they are equal |
| 9317 | when we end up here. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9318 | for (i = 1; i < end_sub; ++i) { |
| 9319 | if (PyUnicode_READ(kind_self, data_self, offset + i) != |
| 9320 | PyUnicode_READ(kind_sub, data_sub, i)) |
| 9321 | return 0; |
| 9322 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9323 | return 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9324 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9325 | } |
| 9326 | |
| 9327 | return 0; |
| 9328 | } |
| 9329 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9330 | Py_ssize_t |
| 9331 | PyUnicode_Tailmatch(PyObject *str, |
| 9332 | PyObject *substr, |
| 9333 | Py_ssize_t start, |
| 9334 | Py_ssize_t end, |
| 9335 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9336 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9337 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9338 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9339 | str = PyUnicode_FromObject(str); |
| 9340 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9341 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9342 | substr = PyUnicode_FromObject(substr); |
| 9343 | if (substr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9344 | Py_DECREF(str); |
| 9345 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9346 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9347 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9348 | result = tailmatch(str, substr, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9349 | start, end, direction); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9350 | Py_DECREF(str); |
| 9351 | Py_DECREF(substr); |
| 9352 | return result; |
| 9353 | } |
| 9354 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9355 | /* Apply fixfct filter to the Unicode object self and return a |
| 9356 | reference to the modified object */ |
| 9357 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9358 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9359 | fixup(PyObject *self, |
| 9360 | Py_UCS4 (*fixfct)(PyObject *s)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9361 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9362 | PyObject *u; |
| 9363 | Py_UCS4 maxchar_old, maxchar_new = 0; |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9364 | PyObject *v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9365 | |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 9366 | u = _PyUnicode_Copy(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9367 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9368 | return NULL; |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 9369 | maxchar_old = PyUnicode_MAX_CHAR_VALUE(u); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9370 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9371 | /* fix functions return the new maximum character in a string, |
| 9372 | if the kind of the resulting unicode object does not change, |
| 9373 | everything is fine. Otherwise we need to change the string kind |
| 9374 | and re-run the fix function. */ |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9375 | maxchar_new = fixfct(u); |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9376 | |
| 9377 | if (maxchar_new == 0) { |
| 9378 | /* no changes */; |
| 9379 | if (PyUnicode_CheckExact(self)) { |
| 9380 | Py_DECREF(u); |
| 9381 | Py_INCREF(self); |
| 9382 | return self; |
| 9383 | } |
| 9384 | else |
| 9385 | return u; |
| 9386 | } |
| 9387 | |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9388 | maxchar_new = align_maxchar(maxchar_new); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9389 | |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9390 | if (maxchar_new == maxchar_old) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9391 | return u; |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9392 | |
| 9393 | /* In case the maximum character changed, we need to |
| 9394 | convert the string to the new category. */ |
| 9395 | v = PyUnicode_New(PyUnicode_GET_LENGTH(self), maxchar_new); |
| 9396 | if (v == NULL) { |
| 9397 | Py_DECREF(u); |
| 9398 | return NULL; |
| 9399 | } |
| 9400 | if (maxchar_new > maxchar_old) { |
| 9401 | /* If the maxchar increased so that the kind changed, not all |
| 9402 | characters are representable anymore and we need to fix the |
| 9403 | string again. This only happens in very few cases. */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9404 | _PyUnicode_FastCopyCharacters(v, 0, |
| 9405 | self, 0, PyUnicode_GET_LENGTH(self)); |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9406 | maxchar_old = fixfct(v); |
| 9407 | assert(maxchar_old > 0 && maxchar_old <= maxchar_new); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9408 | } |
| 9409 | else { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9410 | _PyUnicode_FastCopyCharacters(v, 0, |
| 9411 | u, 0, PyUnicode_GET_LENGTH(self)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9412 | } |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9413 | Py_DECREF(u); |
| 9414 | assert(_PyUnicode_CheckConsistency(v, 1)); |
| 9415 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9416 | } |
| 9417 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9418 | static PyObject * |
| 9419 | ascii_upper_or_lower(PyObject *self, int lower) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9420 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9421 | Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9422 | char *resdata, *data = PyUnicode_DATA(self); |
| 9423 | PyObject *res; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9424 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9425 | res = PyUnicode_New(len, 127); |
| 9426 | if (res == NULL) |
| 9427 | return NULL; |
| 9428 | resdata = PyUnicode_DATA(res); |
| 9429 | if (lower) |
| 9430 | _Py_bytes_lower(resdata, data, len); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9431 | else |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9432 | _Py_bytes_upper(resdata, data, len); |
| 9433 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9434 | } |
| 9435 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9436 | static Py_UCS4 |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9437 | 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] | 9438 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9439 | Py_ssize_t j; |
| 9440 | int final_sigma; |
| 9441 | Py_UCS4 c; |
| 9442 | /* 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] | 9443 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9444 | \p{cased}\p{case-ignorable}*U+03A3!(\p{case-ignorable}*\p{cased}) |
| 9445 | |
| 9446 | where ! is a negation and \p{xxx} is a character with property xxx. |
| 9447 | */ |
| 9448 | for (j = i - 1; j >= 0; j--) { |
| 9449 | c = PyUnicode_READ(kind, data, j); |
| 9450 | if (!_PyUnicode_IsCaseIgnorable(c)) |
| 9451 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9452 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9453 | final_sigma = j >= 0 && _PyUnicode_IsCased(c); |
| 9454 | if (final_sigma) { |
| 9455 | for (j = i + 1; j < length; j++) { |
| 9456 | c = PyUnicode_READ(kind, data, j); |
| 9457 | if (!_PyUnicode_IsCaseIgnorable(c)) |
| 9458 | break; |
| 9459 | } |
| 9460 | final_sigma = j == length || !_PyUnicode_IsCased(c); |
| 9461 | } |
| 9462 | return (final_sigma) ? 0x3C2 : 0x3C3; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9463 | } |
| 9464 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9465 | static int |
| 9466 | lower_ucs4(int kind, void *data, Py_ssize_t length, Py_ssize_t i, |
| 9467 | Py_UCS4 c, Py_UCS4 *mapped) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9468 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9469 | /* Obscure special case. */ |
| 9470 | if (c == 0x3A3) { |
| 9471 | mapped[0] = handle_capital_sigma(kind, data, length, i); |
| 9472 | return 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9473 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9474 | return _PyUnicode_ToLowerFull(c, mapped); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9475 | } |
| 9476 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9477 | static Py_ssize_t |
| 9478 | 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] | 9479 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9480 | Py_ssize_t i, k = 0; |
| 9481 | int n_res, j; |
| 9482 | Py_UCS4 c, mapped[3]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9483 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9484 | c = PyUnicode_READ(kind, data, 0); |
| 9485 | n_res = _PyUnicode_ToUpperFull(c, mapped); |
| 9486 | for (j = 0; j < n_res; j++) { |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9487 | *maxchar = Py_MAX(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9488 | res[k++] = mapped[j]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9489 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9490 | for (i = 1; i < length; i++) { |
| 9491 | c = PyUnicode_READ(kind, data, i); |
| 9492 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9493 | for (j = 0; j < n_res; j++) { |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9494 | *maxchar = Py_MAX(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9495 | res[k++] = mapped[j]; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9496 | } |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9497 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9498 | return k; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9499 | } |
| 9500 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9501 | static Py_ssize_t |
| 9502 | do_swapcase(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) { |
| 9503 | Py_ssize_t i, k = 0; |
| 9504 | |
| 9505 | for (i = 0; i < length; i++) { |
| 9506 | Py_UCS4 c = PyUnicode_READ(kind, data, i), mapped[3]; |
| 9507 | int n_res, j; |
| 9508 | if (Py_UNICODE_ISUPPER(c)) { |
| 9509 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9510 | } |
| 9511 | else if (Py_UNICODE_ISLOWER(c)) { |
| 9512 | n_res = _PyUnicode_ToUpperFull(c, mapped); |
| 9513 | } |
| 9514 | else { |
| 9515 | n_res = 1; |
| 9516 | mapped[0] = c; |
| 9517 | } |
| 9518 | for (j = 0; j < n_res; j++) { |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9519 | *maxchar = Py_MAX(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9520 | res[k++] = mapped[j]; |
| 9521 | } |
| 9522 | } |
| 9523 | return k; |
| 9524 | } |
| 9525 | |
| 9526 | static Py_ssize_t |
| 9527 | do_upper_or_lower(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, |
| 9528 | Py_UCS4 *maxchar, int lower) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9529 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9530 | Py_ssize_t i, k = 0; |
| 9531 | |
| 9532 | for (i = 0; i < length; i++) { |
| 9533 | Py_UCS4 c = PyUnicode_READ(kind, data, i), mapped[3]; |
| 9534 | int n_res, j; |
| 9535 | if (lower) |
| 9536 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9537 | else |
| 9538 | n_res = _PyUnicode_ToUpperFull(c, mapped); |
| 9539 | for (j = 0; j < n_res; j++) { |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9540 | *maxchar = Py_MAX(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9541 | res[k++] = mapped[j]; |
| 9542 | } |
| 9543 | } |
| 9544 | return k; |
| 9545 | } |
| 9546 | |
| 9547 | static Py_ssize_t |
| 9548 | do_upper(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9549 | { |
| 9550 | return do_upper_or_lower(kind, data, length, res, maxchar, 0); |
| 9551 | } |
| 9552 | |
| 9553 | static Py_ssize_t |
| 9554 | do_lower(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9555 | { |
| 9556 | return do_upper_or_lower(kind, data, length, res, maxchar, 1); |
| 9557 | } |
| 9558 | |
Benjamin Peterson | e51757f | 2012-01-12 21:10:29 -0500 | [diff] [blame] | 9559 | static Py_ssize_t |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 9560 | do_casefold(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9561 | { |
| 9562 | Py_ssize_t i, k = 0; |
| 9563 | |
| 9564 | for (i = 0; i < length; i++) { |
| 9565 | Py_UCS4 c = PyUnicode_READ(kind, data, i); |
| 9566 | Py_UCS4 mapped[3]; |
| 9567 | int j, n_res = _PyUnicode_ToFoldedFull(c, mapped); |
| 9568 | for (j = 0; j < n_res; j++) { |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9569 | *maxchar = Py_MAX(*maxchar, mapped[j]); |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 9570 | res[k++] = mapped[j]; |
| 9571 | } |
| 9572 | } |
| 9573 | return k; |
| 9574 | } |
| 9575 | |
| 9576 | static Py_ssize_t |
Benjamin Peterson | e51757f | 2012-01-12 21:10:29 -0500 | [diff] [blame] | 9577 | do_title(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9578 | { |
| 9579 | Py_ssize_t i, k = 0; |
| 9580 | int previous_is_cased; |
| 9581 | |
| 9582 | previous_is_cased = 0; |
| 9583 | for (i = 0; i < length; i++) { |
| 9584 | const Py_UCS4 c = PyUnicode_READ(kind, data, i); |
| 9585 | Py_UCS4 mapped[3]; |
| 9586 | int n_res, j; |
| 9587 | |
| 9588 | if (previous_is_cased) |
| 9589 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9590 | else |
| 9591 | n_res = _PyUnicode_ToTitleFull(c, mapped); |
| 9592 | |
| 9593 | for (j = 0; j < n_res; j++) { |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9594 | *maxchar = Py_MAX(*maxchar, mapped[j]); |
Benjamin Peterson | e51757f | 2012-01-12 21:10:29 -0500 | [diff] [blame] | 9595 | res[k++] = mapped[j]; |
| 9596 | } |
| 9597 | |
| 9598 | previous_is_cased = _PyUnicode_IsCased(c); |
| 9599 | } |
| 9600 | return k; |
| 9601 | } |
| 9602 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9603 | static PyObject * |
| 9604 | case_operation(PyObject *self, |
| 9605 | Py_ssize_t (*perform)(int, void *, Py_ssize_t, Py_UCS4 *, Py_UCS4 *)) |
| 9606 | { |
| 9607 | PyObject *res = NULL; |
| 9608 | Py_ssize_t length, newlength = 0; |
| 9609 | int kind, outkind; |
| 9610 | void *data, *outdata; |
| 9611 | Py_UCS4 maxchar = 0, *tmp, *tmpend; |
| 9612 | |
Benjamin Peterson | eea4846 | 2012-01-16 14:28:50 -0500 | [diff] [blame] | 9613 | assert(PyUnicode_IS_READY(self)); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9614 | |
| 9615 | kind = PyUnicode_KIND(self); |
| 9616 | data = PyUnicode_DATA(self); |
| 9617 | length = PyUnicode_GET_LENGTH(self); |
| 9618 | tmp = PyMem_MALLOC(sizeof(Py_UCS4) * 3 * length); |
| 9619 | if (tmp == NULL) |
| 9620 | return PyErr_NoMemory(); |
| 9621 | newlength = perform(kind, data, length, tmp, &maxchar); |
| 9622 | res = PyUnicode_New(newlength, maxchar); |
| 9623 | if (res == NULL) |
| 9624 | goto leave; |
| 9625 | tmpend = tmp + newlength; |
| 9626 | outdata = PyUnicode_DATA(res); |
| 9627 | outkind = PyUnicode_KIND(res); |
| 9628 | switch (outkind) { |
| 9629 | case PyUnicode_1BYTE_KIND: |
| 9630 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS1, tmp, tmpend, outdata); |
| 9631 | break; |
| 9632 | case PyUnicode_2BYTE_KIND: |
| 9633 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS2, tmp, tmpend, outdata); |
| 9634 | break; |
| 9635 | case PyUnicode_4BYTE_KIND: |
| 9636 | memcpy(outdata, tmp, sizeof(Py_UCS4) * newlength); |
| 9637 | break; |
| 9638 | default: |
| 9639 | assert(0); |
| 9640 | break; |
| 9641 | } |
| 9642 | leave: |
| 9643 | PyMem_FREE(tmp); |
| 9644 | return res; |
| 9645 | } |
| 9646 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9647 | PyObject * |
| 9648 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9649 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9650 | PyObject *sep = NULL; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9651 | Py_ssize_t seplen; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9652 | PyObject *res = NULL; /* the result */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9653 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9654 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
| 9655 | PyObject **items; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9656 | PyObject *item; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9657 | Py_ssize_t sz, i, res_offset; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9658 | Py_UCS4 maxchar; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9659 | Py_UCS4 item_maxchar; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9660 | int use_memcpy; |
| 9661 | unsigned char *res_data = NULL, *sep_data = NULL; |
| 9662 | PyObject *last_obj; |
| 9663 | unsigned int kind = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9664 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9665 | fseq = PySequence_Fast(seq, ""); |
| 9666 | if (fseq == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9667 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9668 | } |
| 9669 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9670 | /* NOTE: the following code can't call back into Python code, |
| 9671 | * so we are sure that fseq won't be mutated. |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9672 | */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9673 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9674 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 9675 | /* If empty sequence, return u"". */ |
| 9676 | if (seqlen == 0) { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9677 | Py_DECREF(fseq); |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 9678 | _Py_RETURN_UNICODE_EMPTY(); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9679 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9680 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9681 | /* If singleton sequence with an exact Unicode, return that. */ |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9682 | last_obj = NULL; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9683 | items = PySequence_Fast_ITEMS(fseq); |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9684 | if (seqlen == 1) { |
| 9685 | if (PyUnicode_CheckExact(items[0])) { |
| 9686 | res = items[0]; |
| 9687 | Py_INCREF(res); |
| 9688 | Py_DECREF(fseq); |
| 9689 | return res; |
| 9690 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9691 | seplen = 0; |
Victor Stinner | c6f0df7 | 2011-10-06 15:58:54 +0200 | [diff] [blame] | 9692 | maxchar = 0; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9693 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9694 | else { |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9695 | /* Set up sep and seplen */ |
| 9696 | if (separator == NULL) { |
| 9697 | /* fall back to a blank space separator */ |
| 9698 | sep = PyUnicode_FromOrdinal(' '); |
| 9699 | if (!sep) |
| 9700 | goto onError; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9701 | seplen = 1; |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9702 | maxchar = 32; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9703 | } |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9704 | else { |
| 9705 | if (!PyUnicode_Check(separator)) { |
| 9706 | PyErr_Format(PyExc_TypeError, |
| 9707 | "separator: expected str instance," |
| 9708 | " %.80s found", |
| 9709 | Py_TYPE(separator)->tp_name); |
| 9710 | goto onError; |
| 9711 | } |
| 9712 | if (PyUnicode_READY(separator)) |
| 9713 | goto onError; |
| 9714 | sep = separator; |
| 9715 | seplen = PyUnicode_GET_LENGTH(separator); |
| 9716 | maxchar = PyUnicode_MAX_CHAR_VALUE(separator); |
| 9717 | /* inc refcount to keep this code path symmetric with the |
| 9718 | above case of a blank separator */ |
| 9719 | Py_INCREF(sep); |
| 9720 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9721 | last_obj = sep; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9722 | } |
| 9723 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9724 | /* There are at least two things to join, or else we have a subclass |
| 9725 | * of str in the sequence. |
| 9726 | * Do a pre-pass to figure out the total amount of space we'll |
| 9727 | * need (sz), and see whether all argument are strings. |
| 9728 | */ |
| 9729 | sz = 0; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9730 | #ifdef Py_DEBUG |
| 9731 | use_memcpy = 0; |
| 9732 | #else |
| 9733 | use_memcpy = 1; |
| 9734 | #endif |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9735 | for (i = 0; i < seqlen; i++) { |
| 9736 | const Py_ssize_t old_sz = sz; |
| 9737 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9738 | if (!PyUnicode_Check(item)) { |
| 9739 | PyErr_Format(PyExc_TypeError, |
| 9740 | "sequence item %zd: expected str instance," |
| 9741 | " %.80s found", |
| 9742 | i, Py_TYPE(item)->tp_name); |
| 9743 | goto onError; |
| 9744 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9745 | if (PyUnicode_READY(item) == -1) |
| 9746 | goto onError; |
| 9747 | sz += PyUnicode_GET_LENGTH(item); |
| 9748 | item_maxchar = PyUnicode_MAX_CHAR_VALUE(item); |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9749 | maxchar = Py_MAX(maxchar, item_maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9750 | if (i != 0) |
| 9751 | sz += seplen; |
| 9752 | if (sz < old_sz || sz > PY_SSIZE_T_MAX) { |
| 9753 | PyErr_SetString(PyExc_OverflowError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9754 | "join() result is too long for a Python string"); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9755 | goto onError; |
| 9756 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9757 | if (use_memcpy && last_obj != NULL) { |
| 9758 | if (PyUnicode_KIND(last_obj) != PyUnicode_KIND(item)) |
| 9759 | use_memcpy = 0; |
| 9760 | } |
| 9761 | last_obj = item; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9762 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9763 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9764 | res = PyUnicode_New(sz, maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9765 | if (res == NULL) |
| 9766 | goto onError; |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9767 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9768 | /* Catenate everything. */ |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9769 | #ifdef Py_DEBUG |
| 9770 | use_memcpy = 0; |
| 9771 | #else |
| 9772 | if (use_memcpy) { |
| 9773 | res_data = PyUnicode_1BYTE_DATA(res); |
| 9774 | kind = PyUnicode_KIND(res); |
| 9775 | if (seplen != 0) |
| 9776 | sep_data = PyUnicode_1BYTE_DATA(sep); |
| 9777 | } |
| 9778 | #endif |
Victor Stinner | 4560f9c | 2013-04-14 18:56:46 +0200 | [diff] [blame] | 9779 | if (use_memcpy) { |
| 9780 | for (i = 0; i < seqlen; ++i) { |
| 9781 | Py_ssize_t itemlen; |
| 9782 | item = items[i]; |
| 9783 | |
| 9784 | /* Copy item, and maybe the separator. */ |
| 9785 | if (i && seplen != 0) { |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9786 | Py_MEMCPY(res_data, |
| 9787 | sep_data, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9788 | kind * seplen); |
| 9789 | res_data += kind * seplen; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9790 | } |
Victor Stinner | 4560f9c | 2013-04-14 18:56:46 +0200 | [diff] [blame] | 9791 | |
| 9792 | itemlen = PyUnicode_GET_LENGTH(item); |
| 9793 | if (itemlen != 0) { |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9794 | Py_MEMCPY(res_data, |
| 9795 | PyUnicode_DATA(item), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9796 | kind * itemlen); |
| 9797 | res_data += kind * itemlen; |
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 | assert(res_data == PyUnicode_1BYTE_DATA(res) |
| 9801 | + kind * PyUnicode_GET_LENGTH(res)); |
| 9802 | } |
| 9803 | else { |
| 9804 | for (i = 0, res_offset = 0; i < seqlen; ++i) { |
| 9805 | Py_ssize_t itemlen; |
| 9806 | item = items[i]; |
| 9807 | |
| 9808 | /* Copy item, and maybe the separator. */ |
| 9809 | if (i && seplen != 0) { |
| 9810 | _PyUnicode_FastCopyCharacters(res, res_offset, sep, 0, seplen); |
| 9811 | res_offset += seplen; |
| 9812 | } |
| 9813 | |
| 9814 | itemlen = PyUnicode_GET_LENGTH(item); |
| 9815 | if (itemlen != 0) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9816 | _PyUnicode_FastCopyCharacters(res, res_offset, item, 0, itemlen); |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9817 | res_offset += itemlen; |
| 9818 | } |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9819 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9820 | assert(res_offset == PyUnicode_GET_LENGTH(res)); |
Victor Stinner | 4560f9c | 2013-04-14 18:56:46 +0200 | [diff] [blame] | 9821 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9822 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9823 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9824 | Py_XDECREF(sep); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9825 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9826 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9827 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9828 | onError: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9829 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9830 | Py_XDECREF(sep); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9831 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9832 | return NULL; |
| 9833 | } |
| 9834 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9835 | #define FILL(kind, data, value, start, length) \ |
| 9836 | do { \ |
| 9837 | Py_ssize_t i_ = 0; \ |
| 9838 | assert(kind != PyUnicode_WCHAR_KIND); \ |
| 9839 | switch ((kind)) { \ |
| 9840 | case PyUnicode_1BYTE_KIND: { \ |
| 9841 | unsigned char * to_ = (unsigned char *)((data)) + (start); \ |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 9842 | memset(to_, (unsigned char)value, (length)); \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9843 | break; \ |
| 9844 | } \ |
| 9845 | case PyUnicode_2BYTE_KIND: { \ |
| 9846 | Py_UCS2 * to_ = (Py_UCS2 *)((data)) + (start); \ |
| 9847 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 9848 | break; \ |
| 9849 | } \ |
Benjamin Peterson | e157cf1 | 2012-01-01 15:56:20 -0600 | [diff] [blame] | 9850 | case PyUnicode_4BYTE_KIND: { \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9851 | Py_UCS4 * to_ = (Py_UCS4 *)((data)) + (start); \ |
| 9852 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 9853 | break; \ |
Benjamin Peterson | e157cf1 | 2012-01-01 15:56:20 -0600 | [diff] [blame] | 9854 | default: assert(0); \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9855 | } \ |
| 9856 | } \ |
| 9857 | } while (0) |
| 9858 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9859 | void |
| 9860 | _PyUnicode_FastFill(PyObject *unicode, Py_ssize_t start, Py_ssize_t length, |
| 9861 | Py_UCS4 fill_char) |
| 9862 | { |
| 9863 | const enum PyUnicode_Kind kind = PyUnicode_KIND(unicode); |
| 9864 | const void *data = PyUnicode_DATA(unicode); |
| 9865 | assert(PyUnicode_IS_READY(unicode)); |
| 9866 | assert(unicode_modifiable(unicode)); |
| 9867 | assert(fill_char <= PyUnicode_MAX_CHAR_VALUE(unicode)); |
| 9868 | assert(start >= 0); |
| 9869 | assert(start + length <= PyUnicode_GET_LENGTH(unicode)); |
| 9870 | FILL(kind, data, fill_char, start, length); |
| 9871 | } |
| 9872 | |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 9873 | Py_ssize_t |
| 9874 | PyUnicode_Fill(PyObject *unicode, Py_ssize_t start, Py_ssize_t length, |
| 9875 | Py_UCS4 fill_char) |
| 9876 | { |
| 9877 | Py_ssize_t maxlen; |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 9878 | |
| 9879 | if (!PyUnicode_Check(unicode)) { |
| 9880 | PyErr_BadInternalCall(); |
| 9881 | return -1; |
| 9882 | } |
| 9883 | if (PyUnicode_READY(unicode) == -1) |
| 9884 | return -1; |
| 9885 | if (unicode_check_modifiable(unicode)) |
| 9886 | return -1; |
| 9887 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9888 | if (start < 0) { |
| 9889 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 9890 | return -1; |
| 9891 | } |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 9892 | if (fill_char > PyUnicode_MAX_CHAR_VALUE(unicode)) { |
| 9893 | PyErr_SetString(PyExc_ValueError, |
| 9894 | "fill character is bigger than " |
| 9895 | "the string maximum character"); |
| 9896 | return -1; |
| 9897 | } |
| 9898 | |
| 9899 | maxlen = PyUnicode_GET_LENGTH(unicode) - start; |
| 9900 | length = Py_MIN(maxlen, length); |
| 9901 | if (length <= 0) |
| 9902 | return 0; |
| 9903 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9904 | _PyUnicode_FastFill(unicode, start, length, fill_char); |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 9905 | return length; |
| 9906 | } |
| 9907 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9908 | static PyObject * |
| 9909 | pad(PyObject *self, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9910 | Py_ssize_t left, |
| 9911 | Py_ssize_t right, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9912 | Py_UCS4 fill) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9913 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9914 | PyObject *u; |
| 9915 | Py_UCS4 maxchar; |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9916 | int kind; |
| 9917 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9918 | |
| 9919 | if (left < 0) |
| 9920 | left = 0; |
| 9921 | if (right < 0) |
| 9922 | right = 0; |
| 9923 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 9924 | if (left == 0 && right == 0) |
| 9925 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9926 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9927 | if (left > PY_SSIZE_T_MAX - _PyUnicode_LENGTH(self) || |
| 9928 | right > PY_SSIZE_T_MAX - (left + _PyUnicode_LENGTH(self))) { |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 9929 | PyErr_SetString(PyExc_OverflowError, "padded string is too long"); |
| 9930 | return NULL; |
| 9931 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9932 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9933 | maxchar = Py_MAX(maxchar, fill); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9934 | u = PyUnicode_New(left + _PyUnicode_LENGTH(self) + right, maxchar); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9935 | if (!u) |
| 9936 | return NULL; |
| 9937 | |
| 9938 | kind = PyUnicode_KIND(u); |
| 9939 | data = PyUnicode_DATA(u); |
| 9940 | if (left) |
| 9941 | FILL(kind, data, fill, 0, left); |
| 9942 | if (right) |
| 9943 | FILL(kind, data, fill, left + _PyUnicode_LENGTH(self), right); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9944 | _PyUnicode_FastCopyCharacters(u, left, self, 0, _PyUnicode_LENGTH(self)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9945 | assert(_PyUnicode_CheckConsistency(u, 1)); |
| 9946 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9947 | } |
| 9948 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9949 | PyObject * |
| 9950 | PyUnicode_Splitlines(PyObject *string, int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9951 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9952 | PyObject *list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9953 | |
| 9954 | string = PyUnicode_FromObject(string); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9955 | if (string == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9956 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9957 | if (PyUnicode_READY(string) == -1) { |
| 9958 | Py_DECREF(string); |
| 9959 | return NULL; |
| 9960 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9961 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9962 | switch (PyUnicode_KIND(string)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9963 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9964 | if (PyUnicode_IS_ASCII(string)) |
| 9965 | list = asciilib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9966 | string, PyUnicode_1BYTE_DATA(string), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9967 | PyUnicode_GET_LENGTH(string), keepends); |
| 9968 | else |
| 9969 | list = ucs1lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9970 | string, PyUnicode_1BYTE_DATA(string), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9971 | PyUnicode_GET_LENGTH(string), keepends); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9972 | break; |
| 9973 | case PyUnicode_2BYTE_KIND: |
| 9974 | list = ucs2lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9975 | string, PyUnicode_2BYTE_DATA(string), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9976 | PyUnicode_GET_LENGTH(string), keepends); |
| 9977 | break; |
| 9978 | case PyUnicode_4BYTE_KIND: |
| 9979 | list = ucs4lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9980 | string, PyUnicode_4BYTE_DATA(string), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9981 | PyUnicode_GET_LENGTH(string), keepends); |
| 9982 | break; |
| 9983 | default: |
| 9984 | assert(0); |
| 9985 | list = 0; |
| 9986 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9987 | Py_DECREF(string); |
| 9988 | return list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9989 | } |
| 9990 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9991 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9992 | split(PyObject *self, |
| 9993 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9994 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9995 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9996 | int kind1, kind2, kind; |
| 9997 | void *buf1, *buf2; |
| 9998 | Py_ssize_t len1, len2; |
| 9999 | PyObject* out; |
| 10000 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10001 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10002 | maxcount = PY_SSIZE_T_MAX; |
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 | if (PyUnicode_READY(self) == -1) |
| 10005 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10006 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10007 | if (substring == NULL) |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10008 | switch (PyUnicode_KIND(self)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10009 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10010 | if (PyUnicode_IS_ASCII(self)) |
| 10011 | return asciilib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10012 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10013 | PyUnicode_GET_LENGTH(self), maxcount |
| 10014 | ); |
| 10015 | else |
| 10016 | return ucs1lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10017 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10018 | PyUnicode_GET_LENGTH(self), maxcount |
| 10019 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10020 | case PyUnicode_2BYTE_KIND: |
| 10021 | return ucs2lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10022 | self, PyUnicode_2BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10023 | PyUnicode_GET_LENGTH(self), maxcount |
| 10024 | ); |
| 10025 | case PyUnicode_4BYTE_KIND: |
| 10026 | return ucs4lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10027 | self, PyUnicode_4BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10028 | PyUnicode_GET_LENGTH(self), maxcount |
| 10029 | ); |
| 10030 | default: |
| 10031 | assert(0); |
| 10032 | return NULL; |
| 10033 | } |
| 10034 | |
| 10035 | if (PyUnicode_READY(substring) == -1) |
| 10036 | return NULL; |
| 10037 | |
| 10038 | kind1 = PyUnicode_KIND(self); |
| 10039 | kind2 = PyUnicode_KIND(substring); |
| 10040 | kind = kind1 > kind2 ? kind1 : kind2; |
| 10041 | buf1 = PyUnicode_DATA(self); |
| 10042 | buf2 = PyUnicode_DATA(substring); |
| 10043 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10044 | buf1 = _PyUnicode_AsKind(self, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10045 | if (!buf1) |
| 10046 | return NULL; |
| 10047 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10048 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10049 | if (!buf2) { |
| 10050 | if (kind1 != kind) PyMem_Free(buf1); |
| 10051 | return NULL; |
| 10052 | } |
| 10053 | len1 = PyUnicode_GET_LENGTH(self); |
| 10054 | len2 = PyUnicode_GET_LENGTH(substring); |
| 10055 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10056 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10057 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10058 | if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) |
| 10059 | out = asciilib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10060 | self, buf1, len1, buf2, len2, maxcount); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10061 | else |
| 10062 | out = ucs1lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10063 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10064 | break; |
| 10065 | case PyUnicode_2BYTE_KIND: |
| 10066 | out = ucs2lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10067 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10068 | break; |
| 10069 | case PyUnicode_4BYTE_KIND: |
| 10070 | out = ucs4lib_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 | default: |
| 10074 | out = NULL; |
| 10075 | } |
| 10076 | if (kind1 != kind) |
| 10077 | PyMem_Free(buf1); |
| 10078 | if (kind2 != kind) |
| 10079 | PyMem_Free(buf2); |
| 10080 | return out; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10081 | } |
| 10082 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10083 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10084 | rsplit(PyObject *self, |
| 10085 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10086 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10087 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10088 | int kind1, kind2, kind; |
| 10089 | void *buf1, *buf2; |
| 10090 | Py_ssize_t len1, len2; |
| 10091 | PyObject* out; |
| 10092 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10093 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10094 | maxcount = PY_SSIZE_T_MAX; |
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 | if (PyUnicode_READY(self) == -1) |
| 10097 | return NULL; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10098 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10099 | if (substring == NULL) |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10100 | switch (PyUnicode_KIND(self)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10101 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10102 | if (PyUnicode_IS_ASCII(self)) |
| 10103 | return asciilib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10104 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10105 | PyUnicode_GET_LENGTH(self), maxcount |
| 10106 | ); |
| 10107 | else |
| 10108 | return ucs1lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10109 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10110 | PyUnicode_GET_LENGTH(self), maxcount |
| 10111 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10112 | case PyUnicode_2BYTE_KIND: |
| 10113 | return ucs2lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10114 | self, PyUnicode_2BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10115 | PyUnicode_GET_LENGTH(self), maxcount |
| 10116 | ); |
| 10117 | case PyUnicode_4BYTE_KIND: |
| 10118 | return ucs4lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10119 | self, PyUnicode_4BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10120 | PyUnicode_GET_LENGTH(self), maxcount |
| 10121 | ); |
| 10122 | default: |
| 10123 | assert(0); |
| 10124 | return NULL; |
| 10125 | } |
| 10126 | |
| 10127 | if (PyUnicode_READY(substring) == -1) |
| 10128 | return NULL; |
| 10129 | |
| 10130 | kind1 = PyUnicode_KIND(self); |
| 10131 | kind2 = PyUnicode_KIND(substring); |
| 10132 | kind = kind1 > kind2 ? kind1 : kind2; |
| 10133 | buf1 = PyUnicode_DATA(self); |
| 10134 | buf2 = PyUnicode_DATA(substring); |
| 10135 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10136 | buf1 = _PyUnicode_AsKind(self, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10137 | if (!buf1) |
| 10138 | return NULL; |
| 10139 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10140 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10141 | if (!buf2) { |
| 10142 | if (kind1 != kind) PyMem_Free(buf1); |
| 10143 | return NULL; |
| 10144 | } |
| 10145 | len1 = PyUnicode_GET_LENGTH(self); |
| 10146 | len2 = PyUnicode_GET_LENGTH(substring); |
| 10147 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10148 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10149 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10150 | if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) |
| 10151 | out = asciilib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10152 | self, buf1, len1, buf2, len2, maxcount); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10153 | else |
| 10154 | out = ucs1lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10155 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10156 | break; |
| 10157 | case PyUnicode_2BYTE_KIND: |
| 10158 | out = ucs2lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10159 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10160 | break; |
| 10161 | case PyUnicode_4BYTE_KIND: |
| 10162 | out = ucs4lib_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 | default: |
| 10166 | out = NULL; |
| 10167 | } |
| 10168 | if (kind1 != kind) |
| 10169 | PyMem_Free(buf1); |
| 10170 | if (kind2 != kind) |
| 10171 | PyMem_Free(buf2); |
| 10172 | return out; |
| 10173 | } |
| 10174 | |
| 10175 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10176 | anylib_find(int kind, PyObject *str1, void *buf1, Py_ssize_t len1, |
| 10177 | 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] | 10178 | { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10179 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10180 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10181 | if (PyUnicode_IS_ASCII(str1) && PyUnicode_IS_ASCII(str2)) |
| 10182 | return asciilib_find(buf1, len1, buf2, len2, offset); |
| 10183 | else |
| 10184 | return ucs1lib_find(buf1, len1, buf2, len2, offset); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10185 | case PyUnicode_2BYTE_KIND: |
| 10186 | return ucs2lib_find(buf1, len1, buf2, len2, offset); |
| 10187 | case PyUnicode_4BYTE_KIND: |
| 10188 | return ucs4lib_find(buf1, len1, buf2, len2, offset); |
| 10189 | } |
| 10190 | assert(0); |
| 10191 | return -1; |
| 10192 | } |
| 10193 | |
| 10194 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10195 | anylib_count(int kind, PyObject *sstr, void* sbuf, Py_ssize_t slen, |
| 10196 | 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] | 10197 | { |
Benjamin Peterson | c0b95d1 | 2011-12-20 17:24:05 -0600 | [diff] [blame] | 10198 | switch (kind) { |
| 10199 | case PyUnicode_1BYTE_KIND: |
| 10200 | if (PyUnicode_IS_ASCII(sstr) && PyUnicode_IS_ASCII(str1)) |
| 10201 | return asciilib_count(sbuf, slen, buf1, len1, maxcount); |
| 10202 | else |
| 10203 | return ucs1lib_count(sbuf, slen, buf1, len1, maxcount); |
| 10204 | case PyUnicode_2BYTE_KIND: |
| 10205 | return ucs2lib_count(sbuf, slen, buf1, len1, maxcount); |
| 10206 | case PyUnicode_4BYTE_KIND: |
| 10207 | return ucs4lib_count(sbuf, slen, buf1, len1, maxcount); |
| 10208 | } |
| 10209 | assert(0); |
| 10210 | return 0; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10211 | } |
| 10212 | |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 10213 | static void |
| 10214 | replace_1char_inplace(PyObject *u, Py_ssize_t pos, |
| 10215 | Py_UCS4 u1, Py_UCS4 u2, Py_ssize_t maxcount) |
| 10216 | { |
| 10217 | int kind = PyUnicode_KIND(u); |
| 10218 | void *data = PyUnicode_DATA(u); |
| 10219 | Py_ssize_t len = PyUnicode_GET_LENGTH(u); |
| 10220 | if (kind == PyUnicode_1BYTE_KIND) { |
| 10221 | ucs1lib_replace_1char_inplace((Py_UCS1 *)data + pos, |
| 10222 | (Py_UCS1 *)data + len, |
| 10223 | u1, u2, maxcount); |
| 10224 | } |
| 10225 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 10226 | ucs2lib_replace_1char_inplace((Py_UCS2 *)data + pos, |
| 10227 | (Py_UCS2 *)data + len, |
| 10228 | u1, u2, maxcount); |
| 10229 | } |
| 10230 | else { |
| 10231 | assert(kind == PyUnicode_4BYTE_KIND); |
| 10232 | ucs4lib_replace_1char_inplace((Py_UCS4 *)data + pos, |
| 10233 | (Py_UCS4 *)data + len, |
| 10234 | u1, u2, maxcount); |
| 10235 | } |
| 10236 | } |
| 10237 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10238 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10239 | replace(PyObject *self, PyObject *str1, |
| 10240 | PyObject *str2, Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10241 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10242 | PyObject *u; |
| 10243 | char *sbuf = PyUnicode_DATA(self); |
| 10244 | char *buf1 = PyUnicode_DATA(str1); |
| 10245 | char *buf2 = PyUnicode_DATA(str2); |
| 10246 | int srelease = 0, release1 = 0, release2 = 0; |
| 10247 | int skind = PyUnicode_KIND(self); |
| 10248 | int kind1 = PyUnicode_KIND(str1); |
| 10249 | int kind2 = PyUnicode_KIND(str2); |
| 10250 | Py_ssize_t slen = PyUnicode_GET_LENGTH(self); |
| 10251 | Py_ssize_t len1 = PyUnicode_GET_LENGTH(str1); |
| 10252 | Py_ssize_t len2 = PyUnicode_GET_LENGTH(str2); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10253 | int mayshrink; |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 10254 | Py_UCS4 maxchar, maxchar_str1, maxchar_str2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10255 | |
| 10256 | if (maxcount < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10257 | maxcount = PY_SSIZE_T_MAX; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10258 | else if (maxcount == 0 || slen == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10259 | goto nothing; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10260 | |
Victor Stinner | 59de0ee | 2011-10-07 10:01:28 +0200 | [diff] [blame] | 10261 | if (str1 == str2) |
| 10262 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10263 | |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10264 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 10265 | maxchar_str1 = PyUnicode_MAX_CHAR_VALUE(str1); |
| 10266 | if (maxchar < maxchar_str1) |
| 10267 | /* substring too wide to be present */ |
| 10268 | goto nothing; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10269 | maxchar_str2 = PyUnicode_MAX_CHAR_VALUE(str2); |
| 10270 | /* Replacing str1 with str2 may cause a maxchar reduction in the |
| 10271 | result string. */ |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 10272 | mayshrink = (maxchar_str2 < maxchar_str1) && (maxchar == maxchar_str1); |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 10273 | maxchar = Py_MAX(maxchar, maxchar_str2); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10274 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10275 | if (len1 == len2) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10276 | /* same length */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10277 | if (len1 == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10278 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10279 | if (len1 == 1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10280 | /* replace characters */ |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10281 | Py_UCS4 u1, u2; |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 10282 | Py_ssize_t pos; |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10283 | |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 10284 | u1 = PyUnicode_READ(kind1, buf1, 0); |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 10285 | pos = findchar(sbuf, skind, slen, u1, 1); |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10286 | if (pos < 0) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10287 | goto nothing; |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 10288 | u2 = PyUnicode_READ(kind2, buf2, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10289 | u = PyUnicode_New(slen, maxchar); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10290 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10291 | goto error; |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10292 | |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 10293 | _PyUnicode_FastCopyCharacters(u, 0, self, 0, slen); |
| 10294 | replace_1char_inplace(u, pos, u1, u2, maxcount); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10295 | } |
| 10296 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10297 | int rkind = skind; |
| 10298 | char *res; |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10299 | Py_ssize_t i; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10300 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10301 | if (kind1 < rkind) { |
| 10302 | /* widen substring */ |
| 10303 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10304 | if (!buf1) goto error; |
| 10305 | release1 = 1; |
| 10306 | } |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10307 | i = anylib_find(rkind, self, sbuf, slen, str1, buf1, len1, 0); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10308 | if (i < 0) |
| 10309 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10310 | if (rkind > kind2) { |
| 10311 | /* widen replacement */ |
| 10312 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 10313 | if (!buf2) goto error; |
| 10314 | release2 = 1; |
| 10315 | } |
| 10316 | else if (rkind < kind2) { |
| 10317 | /* widen self and buf1 */ |
| 10318 | rkind = kind2; |
| 10319 | if (release1) PyMem_Free(buf1); |
Antoine Pitrou | 6d5ad22 | 2012-11-17 23:28:17 +0100 | [diff] [blame] | 10320 | release1 = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10321 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 10322 | if (!sbuf) goto error; |
| 10323 | srelease = 1; |
| 10324 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10325 | if (!buf1) goto error; |
| 10326 | release1 = 1; |
| 10327 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10328 | u = PyUnicode_New(slen, maxchar); |
| 10329 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10330 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10331 | assert(PyUnicode_KIND(u) == rkind); |
| 10332 | res = PyUnicode_DATA(u); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10333 | |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10334 | memcpy(res, sbuf, rkind * slen); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10335 | /* change everything in-place, starting with this one */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10336 | memcpy(res + rkind * i, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10337 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10338 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10339 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10340 | |
| 10341 | while ( --maxcount > 0) { |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10342 | i = anylib_find(rkind, self, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10343 | sbuf+rkind*i, slen-i, |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10344 | str1, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10345 | if (i == -1) |
| 10346 | break; |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10347 | memcpy(res + rkind * i, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10348 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10349 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10350 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10351 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10352 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10353 | } |
| 10354 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10355 | Py_ssize_t n, i, j, ires; |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 10356 | Py_ssize_t new_size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10357 | int rkind = skind; |
| 10358 | char *res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10359 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10360 | if (kind1 < rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10361 | /* widen substring */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10362 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10363 | if (!buf1) goto error; |
| 10364 | release1 = 1; |
| 10365 | } |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10366 | n = anylib_count(rkind, self, sbuf, slen, str1, buf1, len1, maxcount); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10367 | if (n == 0) |
| 10368 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10369 | if (kind2 < rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10370 | /* widen replacement */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10371 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 10372 | if (!buf2) goto error; |
| 10373 | release2 = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10374 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10375 | else if (kind2 > rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10376 | /* widen self and buf1 */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10377 | rkind = kind2; |
| 10378 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 10379 | if (!sbuf) goto error; |
| 10380 | srelease = 1; |
| 10381 | if (release1) PyMem_Free(buf1); |
Antoine Pitrou | 6d5ad22 | 2012-11-17 23:28:17 +0100 | [diff] [blame] | 10382 | release1 = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10383 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10384 | if (!buf1) goto error; |
| 10385 | release1 = 1; |
| 10386 | } |
| 10387 | /* new_size = PyUnicode_GET_LENGTH(self) + n * (PyUnicode_GET_LENGTH(str2) - |
| 10388 | PyUnicode_GET_LENGTH(str1))); */ |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 10389 | 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] | 10390 | PyErr_SetString(PyExc_OverflowError, |
| 10391 | "replace string is too long"); |
| 10392 | goto error; |
| 10393 | } |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 10394 | new_size = slen + n * (len2 - len1); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10395 | if (new_size == 0) { |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 10396 | _Py_INCREF_UNICODE_EMPTY(); |
| 10397 | if (!unicode_empty) |
| 10398 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10399 | u = unicode_empty; |
| 10400 | goto done; |
| 10401 | } |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 10402 | if (new_size > (PY_SSIZE_T_MAX >> (rkind-1))) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10403 | PyErr_SetString(PyExc_OverflowError, |
| 10404 | "replace string is too long"); |
| 10405 | goto error; |
| 10406 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10407 | u = PyUnicode_New(new_size, maxchar); |
| 10408 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10409 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10410 | assert(PyUnicode_KIND(u) == rkind); |
| 10411 | res = PyUnicode_DATA(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10412 | ires = i = 0; |
| 10413 | if (len1 > 0) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10414 | while (n-- > 0) { |
| 10415 | /* look for next match */ |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10416 | j = anylib_find(rkind, self, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10417 | sbuf + rkind * i, slen-i, |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10418 | str1, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10419 | if (j == -1) |
| 10420 | break; |
| 10421 | else if (j > i) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10422 | /* copy unchanged part [i:j] */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10423 | memcpy(res + rkind * ires, |
| 10424 | sbuf + rkind * i, |
| 10425 | rkind * (j-i)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10426 | ires += j - i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10427 | } |
| 10428 | /* copy substitution string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10429 | if (len2 > 0) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10430 | memcpy(res + rkind * ires, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10431 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10432 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10433 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10434 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10435 | i = j + len1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10436 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10437 | if (i < slen) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10438 | /* copy tail [i:] */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10439 | memcpy(res + rkind * ires, |
| 10440 | sbuf + rkind * i, |
| 10441 | rkind * (slen-i)); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10442 | } |
| 10443 | else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10444 | /* interleave */ |
| 10445 | while (n > 0) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10446 | memcpy(res + rkind * ires, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10447 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10448 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10449 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10450 | if (--n <= 0) |
| 10451 | break; |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10452 | memcpy(res + rkind * ires, |
| 10453 | sbuf + rkind * i, |
| 10454 | rkind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10455 | ires++; |
| 10456 | i++; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10457 | } |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10458 | memcpy(res + rkind * ires, |
| 10459 | sbuf + rkind * i, |
| 10460 | rkind * (slen-i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10461 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10462 | } |
| 10463 | |
| 10464 | if (mayshrink) { |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10465 | unicode_adjust_maxchar(&u); |
| 10466 | if (u == NULL) |
| 10467 | goto error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10468 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10469 | |
| 10470 | done: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10471 | if (srelease) |
| 10472 | PyMem_FREE(sbuf); |
| 10473 | if (release1) |
| 10474 | PyMem_FREE(buf1); |
| 10475 | if (release2) |
| 10476 | PyMem_FREE(buf2); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10477 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10478 | return u; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10479 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10480 | nothing: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10481 | /* nothing to replace; return original string (when possible) */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10482 | if (srelease) |
| 10483 | PyMem_FREE(sbuf); |
| 10484 | if (release1) |
| 10485 | PyMem_FREE(buf1); |
| 10486 | if (release2) |
| 10487 | PyMem_FREE(buf2); |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10488 | return unicode_result_unchanged(self); |
| 10489 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10490 | error: |
| 10491 | if (srelease && sbuf) |
| 10492 | PyMem_FREE(sbuf); |
| 10493 | if (release1 && buf1) |
| 10494 | PyMem_FREE(buf1); |
| 10495 | if (release2 && buf2) |
| 10496 | PyMem_FREE(buf2); |
| 10497 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10498 | } |
| 10499 | |
| 10500 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 10501 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10502 | PyDoc_STRVAR(title__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10503 | "S.title() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10504 | \n\ |
| 10505 | 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] | 10506 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10507 | |
| 10508 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10509 | unicode_title(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10510 | { |
Benjamin Peterson | eea4846 | 2012-01-16 14:28:50 -0500 | [diff] [blame] | 10511 | if (PyUnicode_READY(self) == -1) |
| 10512 | return NULL; |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 10513 | return case_operation(self, do_title); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10514 | } |
| 10515 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10516 | PyDoc_STRVAR(capitalize__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10517 | "S.capitalize() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10518 | \n\ |
| 10519 | 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] | 10520 | have upper case and the rest lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10521 | |
| 10522 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10523 | unicode_capitalize(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10524 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 10525 | if (PyUnicode_READY(self) == -1) |
| 10526 | return NULL; |
| 10527 | if (PyUnicode_GET_LENGTH(self) == 0) |
| 10528 | return unicode_result_unchanged(self); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 10529 | return case_operation(self, do_capitalize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10530 | } |
| 10531 | |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 10532 | PyDoc_STRVAR(casefold__doc__, |
| 10533 | "S.casefold() -> str\n\ |
| 10534 | \n\ |
| 10535 | Return a version of S suitable for caseless comparisons."); |
| 10536 | |
| 10537 | static PyObject * |
| 10538 | unicode_casefold(PyObject *self) |
| 10539 | { |
| 10540 | if (PyUnicode_READY(self) == -1) |
| 10541 | return NULL; |
| 10542 | if (PyUnicode_IS_ASCII(self)) |
| 10543 | return ascii_upper_or_lower(self, 1); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 10544 | return case_operation(self, do_casefold); |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 10545 | } |
| 10546 | |
| 10547 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10548 | /* Argument converter. Coerces to a single unicode character */ |
| 10549 | |
| 10550 | static int |
| 10551 | convert_uc(PyObject *obj, void *addr) |
| 10552 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10553 | Py_UCS4 *fillcharloc = (Py_UCS4 *)addr; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10554 | PyObject *uniobj; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10555 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10556 | uniobj = PyUnicode_FromObject(obj); |
| 10557 | if (uniobj == NULL) { |
| 10558 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10559 | "The fill character cannot be converted to Unicode"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10560 | return 0; |
| 10561 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10562 | if (PyUnicode_GET_LENGTH(uniobj) != 1) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10563 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10564 | "The fill character must be exactly one character long"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10565 | Py_DECREF(uniobj); |
| 10566 | return 0; |
| 10567 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10568 | *fillcharloc = PyUnicode_READ_CHAR(uniobj, 0); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10569 | Py_DECREF(uniobj); |
| 10570 | return 1; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10571 | } |
| 10572 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10573 | PyDoc_STRVAR(center__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10574 | "S.center(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10575 | \n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10576 | Return S centered in a string of length width. Padding is\n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10577 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10578 | |
| 10579 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10580 | unicode_center(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10581 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10582 | Py_ssize_t marg, left; |
| 10583 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10584 | Py_UCS4 fillchar = ' '; |
| 10585 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 10586 | 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] | 10587 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10588 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 10589 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10590 | return NULL; |
| 10591 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10592 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 10593 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10594 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10595 | marg = width - PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10596 | left = marg / 2 + (marg & width & 1); |
| 10597 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10598 | return pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10599 | } |
| 10600 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10601 | /* This function assumes that str1 and str2 are readied by the caller. */ |
| 10602 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10603 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10604 | unicode_compare(PyObject *str1, PyObject *str2) |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10605 | { |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10606 | #define COMPARE(TYPE1, TYPE2) \ |
| 10607 | do { \ |
| 10608 | TYPE1* p1 = (TYPE1 *)data1; \ |
| 10609 | TYPE2* p2 = (TYPE2 *)data2; \ |
| 10610 | TYPE1* end = p1 + len; \ |
| 10611 | Py_UCS4 c1, c2; \ |
| 10612 | for (; p1 != end; p1++, p2++) { \ |
| 10613 | c1 = *p1; \ |
| 10614 | c2 = *p2; \ |
| 10615 | if (c1 != c2) \ |
| 10616 | return (c1 < c2) ? -1 : 1; \ |
| 10617 | } \ |
| 10618 | } \ |
| 10619 | while (0) |
| 10620 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10621 | int kind1, kind2; |
| 10622 | void *data1, *data2; |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10623 | Py_ssize_t len1, len2, len; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10624 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10625 | kind1 = PyUnicode_KIND(str1); |
| 10626 | kind2 = PyUnicode_KIND(str2); |
| 10627 | data1 = PyUnicode_DATA(str1); |
| 10628 | data2 = PyUnicode_DATA(str2); |
| 10629 | len1 = PyUnicode_GET_LENGTH(str1); |
| 10630 | len2 = PyUnicode_GET_LENGTH(str2); |
Victor Stinner | 770e19e | 2012-10-04 22:59:45 +0200 | [diff] [blame] | 10631 | len = Py_MIN(len1, len2); |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10632 | |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10633 | switch(kind1) { |
| 10634 | case PyUnicode_1BYTE_KIND: |
| 10635 | { |
| 10636 | switch(kind2) { |
| 10637 | case PyUnicode_1BYTE_KIND: |
| 10638 | { |
| 10639 | int cmp = memcmp(data1, data2, len); |
| 10640 | /* normalize result of memcmp() into the range [-1; 1] */ |
| 10641 | if (cmp < 0) |
| 10642 | return -1; |
| 10643 | if (cmp > 0) |
| 10644 | return 1; |
| 10645 | break; |
Victor Stinner | 770e19e | 2012-10-04 22:59:45 +0200 | [diff] [blame] | 10646 | } |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10647 | case PyUnicode_2BYTE_KIND: |
| 10648 | COMPARE(Py_UCS1, Py_UCS2); |
| 10649 | break; |
| 10650 | case PyUnicode_4BYTE_KIND: |
| 10651 | COMPARE(Py_UCS1, Py_UCS4); |
| 10652 | break; |
| 10653 | default: |
| 10654 | assert(0); |
| 10655 | } |
| 10656 | break; |
| 10657 | } |
| 10658 | case PyUnicode_2BYTE_KIND: |
| 10659 | { |
| 10660 | switch(kind2) { |
| 10661 | case PyUnicode_1BYTE_KIND: |
| 10662 | COMPARE(Py_UCS2, Py_UCS1); |
| 10663 | break; |
| 10664 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | cd777ea | 2013-04-08 22:43:44 +0200 | [diff] [blame] | 10665 | { |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10666 | COMPARE(Py_UCS2, Py_UCS2); |
| 10667 | break; |
Victor Stinner | cd777ea | 2013-04-08 22:43:44 +0200 | [diff] [blame] | 10668 | } |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10669 | case PyUnicode_4BYTE_KIND: |
| 10670 | COMPARE(Py_UCS2, Py_UCS4); |
| 10671 | break; |
| 10672 | default: |
| 10673 | assert(0); |
| 10674 | } |
| 10675 | break; |
| 10676 | } |
| 10677 | case PyUnicode_4BYTE_KIND: |
| 10678 | { |
| 10679 | switch(kind2) { |
| 10680 | case PyUnicode_1BYTE_KIND: |
| 10681 | COMPARE(Py_UCS4, Py_UCS1); |
| 10682 | break; |
| 10683 | case PyUnicode_2BYTE_KIND: |
| 10684 | COMPARE(Py_UCS4, Py_UCS2); |
| 10685 | break; |
| 10686 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | cd777ea | 2013-04-08 22:43:44 +0200 | [diff] [blame] | 10687 | { |
| 10688 | #if defined(HAVE_WMEMCMP) && SIZEOF_WCHAR_T == 4 |
| 10689 | int cmp = wmemcmp((wchar_t *)data1, (wchar_t *)data2, len); |
| 10690 | /* normalize result of wmemcmp() into the range [-1; 1] */ |
| 10691 | if (cmp < 0) |
| 10692 | return -1; |
| 10693 | if (cmp > 0) |
| 10694 | return 1; |
| 10695 | #else |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10696 | COMPARE(Py_UCS4, Py_UCS4); |
Victor Stinner | cd777ea | 2013-04-08 22:43:44 +0200 | [diff] [blame] | 10697 | #endif |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10698 | break; |
Victor Stinner | cd777ea | 2013-04-08 22:43:44 +0200 | [diff] [blame] | 10699 | } |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10700 | default: |
| 10701 | assert(0); |
| 10702 | } |
| 10703 | break; |
| 10704 | } |
| 10705 | default: |
| 10706 | assert(0); |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10707 | } |
| 10708 | |
Victor Stinner | 770e19e | 2012-10-04 22:59:45 +0200 | [diff] [blame] | 10709 | if (len1 == len2) |
| 10710 | return 0; |
| 10711 | if (len1 < len2) |
| 10712 | return -1; |
| 10713 | else |
| 10714 | return 1; |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10715 | |
| 10716 | #undef COMPARE |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10717 | } |
| 10718 | |
Victor Stinner | c8bc537 | 2013-11-04 11:08:10 +0100 | [diff] [blame] | 10719 | Py_LOCAL(int) |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10720 | unicode_compare_eq(PyObject *str1, PyObject *str2) |
| 10721 | { |
| 10722 | int kind; |
| 10723 | void *data1, *data2; |
| 10724 | Py_ssize_t len; |
| 10725 | int cmp; |
| 10726 | |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10727 | len = PyUnicode_GET_LENGTH(str1); |
| 10728 | if (PyUnicode_GET_LENGTH(str2) != len) |
| 10729 | return 0; |
| 10730 | kind = PyUnicode_KIND(str1); |
| 10731 | if (PyUnicode_KIND(str2) != kind) |
| 10732 | return 0; |
| 10733 | data1 = PyUnicode_DATA(str1); |
| 10734 | data2 = PyUnicode_DATA(str2); |
| 10735 | |
| 10736 | cmp = memcmp(data1, data2, len * kind); |
| 10737 | return (cmp == 0); |
| 10738 | } |
| 10739 | |
| 10740 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10741 | int |
| 10742 | PyUnicode_Compare(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10743 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10744 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 10745 | if (PyUnicode_READY(left) == -1 || |
| 10746 | PyUnicode_READY(right) == -1) |
| 10747 | return -1; |
Victor Stinner | f0c7b2a | 2013-11-04 11:27:14 +0100 | [diff] [blame] | 10748 | |
| 10749 | /* a string is equal to itself */ |
| 10750 | if (left == right) |
| 10751 | return 0; |
| 10752 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10753 | return unicode_compare(left, right); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10754 | } |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 10755 | PyErr_Format(PyExc_TypeError, |
| 10756 | "Can't compare %.100s and %.100s", |
| 10757 | left->ob_type->tp_name, |
| 10758 | right->ob_type->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10759 | return -1; |
| 10760 | } |
| 10761 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10762 | int |
Victor Stinner | ad14ccd | 2013-11-07 00:46:04 +0100 | [diff] [blame] | 10763 | _PyUnicode_CompareWithId(PyObject *left, _Py_Identifier *right) |
| 10764 | { |
| 10765 | PyObject *right_str = _PyUnicode_FromId(right); /* borrowed */ |
| 10766 | if (right_str == NULL) |
| 10767 | return -1; |
| 10768 | return PyUnicode_Compare(left, right_str); |
| 10769 | } |
| 10770 | |
| 10771 | int |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10772 | PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str) |
| 10773 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10774 | Py_ssize_t i; |
| 10775 | int kind; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10776 | Py_UCS4 chr; |
| 10777 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 10778 | assert(_PyUnicode_CHECK(uni)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10779 | if (PyUnicode_READY(uni) == -1) |
| 10780 | return -1; |
| 10781 | kind = PyUnicode_KIND(uni); |
Victor Stinner | 602f7cf | 2013-10-29 23:31:50 +0100 | [diff] [blame] | 10782 | if (kind == PyUnicode_1BYTE_KIND) { |
Victor Stinner | a6b9b07 | 2013-10-30 18:27:13 +0100 | [diff] [blame] | 10783 | const void *data = PyUnicode_1BYTE_DATA(uni); |
Victor Stinner | e1b1592 | 2013-11-03 13:53:12 +0100 | [diff] [blame] | 10784 | size_t len1 = (size_t)PyUnicode_GET_LENGTH(uni); |
Victor Stinner | 602f7cf | 2013-10-29 23:31:50 +0100 | [diff] [blame] | 10785 | size_t len, len2 = strlen(str); |
| 10786 | int cmp; |
| 10787 | |
| 10788 | len = Py_MIN(len1, len2); |
| 10789 | cmp = memcmp(data, str, len); |
Victor Stinner | 21ea21e | 2013-11-04 11:28:26 +0100 | [diff] [blame] | 10790 | if (cmp != 0) { |
| 10791 | if (cmp < 0) |
| 10792 | return -1; |
| 10793 | else |
| 10794 | return 1; |
| 10795 | } |
Victor Stinner | 602f7cf | 2013-10-29 23:31:50 +0100 | [diff] [blame] | 10796 | if (len1 > len2) |
| 10797 | return 1; /* uni is longer */ |
| 10798 | if (len2 > len1) |
| 10799 | return -1; /* str is longer */ |
| 10800 | return 0; |
| 10801 | } |
| 10802 | else { |
| 10803 | void *data = PyUnicode_DATA(uni); |
| 10804 | /* Compare Unicode string and source character set string */ |
| 10805 | for (i = 0; (chr = PyUnicode_READ(kind, data, i)) && str[i]; i++) |
| 10806 | if (chr != str[i]) |
| 10807 | return (chr < (unsigned char)(str[i])) ? -1 : 1; |
| 10808 | /* This check keeps Python strings that end in '\0' from comparing equal |
| 10809 | to C strings identical up to that point. */ |
| 10810 | if (PyUnicode_GET_LENGTH(uni) != i || chr) |
| 10811 | return 1; /* uni is longer */ |
| 10812 | if (str[i]) |
| 10813 | return -1; /* str is longer */ |
| 10814 | return 0; |
| 10815 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10816 | } |
| 10817 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10818 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10819 | #define TEST_COND(cond) \ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10820 | ((cond) ? Py_True : Py_False) |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10821 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10822 | PyObject * |
| 10823 | PyUnicode_RichCompare(PyObject *left, PyObject *right, int op) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10824 | { |
| 10825 | int result; |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10826 | PyObject *v; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10827 | |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10828 | if (!PyUnicode_Check(left) || !PyUnicode_Check(right)) |
| 10829 | Py_RETURN_NOTIMPLEMENTED; |
| 10830 | |
| 10831 | if (PyUnicode_READY(left) == -1 || |
| 10832 | PyUnicode_READY(right) == -1) |
| 10833 | return NULL; |
| 10834 | |
Victor Stinner | fd9e44d | 2013-11-04 11:23:05 +0100 | [diff] [blame] | 10835 | if (left == right) { |
| 10836 | switch (op) { |
| 10837 | case Py_EQ: |
| 10838 | case Py_LE: |
| 10839 | case Py_GE: |
| 10840 | /* a string is equal to itself */ |
| 10841 | v = Py_True; |
| 10842 | break; |
| 10843 | case Py_NE: |
| 10844 | case Py_LT: |
| 10845 | case Py_GT: |
| 10846 | v = Py_False; |
| 10847 | break; |
| 10848 | default: |
| 10849 | PyErr_BadArgument(); |
| 10850 | return NULL; |
| 10851 | } |
| 10852 | } |
| 10853 | else if (op == Py_EQ || op == Py_NE) { |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10854 | result = unicode_compare_eq(left, right); |
Victor Stinner | c8bc537 | 2013-11-04 11:08:10 +0100 | [diff] [blame] | 10855 | result ^= (op == Py_NE); |
| 10856 | v = TEST_COND(result); |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10857 | } |
| 10858 | else { |
Victor Stinner | 90db9c4 | 2012-10-04 21:53:50 +0200 | [diff] [blame] | 10859 | result = unicode_compare(left, right); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10860 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10861 | /* Convert the return value to a Boolean */ |
| 10862 | switch (op) { |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10863 | case Py_LE: |
| 10864 | v = TEST_COND(result <= 0); |
| 10865 | break; |
| 10866 | case Py_GE: |
| 10867 | v = TEST_COND(result >= 0); |
| 10868 | break; |
| 10869 | case Py_LT: |
| 10870 | v = TEST_COND(result == -1); |
| 10871 | break; |
| 10872 | case Py_GT: |
| 10873 | v = TEST_COND(result == 1); |
| 10874 | break; |
| 10875 | default: |
| 10876 | PyErr_BadArgument(); |
| 10877 | return NULL; |
| 10878 | } |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10879 | } |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10880 | Py_INCREF(v); |
| 10881 | return v; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10882 | } |
| 10883 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10884 | int |
| 10885 | PyUnicode_Contains(PyObject *container, PyObject *element) |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10886 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10887 | PyObject *str, *sub; |
Victor Stinner | 77282cb | 2013-04-14 19:22:47 +0200 | [diff] [blame] | 10888 | int kind1, kind2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10889 | void *buf1, *buf2; |
| 10890 | Py_ssize_t len1, len2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10891 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10892 | |
| 10893 | /* Coerce the two arguments */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10894 | sub = PyUnicode_FromObject(element); |
| 10895 | if (!sub) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10896 | PyErr_Format(PyExc_TypeError, |
| 10897 | "'in <string>' requires string as left operand, not %s", |
| 10898 | element->ob_type->tp_name); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10899 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10900 | } |
| 10901 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10902 | str = PyUnicode_FromObject(container); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 10903 | if (!str) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10904 | Py_DECREF(sub); |
| 10905 | return -1; |
| 10906 | } |
| 10907 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10908 | kind1 = PyUnicode_KIND(str); |
| 10909 | kind2 = PyUnicode_KIND(sub); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10910 | buf1 = PyUnicode_DATA(str); |
| 10911 | buf2 = PyUnicode_DATA(sub); |
Victor Stinner | 77282cb | 2013-04-14 19:22:47 +0200 | [diff] [blame] | 10912 | if (kind2 != kind1) { |
| 10913 | if (kind2 > kind1) { |
Antoine Pitrou | 758153b | 2012-05-12 15:51:51 +0200 | [diff] [blame] | 10914 | Py_DECREF(sub); |
| 10915 | Py_DECREF(str); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 10916 | return 0; |
Antoine Pitrou | 758153b | 2012-05-12 15:51:51 +0200 | [diff] [blame] | 10917 | } |
Victor Stinner | 77282cb | 2013-04-14 19:22:47 +0200 | [diff] [blame] | 10918 | buf2 = _PyUnicode_AsKind(sub, kind1); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 10919 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10920 | if (!buf2) { |
| 10921 | Py_DECREF(sub); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 10922 | Py_DECREF(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10923 | return -1; |
| 10924 | } |
| 10925 | len1 = PyUnicode_GET_LENGTH(str); |
| 10926 | len2 = PyUnicode_GET_LENGTH(sub); |
| 10927 | |
Victor Stinner | 77282cb | 2013-04-14 19:22:47 +0200 | [diff] [blame] | 10928 | switch (kind1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10929 | case PyUnicode_1BYTE_KIND: |
| 10930 | result = ucs1lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10931 | break; |
| 10932 | case PyUnicode_2BYTE_KIND: |
| 10933 | result = ucs2lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10934 | break; |
| 10935 | case PyUnicode_4BYTE_KIND: |
| 10936 | result = ucs4lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10937 | break; |
| 10938 | default: |
| 10939 | result = -1; |
| 10940 | assert(0); |
| 10941 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10942 | |
| 10943 | Py_DECREF(str); |
| 10944 | Py_DECREF(sub); |
| 10945 | |
Victor Stinner | 77282cb | 2013-04-14 19:22:47 +0200 | [diff] [blame] | 10946 | if (kind2 != kind1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10947 | PyMem_Free(buf2); |
| 10948 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10949 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10950 | } |
| 10951 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10952 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 10953 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10954 | PyObject * |
| 10955 | PyUnicode_Concat(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10956 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10957 | PyObject *u = NULL, *v = NULL, *w; |
Victor Stinner | 127226b | 2011-10-13 01:12:34 +0200 | [diff] [blame] | 10958 | Py_UCS4 maxchar, maxchar2; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10959 | Py_ssize_t u_len, v_len, new_len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10960 | |
| 10961 | /* Coerce the two arguments */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10962 | u = PyUnicode_FromObject(left); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10963 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10964 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10965 | v = PyUnicode_FromObject(right); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10966 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10967 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10968 | |
| 10969 | /* Shortcuts */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 10970 | if (v == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10971 | Py_DECREF(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10972 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10973 | } |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 10974 | if (u == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10975 | Py_DECREF(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10976 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10977 | } |
| 10978 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10979 | u_len = PyUnicode_GET_LENGTH(u); |
| 10980 | v_len = PyUnicode_GET_LENGTH(v); |
| 10981 | if (u_len > PY_SSIZE_T_MAX - v_len) { |
| 10982 | PyErr_SetString(PyExc_OverflowError, |
| 10983 | "strings are too large to concat"); |
| 10984 | goto onError; |
| 10985 | } |
| 10986 | new_len = u_len + v_len; |
| 10987 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10988 | maxchar = PyUnicode_MAX_CHAR_VALUE(u); |
Victor Stinner | 127226b | 2011-10-13 01:12:34 +0200 | [diff] [blame] | 10989 | maxchar2 = PyUnicode_MAX_CHAR_VALUE(v); |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 10990 | maxchar = Py_MAX(maxchar, maxchar2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10991 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10992 | /* Concat the two Unicode strings */ |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10993 | w = PyUnicode_New(new_len, maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10994 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10995 | goto onError; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 10996 | _PyUnicode_FastCopyCharacters(w, 0, u, 0, u_len); |
| 10997 | _PyUnicode_FastCopyCharacters(w, u_len, v, 0, v_len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10998 | Py_DECREF(u); |
| 10999 | Py_DECREF(v); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 11000 | assert(_PyUnicode_CheckConsistency(w, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11001 | return w; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11002 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11003 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11004 | Py_XDECREF(u); |
| 11005 | Py_XDECREF(v); |
| 11006 | return NULL; |
| 11007 | } |
| 11008 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 11009 | void |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11010 | PyUnicode_Append(PyObject **p_left, PyObject *right) |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 11011 | { |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11012 | PyObject *left, *res; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11013 | Py_UCS4 maxchar, maxchar2; |
| 11014 | Py_ssize_t left_len, right_len, new_len; |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11015 | |
| 11016 | if (p_left == NULL) { |
| 11017 | if (!PyErr_Occurred()) |
| 11018 | PyErr_BadInternalCall(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11019 | return; |
| 11020 | } |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11021 | left = *p_left; |
Victor Stinner | f033510 | 2013-04-14 19:13:03 +0200 | [diff] [blame] | 11022 | if (right == NULL || left == NULL |
| 11023 | || !PyUnicode_Check(left) || !PyUnicode_Check(right)) { |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11024 | if (!PyErr_Occurred()) |
| 11025 | PyErr_BadInternalCall(); |
| 11026 | goto error; |
| 11027 | } |
| 11028 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 11029 | if (PyUnicode_READY(left) == -1) |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 11030 | goto error; |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 11031 | if (PyUnicode_READY(right) == -1) |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 11032 | goto error; |
| 11033 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11034 | /* Shortcuts */ |
| 11035 | if (left == unicode_empty) { |
| 11036 | Py_DECREF(left); |
| 11037 | Py_INCREF(right); |
| 11038 | *p_left = right; |
| 11039 | return; |
| 11040 | } |
| 11041 | if (right == unicode_empty) |
| 11042 | return; |
| 11043 | |
| 11044 | left_len = PyUnicode_GET_LENGTH(left); |
| 11045 | right_len = PyUnicode_GET_LENGTH(right); |
| 11046 | if (left_len > PY_SSIZE_T_MAX - right_len) { |
| 11047 | PyErr_SetString(PyExc_OverflowError, |
| 11048 | "strings are too large to concat"); |
| 11049 | goto error; |
| 11050 | } |
| 11051 | new_len = left_len + right_len; |
| 11052 | |
| 11053 | if (unicode_modifiable(left) |
| 11054 | && PyUnicode_CheckExact(right) |
| 11055 | && PyUnicode_KIND(right) <= PyUnicode_KIND(left) |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 11056 | /* Don't resize for ascii += latin1. Convert ascii to latin1 requires |
| 11057 | to change the structure size, but characters are stored just after |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 11058 | the structure, and so it requires to move all characters which is |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 11059 | not so different than duplicating the string. */ |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11060 | && !(PyUnicode_IS_ASCII(left) && !PyUnicode_IS_ASCII(right))) |
| 11061 | { |
| 11062 | /* append inplace */ |
Victor Stinner | bb4503f | 2013-04-18 09:41:34 +0200 | [diff] [blame] | 11063 | if (unicode_resize(p_left, new_len) != 0) |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11064 | goto error; |
Victor Stinner | f033510 | 2013-04-14 19:13:03 +0200 | [diff] [blame] | 11065 | |
Victor Stinner | bb4503f | 2013-04-18 09:41:34 +0200 | [diff] [blame] | 11066 | /* copy 'right' into the newly allocated area of 'left' */ |
| 11067 | _PyUnicode_FastCopyCharacters(*p_left, left_len, right, 0, right_len); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11068 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11069 | else { |
| 11070 | maxchar = PyUnicode_MAX_CHAR_VALUE(left); |
| 11071 | maxchar2 = PyUnicode_MAX_CHAR_VALUE(right); |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 11072 | maxchar = Py_MAX(maxchar, maxchar2); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11073 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11074 | /* Concat the two Unicode strings */ |
| 11075 | res = PyUnicode_New(new_len, maxchar); |
| 11076 | if (res == NULL) |
| 11077 | goto error; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 11078 | _PyUnicode_FastCopyCharacters(res, 0, left, 0, left_len); |
| 11079 | _PyUnicode_FastCopyCharacters(res, left_len, right, 0, right_len); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11080 | Py_DECREF(left); |
Victor Stinner | bb4503f | 2013-04-18 09:41:34 +0200 | [diff] [blame] | 11081 | *p_left = res; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11082 | } |
| 11083 | assert(_PyUnicode_CheckConsistency(*p_left, 1)); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11084 | return; |
| 11085 | |
| 11086 | error: |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11087 | Py_CLEAR(*p_left); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 11088 | } |
| 11089 | |
| 11090 | void |
| 11091 | PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right) |
| 11092 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11093 | PyUnicode_Append(pleft, right); |
| 11094 | Py_XDECREF(right); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 11095 | } |
| 11096 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11097 | PyDoc_STRVAR(count__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11098 | "S.count(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11099 | \n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11100 | Return the number of non-overlapping occurrences of substring sub in\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11101 | 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] | 11102 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11103 | |
| 11104 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11105 | unicode_count(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11106 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11107 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11108 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11109 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11110 | PyObject *result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11111 | int kind1, kind2, kind; |
| 11112 | void *buf1, *buf2; |
| 11113 | Py_ssize_t len1, len2, iresult; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11114 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11115 | if (!stringlib_parse_args_finds_unicode("count", args, &substring, |
| 11116 | &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11117 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11118 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11119 | kind1 = PyUnicode_KIND(self); |
| 11120 | kind2 = PyUnicode_KIND(substring); |
Christian Heimes | d47802e | 2013-06-29 21:33:36 +0200 | [diff] [blame] | 11121 | if (kind2 > kind1) { |
| 11122 | Py_DECREF(substring); |
Benjamin Peterson | b63f49f | 2012-05-03 18:31:07 -0400 | [diff] [blame] | 11123 | return PyLong_FromLong(0); |
Christian Heimes | d47802e | 2013-06-29 21:33:36 +0200 | [diff] [blame] | 11124 | } |
Benjamin Peterson | b63f49f | 2012-05-03 18:31:07 -0400 | [diff] [blame] | 11125 | kind = kind1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11126 | buf1 = PyUnicode_DATA(self); |
| 11127 | buf2 = PyUnicode_DATA(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11128 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11129 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11130 | if (!buf2) { |
| 11131 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11132 | return NULL; |
| 11133 | } |
| 11134 | len1 = PyUnicode_GET_LENGTH(self); |
| 11135 | len2 = PyUnicode_GET_LENGTH(substring); |
| 11136 | |
| 11137 | ADJUST_INDICES(start, end, len1); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 11138 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11139 | case PyUnicode_1BYTE_KIND: |
| 11140 | iresult = ucs1lib_count( |
| 11141 | ((Py_UCS1*)buf1) + start, end - start, |
| 11142 | buf2, len2, PY_SSIZE_T_MAX |
| 11143 | ); |
| 11144 | break; |
| 11145 | case PyUnicode_2BYTE_KIND: |
| 11146 | iresult = ucs2lib_count( |
| 11147 | ((Py_UCS2*)buf1) + start, end - start, |
| 11148 | buf2, len2, PY_SSIZE_T_MAX |
| 11149 | ); |
| 11150 | break; |
| 11151 | case PyUnicode_4BYTE_KIND: |
| 11152 | iresult = ucs4lib_count( |
| 11153 | ((Py_UCS4*)buf1) + start, end - start, |
| 11154 | buf2, len2, PY_SSIZE_T_MAX |
| 11155 | ); |
| 11156 | break; |
| 11157 | default: |
| 11158 | assert(0); iresult = 0; |
| 11159 | } |
| 11160 | |
| 11161 | result = PyLong_FromSsize_t(iresult); |
| 11162 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11163 | if (kind2 != kind) |
| 11164 | PyMem_Free(buf2); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11165 | |
| 11166 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11167 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11168 | return result; |
| 11169 | } |
| 11170 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11171 | PyDoc_STRVAR(encode__doc__, |
Victor Stinner | c911bbf | 2010-11-07 19:04:46 +0000 | [diff] [blame] | 11172 | "S.encode(encoding='utf-8', errors='strict') -> bytes\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11173 | \n\ |
Victor Stinner | e14e212 | 2010-11-07 18:41:46 +0000 | [diff] [blame] | 11174 | Encode S using the codec registered for encoding. Default encoding\n\ |
| 11175 | 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] | 11176 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 11177 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 11178 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 11179 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11180 | |
| 11181 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11182 | unicode_encode(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11183 | { |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 11184 | static char *kwlist[] = {"encoding", "errors", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11185 | char *encoding = NULL; |
| 11186 | char *errors = NULL; |
Guido van Rossum | 35d9428 | 2007-08-27 18:20:11 +0000 | [diff] [blame] | 11187 | |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 11188 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:encode", |
| 11189 | kwlist, &encoding, &errors)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11190 | return NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11191 | return PyUnicode_AsEncodedString(self, encoding, errors); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 11192 | } |
| 11193 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11194 | PyDoc_STRVAR(expandtabs__doc__, |
Ezio Melotti | 745d54d | 2013-11-16 19:10:57 +0200 | [diff] [blame] | 11195 | "S.expandtabs(tabsize=8) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11196 | \n\ |
| 11197 | 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] | 11198 | 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] | 11199 | |
| 11200 | static PyObject* |
Ezio Melotti | 745d54d | 2013-11-16 19:10:57 +0200 | [diff] [blame] | 11201 | unicode_expandtabs(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11202 | { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11203 | Py_ssize_t i, j, line_pos, src_len, incr; |
| 11204 | Py_UCS4 ch; |
| 11205 | PyObject *u; |
| 11206 | void *src_data, *dest_data; |
Ezio Melotti | 745d54d | 2013-11-16 19:10:57 +0200 | [diff] [blame] | 11207 | static char *kwlist[] = {"tabsize", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11208 | int tabsize = 8; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11209 | int kind; |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 11210 | int found; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11211 | |
Ezio Melotti | 745d54d | 2013-11-16 19:10:57 +0200 | [diff] [blame] | 11212 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:expandtabs", |
| 11213 | kwlist, &tabsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11214 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11215 | |
Antoine Pitrou | 2242522 | 2011-10-04 19:10:51 +0200 | [diff] [blame] | 11216 | if (PyUnicode_READY(self) == -1) |
| 11217 | return NULL; |
| 11218 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 11219 | /* First pass: determine size of output string */ |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11220 | src_len = PyUnicode_GET_LENGTH(self); |
| 11221 | i = j = line_pos = 0; |
| 11222 | kind = PyUnicode_KIND(self); |
| 11223 | src_data = PyUnicode_DATA(self); |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 11224 | found = 0; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11225 | for (; i < src_len; i++) { |
| 11226 | ch = PyUnicode_READ(kind, src_data, i); |
| 11227 | if (ch == '\t') { |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 11228 | found = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11229 | if (tabsize > 0) { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11230 | incr = tabsize - (line_pos % tabsize); /* cannot overflow */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11231 | if (j > PY_SSIZE_T_MAX - incr) |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11232 | goto overflow; |
| 11233 | line_pos += incr; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11234 | j += incr; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11235 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11236 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11237 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11238 | if (j > PY_SSIZE_T_MAX - 1) |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11239 | goto overflow; |
| 11240 | line_pos++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11241 | j++; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11242 | if (ch == '\n' || ch == '\r') |
| 11243 | line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11244 | } |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11245 | } |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11246 | if (!found) |
| 11247 | return unicode_result_unchanged(self); |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 11248 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11249 | /* Second pass: create output string and fill it */ |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11250 | u = PyUnicode_New(j, PyUnicode_MAX_CHAR_VALUE(self)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11251 | if (!u) |
| 11252 | return NULL; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11253 | dest_data = PyUnicode_DATA(u); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11254 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11255 | i = j = line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11256 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11257 | for (; i < src_len; i++) { |
| 11258 | ch = PyUnicode_READ(kind, src_data, i); |
| 11259 | if (ch == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11260 | if (tabsize > 0) { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11261 | incr = tabsize - (line_pos % tabsize); |
| 11262 | line_pos += incr; |
Victor Stinner | da79e63 | 2012-02-22 13:37:04 +0100 | [diff] [blame] | 11263 | FILL(kind, dest_data, ' ', j, incr); |
| 11264 | j += incr; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11265 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11266 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11267 | else { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11268 | line_pos++; |
| 11269 | PyUnicode_WRITE(kind, dest_data, j, ch); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11270 | j++; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11271 | if (ch == '\n' || ch == '\r') |
| 11272 | line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11273 | } |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11274 | } |
| 11275 | assert (j == PyUnicode_GET_LENGTH(u)); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 11276 | return unicode_result(u); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11277 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11278 | overflow: |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11279 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 11280 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11281 | } |
| 11282 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11283 | PyDoc_STRVAR(find__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11284 | "S.find(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11285 | \n\ |
| 11286 | Return the lowest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 11287 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11288 | arguments start and end are interpreted as in slice notation.\n\ |
| 11289 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11290 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11291 | |
| 11292 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11293 | unicode_find(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11294 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11295 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 11296 | Py_ssize_t start; |
| 11297 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11298 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11299 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11300 | if (!stringlib_parse_args_finds_unicode("find", args, &substring, |
| 11301 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11302 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11303 | |
Christian Heimes | d47802e | 2013-06-29 21:33:36 +0200 | [diff] [blame] | 11304 | if (PyUnicode_READY(self) == -1) { |
| 11305 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11306 | return NULL; |
Christian Heimes | d47802e | 2013-06-29 21:33:36 +0200 | [diff] [blame] | 11307 | } |
| 11308 | if (PyUnicode_READY(substring) == -1) { |
| 11309 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11310 | return NULL; |
Christian Heimes | d47802e | 2013-06-29 21:33:36 +0200 | [diff] [blame] | 11311 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11312 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11313 | result = any_find_slice(1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11314 | |
| 11315 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11316 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11317 | if (result == -2) |
| 11318 | return NULL; |
| 11319 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11320 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11321 | } |
| 11322 | |
| 11323 | static PyObject * |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 11324 | unicode_getitem(PyObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11325 | { |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 11326 | void *data; |
| 11327 | enum PyUnicode_Kind kind; |
| 11328 | Py_UCS4 ch; |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 11329 | |
| 11330 | if (!PyUnicode_Check(self) || PyUnicode_READY(self) == -1) { |
| 11331 | PyErr_BadArgument(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11332 | return NULL; |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 11333 | } |
| 11334 | if (index < 0 || index >= PyUnicode_GET_LENGTH(self)) { |
| 11335 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 11336 | return NULL; |
| 11337 | } |
| 11338 | kind = PyUnicode_KIND(self); |
| 11339 | data = PyUnicode_DATA(self); |
| 11340 | ch = PyUnicode_READ(kind, data, index); |
Victor Stinner | 985a82a | 2014-01-03 12:53:47 +0100 | [diff] [blame] | 11341 | return unicode_char(ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11342 | } |
| 11343 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11344 | /* Believe it or not, this produces the same value for ASCII strings |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 11345 | as bytes_hash(). */ |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 11346 | static Py_hash_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11347 | unicode_hash(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11348 | { |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11349 | Py_ssize_t len; |
Gregory P. Smith | 27cbcd6 | 2012-12-10 18:15:46 -0800 | [diff] [blame] | 11350 | Py_uhash_t x; /* Unsigned for defined overflow behavior. */ |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11351 | |
Benjamin Peterson | f6622c8 | 2012-04-09 14:53:07 -0400 | [diff] [blame] | 11352 | #ifdef Py_DEBUG |
Benjamin Peterson | 69e9727 | 2012-02-21 11:08:50 -0500 | [diff] [blame] | 11353 | assert(_Py_HashSecret_Initialized); |
Benjamin Peterson | f6622c8 | 2012-04-09 14:53:07 -0400 | [diff] [blame] | 11354 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11355 | if (_PyUnicode_HASH(self) != -1) |
| 11356 | return _PyUnicode_HASH(self); |
| 11357 | if (PyUnicode_READY(self) == -1) |
| 11358 | return -1; |
| 11359 | len = PyUnicode_GET_LENGTH(self); |
Georg Brandl | 16fa2a1 | 2012-02-21 00:50:13 +0100 | [diff] [blame] | 11360 | /* |
| 11361 | We make the hash of the empty string be 0, rather than using |
| 11362 | (prefix ^ suffix), since this slightly obfuscates the hash secret |
| 11363 | */ |
| 11364 | if (len == 0) { |
| 11365 | _PyUnicode_HASH(self) = 0; |
| 11366 | return 0; |
| 11367 | } |
Christian Heimes | 985ecdc | 2013-11-20 11:46:18 +0100 | [diff] [blame] | 11368 | x = _Py_HashBytes(PyUnicode_DATA(self), |
| 11369 | PyUnicode_GET_LENGTH(self) * PyUnicode_KIND(self)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11370 | _PyUnicode_HASH(self) = x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11371 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11372 | } |
| 11373 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11374 | PyDoc_STRVAR(index__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11375 | "S.index(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11376 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11377 | 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] | 11378 | |
| 11379 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11380 | unicode_index(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11381 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11382 | Py_ssize_t result; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11383 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 11384 | Py_ssize_t start; |
| 11385 | Py_ssize_t end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11386 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11387 | if (!stringlib_parse_args_finds_unicode("index", args, &substring, |
| 11388 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11389 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11390 | |
Christian Heimes | d47a045 | 2013-06-29 21:21:37 +0200 | [diff] [blame] | 11391 | if (PyUnicode_READY(self) == -1) { |
| 11392 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11393 | return NULL; |
Christian Heimes | d47a045 | 2013-06-29 21:21:37 +0200 | [diff] [blame] | 11394 | } |
| 11395 | if (PyUnicode_READY(substring) == -1) { |
| 11396 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11397 | return NULL; |
Christian Heimes | d47a045 | 2013-06-29 21:21:37 +0200 | [diff] [blame] | 11398 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11399 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11400 | result = any_find_slice(1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11401 | |
| 11402 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11403 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11404 | if (result == -2) |
| 11405 | return NULL; |
| 11406 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11407 | if (result < 0) { |
| 11408 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 11409 | return NULL; |
| 11410 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11411 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11412 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11413 | } |
| 11414 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11415 | PyDoc_STRVAR(islower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11416 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11417 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11418 | 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] | 11419 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11420 | |
| 11421 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11422 | unicode_islower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11423 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11424 | Py_ssize_t i, length; |
| 11425 | int kind; |
| 11426 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11427 | int cased; |
| 11428 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11429 | if (PyUnicode_READY(self) == -1) |
| 11430 | return NULL; |
| 11431 | length = PyUnicode_GET_LENGTH(self); |
| 11432 | kind = PyUnicode_KIND(self); |
| 11433 | data = PyUnicode_DATA(self); |
| 11434 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11435 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11436 | if (length == 1) |
| 11437 | return PyBool_FromLong( |
| 11438 | Py_UNICODE_ISLOWER(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11439 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11440 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11441 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11442 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11443 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11444 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11445 | for (i = 0; i < length; i++) { |
| 11446 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11447 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11448 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 11449 | return PyBool_FromLong(0); |
| 11450 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 11451 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11452 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11453 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11454 | } |
| 11455 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11456 | PyDoc_STRVAR(isupper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11457 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11458 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11459 | 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] | 11460 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11461 | |
| 11462 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11463 | unicode_isupper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11464 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11465 | Py_ssize_t i, length; |
| 11466 | int kind; |
| 11467 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11468 | int cased; |
| 11469 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11470 | if (PyUnicode_READY(self) == -1) |
| 11471 | return NULL; |
| 11472 | length = PyUnicode_GET_LENGTH(self); |
| 11473 | kind = PyUnicode_KIND(self); |
| 11474 | data = PyUnicode_DATA(self); |
| 11475 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11476 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11477 | if (length == 1) |
| 11478 | return PyBool_FromLong( |
| 11479 | Py_UNICODE_ISUPPER(PyUnicode_READ(kind, data, 0)) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11480 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11481 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11482 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11483 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11484 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11485 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11486 | for (i = 0; i < length; i++) { |
| 11487 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11488 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11489 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 11490 | return PyBool_FromLong(0); |
| 11491 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 11492 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11493 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11494 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11495 | } |
| 11496 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11497 | PyDoc_STRVAR(istitle__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11498 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11499 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11500 | Return True if S is a titlecased string and there is at least one\n\ |
| 11501 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 11502 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 11503 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11504 | |
| 11505 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11506 | unicode_istitle(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11507 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11508 | Py_ssize_t i, length; |
| 11509 | int kind; |
| 11510 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11511 | int cased, previous_is_cased; |
| 11512 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11513 | if (PyUnicode_READY(self) == -1) |
| 11514 | return NULL; |
| 11515 | length = PyUnicode_GET_LENGTH(self); |
| 11516 | kind = PyUnicode_KIND(self); |
| 11517 | data = PyUnicode_DATA(self); |
| 11518 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11519 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11520 | if (length == 1) { |
| 11521 | Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11522 | return PyBool_FromLong((Py_UNICODE_ISTITLE(ch) != 0) || |
| 11523 | (Py_UNICODE_ISUPPER(ch) != 0)); |
| 11524 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11525 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11526 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11527 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11528 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11529 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11530 | cased = 0; |
| 11531 | previous_is_cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11532 | for (i = 0; i < length; i++) { |
| 11533 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11534 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11535 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 11536 | if (previous_is_cased) |
| 11537 | return PyBool_FromLong(0); |
| 11538 | previous_is_cased = 1; |
| 11539 | cased = 1; |
| 11540 | } |
| 11541 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 11542 | if (!previous_is_cased) |
| 11543 | return PyBool_FromLong(0); |
| 11544 | previous_is_cased = 1; |
| 11545 | cased = 1; |
| 11546 | } |
| 11547 | else |
| 11548 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11549 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11550 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11551 | } |
| 11552 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11553 | PyDoc_STRVAR(isspace__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11554 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11555 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11556 | Return True if all characters in S are whitespace\n\ |
| 11557 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11558 | |
| 11559 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11560 | unicode_isspace(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11561 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11562 | Py_ssize_t i, length; |
| 11563 | int kind; |
| 11564 | void *data; |
| 11565 | |
| 11566 | if (PyUnicode_READY(self) == -1) |
| 11567 | return NULL; |
| 11568 | length = PyUnicode_GET_LENGTH(self); |
| 11569 | kind = PyUnicode_KIND(self); |
| 11570 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11571 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11572 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11573 | if (length == 1) |
| 11574 | return PyBool_FromLong( |
| 11575 | Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11576 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11577 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11578 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11579 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11580 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11581 | for (i = 0; i < length; i++) { |
| 11582 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11583 | if (!Py_UNICODE_ISSPACE(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11584 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11585 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11586 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11587 | } |
| 11588 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11589 | PyDoc_STRVAR(isalpha__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11590 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11591 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11592 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11593 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11594 | |
| 11595 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11596 | unicode_isalpha(PyObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11597 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11598 | Py_ssize_t i, length; |
| 11599 | int kind; |
| 11600 | void *data; |
| 11601 | |
| 11602 | if (PyUnicode_READY(self) == -1) |
| 11603 | return NULL; |
| 11604 | length = PyUnicode_GET_LENGTH(self); |
| 11605 | kind = PyUnicode_KIND(self); |
| 11606 | data = PyUnicode_DATA(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11607 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11608 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11609 | if (length == 1) |
| 11610 | return PyBool_FromLong( |
| 11611 | Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, 0))); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11612 | |
| 11613 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11614 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11615 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11616 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11617 | for (i = 0; i < length; i++) { |
| 11618 | if (!Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11619 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11620 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11621 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11622 | } |
| 11623 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11624 | PyDoc_STRVAR(isalnum__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11625 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11626 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11627 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11628 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11629 | |
| 11630 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11631 | unicode_isalnum(PyObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11632 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11633 | int kind; |
| 11634 | void *data; |
| 11635 | Py_ssize_t len, i; |
| 11636 | |
| 11637 | if (PyUnicode_READY(self) == -1) |
| 11638 | return NULL; |
| 11639 | |
| 11640 | kind = PyUnicode_KIND(self); |
| 11641 | data = PyUnicode_DATA(self); |
| 11642 | len = PyUnicode_GET_LENGTH(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11643 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11644 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11645 | if (len == 1) { |
| 11646 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11647 | return PyBool_FromLong(Py_UNICODE_ISALNUM(ch)); |
| 11648 | } |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11649 | |
| 11650 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11651 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11652 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11653 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11654 | for (i = 0; i < len; i++) { |
| 11655 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11656 | if (!Py_UNICODE_ISALNUM(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11657 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11658 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11659 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11660 | } |
| 11661 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11662 | PyDoc_STRVAR(isdecimal__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11663 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11664 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11665 | 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] | 11666 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11667 | |
| 11668 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11669 | unicode_isdecimal(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11670 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11671 | Py_ssize_t i, length; |
| 11672 | int kind; |
| 11673 | void *data; |
| 11674 | |
| 11675 | if (PyUnicode_READY(self) == -1) |
| 11676 | return NULL; |
| 11677 | length = PyUnicode_GET_LENGTH(self); |
| 11678 | kind = PyUnicode_KIND(self); |
| 11679 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11680 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11681 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11682 | if (length == 1) |
| 11683 | return PyBool_FromLong( |
| 11684 | Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11685 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11686 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11687 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11688 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11689 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11690 | for (i = 0; i < length; i++) { |
| 11691 | if (!Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11692 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11693 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11694 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11695 | } |
| 11696 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11697 | PyDoc_STRVAR(isdigit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11698 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11699 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11700 | Return True if all characters in S are digits\n\ |
| 11701 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11702 | |
| 11703 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11704 | unicode_isdigit(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11705 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11706 | Py_ssize_t i, length; |
| 11707 | int kind; |
| 11708 | void *data; |
| 11709 | |
| 11710 | if (PyUnicode_READY(self) == -1) |
| 11711 | return NULL; |
| 11712 | length = PyUnicode_GET_LENGTH(self); |
| 11713 | kind = PyUnicode_KIND(self); |
| 11714 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11715 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11716 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11717 | if (length == 1) { |
| 11718 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11719 | return PyBool_FromLong(Py_UNICODE_ISDIGIT(ch)); |
| 11720 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11721 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11722 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11723 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11724 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11725 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11726 | for (i = 0; i < length; i++) { |
| 11727 | if (!Py_UNICODE_ISDIGIT(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11728 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11729 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11730 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11731 | } |
| 11732 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11733 | PyDoc_STRVAR(isnumeric__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11734 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11735 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11736 | 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] | 11737 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11738 | |
| 11739 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11740 | unicode_isnumeric(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11741 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11742 | Py_ssize_t i, length; |
| 11743 | int kind; |
| 11744 | void *data; |
| 11745 | |
| 11746 | if (PyUnicode_READY(self) == -1) |
| 11747 | return NULL; |
| 11748 | length = PyUnicode_GET_LENGTH(self); |
| 11749 | kind = PyUnicode_KIND(self); |
| 11750 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11751 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11752 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11753 | if (length == 1) |
| 11754 | return PyBool_FromLong( |
| 11755 | Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11756 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11757 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11758 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11759 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11760 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11761 | for (i = 0; i < length; i++) { |
| 11762 | if (!Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11763 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11764 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11765 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11766 | } |
| 11767 | |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11768 | int |
| 11769 | PyUnicode_IsIdentifier(PyObject *self) |
| 11770 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11771 | int kind; |
| 11772 | void *data; |
| 11773 | Py_ssize_t i; |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11774 | Py_UCS4 first; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11775 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11776 | if (PyUnicode_READY(self) == -1) { |
| 11777 | Py_FatalError("identifier not ready"); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11778 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11779 | } |
| 11780 | |
| 11781 | /* Special case for empty strings */ |
| 11782 | if (PyUnicode_GET_LENGTH(self) == 0) |
| 11783 | return 0; |
| 11784 | kind = PyUnicode_KIND(self); |
| 11785 | data = PyUnicode_DATA(self); |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11786 | |
| 11787 | /* PEP 3131 says that the first character must be in |
| 11788 | XID_Start and subsequent characters in XID_Continue, |
| 11789 | and for the ASCII range, the 2.x rules apply (i.e |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11790 | start with letters and underscore, continue with |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11791 | letters, digits, underscore). However, given the current |
| 11792 | definition of XID_Start and XID_Continue, it is sufficient |
| 11793 | to check just for these, except that _ must be allowed |
| 11794 | as starting an identifier. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11795 | first = PyUnicode_READ(kind, data, 0); |
Benjamin Peterson | f413b80 | 2011-08-12 22:17:18 -0500 | [diff] [blame] | 11796 | if (!_PyUnicode_IsXidStart(first) && first != 0x5F /* LOW LINE */) |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11797 | return 0; |
| 11798 | |
Benjamin Peterson | 9c6e6a0 | 2011-09-28 08:09:05 -0400 | [diff] [blame] | 11799 | for (i = 1; i < PyUnicode_GET_LENGTH(self); i++) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11800 | if (!_PyUnicode_IsXidContinue(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11801 | return 0; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11802 | return 1; |
| 11803 | } |
| 11804 | |
| 11805 | PyDoc_STRVAR(isidentifier__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11806 | "S.isidentifier() -> bool\n\ |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11807 | \n\ |
| 11808 | Return True if S is a valid identifier according\n\ |
Raymond Hettinger | 378170d | 2013-03-23 08:21:12 -0700 | [diff] [blame] | 11809 | to the language definition.\n\ |
| 11810 | \n\ |
| 11811 | Use keyword.iskeyword() to test for reserved identifiers\n\ |
| 11812 | such as \"def\" and \"class\".\n"); |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11813 | |
| 11814 | static PyObject* |
| 11815 | unicode_isidentifier(PyObject *self) |
| 11816 | { |
| 11817 | return PyBool_FromLong(PyUnicode_IsIdentifier(self)); |
| 11818 | } |
| 11819 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11820 | PyDoc_STRVAR(isprintable__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11821 | "S.isprintable() -> bool\n\ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11822 | \n\ |
| 11823 | Return True if all characters in S are considered\n\ |
| 11824 | printable in repr() or S is empty, False otherwise."); |
| 11825 | |
| 11826 | static PyObject* |
| 11827 | unicode_isprintable(PyObject *self) |
| 11828 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11829 | Py_ssize_t i, length; |
| 11830 | int kind; |
| 11831 | void *data; |
| 11832 | |
| 11833 | if (PyUnicode_READY(self) == -1) |
| 11834 | return NULL; |
| 11835 | length = PyUnicode_GET_LENGTH(self); |
| 11836 | kind = PyUnicode_KIND(self); |
| 11837 | data = PyUnicode_DATA(self); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11838 | |
| 11839 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11840 | if (length == 1) |
| 11841 | return PyBool_FromLong( |
| 11842 | Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, 0))); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11843 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11844 | for (i = 0; i < length; i++) { |
| 11845 | if (!Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, i))) { |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11846 | Py_RETURN_FALSE; |
| 11847 | } |
| 11848 | } |
| 11849 | Py_RETURN_TRUE; |
| 11850 | } |
| 11851 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11852 | PyDoc_STRVAR(join__doc__, |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 11853 | "S.join(iterable) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11854 | \n\ |
| 11855 | 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] | 11856 | iterable. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11857 | |
| 11858 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11859 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11860 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11861 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11862 | } |
| 11863 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11864 | static Py_ssize_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11865 | unicode_length(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11866 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11867 | if (PyUnicode_READY(self) == -1) |
| 11868 | return -1; |
| 11869 | return PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11870 | } |
| 11871 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11872 | PyDoc_STRVAR(ljust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11873 | "S.ljust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11874 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 11875 | 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] | 11876 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11877 | |
| 11878 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11879 | unicode_ljust(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11880 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11881 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11882 | Py_UCS4 fillchar = ' '; |
| 11883 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11884 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11885 | return NULL; |
| 11886 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 11887 | if (PyUnicode_READY(self) == -1) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11888 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11889 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11890 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 11891 | return unicode_result_unchanged(self); |
| 11892 | |
| 11893 | return pad(self, 0, width - PyUnicode_GET_LENGTH(self), fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11894 | } |
| 11895 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11896 | PyDoc_STRVAR(lower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11897 | "S.lower() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11898 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11899 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11900 | |
| 11901 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11902 | unicode_lower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11903 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 11904 | if (PyUnicode_READY(self) == -1) |
| 11905 | return NULL; |
| 11906 | if (PyUnicode_IS_ASCII(self)) |
| 11907 | return ascii_upper_or_lower(self, 1); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 11908 | return case_operation(self, do_lower); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11909 | } |
| 11910 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11911 | #define LEFTSTRIP 0 |
| 11912 | #define RIGHTSTRIP 1 |
| 11913 | #define BOTHSTRIP 2 |
| 11914 | |
| 11915 | /* Arrays indexed by above */ |
| 11916 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 11917 | |
| 11918 | #define STRIPNAME(i) (stripformat[i]+3) |
| 11919 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11920 | /* externally visible for str.strip(unicode) */ |
| 11921 | PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11922 | _PyUnicode_XStrip(PyObject *self, int striptype, PyObject *sepobj) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11923 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11924 | void *data; |
| 11925 | int kind; |
| 11926 | Py_ssize_t i, j, len; |
| 11927 | BLOOM_MASK sepmask; |
Victor Stinner | b3a6014 | 2013-04-09 22:19:21 +0200 | [diff] [blame] | 11928 | Py_ssize_t seplen; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11929 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11930 | if (PyUnicode_READY(self) == -1 || PyUnicode_READY(sepobj) == -1) |
| 11931 | return NULL; |
| 11932 | |
| 11933 | kind = PyUnicode_KIND(self); |
| 11934 | data = PyUnicode_DATA(self); |
| 11935 | len = PyUnicode_GET_LENGTH(self); |
Victor Stinner | b3a6014 | 2013-04-09 22:19:21 +0200 | [diff] [blame] | 11936 | seplen = PyUnicode_GET_LENGTH(sepobj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11937 | sepmask = make_bloom_mask(PyUnicode_KIND(sepobj), |
| 11938 | PyUnicode_DATA(sepobj), |
Victor Stinner | b3a6014 | 2013-04-09 22:19:21 +0200 | [diff] [blame] | 11939 | seplen); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11940 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11941 | i = 0; |
| 11942 | if (striptype != RIGHTSTRIP) { |
Victor Stinner | b3a6014 | 2013-04-09 22:19:21 +0200 | [diff] [blame] | 11943 | while (i < len) { |
| 11944 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 11945 | if (!BLOOM(sepmask, ch)) |
| 11946 | break; |
| 11947 | if (PyUnicode_FindChar(sepobj, ch, 0, seplen, 1) < 0) |
| 11948 | break; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11949 | i++; |
| 11950 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11951 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11952 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11953 | j = len; |
| 11954 | if (striptype != LEFTSTRIP) { |
Victor Stinner | b3a6014 | 2013-04-09 22:19:21 +0200 | [diff] [blame] | 11955 | j--; |
| 11956 | while (j >= i) { |
| 11957 | Py_UCS4 ch = PyUnicode_READ(kind, data, j); |
| 11958 | if (!BLOOM(sepmask, ch)) |
| 11959 | break; |
| 11960 | if (PyUnicode_FindChar(sepobj, ch, 0, seplen, 1) < 0) |
| 11961 | break; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11962 | j--; |
Victor Stinner | b3a6014 | 2013-04-09 22:19:21 +0200 | [diff] [blame] | 11963 | } |
| 11964 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11965 | j++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11966 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11967 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11968 | return PyUnicode_Substring(self, i, j); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11969 | } |
| 11970 | |
| 11971 | PyObject* |
| 11972 | PyUnicode_Substring(PyObject *self, Py_ssize_t start, Py_ssize_t end) |
| 11973 | { |
| 11974 | unsigned char *data; |
| 11975 | int kind; |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11976 | Py_ssize_t length; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11977 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 11978 | if (PyUnicode_READY(self) == -1) |
| 11979 | return NULL; |
| 11980 | |
Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 11981 | length = PyUnicode_GET_LENGTH(self); |
| 11982 | end = Py_MIN(end, length); |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 11983 | |
Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 11984 | if (start == 0 && end == length) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11985 | return unicode_result_unchanged(self); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11986 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 11987 | if (start < 0 || end < 0) { |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11988 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 11989 | return NULL; |
| 11990 | } |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 11991 | if (start >= length || end < start) |
| 11992 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11993 | |
Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 11994 | length = end - start; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11995 | if (PyUnicode_IS_ASCII(self)) { |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11996 | data = PyUnicode_1BYTE_DATA(self); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 11997 | return _PyUnicode_FromASCII((char*)(data + start), length); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11998 | } |
| 11999 | else { |
| 12000 | kind = PyUnicode_KIND(self); |
| 12001 | data = PyUnicode_1BYTE_DATA(self); |
| 12002 | return PyUnicode_FromKindAndData(kind, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 12003 | data + kind * start, |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 12004 | length); |
| 12005 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12006 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12007 | |
| 12008 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12009 | do_strip(PyObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12010 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12011 | Py_ssize_t len, i, j; |
| 12012 | |
| 12013 | if (PyUnicode_READY(self) == -1) |
| 12014 | return NULL; |
| 12015 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12016 | len = PyUnicode_GET_LENGTH(self); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12017 | |
Victor Stinner | cc7af72 | 2013-04-09 22:39:24 +0200 | [diff] [blame] | 12018 | if (PyUnicode_IS_ASCII(self)) { |
| 12019 | Py_UCS1 *data = PyUnicode_1BYTE_DATA(self); |
| 12020 | |
| 12021 | i = 0; |
| 12022 | if (striptype != RIGHTSTRIP) { |
| 12023 | while (i < len) { |
Victor Stinner | d92e078 | 2013-04-14 19:17:42 +0200 | [diff] [blame] | 12024 | Py_UCS1 ch = data[i]; |
Victor Stinner | cc7af72 | 2013-04-09 22:39:24 +0200 | [diff] [blame] | 12025 | if (!_Py_ascii_whitespace[ch]) |
| 12026 | break; |
| 12027 | i++; |
| 12028 | } |
| 12029 | } |
| 12030 | |
| 12031 | j = len; |
| 12032 | if (striptype != LEFTSTRIP) { |
| 12033 | j--; |
| 12034 | while (j >= i) { |
Victor Stinner | d92e078 | 2013-04-14 19:17:42 +0200 | [diff] [blame] | 12035 | Py_UCS1 ch = data[j]; |
Victor Stinner | cc7af72 | 2013-04-09 22:39:24 +0200 | [diff] [blame] | 12036 | if (!_Py_ascii_whitespace[ch]) |
| 12037 | break; |
| 12038 | j--; |
| 12039 | } |
| 12040 | j++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12041 | } |
| 12042 | } |
Victor Stinner | cc7af72 | 2013-04-09 22:39:24 +0200 | [diff] [blame] | 12043 | else { |
| 12044 | int kind = PyUnicode_KIND(self); |
| 12045 | void *data = PyUnicode_DATA(self); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12046 | |
Victor Stinner | cc7af72 | 2013-04-09 22:39:24 +0200 | [diff] [blame] | 12047 | i = 0; |
| 12048 | if (striptype != RIGHTSTRIP) { |
| 12049 | while (i < len) { |
| 12050 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 12051 | if (!Py_UNICODE_ISSPACE(ch)) |
| 12052 | break; |
| 12053 | i++; |
| 12054 | } |
Victor Stinner | 9c79e41 | 2013-04-09 22:21:08 +0200 | [diff] [blame] | 12055 | } |
Victor Stinner | cc7af72 | 2013-04-09 22:39:24 +0200 | [diff] [blame] | 12056 | |
| 12057 | j = len; |
| 12058 | if (striptype != LEFTSTRIP) { |
| 12059 | j--; |
| 12060 | while (j >= i) { |
| 12061 | Py_UCS4 ch = PyUnicode_READ(kind, data, j); |
| 12062 | if (!Py_UNICODE_ISSPACE(ch)) |
| 12063 | break; |
| 12064 | j--; |
| 12065 | } |
| 12066 | j++; |
| 12067 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12068 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12069 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12070 | return PyUnicode_Substring(self, i, j); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12071 | } |
| 12072 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12073 | |
| 12074 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12075 | do_argstrip(PyObject *self, int striptype, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12076 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12077 | PyObject *sep = NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12078 | |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 12079 | if (!PyArg_ParseTuple(args, stripformat[striptype], &sep)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12080 | return NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12081 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12082 | if (sep != NULL && sep != Py_None) { |
| 12083 | if (PyUnicode_Check(sep)) |
| 12084 | return _PyUnicode_XStrip(self, striptype, sep); |
| 12085 | else { |
| 12086 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12087 | "%s arg must be None or str", |
| 12088 | STRIPNAME(striptype)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12089 | return NULL; |
| 12090 | } |
| 12091 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12092 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12093 | return do_strip(self, striptype); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12094 | } |
| 12095 | |
| 12096 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12097 | PyDoc_STRVAR(strip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12098 | "S.strip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12099 | \n\ |
| 12100 | Return a copy of the string S with leading and trailing\n\ |
| 12101 | whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12102 | 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] | 12103 | |
| 12104 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12105 | unicode_strip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12106 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12107 | if (PyTuple_GET_SIZE(args) == 0) |
| 12108 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 12109 | else |
| 12110 | return do_argstrip(self, BOTHSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12111 | } |
| 12112 | |
| 12113 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12114 | PyDoc_STRVAR(lstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12115 | "S.lstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12116 | \n\ |
| 12117 | Return a copy of the string S with leading whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12118 | 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] | 12119 | |
| 12120 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12121 | unicode_lstrip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12122 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12123 | if (PyTuple_GET_SIZE(args) == 0) |
| 12124 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 12125 | else |
| 12126 | return do_argstrip(self, LEFTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12127 | } |
| 12128 | |
| 12129 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12130 | PyDoc_STRVAR(rstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12131 | "S.rstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12132 | \n\ |
| 12133 | Return a copy of the string S with trailing whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12134 | 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] | 12135 | |
| 12136 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12137 | unicode_rstrip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12138 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12139 | if (PyTuple_GET_SIZE(args) == 0) |
| 12140 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 12141 | else |
| 12142 | return do_argstrip(self, RIGHTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12143 | } |
| 12144 | |
| 12145 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12146 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12147 | unicode_repeat(PyObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12148 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12149 | PyObject *u; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12150 | Py_ssize_t nchars, n; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12151 | |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 12152 | if (len < 1) |
| 12153 | _Py_RETURN_UNICODE_EMPTY(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12154 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12155 | /* no repeat, return original string */ |
| 12156 | if (len == 1) |
| 12157 | return unicode_result_unchanged(str); |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 12158 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 12159 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12160 | return NULL; |
| 12161 | |
Victor Stinner | c759f3e | 2011-10-01 03:09:58 +0200 | [diff] [blame] | 12162 | if (PyUnicode_GET_LENGTH(str) > PY_SSIZE_T_MAX / len) { |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12163 | PyErr_SetString(PyExc_OverflowError, |
| 12164 | "repeated string is too long"); |
| 12165 | return NULL; |
| 12166 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12167 | nchars = len * PyUnicode_GET_LENGTH(str); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12168 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12169 | u = PyUnicode_New(nchars, PyUnicode_MAX_CHAR_VALUE(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12170 | if (!u) |
| 12171 | return NULL; |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12172 | assert(PyUnicode_KIND(u) == PyUnicode_KIND(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12173 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12174 | if (PyUnicode_GET_LENGTH(str) == 1) { |
| 12175 | const int kind = PyUnicode_KIND(str); |
| 12176 | const Py_UCS4 fill_char = PyUnicode_READ(kind, PyUnicode_DATA(str), 0); |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 12177 | if (kind == PyUnicode_1BYTE_KIND) { |
| 12178 | void *to = PyUnicode_DATA(u); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12179 | memset(to, (unsigned char)fill_char, len); |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 12180 | } |
| 12181 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 12182 | Py_UCS2 *ucs2 = PyUnicode_2BYTE_DATA(u); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12183 | for (n = 0; n < len; ++n) |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 12184 | ucs2[n] = fill_char; |
| 12185 | } else { |
| 12186 | Py_UCS4 *ucs4 = PyUnicode_4BYTE_DATA(u); |
| 12187 | assert(kind == PyUnicode_4BYTE_KIND); |
| 12188 | for (n = 0; n < len; ++n) |
| 12189 | ucs4[n] = fill_char; |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12190 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12191 | } |
| 12192 | else { |
| 12193 | /* number of characters copied this far */ |
| 12194 | Py_ssize_t done = PyUnicode_GET_LENGTH(str); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 12195 | const Py_ssize_t char_size = PyUnicode_KIND(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12196 | char *to = (char *) PyUnicode_DATA(u); |
| 12197 | Py_MEMCPY(to, PyUnicode_DATA(str), |
| 12198 | PyUnicode_GET_LENGTH(str) * char_size); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12199 | while (done < nchars) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12200 | n = (done <= nchars-done) ? done : nchars-done; |
| 12201 | Py_MEMCPY(to + (done * char_size), to, n * char_size); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12202 | done += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12203 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12204 | } |
| 12205 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12206 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12207 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12208 | } |
| 12209 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12210 | PyObject * |
| 12211 | PyUnicode_Replace(PyObject *obj, |
| 12212 | PyObject *subobj, |
| 12213 | PyObject *replobj, |
| 12214 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12215 | { |
| 12216 | PyObject *self; |
| 12217 | PyObject *str1; |
| 12218 | PyObject *str2; |
| 12219 | PyObject *result; |
| 12220 | |
| 12221 | self = PyUnicode_FromObject(obj); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12222 | if (self == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12223 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12224 | str1 = PyUnicode_FromObject(subobj); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12225 | if (str1 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12226 | Py_DECREF(self); |
| 12227 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12228 | } |
| 12229 | str2 = PyUnicode_FromObject(replobj); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12230 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12231 | Py_DECREF(self); |
| 12232 | Py_DECREF(str1); |
| 12233 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12234 | } |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12235 | if (PyUnicode_READY(self) == -1 || |
| 12236 | PyUnicode_READY(str1) == -1 || |
| 12237 | PyUnicode_READY(str2) == -1) |
| 12238 | result = NULL; |
| 12239 | else |
| 12240 | result = replace(self, str1, str2, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12241 | Py_DECREF(self); |
| 12242 | Py_DECREF(str1); |
| 12243 | Py_DECREF(str2); |
| 12244 | return result; |
| 12245 | } |
| 12246 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12247 | PyDoc_STRVAR(replace__doc__, |
Ezio Melotti | c1897e7 | 2010-06-26 18:50:39 +0000 | [diff] [blame] | 12248 | "S.replace(old, new[, count]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12249 | \n\ |
| 12250 | Return a copy of S with all occurrences of substring\n\ |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 12251 | old replaced by new. If the optional argument count is\n\ |
| 12252 | given, only the first count occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12253 | |
| 12254 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12255 | unicode_replace(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12256 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12257 | PyObject *str1; |
| 12258 | PyObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12259 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12260 | PyObject *result; |
| 12261 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12262 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12263 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12264 | if (PyUnicode_READY(self) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12265 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12266 | str1 = PyUnicode_FromObject(str1); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12267 | if (str1 == NULL) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12268 | return NULL; |
| 12269 | str2 = PyUnicode_FromObject(str2); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12270 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12271 | Py_DECREF(str1); |
| 12272 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 12273 | } |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12274 | if (PyUnicode_READY(str1) == -1 || PyUnicode_READY(str2) == -1) |
| 12275 | result = NULL; |
| 12276 | else |
| 12277 | result = replace(self, str1, str2, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12278 | |
| 12279 | Py_DECREF(str1); |
| 12280 | Py_DECREF(str2); |
| 12281 | return result; |
| 12282 | } |
| 12283 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12284 | static PyObject * |
| 12285 | unicode_repr(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12286 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12287 | PyObject *repr; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12288 | Py_ssize_t isize; |
| 12289 | Py_ssize_t osize, squote, dquote, i, o; |
| 12290 | Py_UCS4 max, quote; |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12291 | int ikind, okind, unchanged; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12292 | void *idata, *odata; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12293 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12294 | if (PyUnicode_READY(unicode) == -1) |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12295 | return NULL; |
| 12296 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12297 | isize = PyUnicode_GET_LENGTH(unicode); |
| 12298 | idata = PyUnicode_DATA(unicode); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12299 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12300 | /* Compute length of output, quote characters, and |
| 12301 | maximum character */ |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12302 | osize = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12303 | max = 127; |
| 12304 | squote = dquote = 0; |
| 12305 | ikind = PyUnicode_KIND(unicode); |
| 12306 | for (i = 0; i < isize; i++) { |
| 12307 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
| 12308 | switch (ch) { |
| 12309 | case '\'': squote++; osize++; break; |
| 12310 | case '"': dquote++; osize++; break; |
| 12311 | case '\\': case '\t': case '\r': case '\n': |
| 12312 | osize += 2; break; |
| 12313 | default: |
| 12314 | /* Fast-path ASCII */ |
| 12315 | if (ch < ' ' || ch == 0x7f) |
| 12316 | osize += 4; /* \xHH */ |
| 12317 | else if (ch < 0x7f) |
| 12318 | osize++; |
| 12319 | else if (Py_UNICODE_ISPRINTABLE(ch)) { |
| 12320 | osize++; |
| 12321 | max = ch > max ? ch : max; |
| 12322 | } |
| 12323 | else if (ch < 0x100) |
| 12324 | osize += 4; /* \xHH */ |
| 12325 | else if (ch < 0x10000) |
| 12326 | osize += 6; /* \uHHHH */ |
| 12327 | else |
| 12328 | osize += 10; /* \uHHHHHHHH */ |
| 12329 | } |
| 12330 | } |
| 12331 | |
| 12332 | quote = '\''; |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12333 | unchanged = (osize == isize); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12334 | if (squote) { |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12335 | unchanged = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12336 | if (dquote) |
| 12337 | /* Both squote and dquote present. Use squote, |
| 12338 | and escape them */ |
| 12339 | osize += squote; |
| 12340 | else |
| 12341 | quote = '"'; |
| 12342 | } |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12343 | osize += 2; /* quotes */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12344 | |
| 12345 | repr = PyUnicode_New(osize, max); |
| 12346 | if (repr == NULL) |
| 12347 | return NULL; |
| 12348 | okind = PyUnicode_KIND(repr); |
| 12349 | odata = PyUnicode_DATA(repr); |
| 12350 | |
| 12351 | PyUnicode_WRITE(okind, odata, 0, quote); |
| 12352 | PyUnicode_WRITE(okind, odata, osize-1, quote); |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12353 | if (unchanged) { |
| 12354 | _PyUnicode_FastCopyCharacters(repr, 1, |
| 12355 | unicode, 0, |
| 12356 | isize); |
| 12357 | } |
| 12358 | else { |
| 12359 | for (i = 0, o = 1; i < isize; i++) { |
| 12360 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12361 | |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12362 | /* Escape quotes and backslashes */ |
| 12363 | if ((ch == quote) || (ch == '\\')) { |
Kristján Valur Jónsson | 55e5dc8 | 2012-06-06 21:58:08 +0000 | [diff] [blame] | 12364 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12365 | PyUnicode_WRITE(okind, odata, o++, ch); |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12366 | continue; |
| 12367 | } |
| 12368 | |
| 12369 | /* Map special whitespace to '\t', \n', '\r' */ |
| 12370 | if (ch == '\t') { |
| 12371 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12372 | PyUnicode_WRITE(okind, odata, o++, 't'); |
| 12373 | } |
| 12374 | else if (ch == '\n') { |
| 12375 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12376 | PyUnicode_WRITE(okind, odata, o++, 'n'); |
| 12377 | } |
| 12378 | else if (ch == '\r') { |
| 12379 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12380 | PyUnicode_WRITE(okind, odata, o++, 'r'); |
| 12381 | } |
| 12382 | |
| 12383 | /* Map non-printable US ASCII to '\xhh' */ |
| 12384 | else if (ch < ' ' || ch == 0x7F) { |
| 12385 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12386 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
| 12387 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0x000F]); |
| 12388 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0x000F]); |
| 12389 | } |
| 12390 | |
| 12391 | /* Copy ASCII characters as-is */ |
| 12392 | else if (ch < 0x7F) { |
| 12393 | PyUnicode_WRITE(okind, odata, o++, ch); |
| 12394 | } |
| 12395 | |
| 12396 | /* Non-ASCII characters */ |
| 12397 | else { |
| 12398 | /* Map Unicode whitespace and control characters |
| 12399 | (categories Z* and C* except ASCII space) |
| 12400 | */ |
| 12401 | if (!Py_UNICODE_ISPRINTABLE(ch)) { |
| 12402 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12403 | /* Map 8-bit characters to '\xhh' */ |
| 12404 | if (ch <= 0xff) { |
| 12405 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
| 12406 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0x000F]); |
| 12407 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0x000F]); |
| 12408 | } |
| 12409 | /* Map 16-bit characters to '\uxxxx' */ |
| 12410 | else if (ch <= 0xffff) { |
| 12411 | PyUnicode_WRITE(okind, odata, o++, 'u'); |
| 12412 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 12) & 0xF]); |
| 12413 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 8) & 0xF]); |
| 12414 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0xF]); |
| 12415 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0xF]); |
| 12416 | } |
| 12417 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 12418 | else { |
| 12419 | PyUnicode_WRITE(okind, odata, o++, 'U'); |
| 12420 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 28) & 0xF]); |
| 12421 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 24) & 0xF]); |
| 12422 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 20) & 0xF]); |
| 12423 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 16) & 0xF]); |
| 12424 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 12) & 0xF]); |
| 12425 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 8) & 0xF]); |
| 12426 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0xF]); |
| 12427 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0xF]); |
| 12428 | } |
| 12429 | } |
| 12430 | /* Copy characters as-is */ |
| 12431 | else { |
| 12432 | PyUnicode_WRITE(okind, odata, o++, ch); |
| 12433 | } |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12434 | } |
| 12435 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12436 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12437 | /* Closing quote already added at the beginning */ |
Victor Stinner | 05d1189 | 2011-10-06 01:13:58 +0200 | [diff] [blame] | 12438 | assert(_PyUnicode_CheckConsistency(repr, 1)); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12439 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12440 | } |
| 12441 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12442 | PyDoc_STRVAR(rfind__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12443 | "S.rfind(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12444 | \n\ |
| 12445 | Return the highest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 12446 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12447 | arguments start and end are interpreted as in slice notation.\n\ |
| 12448 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12449 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12450 | |
| 12451 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12452 | unicode_rfind(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12453 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12454 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 12455 | Py_ssize_t start; |
| 12456 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12457 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12458 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12459 | if (!stringlib_parse_args_finds_unicode("rfind", args, &substring, |
| 12460 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12461 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12462 | |
Christian Heimes | ea71a52 | 2013-06-29 21:17:34 +0200 | [diff] [blame] | 12463 | if (PyUnicode_READY(self) == -1) { |
| 12464 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12465 | return NULL; |
Christian Heimes | ea71a52 | 2013-06-29 21:17:34 +0200 | [diff] [blame] | 12466 | } |
| 12467 | if (PyUnicode_READY(substring) == -1) { |
| 12468 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12469 | return NULL; |
Christian Heimes | ea71a52 | 2013-06-29 21:17:34 +0200 | [diff] [blame] | 12470 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12471 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12472 | result = any_find_slice(-1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12473 | |
| 12474 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12475 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12476 | if (result == -2) |
| 12477 | return NULL; |
| 12478 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12479 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12480 | } |
| 12481 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12482 | PyDoc_STRVAR(rindex__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12483 | "S.rindex(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12484 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12485 | 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] | 12486 | |
| 12487 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12488 | unicode_rindex(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12489 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12490 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 12491 | Py_ssize_t start; |
| 12492 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12493 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12494 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12495 | if (!stringlib_parse_args_finds_unicode("rindex", args, &substring, |
| 12496 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12497 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12498 | |
Christian Heimes | ea71a52 | 2013-06-29 21:17:34 +0200 | [diff] [blame] | 12499 | if (PyUnicode_READY(self) == -1) { |
| 12500 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12501 | return NULL; |
Christian Heimes | ea71a52 | 2013-06-29 21:17:34 +0200 | [diff] [blame] | 12502 | } |
| 12503 | if (PyUnicode_READY(substring) == -1) { |
| 12504 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12505 | return NULL; |
Christian Heimes | ea71a52 | 2013-06-29 21:17:34 +0200 | [diff] [blame] | 12506 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12507 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12508 | result = any_find_slice(-1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12509 | |
| 12510 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12511 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12512 | if (result == -2) |
| 12513 | return NULL; |
| 12514 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12515 | if (result < 0) { |
| 12516 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 12517 | return NULL; |
| 12518 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12519 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12520 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12521 | } |
| 12522 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12523 | PyDoc_STRVAR(rjust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12524 | "S.rjust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12525 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 12526 | 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] | 12527 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12528 | |
| 12529 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12530 | unicode_rjust(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12531 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12532 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12533 | Py_UCS4 fillchar = ' '; |
| 12534 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 12535 | 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] | 12536 | return NULL; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 12537 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 12538 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12539 | return NULL; |
| 12540 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12541 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 12542 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12543 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12544 | return pad(self, width - PyUnicode_GET_LENGTH(self), 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12545 | } |
| 12546 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12547 | PyObject * |
| 12548 | PyUnicode_Split(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12549 | { |
| 12550 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 12551 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12552 | s = PyUnicode_FromObject(s); |
| 12553 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12554 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12555 | if (sep != NULL) { |
| 12556 | sep = PyUnicode_FromObject(sep); |
| 12557 | if (sep == NULL) { |
| 12558 | Py_DECREF(s); |
| 12559 | return NULL; |
| 12560 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12561 | } |
| 12562 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12563 | result = split(s, sep, maxsplit); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12564 | |
| 12565 | Py_DECREF(s); |
| 12566 | Py_XDECREF(sep); |
| 12567 | return result; |
| 12568 | } |
| 12569 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12570 | PyDoc_STRVAR(split__doc__, |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12571 | "S.split(sep=None, maxsplit=-1) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12572 | \n\ |
| 12573 | Return a list of the words in S, using sep as the\n\ |
| 12574 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 12575 | 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] | 12576 | whitespace string is a separator and empty strings are\n\ |
| 12577 | removed from the result."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12578 | |
| 12579 | static PyObject* |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12580 | unicode_split(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12581 | { |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12582 | static char *kwlist[] = {"sep", "maxsplit", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12583 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12584 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12585 | |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12586 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|On:split", |
| 12587 | kwlist, &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12588 | return NULL; |
| 12589 | |
| 12590 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12591 | return split(self, NULL, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12592 | else if (PyUnicode_Check(substring)) |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12593 | return split(self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12594 | else |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12595 | return PyUnicode_Split(self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12596 | } |
| 12597 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12598 | PyObject * |
| 12599 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 12600 | { |
| 12601 | PyObject* str_obj; |
| 12602 | PyObject* sep_obj; |
| 12603 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12604 | int kind1, kind2, kind; |
| 12605 | void *buf1 = NULL, *buf2 = NULL; |
| 12606 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12607 | |
| 12608 | str_obj = PyUnicode_FromObject(str_in); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12609 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12610 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12611 | sep_obj = PyUnicode_FromObject(sep_in); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12612 | if (!sep_obj) { |
| 12613 | Py_DECREF(str_obj); |
| 12614 | return NULL; |
| 12615 | } |
| 12616 | if (PyUnicode_READY(sep_obj) == -1 || PyUnicode_READY(str_obj) == -1) { |
| 12617 | Py_DECREF(sep_obj); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12618 | Py_DECREF(str_obj); |
| 12619 | return NULL; |
| 12620 | } |
| 12621 | |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12622 | kind1 = PyUnicode_KIND(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12623 | kind2 = PyUnicode_KIND(sep_obj); |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12624 | kind = Py_MAX(kind1, kind2); |
| 12625 | buf1 = PyUnicode_DATA(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12626 | if (kind1 != kind) |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12627 | buf1 = _PyUnicode_AsKind(str_obj, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12628 | if (!buf1) |
| 12629 | goto onError; |
| 12630 | buf2 = PyUnicode_DATA(sep_obj); |
| 12631 | if (kind2 != kind) |
| 12632 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 12633 | if (!buf2) |
| 12634 | goto onError; |
| 12635 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 12636 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 12637 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 12638 | switch (PyUnicode_KIND(str_obj)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12639 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 12640 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) |
| 12641 | out = asciilib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12642 | else |
| 12643 | 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] | 12644 | break; |
| 12645 | case PyUnicode_2BYTE_KIND: |
| 12646 | out = ucs2lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12647 | break; |
| 12648 | case PyUnicode_4BYTE_KIND: |
| 12649 | out = ucs4lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12650 | break; |
| 12651 | default: |
| 12652 | assert(0); |
| 12653 | out = 0; |
| 12654 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12655 | |
| 12656 | Py_DECREF(sep_obj); |
| 12657 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12658 | if (kind1 != kind) |
| 12659 | PyMem_Free(buf1); |
| 12660 | if (kind2 != kind) |
| 12661 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12662 | |
| 12663 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12664 | onError: |
| 12665 | Py_DECREF(sep_obj); |
| 12666 | Py_DECREF(str_obj); |
| 12667 | if (kind1 != kind && buf1) |
| 12668 | PyMem_Free(buf1); |
| 12669 | if (kind2 != kind && buf2) |
| 12670 | PyMem_Free(buf2); |
| 12671 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12672 | } |
| 12673 | |
| 12674 | |
| 12675 | PyObject * |
| 12676 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 12677 | { |
| 12678 | PyObject* str_obj; |
| 12679 | PyObject* sep_obj; |
| 12680 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12681 | int kind1, kind2, kind; |
| 12682 | void *buf1 = NULL, *buf2 = NULL; |
| 12683 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12684 | |
| 12685 | str_obj = PyUnicode_FromObject(str_in); |
| 12686 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12687 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12688 | sep_obj = PyUnicode_FromObject(sep_in); |
| 12689 | if (!sep_obj) { |
| 12690 | Py_DECREF(str_obj); |
| 12691 | return NULL; |
| 12692 | } |
| 12693 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12694 | kind1 = PyUnicode_KIND(str_in); |
| 12695 | kind2 = PyUnicode_KIND(sep_obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 12696 | kind = Py_MAX(kind1, kind2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12697 | buf1 = PyUnicode_DATA(str_in); |
| 12698 | if (kind1 != kind) |
| 12699 | buf1 = _PyUnicode_AsKind(str_in, kind); |
| 12700 | if (!buf1) |
| 12701 | goto onError; |
| 12702 | buf2 = PyUnicode_DATA(sep_obj); |
| 12703 | if (kind2 != kind) |
| 12704 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 12705 | if (!buf2) |
| 12706 | goto onError; |
| 12707 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 12708 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 12709 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 12710 | switch (PyUnicode_KIND(str_in)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12711 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 12712 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) |
| 12713 | out = asciilib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12714 | else |
| 12715 | 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] | 12716 | break; |
| 12717 | case PyUnicode_2BYTE_KIND: |
| 12718 | out = ucs2lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12719 | break; |
| 12720 | case PyUnicode_4BYTE_KIND: |
| 12721 | out = ucs4lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12722 | break; |
| 12723 | default: |
| 12724 | assert(0); |
| 12725 | out = 0; |
| 12726 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12727 | |
| 12728 | Py_DECREF(sep_obj); |
| 12729 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12730 | if (kind1 != kind) |
| 12731 | PyMem_Free(buf1); |
| 12732 | if (kind2 != kind) |
| 12733 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12734 | |
| 12735 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12736 | onError: |
| 12737 | Py_DECREF(sep_obj); |
| 12738 | Py_DECREF(str_obj); |
| 12739 | if (kind1 != kind && buf1) |
| 12740 | PyMem_Free(buf1); |
| 12741 | if (kind2 != kind && buf2) |
| 12742 | PyMem_Free(buf2); |
| 12743 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12744 | } |
| 12745 | |
| 12746 | PyDoc_STRVAR(partition__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12747 | "S.partition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12748 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 12749 | 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] | 12750 | 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] | 12751 | found, return S and two empty strings."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12752 | |
| 12753 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12754 | unicode_partition(PyObject *self, PyObject *separator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12755 | { |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12756 | return PyUnicode_Partition(self, separator); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12757 | } |
| 12758 | |
| 12759 | PyDoc_STRVAR(rpartition__doc__, |
Ezio Melotti | 5b2b242 | 2010-01-25 11:58:28 +0000 | [diff] [blame] | 12760 | "S.rpartition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12761 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 12762 | 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] | 12763 | 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] | 12764 | separator is not found, return two empty strings and S."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12765 | |
| 12766 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12767 | unicode_rpartition(PyObject *self, PyObject *separator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12768 | { |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12769 | return PyUnicode_RPartition(self, separator); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12770 | } |
| 12771 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12772 | PyObject * |
| 12773 | PyUnicode_RSplit(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12774 | { |
| 12775 | PyObject *result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12776 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12777 | s = PyUnicode_FromObject(s); |
| 12778 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12779 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12780 | if (sep != NULL) { |
| 12781 | sep = PyUnicode_FromObject(sep); |
| 12782 | if (sep == NULL) { |
| 12783 | Py_DECREF(s); |
| 12784 | return NULL; |
| 12785 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12786 | } |
| 12787 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12788 | result = rsplit(s, sep, maxsplit); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12789 | |
| 12790 | Py_DECREF(s); |
| 12791 | Py_XDECREF(sep); |
| 12792 | return result; |
| 12793 | } |
| 12794 | |
| 12795 | PyDoc_STRVAR(rsplit__doc__, |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12796 | "S.rsplit(sep=None, maxsplit=-1) -> list of strings\n\ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12797 | \n\ |
| 12798 | Return a list of the words in S, using sep as the\n\ |
| 12799 | delimiter string, starting at the end of the string and\n\ |
| 12800 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 12801 | splits are done. If sep is not specified, any whitespace string\n\ |
| 12802 | is a separator."); |
| 12803 | |
| 12804 | static PyObject* |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12805 | unicode_rsplit(PyObject *self, PyObject *args, PyObject *kwds) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12806 | { |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12807 | static char *kwlist[] = {"sep", "maxsplit", 0}; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12808 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12809 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12810 | |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12811 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|On:rsplit", |
| 12812 | kwlist, &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12813 | return NULL; |
| 12814 | |
| 12815 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12816 | return rsplit(self, NULL, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12817 | else if (PyUnicode_Check(substring)) |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12818 | return rsplit(self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12819 | else |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12820 | return PyUnicode_RSplit(self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12821 | } |
| 12822 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12823 | PyDoc_STRVAR(splitlines__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12824 | "S.splitlines([keepends]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12825 | \n\ |
| 12826 | 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] | 12827 | 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] | 12828 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12829 | |
| 12830 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12831 | unicode_splitlines(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12832 | { |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12833 | static char *kwlist[] = {"keepends", 0}; |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 12834 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12835 | |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12836 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:splitlines", |
| 12837 | kwlist, &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12838 | return NULL; |
| 12839 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12840 | return PyUnicode_Splitlines(self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12841 | } |
| 12842 | |
| 12843 | static |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 12844 | PyObject *unicode_str(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12845 | { |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12846 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12847 | } |
| 12848 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12849 | PyDoc_STRVAR(swapcase__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12850 | "S.swapcase() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12851 | \n\ |
| 12852 | 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] | 12853 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12854 | |
| 12855 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12856 | unicode_swapcase(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12857 | { |
Benjamin Peterson | eea4846 | 2012-01-16 14:28:50 -0500 | [diff] [blame] | 12858 | if (PyUnicode_READY(self) == -1) |
| 12859 | return NULL; |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 12860 | return case_operation(self, do_swapcase); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12861 | } |
| 12862 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 12863 | /*[clinic] |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12864 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 12865 | @staticmethod |
| 12866 | str.maketrans as unicode_maketrans |
| 12867 | |
| 12868 | x: object |
| 12869 | |
| 12870 | y: unicode=NULL |
| 12871 | |
| 12872 | z: unicode=NULL |
| 12873 | |
| 12874 | / |
| 12875 | |
| 12876 | Return a translation table usable for str.translate(). |
| 12877 | |
| 12878 | If there is only one argument, it must be a dictionary mapping Unicode |
| 12879 | ordinals (integers) or characters to Unicode ordinals, strings or None. |
| 12880 | Character keys will be then converted to ordinals. |
| 12881 | If there are two arguments, they must be strings of equal length, and |
| 12882 | in the resulting dictionary, each character in x will be mapped to the |
| 12883 | character at the same position in y. If there is a third argument, it |
| 12884 | must be a string, whose characters will be mapped to None in the result. |
| 12885 | [clinic]*/ |
| 12886 | |
| 12887 | PyDoc_STRVAR(unicode_maketrans__doc__, |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 12888 | "maketrans(x, y=None, z=None)\n" |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 12889 | "Return a translation table usable for str.translate().\n" |
| 12890 | "\n" |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 12891 | "If there is only one argument, it must be a dictionary mapping Unicode\n" |
| 12892 | "ordinals (integers) or characters to Unicode ordinals, strings or None.\n" |
| 12893 | "Character keys will be then converted to ordinals.\n" |
| 12894 | "If there are two arguments, they must be strings of equal length, and\n" |
| 12895 | "in the resulting dictionary, each character in x will be mapped to the\n" |
| 12896 | "character at the same position in y. If there is a third argument, it\n" |
| 12897 | "must be a string, whose characters will be mapped to None in the result."); |
| 12898 | |
| 12899 | #define UNICODE_MAKETRANS_METHODDEF \ |
| 12900 | {"maketrans", (PyCFunction)unicode_maketrans, METH_VARARGS|METH_STATIC, unicode_maketrans__doc__}, |
| 12901 | |
| 12902 | static PyObject * |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 12903 | unicode_maketrans_impl(void *null, PyObject *x, PyObject *y, PyObject *z); |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 12904 | |
| 12905 | static PyObject * |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 12906 | unicode_maketrans(void *null, PyObject *args) |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12907 | { |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 12908 | PyObject *return_value = NULL; |
| 12909 | PyObject *x; |
| 12910 | PyObject *y = NULL; |
| 12911 | PyObject *z = NULL; |
| 12912 | |
| 12913 | if (!PyArg_ParseTuple(args, |
| 12914 | "O|UU:maketrans", |
| 12915 | &x, &y, &z)) |
| 12916 | goto exit; |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 12917 | return_value = unicode_maketrans_impl(null, x, y, z); |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 12918 | |
| 12919 | exit: |
| 12920 | return return_value; |
| 12921 | } |
| 12922 | |
| 12923 | static PyObject * |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 12924 | unicode_maketrans_impl(void *null, PyObject *x, PyObject *y, PyObject *z) |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 12925 | /*[clinic checksum: 7f76f414a0dfd0c614e0d4717872eeb520516da7]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 12926 | { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12927 | PyObject *new = NULL, *key, *value; |
| 12928 | Py_ssize_t i = 0; |
| 12929 | int res; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12930 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12931 | new = PyDict_New(); |
| 12932 | if (!new) |
| 12933 | return NULL; |
| 12934 | if (y != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12935 | int x_kind, y_kind, z_kind; |
| 12936 | void *x_data, *y_data, *z_data; |
| 12937 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12938 | /* x must be a string too, of equal length */ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12939 | if (!PyUnicode_Check(x)) { |
| 12940 | PyErr_SetString(PyExc_TypeError, "first maketrans argument must " |
| 12941 | "be a string if there is a second argument"); |
| 12942 | goto err; |
| 12943 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12944 | if (PyUnicode_GET_LENGTH(x) != PyUnicode_GET_LENGTH(y)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12945 | PyErr_SetString(PyExc_ValueError, "the first two maketrans " |
| 12946 | "arguments must have equal length"); |
| 12947 | goto err; |
| 12948 | } |
| 12949 | /* 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] | 12950 | x_kind = PyUnicode_KIND(x); |
| 12951 | y_kind = PyUnicode_KIND(y); |
| 12952 | x_data = PyUnicode_DATA(x); |
| 12953 | y_data = PyUnicode_DATA(y); |
| 12954 | for (i = 0; i < PyUnicode_GET_LENGTH(x); i++) { |
| 12955 | key = PyLong_FromLong(PyUnicode_READ(x_kind, x_data, i)); |
Benjamin Peterson | 53aa1d7 | 2011-12-20 13:29:45 -0600 | [diff] [blame] | 12956 | if (!key) |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12957 | goto err; |
Benjamin Peterson | 822c790 | 2011-12-20 13:32:50 -0600 | [diff] [blame] | 12958 | value = PyLong_FromLong(PyUnicode_READ(y_kind, y_data, i)); |
Benjamin Peterson | 53aa1d7 | 2011-12-20 13:29:45 -0600 | [diff] [blame] | 12959 | if (!value) { |
| 12960 | Py_DECREF(key); |
| 12961 | goto err; |
| 12962 | } |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12963 | res = PyDict_SetItem(new, key, value); |
| 12964 | Py_DECREF(key); |
| 12965 | Py_DECREF(value); |
| 12966 | if (res < 0) |
| 12967 | goto err; |
| 12968 | } |
| 12969 | /* create entries for deleting chars in z */ |
| 12970 | if (z != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12971 | z_kind = PyUnicode_KIND(z); |
| 12972 | z_data = PyUnicode_DATA(z); |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 12973 | for (i = 0; i < PyUnicode_GET_LENGTH(z); i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12974 | key = PyLong_FromLong(PyUnicode_READ(z_kind, z_data, i)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12975 | if (!key) |
| 12976 | goto err; |
| 12977 | res = PyDict_SetItem(new, key, Py_None); |
| 12978 | Py_DECREF(key); |
| 12979 | if (res < 0) |
| 12980 | goto err; |
| 12981 | } |
| 12982 | } |
| 12983 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12984 | int kind; |
| 12985 | void *data; |
| 12986 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12987 | /* x must be a dict */ |
Raymond Hettinger | 3ad0576 | 2009-05-29 22:11:22 +0000 | [diff] [blame] | 12988 | if (!PyDict_CheckExact(x)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12989 | PyErr_SetString(PyExc_TypeError, "if you give only one argument " |
| 12990 | "to maketrans it must be a dict"); |
| 12991 | goto err; |
| 12992 | } |
| 12993 | /* copy entries into the new dict, converting string keys to int keys */ |
| 12994 | while (PyDict_Next(x, &i, &key, &value)) { |
| 12995 | if (PyUnicode_Check(key)) { |
| 12996 | /* convert string keys to integer keys */ |
| 12997 | PyObject *newkey; |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 12998 | if (PyUnicode_GET_LENGTH(key) != 1) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12999 | PyErr_SetString(PyExc_ValueError, "string keys in translate " |
| 13000 | "table must be of length 1"); |
| 13001 | goto err; |
| 13002 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13003 | kind = PyUnicode_KIND(key); |
| 13004 | data = PyUnicode_DATA(key); |
| 13005 | newkey = PyLong_FromLong(PyUnicode_READ(kind, data, 0)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 13006 | if (!newkey) |
| 13007 | goto err; |
| 13008 | res = PyDict_SetItem(new, newkey, value); |
| 13009 | Py_DECREF(newkey); |
| 13010 | if (res < 0) |
| 13011 | goto err; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 13012 | } else if (PyLong_Check(key)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 13013 | /* just keep integer keys */ |
| 13014 | if (PyDict_SetItem(new, key, value) < 0) |
| 13015 | goto err; |
| 13016 | } else { |
| 13017 | PyErr_SetString(PyExc_TypeError, "keys in translate table must " |
| 13018 | "be strings or integers"); |
| 13019 | goto err; |
| 13020 | } |
| 13021 | } |
| 13022 | } |
| 13023 | return new; |
| 13024 | err: |
| 13025 | Py_DECREF(new); |
| 13026 | return NULL; |
| 13027 | } |
| 13028 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13029 | PyDoc_STRVAR(translate__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13030 | "S.translate(table) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13031 | \n\ |
| 13032 | Return a copy of the string S, where all characters have been mapped\n\ |
| 13033 | through the given translation table, which must be a mapping of\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 13034 | Unicode ordinals to Unicode ordinals, strings, or None.\n\ |
Walter Dörwald | 5c1ee17 | 2002-09-04 20:31:32 +0000 | [diff] [blame] | 13035 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 13036 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13037 | |
| 13038 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13039 | unicode_translate(PyObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13040 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13041 | return _PyUnicode_TranslateCharmap(self, table, "ignore"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13042 | } |
| 13043 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13044 | PyDoc_STRVAR(upper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13045 | "S.upper() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13046 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13047 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13048 | |
| 13049 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 13050 | unicode_upper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13051 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 13052 | if (PyUnicode_READY(self) == -1) |
| 13053 | return NULL; |
| 13054 | if (PyUnicode_IS_ASCII(self)) |
| 13055 | return ascii_upper_or_lower(self, 0); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 13056 | return case_operation(self, do_upper); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13057 | } |
| 13058 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13059 | PyDoc_STRVAR(zfill__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13060 | "S.zfill(width) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13061 | \n\ |
Benjamin Peterson | 9aa4299 | 2008-09-10 21:57:34 +0000 | [diff] [blame] | 13062 | Pad a numeric string S with zeros on the left, to fill a field\n\ |
| 13063 | of the specified width. The string S is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13064 | |
| 13065 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 13066 | unicode_zfill(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13067 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13068 | Py_ssize_t fill; |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 13069 | PyObject *u; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13070 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13071 | int kind; |
| 13072 | void *data; |
| 13073 | Py_UCS4 chr; |
| 13074 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13075 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13076 | return NULL; |
| 13077 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 13078 | if (PyUnicode_READY(self) == -1) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 13079 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13080 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 13081 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 13082 | return unicode_result_unchanged(self); |
| 13083 | |
| 13084 | fill = width - PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13085 | |
| 13086 | u = pad(self, fill, 0, '0'); |
| 13087 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 13088 | if (u == NULL) |
| 13089 | return NULL; |
| 13090 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13091 | kind = PyUnicode_KIND(u); |
| 13092 | data = PyUnicode_DATA(u); |
| 13093 | chr = PyUnicode_READ(kind, data, fill); |
| 13094 | |
| 13095 | if (chr == '+' || chr == '-') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13096 | /* move sign to beginning of string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13097 | PyUnicode_WRITE(kind, data, 0, chr); |
| 13098 | PyUnicode_WRITE(kind, data, fill, '0'); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13099 | } |
| 13100 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13101 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 13102 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13103 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13104 | |
| 13105 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 13106 | static PyObject * |
| 13107 | unicode__decimal2ascii(PyObject *self) |
| 13108 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13109 | return PyUnicode_TransformDecimalAndSpaceToASCII(self); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 13110 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13111 | #endif |
| 13112 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13113 | PyDoc_STRVAR(startswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13114 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13115 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 13116 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 13117 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13118 | With optional end, stop comparing S at that position.\n\ |
| 13119 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13120 | |
| 13121 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13122 | unicode_startswith(PyObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13123 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13124 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13125 | PyObject *subobj; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13126 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13127 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 13128 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13129 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13130 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 13131 | if (!stringlib_parse_args_finds("startswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13132 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13133 | if (PyTuple_Check(subobj)) { |
| 13134 | Py_ssize_t i; |
| 13135 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13136 | substring = PyUnicode_FromObject(PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13137 | if (substring == NULL) |
| 13138 | return NULL; |
| 13139 | result = tailmatch(self, substring, start, end, -1); |
| 13140 | Py_DECREF(substring); |
Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame] | 13141 | if (result == -1) |
| 13142 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13143 | if (result) { |
| 13144 | Py_RETURN_TRUE; |
| 13145 | } |
| 13146 | } |
| 13147 | /* nothing matched */ |
| 13148 | Py_RETURN_FALSE; |
| 13149 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13150 | substring = PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 13151 | if (substring == NULL) { |
| 13152 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 13153 | PyErr_Format(PyExc_TypeError, "startswith first arg must be str or " |
| 13154 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13155 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 13156 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13157 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13158 | Py_DECREF(substring); |
Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame] | 13159 | if (result == -1) |
| 13160 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13161 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13162 | } |
| 13163 | |
| 13164 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13165 | PyDoc_STRVAR(endswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13166 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13167 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 13168 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 13169 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13170 | With optional end, stop comparing S at that position.\n\ |
| 13171 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13172 | |
| 13173 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13174 | unicode_endswith(PyObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13175 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13176 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13177 | PyObject *subobj; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13178 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13179 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 13180 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13181 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13182 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 13183 | if (!stringlib_parse_args_finds("endswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13184 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13185 | if (PyTuple_Check(subobj)) { |
| 13186 | Py_ssize_t i; |
| 13187 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13188 | substring = PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13189 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13190 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13191 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13192 | result = tailmatch(self, substring, start, end, +1); |
| 13193 | Py_DECREF(substring); |
Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame] | 13194 | if (result == -1) |
| 13195 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13196 | if (result) { |
| 13197 | Py_RETURN_TRUE; |
| 13198 | } |
| 13199 | } |
| 13200 | Py_RETURN_FALSE; |
| 13201 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13202 | substring = PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 13203 | if (substring == NULL) { |
| 13204 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 13205 | PyErr_Format(PyExc_TypeError, "endswith first arg must be str or " |
| 13206 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13207 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 13208 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13209 | result = tailmatch(self, substring, start, end, +1); |
Christian Heimes | 305e49e | 2013-06-29 20:41:06 +0200 | [diff] [blame] | 13210 | Py_DECREF(substring); |
Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame] | 13211 | if (result == -1) |
| 13212 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13213 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13214 | } |
| 13215 | |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13216 | Py_LOCAL_INLINE(void) |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 13217 | _PyUnicodeWriter_Update(_PyUnicodeWriter *writer) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13218 | { |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13219 | if (!writer->readonly) |
| 13220 | writer->size = PyUnicode_GET_LENGTH(writer->buffer); |
| 13221 | else { |
| 13222 | /* Copy-on-write mode: set buffer size to 0 so |
| 13223 | * _PyUnicodeWriter_Prepare() will copy (and enlarge) the buffer on |
| 13224 | * next write. */ |
| 13225 | writer->size = 0; |
| 13226 | } |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13227 | writer->maxchar = PyUnicode_MAX_CHAR_VALUE(writer->buffer); |
| 13228 | writer->data = PyUnicode_DATA(writer->buffer); |
| 13229 | writer->kind = PyUnicode_KIND(writer->buffer); |
| 13230 | } |
| 13231 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13232 | void |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13233 | _PyUnicodeWriter_Init(_PyUnicodeWriter *writer) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13234 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13235 | memset(writer, 0, sizeof(*writer)); |
| 13236 | #ifdef Py_DEBUG |
| 13237 | writer->kind = 5; /* invalid kind */ |
| 13238 | #endif |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13239 | writer->min_char = 127; |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13240 | } |
| 13241 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13242 | int |
| 13243 | _PyUnicodeWriter_PrepareInternal(_PyUnicodeWriter *writer, |
| 13244 | Py_ssize_t length, Py_UCS4 maxchar) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13245 | { |
Victor Stinner | 6989ba0 | 2013-11-18 21:08:39 +0100 | [diff] [blame] | 13246 | #ifdef MS_WINDOWS |
| 13247 | /* On Windows, overallocate by 50% is the best factor */ |
| 13248 | # define OVERALLOCATE_FACTOR 2 |
| 13249 | #else |
| 13250 | /* On Linux, overallocate by 25% is the best factor */ |
| 13251 | # define OVERALLOCATE_FACTOR 4 |
| 13252 | #endif |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13253 | Py_ssize_t newlen; |
| 13254 | PyObject *newbuffer; |
| 13255 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13256 | assert(length > 0); |
| 13257 | |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13258 | if (length > PY_SSIZE_T_MAX - writer->pos) { |
| 13259 | PyErr_NoMemory(); |
| 13260 | return -1; |
| 13261 | } |
| 13262 | newlen = writer->pos + length; |
| 13263 | |
Benjamin Peterson | 3164f5d | 2013-06-10 09:24:01 -0700 | [diff] [blame] | 13264 | maxchar = Py_MAX(maxchar, writer->min_char); |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13265 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13266 | if (writer->buffer == NULL) { |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13267 | assert(!writer->readonly); |
Victor Stinner | 6989ba0 | 2013-11-18 21:08:39 +0100 | [diff] [blame] | 13268 | if (writer->overallocate |
| 13269 | && newlen <= (PY_SSIZE_T_MAX - newlen / OVERALLOCATE_FACTOR)) { |
| 13270 | /* overallocate to limit the number of realloc() */ |
| 13271 | newlen += newlen / OVERALLOCATE_FACTOR; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13272 | } |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13273 | if (newlen < writer->min_length) |
| 13274 | newlen = writer->min_length; |
| 13275 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13276 | writer->buffer = PyUnicode_New(newlen, maxchar); |
| 13277 | if (writer->buffer == NULL) |
| 13278 | return -1; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13279 | } |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13280 | else if (newlen > writer->size) { |
Victor Stinner | 6989ba0 | 2013-11-18 21:08:39 +0100 | [diff] [blame] | 13281 | if (writer->overallocate |
| 13282 | && newlen <= (PY_SSIZE_T_MAX - newlen / OVERALLOCATE_FACTOR)) { |
| 13283 | /* overallocate to limit the number of realloc() */ |
| 13284 | newlen += newlen / OVERALLOCATE_FACTOR; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13285 | } |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13286 | if (newlen < writer->min_length) |
| 13287 | newlen = writer->min_length; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13288 | |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 13289 | if (maxchar > writer->maxchar || writer->readonly) { |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13290 | /* resize + widen */ |
| 13291 | newbuffer = PyUnicode_New(newlen, maxchar); |
| 13292 | if (newbuffer == NULL) |
| 13293 | return -1; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13294 | _PyUnicode_FastCopyCharacters(newbuffer, 0, |
| 13295 | writer->buffer, 0, writer->pos); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13296 | Py_DECREF(writer->buffer); |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 13297 | writer->readonly = 0; |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13298 | } |
| 13299 | else { |
| 13300 | newbuffer = resize_compact(writer->buffer, newlen); |
| 13301 | if (newbuffer == NULL) |
| 13302 | return -1; |
| 13303 | } |
| 13304 | writer->buffer = newbuffer; |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13305 | } |
| 13306 | else if (maxchar > writer->maxchar) { |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 13307 | assert(!writer->readonly); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13308 | newbuffer = PyUnicode_New(writer->size, maxchar); |
| 13309 | if (newbuffer == NULL) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13310 | return -1; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13311 | _PyUnicode_FastCopyCharacters(newbuffer, 0, |
| 13312 | writer->buffer, 0, writer->pos); |
| 13313 | Py_DECREF(writer->buffer); |
| 13314 | writer->buffer = newbuffer; |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13315 | } |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13316 | _PyUnicodeWriter_Update(writer); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13317 | return 0; |
Victor Stinner | 6989ba0 | 2013-11-18 21:08:39 +0100 | [diff] [blame] | 13318 | |
| 13319 | #undef OVERALLOCATE_FACTOR |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13320 | } |
| 13321 | |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 13322 | Py_LOCAL_INLINE(int) |
| 13323 | _PyUnicodeWriter_WriteCharInline(_PyUnicodeWriter *writer, Py_UCS4 ch) |
Victor Stinner | a0dd021 | 2013-04-11 22:09:04 +0200 | [diff] [blame] | 13324 | { |
| 13325 | if (_PyUnicodeWriter_Prepare(writer, 1, ch) < 0) |
| 13326 | return -1; |
| 13327 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, ch); |
| 13328 | writer->pos++; |
| 13329 | return 0; |
| 13330 | } |
| 13331 | |
| 13332 | int |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 13333 | _PyUnicodeWriter_WriteChar(_PyUnicodeWriter *writer, Py_UCS4 ch) |
| 13334 | { |
| 13335 | return _PyUnicodeWriter_WriteCharInline(writer, ch); |
| 13336 | } |
| 13337 | |
| 13338 | int |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13339 | _PyUnicodeWriter_WriteStr(_PyUnicodeWriter *writer, PyObject *str) |
| 13340 | { |
| 13341 | Py_UCS4 maxchar; |
| 13342 | Py_ssize_t len; |
| 13343 | |
| 13344 | if (PyUnicode_READY(str) == -1) |
| 13345 | return -1; |
| 13346 | len = PyUnicode_GET_LENGTH(str); |
| 13347 | if (len == 0) |
| 13348 | return 0; |
| 13349 | maxchar = PyUnicode_MAX_CHAR_VALUE(str); |
| 13350 | if (maxchar > writer->maxchar || len > writer->size - writer->pos) { |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 13351 | if (writer->buffer == NULL && !writer->overallocate) { |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13352 | writer->readonly = 1; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13353 | Py_INCREF(str); |
| 13354 | writer->buffer = str; |
| 13355 | _PyUnicodeWriter_Update(writer); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13356 | writer->pos += len; |
| 13357 | return 0; |
| 13358 | } |
| 13359 | if (_PyUnicodeWriter_PrepareInternal(writer, len, maxchar) == -1) |
| 13360 | return -1; |
| 13361 | } |
| 13362 | _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos, |
| 13363 | str, 0, len); |
| 13364 | writer->pos += len; |
| 13365 | return 0; |
| 13366 | } |
| 13367 | |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 13368 | int |
Victor Stinner | cfc4c13 | 2013-04-03 01:48:39 +0200 | [diff] [blame] | 13369 | _PyUnicodeWriter_WriteSubstring(_PyUnicodeWriter *writer, PyObject *str, |
| 13370 | Py_ssize_t start, Py_ssize_t end) |
| 13371 | { |
| 13372 | Py_UCS4 maxchar; |
| 13373 | Py_ssize_t len; |
| 13374 | |
| 13375 | if (PyUnicode_READY(str) == -1) |
| 13376 | return -1; |
| 13377 | |
| 13378 | assert(0 <= start); |
| 13379 | assert(end <= PyUnicode_GET_LENGTH(str)); |
| 13380 | assert(start <= end); |
| 13381 | |
| 13382 | if (end == 0) |
| 13383 | return 0; |
| 13384 | |
| 13385 | if (start == 0 && end == PyUnicode_GET_LENGTH(str)) |
| 13386 | return _PyUnicodeWriter_WriteStr(writer, str); |
| 13387 | |
| 13388 | if (PyUnicode_MAX_CHAR_VALUE(str) > writer->maxchar) |
| 13389 | maxchar = _PyUnicode_FindMaxChar(str, start, end); |
| 13390 | else |
| 13391 | maxchar = writer->maxchar; |
| 13392 | len = end - start; |
| 13393 | |
| 13394 | if (_PyUnicodeWriter_Prepare(writer, len, maxchar) < 0) |
| 13395 | return -1; |
| 13396 | |
| 13397 | _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos, |
| 13398 | str, start, len); |
| 13399 | writer->pos += len; |
| 13400 | return 0; |
| 13401 | } |
| 13402 | |
| 13403 | int |
Victor Stinner | 4a58707 | 2013-11-19 12:54:53 +0100 | [diff] [blame] | 13404 | _PyUnicodeWriter_WriteASCIIString(_PyUnicodeWriter *writer, |
| 13405 | const char *ascii, Py_ssize_t len) |
| 13406 | { |
| 13407 | if (len == -1) |
| 13408 | len = strlen(ascii); |
| 13409 | |
| 13410 | assert(ucs1lib_find_max_char((Py_UCS1*)ascii, (Py_UCS1*)ascii + len) < 128); |
| 13411 | |
| 13412 | if (writer->buffer == NULL && !writer->overallocate) { |
| 13413 | PyObject *str; |
| 13414 | |
| 13415 | str = _PyUnicode_FromASCII(ascii, len); |
| 13416 | if (str == NULL) |
| 13417 | return -1; |
| 13418 | |
| 13419 | writer->readonly = 1; |
| 13420 | writer->buffer = str; |
| 13421 | _PyUnicodeWriter_Update(writer); |
| 13422 | writer->pos += len; |
| 13423 | return 0; |
| 13424 | } |
| 13425 | |
| 13426 | if (_PyUnicodeWriter_Prepare(writer, len, 127) == -1) |
| 13427 | return -1; |
| 13428 | |
| 13429 | switch (writer->kind) |
| 13430 | { |
| 13431 | case PyUnicode_1BYTE_KIND: |
| 13432 | { |
| 13433 | const Py_UCS1 *str = (const Py_UCS1 *)ascii; |
| 13434 | Py_UCS1 *data = writer->data; |
| 13435 | |
| 13436 | Py_MEMCPY(data + writer->pos, str, len); |
| 13437 | break; |
| 13438 | } |
| 13439 | case PyUnicode_2BYTE_KIND: |
| 13440 | { |
| 13441 | _PyUnicode_CONVERT_BYTES( |
| 13442 | Py_UCS1, Py_UCS2, |
| 13443 | ascii, ascii + len, |
| 13444 | (Py_UCS2 *)writer->data + writer->pos); |
| 13445 | break; |
| 13446 | } |
| 13447 | case PyUnicode_4BYTE_KIND: |
| 13448 | { |
| 13449 | _PyUnicode_CONVERT_BYTES( |
| 13450 | Py_UCS1, Py_UCS4, |
| 13451 | ascii, ascii + len, |
| 13452 | (Py_UCS4 *)writer->data + writer->pos); |
| 13453 | break; |
| 13454 | } |
| 13455 | default: |
| 13456 | assert(0); |
| 13457 | } |
| 13458 | |
| 13459 | writer->pos += len; |
| 13460 | return 0; |
| 13461 | } |
| 13462 | |
| 13463 | int |
| 13464 | _PyUnicodeWriter_WriteLatin1String(_PyUnicodeWriter *writer, |
| 13465 | const char *str, Py_ssize_t len) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 13466 | { |
| 13467 | Py_UCS4 maxchar; |
| 13468 | |
| 13469 | maxchar = ucs1lib_find_max_char((Py_UCS1*)str, (Py_UCS1*)str + len); |
| 13470 | if (_PyUnicodeWriter_Prepare(writer, len, maxchar) == -1) |
| 13471 | return -1; |
| 13472 | unicode_write_cstr(writer->buffer, writer->pos, str, len); |
| 13473 | writer->pos += len; |
| 13474 | return 0; |
| 13475 | } |
| 13476 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13477 | PyObject * |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 13478 | _PyUnicodeWriter_Finish(_PyUnicodeWriter *writer) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13479 | { |
Victor Stinner | 15a0bd3 | 2013-07-08 22:29:55 +0200 | [diff] [blame] | 13480 | PyObject *str; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13481 | if (writer->pos == 0) { |
Victor Stinner | 9e6b4d7 | 2013-07-09 00:37:24 +0200 | [diff] [blame] | 13482 | Py_CLEAR(writer->buffer); |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 13483 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13484 | } |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 13485 | if (writer->readonly) { |
Victor Stinner | 9e6b4d7 | 2013-07-09 00:37:24 +0200 | [diff] [blame] | 13486 | str = writer->buffer; |
| 13487 | writer->buffer = NULL; |
| 13488 | assert(PyUnicode_GET_LENGTH(str) == writer->pos); |
| 13489 | return str; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13490 | } |
| 13491 | if (PyUnicode_GET_LENGTH(writer->buffer) != writer->pos) { |
| 13492 | PyObject *newbuffer; |
| 13493 | newbuffer = resize_compact(writer->buffer, writer->pos); |
| 13494 | if (newbuffer == NULL) { |
| 13495 | Py_DECREF(writer->buffer); |
Victor Stinner | 9e6b4d7 | 2013-07-09 00:37:24 +0200 | [diff] [blame] | 13496 | writer->buffer = NULL; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13497 | return NULL; |
| 13498 | } |
| 13499 | writer->buffer = newbuffer; |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13500 | } |
Victor Stinner | 15a0bd3 | 2013-07-08 22:29:55 +0200 | [diff] [blame] | 13501 | str = writer->buffer; |
| 13502 | writer->buffer = NULL; |
| 13503 | assert(_PyUnicode_CheckConsistency(str, 1)); |
| 13504 | return unicode_result_ready(str); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13505 | } |
| 13506 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13507 | void |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 13508 | _PyUnicodeWriter_Dealloc(_PyUnicodeWriter *writer) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13509 | { |
| 13510 | Py_CLEAR(writer->buffer); |
| 13511 | } |
| 13512 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13513 | #include "stringlib/unicode_format.h" |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13514 | |
| 13515 | PyDoc_STRVAR(format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13516 | "S.format(*args, **kwargs) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13517 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 13518 | Return a formatted version of S, using substitutions from args and kwargs.\n\ |
| 13519 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13520 | |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 13521 | PyDoc_STRVAR(format_map__doc__, |
| 13522 | "S.format_map(mapping) -> str\n\ |
| 13523 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 13524 | Return a formatted version of S, using substitutions from mapping.\n\ |
| 13525 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 13526 | |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13527 | static PyObject * |
| 13528 | unicode__format__(PyObject* self, PyObject* args) |
| 13529 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13530 | PyObject *format_spec; |
| 13531 | _PyUnicodeWriter writer; |
| 13532 | int ret; |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13533 | |
| 13534 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
| 13535 | return NULL; |
| 13536 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13537 | if (PyUnicode_READY(self) == -1) |
| 13538 | return NULL; |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13539 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13540 | ret = _PyUnicode_FormatAdvancedWriter(&writer, |
| 13541 | self, format_spec, 0, |
| 13542 | PyUnicode_GET_LENGTH(format_spec)); |
| 13543 | if (ret == -1) { |
| 13544 | _PyUnicodeWriter_Dealloc(&writer); |
| 13545 | return NULL; |
| 13546 | } |
| 13547 | return _PyUnicodeWriter_Finish(&writer); |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13548 | } |
| 13549 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13550 | PyDoc_STRVAR(p_format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13551 | "S.__format__(format_spec) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13552 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 13553 | Return a formatted version of S as described by format_spec."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13554 | |
| 13555 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13556 | unicode__sizeof__(PyObject *v) |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13557 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13558 | Py_ssize_t size; |
| 13559 | |
| 13560 | /* If it's a compact object, account for base structure + |
| 13561 | character data. */ |
| 13562 | if (PyUnicode_IS_COMPACT_ASCII(v)) |
| 13563 | size = sizeof(PyASCIIObject) + PyUnicode_GET_LENGTH(v) + 1; |
| 13564 | else if (PyUnicode_IS_COMPACT(v)) |
| 13565 | size = sizeof(PyCompactUnicodeObject) + |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 13566 | (PyUnicode_GET_LENGTH(v) + 1) * PyUnicode_KIND(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13567 | else { |
| 13568 | /* If it is a two-block object, account for base object, and |
| 13569 | for character block if present. */ |
| 13570 | size = sizeof(PyUnicodeObject); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 13571 | if (_PyUnicode_DATA_ANY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13572 | size += (PyUnicode_GET_LENGTH(v) + 1) * |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 13573 | PyUnicode_KIND(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13574 | } |
| 13575 | /* 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] | 13576 | with the data pointer. Check if the data is not shared. */ |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 13577 | if (_PyUnicode_HAS_WSTR_MEMORY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13578 | size += (PyUnicode_WSTR_LENGTH(v) + 1) * sizeof(wchar_t); |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 13579 | if (_PyUnicode_HAS_UTF8_MEMORY(v)) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 13580 | size += PyUnicode_UTF8_LENGTH(v) + 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13581 | |
| 13582 | return PyLong_FromSsize_t(size); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13583 | } |
| 13584 | |
| 13585 | PyDoc_STRVAR(sizeof__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13586 | "S.__sizeof__() -> size of S in memory, in bytes"); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13587 | |
| 13588 | static PyObject * |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 13589 | unicode_getnewargs(PyObject *v) |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 13590 | { |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 13591 | PyObject *copy = _PyUnicode_Copy(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13592 | if (!copy) |
| 13593 | return NULL; |
| 13594 | return Py_BuildValue("(N)", copy); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 13595 | } |
| 13596 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13597 | static PyMethodDef unicode_methods[] = { |
Benjamin Peterson | 28a4dce | 2010-12-12 01:33:04 +0000 | [diff] [blame] | 13598 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS | METH_KEYWORDS, encode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13599 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 13600 | {"split", (PyCFunction) unicode_split, METH_VARARGS | METH_KEYWORDS, split__doc__}, |
| 13601 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS | METH_KEYWORDS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13602 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 13603 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 13604 | {"casefold", (PyCFunction) unicode_casefold, METH_NOARGS, casefold__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13605 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 13606 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 13607 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
Ezio Melotti | 745d54d | 2013-11-16 19:10:57 +0200 | [diff] [blame] | 13608 | {"expandtabs", (PyCFunction) unicode_expandtabs, |
| 13609 | METH_VARARGS | METH_KEYWORDS, expandtabs__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13610 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13611 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13612 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 13613 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 13614 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 13615 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13616 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 13617 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 13618 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 13619 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13620 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Ezio Melotti | 745d54d | 2013-11-16 19:10:57 +0200 | [diff] [blame] | 13621 | {"splitlines", (PyCFunction) unicode_splitlines, |
| 13622 | METH_VARARGS | METH_KEYWORDS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 13623 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13624 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 13625 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 13626 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 13627 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 13628 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 13629 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 13630 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 13631 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 13632 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 13633 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 13634 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 13635 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 13636 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 13637 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 13638 | {"isidentifier", (PyCFunction) unicode_isidentifier, METH_NOARGS, isidentifier__doc__}, |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 13639 | {"isprintable", (PyCFunction) unicode_isprintable, METH_NOARGS, isprintable__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13640 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Eric Smith | 9cd1e09 | 2007-08-31 18:39:38 +0000 | [diff] [blame] | 13641 | {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 13642 | {"format_map", (PyCFunction) do_string_format_map, METH_O, format_map__doc__}, |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13643 | {"__format__", (PyCFunction) unicode__format__, METH_VARARGS, p_format__doc__}, |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 13644 | UNICODE_MAKETRANS_METHODDEF |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13645 | {"__sizeof__", (PyCFunction) unicode__sizeof__, METH_NOARGS, sizeof__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 13646 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 13647 | /* These methods are just used for debugging the implementation. */ |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 13648 | {"_decimal2ascii", (PyCFunction) unicode__decimal2ascii, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13649 | #endif |
| 13650 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13651 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13652 | {NULL, NULL} |
| 13653 | }; |
| 13654 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13655 | static PyObject * |
| 13656 | unicode_mod(PyObject *v, PyObject *w) |
| 13657 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 13658 | if (!PyUnicode_Check(v)) |
| 13659 | Py_RETURN_NOTIMPLEMENTED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13660 | return PyUnicode_Format(v, w); |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13661 | } |
| 13662 | |
| 13663 | static PyNumberMethods unicode_as_number = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13664 | 0, /*nb_add*/ |
| 13665 | 0, /*nb_subtract*/ |
| 13666 | 0, /*nb_multiply*/ |
| 13667 | unicode_mod, /*nb_remainder*/ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13668 | }; |
| 13669 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13670 | static PySequenceMethods unicode_as_sequence = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13671 | (lenfunc) unicode_length, /* sq_length */ |
| 13672 | PyUnicode_Concat, /* sq_concat */ |
| 13673 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 13674 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
| 13675 | 0, /* sq_slice */ |
| 13676 | 0, /* sq_ass_item */ |
| 13677 | 0, /* sq_ass_slice */ |
| 13678 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13679 | }; |
| 13680 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13681 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13682 | unicode_subscript(PyObject* self, PyObject* item) |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13683 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13684 | if (PyUnicode_READY(self) == -1) |
| 13685 | return NULL; |
| 13686 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 13687 | if (PyIndex_Check(item)) { |
| 13688 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13689 | if (i == -1 && PyErr_Occurred()) |
| 13690 | return NULL; |
| 13691 | if (i < 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13692 | i += PyUnicode_GET_LENGTH(self); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13693 | return unicode_getitem(self, i); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13694 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13695 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13696 | PyObject *result; |
| 13697 | void *src_data, *dest_data; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13698 | int src_kind, dest_kind; |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 13699 | Py_UCS4 ch, max_char, kind_limit; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13700 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13701 | if (PySlice_GetIndicesEx(item, PyUnicode_GET_LENGTH(self), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13702 | &start, &stop, &step, &slicelength) < 0) { |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13703 | return NULL; |
| 13704 | } |
| 13705 | |
| 13706 | if (slicelength <= 0) { |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 13707 | _Py_RETURN_UNICODE_EMPTY(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13708 | } else if (start == 0 && step == 1 && |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 13709 | slicelength == PyUnicode_GET_LENGTH(self)) { |
| 13710 | return unicode_result_unchanged(self); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 13711 | } else if (step == 1) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13712 | return PyUnicode_Substring(self, |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 13713 | start, start + slicelength); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13714 | } |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13715 | /* General case */ |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13716 | src_kind = PyUnicode_KIND(self); |
| 13717 | src_data = PyUnicode_DATA(self); |
Victor Stinner | 55c9911 | 2011-10-13 01:17:06 +0200 | [diff] [blame] | 13718 | if (!PyUnicode_IS_ASCII(self)) { |
| 13719 | kind_limit = kind_maxchar_limit(src_kind); |
| 13720 | max_char = 0; |
| 13721 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 13722 | ch = PyUnicode_READ(src_kind, src_data, cur); |
| 13723 | if (ch > max_char) { |
| 13724 | max_char = ch; |
| 13725 | if (max_char >= kind_limit) |
| 13726 | break; |
| 13727 | } |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 13728 | } |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13729 | } |
Victor Stinner | 55c9911 | 2011-10-13 01:17:06 +0200 | [diff] [blame] | 13730 | else |
| 13731 | max_char = 127; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13732 | result = PyUnicode_New(slicelength, max_char); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13733 | if (result == NULL) |
| 13734 | return NULL; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13735 | dest_kind = PyUnicode_KIND(result); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13736 | dest_data = PyUnicode_DATA(result); |
| 13737 | |
| 13738 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13739 | Py_UCS4 ch = PyUnicode_READ(src_kind, src_data, cur); |
| 13740 | PyUnicode_WRITE(dest_kind, dest_data, i, ch); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13741 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13742 | assert(_PyUnicode_CheckConsistency(result, 1)); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13743 | return result; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13744 | } else { |
| 13745 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 13746 | return NULL; |
| 13747 | } |
| 13748 | } |
| 13749 | |
| 13750 | static PyMappingMethods unicode_as_mapping = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13751 | (lenfunc)unicode_length, /* mp_length */ |
| 13752 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 13753 | (objobjargproc)0, /* mp_ass_subscript */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13754 | }; |
| 13755 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13756 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13757 | /* Helpers for PyUnicode_Format() */ |
| 13758 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13759 | struct unicode_formatter_t { |
| 13760 | PyObject *args; |
| 13761 | int args_owned; |
| 13762 | Py_ssize_t arglen, argidx; |
| 13763 | PyObject *dict; |
| 13764 | |
| 13765 | enum PyUnicode_Kind fmtkind; |
| 13766 | Py_ssize_t fmtcnt, fmtpos; |
| 13767 | void *fmtdata; |
| 13768 | PyObject *fmtstr; |
| 13769 | |
| 13770 | _PyUnicodeWriter writer; |
| 13771 | }; |
| 13772 | |
| 13773 | struct unicode_format_arg_t { |
| 13774 | Py_UCS4 ch; |
| 13775 | int flags; |
| 13776 | Py_ssize_t width; |
| 13777 | int prec; |
| 13778 | int sign; |
| 13779 | }; |
| 13780 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13781 | static PyObject * |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13782 | unicode_format_getnextarg(struct unicode_formatter_t *ctx) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13783 | { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13784 | Py_ssize_t argidx = ctx->argidx; |
| 13785 | |
| 13786 | if (argidx < ctx->arglen) { |
| 13787 | ctx->argidx++; |
| 13788 | if (ctx->arglen < 0) |
| 13789 | return ctx->args; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13790 | else |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13791 | return PyTuple_GetItem(ctx->args, argidx); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13792 | } |
| 13793 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13794 | "not enough arguments for format string"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13795 | return NULL; |
| 13796 | } |
| 13797 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13798 | /* 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] | 13799 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13800 | /* Format a float into the writer if the writer is not NULL, or into *p_output |
| 13801 | otherwise. |
| 13802 | |
| 13803 | Return 0 on success, raise an exception and return -1 on error. */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13804 | static int |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13805 | formatfloat(PyObject *v, struct unicode_format_arg_t *arg, |
| 13806 | PyObject **p_output, |
| 13807 | _PyUnicodeWriter *writer) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13808 | { |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13809 | char *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13810 | double x; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13811 | Py_ssize_t len; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13812 | int prec; |
| 13813 | int dtoa_flags; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 13814 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13815 | x = PyFloat_AsDouble(v); |
| 13816 | if (x == -1.0 && PyErr_Occurred()) |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13817 | return -1; |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13818 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13819 | prec = arg->prec; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13820 | if (prec < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13821 | prec = 6; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13822 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13823 | if (arg->flags & F_ALT) |
| 13824 | dtoa_flags = Py_DTSF_ALT; |
| 13825 | else |
| 13826 | dtoa_flags = 0; |
| 13827 | p = PyOS_double_to_string(x, arg->ch, prec, dtoa_flags, NULL); |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13828 | if (p == NULL) |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13829 | return -1; |
| 13830 | len = strlen(p); |
| 13831 | if (writer) { |
Victor Stinner | 4a58707 | 2013-11-19 12:54:53 +0100 | [diff] [blame] | 13832 | if (_PyUnicodeWriter_WriteASCIIString(writer, p, len) < 0) { |
Christian Heimes | f4f9939 | 2012-09-10 11:48:41 +0200 | [diff] [blame] | 13833 | PyMem_Free(p); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13834 | return -1; |
Christian Heimes | f4f9939 | 2012-09-10 11:48:41 +0200 | [diff] [blame] | 13835 | } |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13836 | } |
| 13837 | else |
| 13838 | *p_output = _PyUnicode_FromASCII(p, len); |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13839 | PyMem_Free(p); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13840 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13841 | } |
| 13842 | |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13843 | /* formatlong() emulates the format codes d, u, o, x and X, and |
| 13844 | * the F_ALT flag, for Python's long (unbounded) ints. It's not used for |
| 13845 | * Python's regular ints. |
| 13846 | * Return value: a new PyUnicodeObject*, or NULL if error. |
| 13847 | * The output string is of the form |
| 13848 | * "-"? ("0x" | "0X")? digit+ |
| 13849 | * "0x"/"0X" are present only for x and X conversions, with F_ALT |
| 13850 | * set in flags. The case of hex digits will be correct, |
| 13851 | * There will be at least prec digits, zero-filled on the left if |
| 13852 | * necessary to get that many. |
| 13853 | * val object to be converted |
| 13854 | * flags bitmask of format flags; only F_ALT is looked at |
| 13855 | * prec minimum number of digits; 0-fill on left if needed |
| 13856 | * type a character in [duoxX]; u acts the same as d |
| 13857 | * |
| 13858 | * CAUTION: o, x and X conversions on regular ints can never |
| 13859 | * produce a '-' sign, but can for Python's unbounded ints. |
| 13860 | */ |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13861 | static PyObject* |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13862 | formatlong(PyObject *val, struct unicode_format_arg_t *arg) |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13863 | { |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13864 | PyObject *result = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13865 | char *buf; |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13866 | Py_ssize_t i; |
| 13867 | int sign; /* 1 if '-', else 0 */ |
| 13868 | int len; /* number of characters */ |
| 13869 | Py_ssize_t llen; |
| 13870 | int numdigits; /* len == numnondigits + numdigits */ |
| 13871 | int numnondigits = 0; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13872 | int prec = arg->prec; |
| 13873 | int type = arg->ch; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13874 | |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13875 | /* Avoid exceeding SSIZE_T_MAX */ |
| 13876 | if (prec > INT_MAX-3) { |
| 13877 | PyErr_SetString(PyExc_OverflowError, |
| 13878 | "precision too large"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13879 | return NULL; |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13880 | } |
| 13881 | |
| 13882 | assert(PyLong_Check(val)); |
| 13883 | |
| 13884 | switch (type) { |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13885 | default: |
| 13886 | assert(!"'type' not in [diuoxX]"); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13887 | case 'd': |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13888 | case 'i': |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13889 | case 'u': |
Ethan Furman | fb13721 | 2013-08-31 10:18:55 -0700 | [diff] [blame] | 13890 | /* int and int subclasses should print numerically when a numeric */ |
| 13891 | /* format code is used (see issue18780) */ |
| 13892 | result = PyNumber_ToBase(val, 10); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13893 | break; |
| 13894 | case 'o': |
| 13895 | numnondigits = 2; |
| 13896 | result = PyNumber_ToBase(val, 8); |
| 13897 | break; |
| 13898 | case 'x': |
| 13899 | case 'X': |
| 13900 | numnondigits = 2; |
| 13901 | result = PyNumber_ToBase(val, 16); |
| 13902 | break; |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13903 | } |
| 13904 | if (!result) |
| 13905 | return NULL; |
| 13906 | |
| 13907 | assert(unicode_modifiable(result)); |
| 13908 | assert(PyUnicode_IS_READY(result)); |
| 13909 | assert(PyUnicode_IS_ASCII(result)); |
| 13910 | |
| 13911 | /* To modify the string in-place, there can only be one reference. */ |
| 13912 | if (Py_REFCNT(result) != 1) { |
Christian Heimes | d47802e | 2013-06-29 21:33:36 +0200 | [diff] [blame] | 13913 | Py_DECREF(result); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13914 | PyErr_BadInternalCall(); |
| 13915 | return NULL; |
| 13916 | } |
| 13917 | buf = PyUnicode_DATA(result); |
| 13918 | llen = PyUnicode_GET_LENGTH(result); |
| 13919 | if (llen > INT_MAX) { |
Christian Heimes | d47802e | 2013-06-29 21:33:36 +0200 | [diff] [blame] | 13920 | Py_DECREF(result); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13921 | PyErr_SetString(PyExc_ValueError, |
| 13922 | "string too large in _PyBytes_FormatLong"); |
| 13923 | return NULL; |
| 13924 | } |
| 13925 | len = (int)llen; |
| 13926 | sign = buf[0] == '-'; |
| 13927 | numnondigits += sign; |
| 13928 | numdigits = len - numnondigits; |
| 13929 | assert(numdigits > 0); |
| 13930 | |
| 13931 | /* Get rid of base marker unless F_ALT */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13932 | if (((arg->flags & F_ALT) == 0 && |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13933 | (type == 'o' || type == 'x' || type == 'X'))) { |
| 13934 | assert(buf[sign] == '0'); |
| 13935 | assert(buf[sign+1] == 'x' || buf[sign+1] == 'X' || |
| 13936 | buf[sign+1] == 'o'); |
| 13937 | numnondigits -= 2; |
| 13938 | buf += 2; |
| 13939 | len -= 2; |
| 13940 | if (sign) |
| 13941 | buf[0] = '-'; |
| 13942 | assert(len == numnondigits + numdigits); |
| 13943 | assert(numdigits > 0); |
| 13944 | } |
| 13945 | |
| 13946 | /* Fill with leading zeroes to meet minimum width. */ |
| 13947 | if (prec > numdigits) { |
| 13948 | PyObject *r1 = PyBytes_FromStringAndSize(NULL, |
| 13949 | numnondigits + prec); |
| 13950 | char *b1; |
| 13951 | if (!r1) { |
| 13952 | Py_DECREF(result); |
| 13953 | return NULL; |
| 13954 | } |
| 13955 | b1 = PyBytes_AS_STRING(r1); |
| 13956 | for (i = 0; i < numnondigits; ++i) |
| 13957 | *b1++ = *buf++; |
| 13958 | for (i = 0; i < prec - numdigits; i++) |
| 13959 | *b1++ = '0'; |
| 13960 | for (i = 0; i < numdigits; i++) |
| 13961 | *b1++ = *buf++; |
| 13962 | *b1 = '\0'; |
| 13963 | Py_DECREF(result); |
| 13964 | result = r1; |
| 13965 | buf = PyBytes_AS_STRING(result); |
| 13966 | len = numnondigits + prec; |
| 13967 | } |
| 13968 | |
| 13969 | /* Fix up case for hex conversions. */ |
| 13970 | if (type == 'X') { |
| 13971 | /* Need to convert all lower case letters to upper case. |
| 13972 | and need to convert 0x to 0X (and -0x to -0X). */ |
| 13973 | for (i = 0; i < len; i++) |
| 13974 | if (buf[i] >= 'a' && buf[i] <= 'x') |
| 13975 | buf[i] -= 'a'-'A'; |
| 13976 | } |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13977 | if (!PyUnicode_Check(result) |
| 13978 | || buf != PyUnicode_DATA(result)) { |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13979 | PyObject *unicode; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13980 | unicode = _PyUnicode_FromASCII(buf, len); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13981 | Py_DECREF(result); |
| 13982 | result = unicode; |
| 13983 | } |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13984 | else if (len != PyUnicode_GET_LENGTH(result)) { |
| 13985 | if (PyUnicode_Resize(&result, len) < 0) |
| 13986 | Py_CLEAR(result); |
| 13987 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13988 | return result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13989 | } |
| 13990 | |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13991 | /* Format an integer. |
| 13992 | * Return 1 if the number has been formatted into the writer, |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13993 | * 0 if the number has been formatted into *p_output |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13994 | * -1 and raise an exception on error */ |
| 13995 | static int |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13996 | mainformatlong(PyObject *v, |
| 13997 | struct unicode_format_arg_t *arg, |
| 13998 | PyObject **p_output, |
| 13999 | _PyUnicodeWriter *writer) |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14000 | { |
| 14001 | PyObject *iobj, *res; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14002 | char type = (char)arg->ch; |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14003 | |
| 14004 | if (!PyNumber_Check(v)) |
| 14005 | goto wrongtype; |
| 14006 | |
| 14007 | if (!PyLong_Check(v)) { |
| 14008 | iobj = PyNumber_Long(v); |
| 14009 | if (iobj == NULL) { |
| 14010 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 14011 | goto wrongtype; |
| 14012 | return -1; |
| 14013 | } |
| 14014 | assert(PyLong_Check(iobj)); |
| 14015 | } |
| 14016 | else { |
| 14017 | iobj = v; |
| 14018 | Py_INCREF(iobj); |
| 14019 | } |
| 14020 | |
| 14021 | if (PyLong_CheckExact(v) |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14022 | && arg->width == -1 && arg->prec == -1 |
| 14023 | && !(arg->flags & (F_SIGN | F_BLANK)) |
| 14024 | && type != 'X') |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14025 | { |
| 14026 | /* Fast path */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14027 | int alternate = arg->flags & F_ALT; |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14028 | int base; |
| 14029 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14030 | switch(type) |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14031 | { |
| 14032 | default: |
| 14033 | assert(0 && "'type' not in [diuoxX]"); |
| 14034 | case 'd': |
| 14035 | case 'i': |
| 14036 | case 'u': |
| 14037 | base = 10; |
| 14038 | break; |
| 14039 | case 'o': |
| 14040 | base = 8; |
| 14041 | break; |
| 14042 | case 'x': |
| 14043 | case 'X': |
| 14044 | base = 16; |
| 14045 | break; |
| 14046 | } |
| 14047 | |
Victor Stinner | c89d28f | 2012-10-02 12:54:07 +0200 | [diff] [blame] | 14048 | if (_PyLong_FormatWriter(writer, v, base, alternate) == -1) { |
| 14049 | Py_DECREF(iobj); |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14050 | return -1; |
Victor Stinner | c89d28f | 2012-10-02 12:54:07 +0200 | [diff] [blame] | 14051 | } |
| 14052 | Py_DECREF(iobj); |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14053 | return 1; |
| 14054 | } |
| 14055 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14056 | res = formatlong(iobj, arg); |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14057 | Py_DECREF(iobj); |
| 14058 | if (res == NULL) |
| 14059 | return -1; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14060 | *p_output = res; |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14061 | return 0; |
| 14062 | |
| 14063 | wrongtype: |
| 14064 | PyErr_Format(PyExc_TypeError, |
| 14065 | "%%%c format: a number is required, " |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14066 | "not %.200s", |
| 14067 | type, Py_TYPE(v)->tp_name); |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14068 | return -1; |
| 14069 | } |
| 14070 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14071 | static Py_UCS4 |
| 14072 | formatchar(PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14073 | { |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 14074 | /* presume that the buffer is at least 3 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 14075 | if (PyUnicode_Check(v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14076 | if (PyUnicode_GET_LENGTH(v) == 1) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14077 | return PyUnicode_READ_CHAR(v, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14078 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14079 | goto onError; |
| 14080 | } |
| 14081 | else { |
| 14082 | /* Integer input truncated to a character */ |
| 14083 | long x; |
| 14084 | x = PyLong_AsLong(v); |
| 14085 | if (x == -1 && PyErr_Occurred()) |
| 14086 | goto onError; |
| 14087 | |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 14088 | if (x < 0 || x > MAX_UNICODE) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14089 | PyErr_SetString(PyExc_OverflowError, |
| 14090 | "%c arg not in range(0x110000)"); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14091 | return (Py_UCS4) -1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14092 | } |
| 14093 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14094 | return (Py_UCS4) x; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14095 | } |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 14096 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14097 | onError: |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 14098 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14099 | "%c requires int or char"); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14100 | return (Py_UCS4) -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14101 | } |
| 14102 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14103 | /* Parse options of an argument: flags, width, precision. |
| 14104 | Handle also "%(name)" syntax. |
| 14105 | |
| 14106 | Return 0 if the argument has been formatted into arg->str. |
| 14107 | Return 1 if the argument has been written into ctx->writer, |
| 14108 | Raise an exception and return -1 on error. */ |
| 14109 | static int |
| 14110 | unicode_format_arg_parse(struct unicode_formatter_t *ctx, |
| 14111 | struct unicode_format_arg_t *arg) |
| 14112 | { |
| 14113 | #define FORMAT_READ(ctx) \ |
| 14114 | PyUnicode_READ((ctx)->fmtkind, (ctx)->fmtdata, (ctx)->fmtpos) |
| 14115 | |
| 14116 | PyObject *v; |
| 14117 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14118 | if (arg->ch == '(') { |
| 14119 | /* Get argument value from a dictionary. Example: "%(name)s". */ |
| 14120 | Py_ssize_t keystart; |
| 14121 | Py_ssize_t keylen; |
| 14122 | PyObject *key; |
| 14123 | int pcount = 1; |
| 14124 | |
| 14125 | if (ctx->dict == NULL) { |
| 14126 | PyErr_SetString(PyExc_TypeError, |
| 14127 | "format requires a mapping"); |
| 14128 | return -1; |
| 14129 | } |
| 14130 | ++ctx->fmtpos; |
| 14131 | --ctx->fmtcnt; |
| 14132 | keystart = ctx->fmtpos; |
| 14133 | /* Skip over balanced parentheses */ |
| 14134 | while (pcount > 0 && --ctx->fmtcnt >= 0) { |
| 14135 | arg->ch = FORMAT_READ(ctx); |
| 14136 | if (arg->ch == ')') |
| 14137 | --pcount; |
| 14138 | else if (arg->ch == '(') |
| 14139 | ++pcount; |
| 14140 | ctx->fmtpos++; |
| 14141 | } |
| 14142 | keylen = ctx->fmtpos - keystart - 1; |
| 14143 | if (ctx->fmtcnt < 0 || pcount > 0) { |
| 14144 | PyErr_SetString(PyExc_ValueError, |
| 14145 | "incomplete format key"); |
| 14146 | return -1; |
| 14147 | } |
| 14148 | key = PyUnicode_Substring(ctx->fmtstr, |
| 14149 | keystart, keystart + keylen); |
| 14150 | if (key == NULL) |
| 14151 | return -1; |
| 14152 | if (ctx->args_owned) { |
| 14153 | Py_DECREF(ctx->args); |
| 14154 | ctx->args_owned = 0; |
| 14155 | } |
| 14156 | ctx->args = PyObject_GetItem(ctx->dict, key); |
| 14157 | Py_DECREF(key); |
| 14158 | if (ctx->args == NULL) |
| 14159 | return -1; |
| 14160 | ctx->args_owned = 1; |
| 14161 | ctx->arglen = -1; |
| 14162 | ctx->argidx = -2; |
| 14163 | } |
| 14164 | |
| 14165 | /* Parse flags. Example: "%+i" => flags=F_SIGN. */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14166 | while (--ctx->fmtcnt >= 0) { |
| 14167 | arg->ch = FORMAT_READ(ctx); |
| 14168 | ctx->fmtpos++; |
| 14169 | switch (arg->ch) { |
| 14170 | case '-': arg->flags |= F_LJUST; continue; |
| 14171 | case '+': arg->flags |= F_SIGN; continue; |
| 14172 | case ' ': arg->flags |= F_BLANK; continue; |
| 14173 | case '#': arg->flags |= F_ALT; continue; |
| 14174 | case '0': arg->flags |= F_ZERO; continue; |
| 14175 | } |
| 14176 | break; |
| 14177 | } |
| 14178 | |
| 14179 | /* Parse width. Example: "%10s" => width=10 */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14180 | if (arg->ch == '*') { |
| 14181 | v = unicode_format_getnextarg(ctx); |
| 14182 | if (v == NULL) |
| 14183 | return -1; |
| 14184 | if (!PyLong_Check(v)) { |
| 14185 | PyErr_SetString(PyExc_TypeError, |
| 14186 | "* wants int"); |
| 14187 | return -1; |
| 14188 | } |
Serhiy Storchaka | 7898043 | 2013-01-15 01:12:17 +0200 | [diff] [blame] | 14189 | arg->width = PyLong_AsSsize_t(v); |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14190 | if (arg->width == -1 && PyErr_Occurred()) |
| 14191 | return -1; |
| 14192 | if (arg->width < 0) { |
| 14193 | arg->flags |= F_LJUST; |
| 14194 | arg->width = -arg->width; |
| 14195 | } |
| 14196 | if (--ctx->fmtcnt >= 0) { |
| 14197 | arg->ch = FORMAT_READ(ctx); |
| 14198 | ctx->fmtpos++; |
| 14199 | } |
| 14200 | } |
| 14201 | else if (arg->ch >= '0' && arg->ch <= '9') { |
| 14202 | arg->width = arg->ch - '0'; |
| 14203 | while (--ctx->fmtcnt >= 0) { |
| 14204 | arg->ch = FORMAT_READ(ctx); |
| 14205 | ctx->fmtpos++; |
| 14206 | if (arg->ch < '0' || arg->ch > '9') |
| 14207 | break; |
| 14208 | /* Since arg->ch is unsigned, the RHS would end up as unsigned, |
| 14209 | mixing signed and unsigned comparison. Since arg->ch is between |
| 14210 | '0' and '9', casting to int is safe. */ |
| 14211 | if (arg->width > (PY_SSIZE_T_MAX - ((int)arg->ch - '0')) / 10) { |
| 14212 | PyErr_SetString(PyExc_ValueError, |
| 14213 | "width too big"); |
| 14214 | return -1; |
| 14215 | } |
| 14216 | arg->width = arg->width*10 + (arg->ch - '0'); |
| 14217 | } |
| 14218 | } |
| 14219 | |
| 14220 | /* Parse precision. Example: "%.3f" => prec=3 */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14221 | if (arg->ch == '.') { |
| 14222 | arg->prec = 0; |
| 14223 | if (--ctx->fmtcnt >= 0) { |
| 14224 | arg->ch = FORMAT_READ(ctx); |
| 14225 | ctx->fmtpos++; |
| 14226 | } |
| 14227 | if (arg->ch == '*') { |
| 14228 | v = unicode_format_getnextarg(ctx); |
| 14229 | if (v == NULL) |
| 14230 | return -1; |
| 14231 | if (!PyLong_Check(v)) { |
| 14232 | PyErr_SetString(PyExc_TypeError, |
| 14233 | "* wants int"); |
| 14234 | return -1; |
| 14235 | } |
Serhiy Storchaka | 7898043 | 2013-01-15 01:12:17 +0200 | [diff] [blame] | 14236 | arg->prec = _PyLong_AsInt(v); |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14237 | if (arg->prec == -1 && PyErr_Occurred()) |
| 14238 | return -1; |
| 14239 | if (arg->prec < 0) |
| 14240 | arg->prec = 0; |
| 14241 | if (--ctx->fmtcnt >= 0) { |
| 14242 | arg->ch = FORMAT_READ(ctx); |
| 14243 | ctx->fmtpos++; |
| 14244 | } |
| 14245 | } |
| 14246 | else if (arg->ch >= '0' && arg->ch <= '9') { |
| 14247 | arg->prec = arg->ch - '0'; |
| 14248 | while (--ctx->fmtcnt >= 0) { |
| 14249 | arg->ch = FORMAT_READ(ctx); |
| 14250 | ctx->fmtpos++; |
| 14251 | if (arg->ch < '0' || arg->ch > '9') |
| 14252 | break; |
| 14253 | if (arg->prec > (INT_MAX - ((int)arg->ch - '0')) / 10) { |
| 14254 | PyErr_SetString(PyExc_ValueError, |
Victor Stinner | 3921e90 | 2012-10-06 23:05:00 +0200 | [diff] [blame] | 14255 | "precision too big"); |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14256 | return -1; |
| 14257 | } |
| 14258 | arg->prec = arg->prec*10 + (arg->ch - '0'); |
| 14259 | } |
| 14260 | } |
| 14261 | } |
| 14262 | |
| 14263 | /* Ignore "h", "l" and "L" format prefix (ex: "%hi" or "%ls") */ |
| 14264 | if (ctx->fmtcnt >= 0) { |
| 14265 | if (arg->ch == 'h' || arg->ch == 'l' || arg->ch == 'L') { |
| 14266 | if (--ctx->fmtcnt >= 0) { |
| 14267 | arg->ch = FORMAT_READ(ctx); |
| 14268 | ctx->fmtpos++; |
| 14269 | } |
| 14270 | } |
| 14271 | } |
| 14272 | if (ctx->fmtcnt < 0) { |
| 14273 | PyErr_SetString(PyExc_ValueError, |
| 14274 | "incomplete format"); |
| 14275 | return -1; |
| 14276 | } |
| 14277 | return 0; |
| 14278 | |
| 14279 | #undef FORMAT_READ |
| 14280 | } |
| 14281 | |
| 14282 | /* Format one argument. Supported conversion specifiers: |
| 14283 | |
| 14284 | - "s", "r", "a": any type |
| 14285 | - "i", "d", "u", "o", "x", "X": int |
| 14286 | - "e", "E", "f", "F", "g", "G": float |
| 14287 | - "c": int or str (1 character) |
| 14288 | |
Victor Stinner | 8dbd421 | 2012-12-04 09:30:24 +0100 | [diff] [blame] | 14289 | When possible, the output is written directly into the Unicode writer |
| 14290 | (ctx->writer). A string is created when padding is required. |
| 14291 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14292 | Return 0 if the argument has been formatted into *p_str, |
| 14293 | 1 if the argument has been written into ctx->writer, |
Victor Stinner | 8dbd421 | 2012-12-04 09:30:24 +0100 | [diff] [blame] | 14294 | -1 on error. */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14295 | static int |
| 14296 | unicode_format_arg_format(struct unicode_formatter_t *ctx, |
| 14297 | struct unicode_format_arg_t *arg, |
| 14298 | PyObject **p_str) |
| 14299 | { |
| 14300 | PyObject *v; |
| 14301 | _PyUnicodeWriter *writer = &ctx->writer; |
| 14302 | |
| 14303 | if (ctx->fmtcnt == 0) |
| 14304 | ctx->writer.overallocate = 0; |
| 14305 | |
| 14306 | if (arg->ch == '%') { |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 14307 | if (_PyUnicodeWriter_WriteCharInline(writer, '%') < 0) |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14308 | return -1; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14309 | return 1; |
| 14310 | } |
| 14311 | |
| 14312 | v = unicode_format_getnextarg(ctx); |
| 14313 | if (v == NULL) |
| 14314 | return -1; |
| 14315 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14316 | |
| 14317 | switch (arg->ch) { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14318 | case 's': |
| 14319 | case 'r': |
| 14320 | case 'a': |
| 14321 | if (PyLong_CheckExact(v) && arg->width == -1 && arg->prec == -1) { |
| 14322 | /* Fast path */ |
| 14323 | if (_PyLong_FormatWriter(writer, v, 10, arg->flags & F_ALT) == -1) |
| 14324 | return -1; |
| 14325 | return 1; |
| 14326 | } |
| 14327 | |
| 14328 | if (PyUnicode_CheckExact(v) && arg->ch == 's') { |
| 14329 | *p_str = v; |
| 14330 | Py_INCREF(*p_str); |
| 14331 | } |
| 14332 | else { |
| 14333 | if (arg->ch == 's') |
| 14334 | *p_str = PyObject_Str(v); |
| 14335 | else if (arg->ch == 'r') |
| 14336 | *p_str = PyObject_Repr(v); |
| 14337 | else |
| 14338 | *p_str = PyObject_ASCII(v); |
| 14339 | } |
| 14340 | break; |
| 14341 | |
| 14342 | case 'i': |
| 14343 | case 'd': |
| 14344 | case 'u': |
| 14345 | case 'o': |
| 14346 | case 'x': |
| 14347 | case 'X': |
| 14348 | { |
| 14349 | int ret = mainformatlong(v, arg, p_str, writer); |
| 14350 | if (ret != 0) |
| 14351 | return ret; |
| 14352 | arg->sign = 1; |
| 14353 | break; |
| 14354 | } |
| 14355 | |
| 14356 | case 'e': |
| 14357 | case 'E': |
| 14358 | case 'f': |
| 14359 | case 'F': |
| 14360 | case 'g': |
| 14361 | case 'G': |
| 14362 | if (arg->width == -1 && arg->prec == -1 |
| 14363 | && !(arg->flags & (F_SIGN | F_BLANK))) |
| 14364 | { |
| 14365 | /* Fast path */ |
| 14366 | if (formatfloat(v, arg, NULL, writer) == -1) |
| 14367 | return -1; |
| 14368 | return 1; |
| 14369 | } |
| 14370 | |
| 14371 | arg->sign = 1; |
| 14372 | if (formatfloat(v, arg, p_str, NULL) == -1) |
| 14373 | return -1; |
| 14374 | break; |
| 14375 | |
| 14376 | case 'c': |
| 14377 | { |
| 14378 | Py_UCS4 ch = formatchar(v); |
| 14379 | if (ch == (Py_UCS4) -1) |
| 14380 | return -1; |
| 14381 | if (arg->width == -1 && arg->prec == -1) { |
| 14382 | /* Fast path */ |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 14383 | if (_PyUnicodeWriter_WriteCharInline(writer, ch) < 0) |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14384 | return -1; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14385 | return 1; |
| 14386 | } |
| 14387 | *p_str = PyUnicode_FromOrdinal(ch); |
| 14388 | break; |
| 14389 | } |
| 14390 | |
| 14391 | default: |
| 14392 | PyErr_Format(PyExc_ValueError, |
| 14393 | "unsupported format character '%c' (0x%x) " |
| 14394 | "at index %zd", |
| 14395 | (31<=arg->ch && arg->ch<=126) ? (char)arg->ch : '?', |
| 14396 | (int)arg->ch, |
| 14397 | ctx->fmtpos - 1); |
| 14398 | return -1; |
| 14399 | } |
| 14400 | if (*p_str == NULL) |
| 14401 | return -1; |
| 14402 | assert (PyUnicode_Check(*p_str)); |
| 14403 | return 0; |
| 14404 | } |
| 14405 | |
| 14406 | static int |
| 14407 | unicode_format_arg_output(struct unicode_formatter_t *ctx, |
| 14408 | struct unicode_format_arg_t *arg, |
| 14409 | PyObject *str) |
| 14410 | { |
| 14411 | Py_ssize_t len; |
| 14412 | enum PyUnicode_Kind kind; |
| 14413 | void *pbuf; |
| 14414 | Py_ssize_t pindex; |
| 14415 | Py_UCS4 signchar; |
| 14416 | Py_ssize_t buflen; |
Victor Stinner | eb4b5ac | 2013-04-03 02:02:33 +0200 | [diff] [blame] | 14417 | Py_UCS4 maxchar; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14418 | Py_ssize_t sublen; |
| 14419 | _PyUnicodeWriter *writer = &ctx->writer; |
| 14420 | Py_UCS4 fill; |
| 14421 | |
| 14422 | fill = ' '; |
| 14423 | if (arg->sign && arg->flags & F_ZERO) |
| 14424 | fill = '0'; |
| 14425 | |
| 14426 | if (PyUnicode_READY(str) == -1) |
| 14427 | return -1; |
| 14428 | |
| 14429 | len = PyUnicode_GET_LENGTH(str); |
| 14430 | if ((arg->width == -1 || arg->width <= len) |
| 14431 | && (arg->prec == -1 || arg->prec >= len) |
| 14432 | && !(arg->flags & (F_SIGN | F_BLANK))) |
| 14433 | { |
| 14434 | /* Fast path */ |
| 14435 | if (_PyUnicodeWriter_WriteStr(writer, str) == -1) |
| 14436 | return -1; |
| 14437 | return 0; |
| 14438 | } |
| 14439 | |
| 14440 | /* Truncate the string for "s", "r" and "a" formats |
| 14441 | if the precision is set */ |
| 14442 | if (arg->ch == 's' || arg->ch == 'r' || arg->ch == 'a') { |
| 14443 | if (arg->prec >= 0 && len > arg->prec) |
| 14444 | len = arg->prec; |
| 14445 | } |
| 14446 | |
| 14447 | /* Adjust sign and width */ |
| 14448 | kind = PyUnicode_KIND(str); |
| 14449 | pbuf = PyUnicode_DATA(str); |
| 14450 | pindex = 0; |
| 14451 | signchar = '\0'; |
| 14452 | if (arg->sign) { |
| 14453 | Py_UCS4 ch = PyUnicode_READ(kind, pbuf, pindex); |
| 14454 | if (ch == '-' || ch == '+') { |
| 14455 | signchar = ch; |
| 14456 | len--; |
| 14457 | pindex++; |
| 14458 | } |
| 14459 | else if (arg->flags & F_SIGN) |
| 14460 | signchar = '+'; |
| 14461 | else if (arg->flags & F_BLANK) |
| 14462 | signchar = ' '; |
| 14463 | else |
| 14464 | arg->sign = 0; |
| 14465 | } |
| 14466 | if (arg->width < len) |
| 14467 | arg->width = len; |
| 14468 | |
| 14469 | /* Prepare the writer */ |
Victor Stinner | eb4b5ac | 2013-04-03 02:02:33 +0200 | [diff] [blame] | 14470 | maxchar = writer->maxchar; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14471 | if (!(arg->flags & F_LJUST)) { |
| 14472 | if (arg->sign) { |
| 14473 | if ((arg->width-1) > len) |
Benjamin Peterson | 3164f5d | 2013-06-10 09:24:01 -0700 | [diff] [blame] | 14474 | maxchar = Py_MAX(maxchar, fill); |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14475 | } |
| 14476 | else { |
| 14477 | if (arg->width > len) |
Benjamin Peterson | 3164f5d | 2013-06-10 09:24:01 -0700 | [diff] [blame] | 14478 | maxchar = Py_MAX(maxchar, fill); |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14479 | } |
| 14480 | } |
Victor Stinner | eb4b5ac | 2013-04-03 02:02:33 +0200 | [diff] [blame] | 14481 | if (PyUnicode_MAX_CHAR_VALUE(str) > maxchar) { |
| 14482 | Py_UCS4 strmaxchar = _PyUnicode_FindMaxChar(str, 0, pindex+len); |
Benjamin Peterson | 3164f5d | 2013-06-10 09:24:01 -0700 | [diff] [blame] | 14483 | maxchar = Py_MAX(maxchar, strmaxchar); |
Victor Stinner | eb4b5ac | 2013-04-03 02:02:33 +0200 | [diff] [blame] | 14484 | } |
| 14485 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14486 | buflen = arg->width; |
| 14487 | if (arg->sign && len == arg->width) |
| 14488 | buflen++; |
Victor Stinner | eb4b5ac | 2013-04-03 02:02:33 +0200 | [diff] [blame] | 14489 | if (_PyUnicodeWriter_Prepare(writer, buflen, maxchar) == -1) |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14490 | return -1; |
| 14491 | |
| 14492 | /* Write the sign if needed */ |
| 14493 | if (arg->sign) { |
| 14494 | if (fill != ' ') { |
| 14495 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, signchar); |
| 14496 | writer->pos += 1; |
| 14497 | } |
| 14498 | if (arg->width > len) |
| 14499 | arg->width--; |
| 14500 | } |
| 14501 | |
| 14502 | /* Write the numeric prefix for "x", "X" and "o" formats |
| 14503 | if the alternate form is used. |
| 14504 | For example, write "0x" for the "%#x" format. */ |
| 14505 | if ((arg->flags & F_ALT) && (arg->ch == 'x' || arg->ch == 'X' || arg->ch == 'o')) { |
| 14506 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
| 14507 | assert(PyUnicode_READ(kind, pbuf, pindex + 1) == arg->ch); |
| 14508 | if (fill != ' ') { |
| 14509 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, '0'); |
| 14510 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos+1, arg->ch); |
| 14511 | writer->pos += 2; |
| 14512 | pindex += 2; |
| 14513 | } |
| 14514 | arg->width -= 2; |
| 14515 | if (arg->width < 0) |
| 14516 | arg->width = 0; |
| 14517 | len -= 2; |
| 14518 | } |
| 14519 | |
| 14520 | /* Pad left with the fill character if needed */ |
| 14521 | if (arg->width > len && !(arg->flags & F_LJUST)) { |
| 14522 | sublen = arg->width - len; |
| 14523 | FILL(writer->kind, writer->data, fill, writer->pos, sublen); |
| 14524 | writer->pos += sublen; |
| 14525 | arg->width = len; |
| 14526 | } |
| 14527 | |
| 14528 | /* If padding with spaces: write sign if needed and/or numeric prefix if |
| 14529 | the alternate form is used */ |
| 14530 | if (fill == ' ') { |
| 14531 | if (arg->sign) { |
| 14532 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, signchar); |
| 14533 | writer->pos += 1; |
| 14534 | } |
| 14535 | if ((arg->flags & F_ALT) && (arg->ch == 'x' || arg->ch == 'X' || arg->ch == 'o')) { |
| 14536 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
| 14537 | assert(PyUnicode_READ(kind, pbuf, pindex+1) == arg->ch); |
| 14538 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, '0'); |
| 14539 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos+1, arg->ch); |
| 14540 | writer->pos += 2; |
| 14541 | pindex += 2; |
| 14542 | } |
| 14543 | } |
| 14544 | |
| 14545 | /* Write characters */ |
| 14546 | if (len) { |
| 14547 | _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos, |
| 14548 | str, pindex, len); |
| 14549 | writer->pos += len; |
| 14550 | } |
| 14551 | |
| 14552 | /* Pad right with the fill character if needed */ |
| 14553 | if (arg->width > len) { |
| 14554 | sublen = arg->width - len; |
| 14555 | FILL(writer->kind, writer->data, ' ', writer->pos, sublen); |
| 14556 | writer->pos += sublen; |
| 14557 | } |
| 14558 | return 0; |
| 14559 | } |
| 14560 | |
| 14561 | /* Helper of PyUnicode_Format(): format one arg. |
| 14562 | Return 0 on success, raise an exception and return -1 on error. */ |
| 14563 | static int |
| 14564 | unicode_format_arg(struct unicode_formatter_t *ctx) |
| 14565 | { |
| 14566 | struct unicode_format_arg_t arg; |
| 14567 | PyObject *str; |
| 14568 | int ret; |
| 14569 | |
Victor Stinner | 8dbd421 | 2012-12-04 09:30:24 +0100 | [diff] [blame] | 14570 | arg.ch = PyUnicode_READ(ctx->fmtkind, ctx->fmtdata, ctx->fmtpos); |
| 14571 | arg.flags = 0; |
| 14572 | arg.width = -1; |
| 14573 | arg.prec = -1; |
| 14574 | arg.sign = 0; |
| 14575 | str = NULL; |
| 14576 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14577 | ret = unicode_format_arg_parse(ctx, &arg); |
| 14578 | if (ret == -1) |
| 14579 | return -1; |
| 14580 | |
| 14581 | ret = unicode_format_arg_format(ctx, &arg, &str); |
| 14582 | if (ret == -1) |
| 14583 | return -1; |
| 14584 | |
| 14585 | if (ret != 1) { |
| 14586 | ret = unicode_format_arg_output(ctx, &arg, str); |
| 14587 | Py_DECREF(str); |
| 14588 | if (ret == -1) |
| 14589 | return -1; |
| 14590 | } |
| 14591 | |
| 14592 | if (ctx->dict && (ctx->argidx < ctx->arglen) && arg.ch != '%') { |
| 14593 | PyErr_SetString(PyExc_TypeError, |
| 14594 | "not all arguments converted during string formatting"); |
| 14595 | return -1; |
| 14596 | } |
| 14597 | return 0; |
| 14598 | } |
| 14599 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 14600 | PyObject * |
| 14601 | PyUnicode_Format(PyObject *format, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14602 | { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14603 | struct unicode_formatter_t ctx; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 14604 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14605 | if (format == NULL || args == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14606 | PyErr_BadInternalCall(); |
| 14607 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14608 | } |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14609 | |
| 14610 | ctx.fmtstr = PyUnicode_FromObject(format); |
| 14611 | if (ctx.fmtstr == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14612 | return NULL; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14613 | if (PyUnicode_READY(ctx.fmtstr) == -1) { |
| 14614 | Py_DECREF(ctx.fmtstr); |
| 14615 | return NULL; |
| 14616 | } |
| 14617 | ctx.fmtdata = PyUnicode_DATA(ctx.fmtstr); |
| 14618 | ctx.fmtkind = PyUnicode_KIND(ctx.fmtstr); |
| 14619 | ctx.fmtcnt = PyUnicode_GET_LENGTH(ctx.fmtstr); |
| 14620 | ctx.fmtpos = 0; |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 14621 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 14622 | _PyUnicodeWriter_Init(&ctx.writer); |
| 14623 | ctx.writer.min_length = ctx.fmtcnt + 100; |
| 14624 | ctx.writer.overallocate = 1; |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 14625 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14626 | if (PyTuple_Check(args)) { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14627 | ctx.arglen = PyTuple_Size(args); |
| 14628 | ctx.argidx = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14629 | } |
| 14630 | else { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14631 | ctx.arglen = -1; |
| 14632 | ctx.argidx = -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14633 | } |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14634 | ctx.args_owned = 0; |
Benjamin Peterson | 28a6cfa | 2012-08-28 17:55:35 -0400 | [diff] [blame] | 14635 | if (PyMapping_Check(args) && !PyTuple_Check(args) && !PyUnicode_Check(args)) |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14636 | ctx.dict = args; |
| 14637 | else |
| 14638 | ctx.dict = NULL; |
| 14639 | ctx.args = args; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14640 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14641 | while (--ctx.fmtcnt >= 0) { |
| 14642 | if (PyUnicode_READ(ctx.fmtkind, ctx.fmtdata, ctx.fmtpos) != '%') { |
Victor Stinner | cfc4c13 | 2013-04-03 01:48:39 +0200 | [diff] [blame] | 14643 | Py_ssize_t nonfmtpos; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14644 | |
| 14645 | nonfmtpos = ctx.fmtpos++; |
| 14646 | while (ctx.fmtcnt >= 0 && |
| 14647 | PyUnicode_READ(ctx.fmtkind, ctx.fmtdata, ctx.fmtpos) != '%') { |
| 14648 | ctx.fmtpos++; |
| 14649 | ctx.fmtcnt--; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14650 | } |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14651 | if (ctx.fmtcnt < 0) { |
| 14652 | ctx.fmtpos--; |
| 14653 | ctx.writer.overallocate = 0; |
Victor Stinner | a049443 | 2012-10-03 23:03:46 +0200 | [diff] [blame] | 14654 | } |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 14655 | |
Victor Stinner | cfc4c13 | 2013-04-03 01:48:39 +0200 | [diff] [blame] | 14656 | if (_PyUnicodeWriter_WriteSubstring(&ctx.writer, ctx.fmtstr, |
| 14657 | nonfmtpos, ctx.fmtpos) < 0) |
| 14658 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14659 | } |
| 14660 | else { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14661 | ctx.fmtpos++; |
| 14662 | if (unicode_format_arg(&ctx) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14663 | goto onError; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14664 | } |
| 14665 | } |
Victor Stinner | aff3cc6 | 2012-04-30 05:19:21 +0200 | [diff] [blame] | 14666 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14667 | if (ctx.argidx < ctx.arglen && !ctx.dict) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14668 | PyErr_SetString(PyExc_TypeError, |
| 14669 | "not all arguments converted during string formatting"); |
| 14670 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14671 | } |
| 14672 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14673 | if (ctx.args_owned) { |
| 14674 | Py_DECREF(ctx.args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14675 | } |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14676 | Py_DECREF(ctx.fmtstr); |
| 14677 | return _PyUnicodeWriter_Finish(&ctx.writer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14678 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14679 | onError: |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14680 | Py_DECREF(ctx.fmtstr); |
| 14681 | _PyUnicodeWriter_Dealloc(&ctx.writer); |
| 14682 | if (ctx.args_owned) { |
| 14683 | Py_DECREF(ctx.args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14684 | } |
| 14685 | return NULL; |
| 14686 | } |
| 14687 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 14688 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14689 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 14690 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14691 | static PyObject * |
| 14692 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 14693 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14694 | PyObject *x = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14695 | static char *kwlist[] = {"object", "encoding", "errors", 0}; |
| 14696 | char *encoding = NULL; |
| 14697 | char *errors = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14698 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14699 | if (type != &PyUnicode_Type) |
| 14700 | return unicode_subtype_new(type, args, kwds); |
| 14701 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:str", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14702 | kwlist, &x, &encoding, &errors)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14703 | return NULL; |
| 14704 | if (x == NULL) |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 14705 | _Py_RETURN_UNICODE_EMPTY(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14706 | if (encoding == NULL && errors == NULL) |
| 14707 | return PyObject_Str(x); |
| 14708 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14709 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14710 | } |
| 14711 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14712 | static PyObject * |
| 14713 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 14714 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14715 | PyObject *unicode, *self; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14716 | Py_ssize_t length, char_size; |
| 14717 | int share_wstr, share_utf8; |
| 14718 | unsigned int kind; |
| 14719 | void *data; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14720 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14721 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14722 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14723 | unicode = unicode_new(&PyUnicode_Type, args, kwds); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14724 | if (unicode == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14725 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 14726 | assert(_PyUnicode_CHECK(unicode)); |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 14727 | if (PyUnicode_READY(unicode) == -1) { |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 14728 | Py_DECREF(unicode); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14729 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 14730 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14731 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14732 | self = type->tp_alloc(type, 0); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14733 | if (self == NULL) { |
| 14734 | Py_DECREF(unicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14735 | return NULL; |
| 14736 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14737 | kind = PyUnicode_KIND(unicode); |
| 14738 | length = PyUnicode_GET_LENGTH(unicode); |
| 14739 | |
| 14740 | _PyUnicode_LENGTH(self) = length; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 14741 | #ifdef Py_DEBUG |
| 14742 | _PyUnicode_HASH(self) = -1; |
| 14743 | #else |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14744 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 14745 | #endif |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14746 | _PyUnicode_STATE(self).interned = 0; |
| 14747 | _PyUnicode_STATE(self).kind = kind; |
| 14748 | _PyUnicode_STATE(self).compact = 0; |
Victor Stinner | 3cf4637 | 2011-10-03 14:42:15 +0200 | [diff] [blame] | 14749 | _PyUnicode_STATE(self).ascii = _PyUnicode_STATE(unicode).ascii; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14750 | _PyUnicode_STATE(self).ready = 1; |
| 14751 | _PyUnicode_WSTR(self) = NULL; |
| 14752 | _PyUnicode_UTF8_LENGTH(self) = 0; |
| 14753 | _PyUnicode_UTF8(self) = NULL; |
| 14754 | _PyUnicode_WSTR_LENGTH(self) = 0; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 14755 | _PyUnicode_DATA_ANY(self) = NULL; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14756 | |
| 14757 | share_utf8 = 0; |
| 14758 | share_wstr = 0; |
| 14759 | if (kind == PyUnicode_1BYTE_KIND) { |
| 14760 | char_size = 1; |
| 14761 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) |
| 14762 | share_utf8 = 1; |
| 14763 | } |
| 14764 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 14765 | char_size = 2; |
| 14766 | if (sizeof(wchar_t) == 2) |
| 14767 | share_wstr = 1; |
| 14768 | } |
| 14769 | else { |
| 14770 | assert(kind == PyUnicode_4BYTE_KIND); |
| 14771 | char_size = 4; |
| 14772 | if (sizeof(wchar_t) == 4) |
| 14773 | share_wstr = 1; |
| 14774 | } |
| 14775 | |
| 14776 | /* Ensure we won't overflow the length. */ |
| 14777 | if (length > (PY_SSIZE_T_MAX / char_size - 1)) { |
| 14778 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14779 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14780 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14781 | data = PyObject_MALLOC((length + 1) * char_size); |
| 14782 | if (data == NULL) { |
| 14783 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14784 | goto onError; |
| 14785 | } |
| 14786 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 14787 | _PyUnicode_DATA_ANY(self) = data; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14788 | if (share_utf8) { |
| 14789 | _PyUnicode_UTF8_LENGTH(self) = length; |
| 14790 | _PyUnicode_UTF8(self) = data; |
| 14791 | } |
| 14792 | if (share_wstr) { |
| 14793 | _PyUnicode_WSTR_LENGTH(self) = length; |
| 14794 | _PyUnicode_WSTR(self) = (wchar_t *)data; |
| 14795 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14796 | |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14797 | Py_MEMCPY(data, PyUnicode_DATA(unicode), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 14798 | kind * (length + 1)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 14799 | assert(_PyUnicode_CheckConsistency(self, 1)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 14800 | #ifdef Py_DEBUG |
| 14801 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
| 14802 | #endif |
Victor Stinner | dd18d3a | 2011-10-22 11:08:10 +0200 | [diff] [blame] | 14803 | Py_DECREF(unicode); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 14804 | return self; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14805 | |
| 14806 | onError: |
| 14807 | Py_DECREF(unicode); |
| 14808 | Py_DECREF(self); |
| 14809 | return NULL; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14810 | } |
| 14811 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 14812 | PyDoc_STRVAR(unicode_doc, |
Chris Jerdonek | 83fe2e1 | 2012-10-07 14:48:36 -0700 | [diff] [blame] | 14813 | "str(object='') -> str\n\ |
| 14814 | str(bytes_or_buffer[, encoding[, errors]]) -> str\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14815 | \n\ |
Nick Coghlan | 573b1fd | 2012-08-16 14:13:07 +1000 | [diff] [blame] | 14816 | Create a new string object from the given object. If encoding or\n\ |
| 14817 | errors is specified, then the object must expose a data buffer\n\ |
| 14818 | that will be decoded using the given encoding and error handler.\n\ |
| 14819 | Otherwise, returns the result of object.__str__() (if defined)\n\ |
| 14820 | or repr(object).\n\ |
| 14821 | encoding defaults to sys.getdefaultencoding().\n\ |
| 14822 | errors defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14823 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14824 | static PyObject *unicode_iter(PyObject *seq); |
| 14825 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14826 | PyTypeObject PyUnicode_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 14827 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14828 | "str", /* tp_name */ |
| 14829 | sizeof(PyUnicodeObject), /* tp_size */ |
| 14830 | 0, /* tp_itemsize */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14831 | /* Slots */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14832 | (destructor)unicode_dealloc, /* tp_dealloc */ |
| 14833 | 0, /* tp_print */ |
| 14834 | 0, /* tp_getattr */ |
| 14835 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 14836 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14837 | unicode_repr, /* tp_repr */ |
| 14838 | &unicode_as_number, /* tp_as_number */ |
| 14839 | &unicode_as_sequence, /* tp_as_sequence */ |
| 14840 | &unicode_as_mapping, /* tp_as_mapping */ |
| 14841 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 14842 | 0, /* tp_call*/ |
| 14843 | (reprfunc) unicode_str, /* tp_str */ |
| 14844 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 14845 | 0, /* tp_setattro */ |
| 14846 | 0, /* tp_as_buffer */ |
| 14847 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14848 | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14849 | unicode_doc, /* tp_doc */ |
| 14850 | 0, /* tp_traverse */ |
| 14851 | 0, /* tp_clear */ |
| 14852 | PyUnicode_RichCompare, /* tp_richcompare */ |
| 14853 | 0, /* tp_weaklistoffset */ |
| 14854 | unicode_iter, /* tp_iter */ |
| 14855 | 0, /* tp_iternext */ |
| 14856 | unicode_methods, /* tp_methods */ |
| 14857 | 0, /* tp_members */ |
| 14858 | 0, /* tp_getset */ |
| 14859 | &PyBaseObject_Type, /* tp_base */ |
| 14860 | 0, /* tp_dict */ |
| 14861 | 0, /* tp_descr_get */ |
| 14862 | 0, /* tp_descr_set */ |
| 14863 | 0, /* tp_dictoffset */ |
| 14864 | 0, /* tp_init */ |
| 14865 | 0, /* tp_alloc */ |
| 14866 | unicode_new, /* tp_new */ |
| 14867 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14868 | }; |
| 14869 | |
| 14870 | /* Initialize the Unicode implementation */ |
| 14871 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 14872 | int _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14873 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14874 | /* XXX - move this array to unicodectype.c ? */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14875 | Py_UCS2 linebreak[] = { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14876 | 0x000A, /* LINE FEED */ |
| 14877 | 0x000D, /* CARRIAGE RETURN */ |
| 14878 | 0x001C, /* FILE SEPARATOR */ |
| 14879 | 0x001D, /* GROUP SEPARATOR */ |
| 14880 | 0x001E, /* RECORD SEPARATOR */ |
| 14881 | 0x0085, /* NEXT LINE */ |
| 14882 | 0x2028, /* LINE SEPARATOR */ |
| 14883 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 14884 | }; |
| 14885 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 14886 | /* Init the implementation */ |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 14887 | _Py_INCREF_UNICODE_EMPTY(); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 14888 | if (!unicode_empty) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14889 | Py_FatalError("Can't create empty string"); |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 14890 | Py_DECREF(unicode_empty); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 14891 | |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 14892 | if (PyType_Ready(&PyUnicode_Type) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14893 | Py_FatalError("Can't initialize 'unicode'"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14894 | |
| 14895 | /* initialize the linebreak bloom filter */ |
| 14896 | bloom_linebreak = make_bloom_mask( |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14897 | PyUnicode_2BYTE_KIND, linebreak, |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 14898 | Py_ARRAY_LENGTH(linebreak)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 14899 | |
Christian Heimes | 26532f7 | 2013-07-20 14:57:16 +0200 | [diff] [blame] | 14900 | if (PyType_Ready(&EncodingMapType) < 0) |
| 14901 | Py_FatalError("Can't initialize encoding map type"); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 14902 | |
Benjamin Peterson | c431128 | 2012-10-30 23:21:10 -0400 | [diff] [blame] | 14903 | if (PyType_Ready(&PyFieldNameIter_Type) < 0) |
| 14904 | Py_FatalError("Can't initialize field name iterator type"); |
| 14905 | |
| 14906 | if (PyType_Ready(&PyFormatterIter_Type) < 0) |
| 14907 | Py_FatalError("Can't initialize formatter iter type"); |
Benjamin Peterson | e8ea97f | 2012-10-30 23:27:52 -0400 | [diff] [blame] | 14908 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 14909 | #ifdef HAVE_MBCS |
| 14910 | winver.dwOSVersionInfoSize = sizeof(winver); |
| 14911 | if (!GetVersionEx((OSVERSIONINFO*)&winver)) { |
| 14912 | PyErr_SetFromWindowsErr(0); |
| 14913 | return -1; |
| 14914 | } |
| 14915 | #endif |
| 14916 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14917 | } |
| 14918 | |
| 14919 | /* Finalize the Unicode implementation */ |
| 14920 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 14921 | int |
| 14922 | PyUnicode_ClearFreeList(void) |
| 14923 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14924 | return 0; |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 14925 | } |
| 14926 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14927 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 14928 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14929 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 14930 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14931 | |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 14932 | Py_CLEAR(unicode_empty); |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 14933 | |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 14934 | for (i = 0; i < 256; i++) |
| 14935 | Py_CLEAR(unicode_latin1[i]); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 14936 | _PyUnicode_ClearStaticStrings(); |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 14937 | (void)PyUnicode_ClearFreeList(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14938 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 14939 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14940 | void |
| 14941 | PyUnicode_InternInPlace(PyObject **p) |
| 14942 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 14943 | PyObject *s = *p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14944 | PyObject *t; |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 14945 | #ifdef Py_DEBUG |
| 14946 | assert(s != NULL); |
| 14947 | assert(_PyUnicode_CHECK(s)); |
| 14948 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14949 | if (s == NULL || !PyUnicode_Check(s)) |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 14950 | return; |
| 14951 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14952 | /* If it's a subclass, we don't really know what putting |
| 14953 | it in the interned dict might do. */ |
| 14954 | if (!PyUnicode_CheckExact(s)) |
| 14955 | return; |
| 14956 | if (PyUnicode_CHECK_INTERNED(s)) |
| 14957 | return; |
| 14958 | if (interned == NULL) { |
| 14959 | interned = PyDict_New(); |
| 14960 | if (interned == NULL) { |
| 14961 | PyErr_Clear(); /* Don't leave an exception */ |
| 14962 | return; |
| 14963 | } |
| 14964 | } |
| 14965 | /* It might be that the GetItem call fails even |
| 14966 | though the key is present in the dictionary, |
| 14967 | namely when this happens during a stack overflow. */ |
| 14968 | Py_ALLOW_RECURSION |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 14969 | t = PyDict_GetItem(interned, s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14970 | Py_END_ALLOW_RECURSION |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 14971 | |
Victor Stinner | f033510 | 2013-04-14 19:13:03 +0200 | [diff] [blame] | 14972 | if (t) { |
| 14973 | Py_INCREF(t); |
| 14974 | Py_DECREF(*p); |
| 14975 | *p = t; |
| 14976 | return; |
| 14977 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14978 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14979 | PyThreadState_GET()->recursion_critical = 1; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 14980 | if (PyDict_SetItem(interned, s, s) < 0) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14981 | PyErr_Clear(); |
| 14982 | PyThreadState_GET()->recursion_critical = 0; |
| 14983 | return; |
| 14984 | } |
| 14985 | PyThreadState_GET()->recursion_critical = 0; |
| 14986 | /* The two references in interned are not counted by refcnt. |
| 14987 | The deallocator will take care of this */ |
| 14988 | Py_REFCNT(s) -= 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14989 | _PyUnicode_STATE(s).interned = SSTATE_INTERNED_MORTAL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14990 | } |
| 14991 | |
| 14992 | void |
| 14993 | PyUnicode_InternImmortal(PyObject **p) |
| 14994 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14995 | PyUnicode_InternInPlace(p); |
| 14996 | if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { |
Victor Stinner | af9e4b8 | 2011-10-23 20:07:00 +0200 | [diff] [blame] | 14997 | _PyUnicode_STATE(*p).interned = SSTATE_INTERNED_IMMORTAL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14998 | Py_INCREF(*p); |
| 14999 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 15000 | } |
| 15001 | |
| 15002 | PyObject * |
| 15003 | PyUnicode_InternFromString(const char *cp) |
| 15004 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15005 | PyObject *s = PyUnicode_FromString(cp); |
| 15006 | if (s == NULL) |
| 15007 | return NULL; |
| 15008 | PyUnicode_InternInPlace(&s); |
| 15009 | return s; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 15010 | } |
| 15011 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 15012 | void |
| 15013 | _Py_ReleaseInternedUnicodeStrings(void) |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 15014 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15015 | PyObject *keys; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 15016 | PyObject *s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15017 | Py_ssize_t i, n; |
| 15018 | Py_ssize_t immortal_size = 0, mortal_size = 0; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 15019 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15020 | if (interned == NULL || !PyDict_Check(interned)) |
| 15021 | return; |
| 15022 | keys = PyDict_Keys(interned); |
| 15023 | if (keys == NULL || !PyList_Check(keys)) { |
| 15024 | PyErr_Clear(); |
| 15025 | return; |
| 15026 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 15027 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15028 | /* Since _Py_ReleaseInternedUnicodeStrings() is intended to help a leak |
| 15029 | detector, interned unicode strings are not forcibly deallocated; |
| 15030 | rather, we give them their stolen references back, and then clear |
| 15031 | and DECREF the interned dict. */ |
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 | n = PyList_GET_SIZE(keys); |
| 15034 | fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 15035 | n); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15036 | for (i = 0; i < n; i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 15037 | s = PyList_GET_ITEM(keys, i); |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 15038 | if (PyUnicode_READY(s) == -1) { |
| 15039 | assert(0 && "could not ready string"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15040 | fprintf(stderr, "could not ready string\n"); |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 15041 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15042 | switch (PyUnicode_CHECK_INTERNED(s)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15043 | case SSTATE_NOT_INTERNED: |
| 15044 | /* XXX Shouldn't happen */ |
| 15045 | break; |
| 15046 | case SSTATE_INTERNED_IMMORTAL: |
| 15047 | Py_REFCNT(s) += 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15048 | immortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15049 | break; |
| 15050 | case SSTATE_INTERNED_MORTAL: |
| 15051 | Py_REFCNT(s) += 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15052 | mortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15053 | break; |
| 15054 | default: |
| 15055 | Py_FatalError("Inconsistent interned string state."); |
| 15056 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15057 | _PyUnicode_STATE(s).interned = SSTATE_NOT_INTERNED; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15058 | } |
| 15059 | fprintf(stderr, "total size of all interned strings: " |
| 15060 | "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " |
| 15061 | "mortal/immortal\n", mortal_size, immortal_size); |
| 15062 | Py_DECREF(keys); |
| 15063 | PyDict_Clear(interned); |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 15064 | Py_CLEAR(interned); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 15065 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15066 | |
| 15067 | |
| 15068 | /********************* Unicode Iterator **************************/ |
| 15069 | |
| 15070 | typedef struct { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15071 | PyObject_HEAD |
| 15072 | Py_ssize_t it_index; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 15073 | PyObject *it_seq; /* Set to NULL when iterator is exhausted */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15074 | } unicodeiterobject; |
| 15075 | |
| 15076 | static void |
| 15077 | unicodeiter_dealloc(unicodeiterobject *it) |
| 15078 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15079 | _PyObject_GC_UNTRACK(it); |
| 15080 | Py_XDECREF(it->it_seq); |
| 15081 | PyObject_GC_Del(it); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15082 | } |
| 15083 | |
| 15084 | static int |
| 15085 | unicodeiter_traverse(unicodeiterobject *it, visitproc visit, void *arg) |
| 15086 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15087 | Py_VISIT(it->it_seq); |
| 15088 | return 0; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15089 | } |
| 15090 | |
| 15091 | static PyObject * |
| 15092 | unicodeiter_next(unicodeiterobject *it) |
| 15093 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 15094 | PyObject *seq, *item; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15095 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15096 | assert(it != NULL); |
| 15097 | seq = it->it_seq; |
| 15098 | if (seq == NULL) |
| 15099 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 15100 | assert(_PyUnicode_CHECK(seq)); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15101 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15102 | if (it->it_index < PyUnicode_GET_LENGTH(seq)) { |
| 15103 | int kind = PyUnicode_KIND(seq); |
| 15104 | void *data = PyUnicode_DATA(seq); |
| 15105 | Py_UCS4 chr = PyUnicode_READ(kind, data, it->it_index); |
| 15106 | item = PyUnicode_FromOrdinal(chr); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15107 | if (item != NULL) |
| 15108 | ++it->it_index; |
| 15109 | return item; |
| 15110 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15111 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15112 | Py_DECREF(seq); |
| 15113 | it->it_seq = NULL; |
| 15114 | return NULL; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15115 | } |
| 15116 | |
| 15117 | static PyObject * |
| 15118 | unicodeiter_len(unicodeiterobject *it) |
| 15119 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15120 | Py_ssize_t len = 0; |
| 15121 | if (it->it_seq) |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 15122 | len = PyUnicode_GET_LENGTH(it->it_seq) - it->it_index; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15123 | return PyLong_FromSsize_t(len); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15124 | } |
| 15125 | |
| 15126 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
| 15127 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 15128 | static PyObject * |
| 15129 | unicodeiter_reduce(unicodeiterobject *it) |
| 15130 | { |
| 15131 | if (it->it_seq != NULL) { |
Antoine Pitrou | a701388 | 2012-04-05 00:04:20 +0200 | [diff] [blame] | 15132 | return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("iter"), |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 15133 | it->it_seq, it->it_index); |
| 15134 | } else { |
| 15135 | PyObject *u = PyUnicode_FromUnicode(NULL, 0); |
| 15136 | if (u == NULL) |
| 15137 | return NULL; |
Antoine Pitrou | a701388 | 2012-04-05 00:04:20 +0200 | [diff] [blame] | 15138 | return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), u); |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 15139 | } |
| 15140 | } |
| 15141 | |
| 15142 | PyDoc_STRVAR(reduce_doc, "Return state information for pickling."); |
| 15143 | |
| 15144 | static PyObject * |
| 15145 | unicodeiter_setstate(unicodeiterobject *it, PyObject *state) |
| 15146 | { |
| 15147 | Py_ssize_t index = PyLong_AsSsize_t(state); |
| 15148 | if (index == -1 && PyErr_Occurred()) |
| 15149 | return NULL; |
| 15150 | if (index < 0) |
| 15151 | index = 0; |
| 15152 | it->it_index = index; |
| 15153 | Py_RETURN_NONE; |
| 15154 | } |
| 15155 | |
| 15156 | PyDoc_STRVAR(setstate_doc, "Set state information for unpickling."); |
| 15157 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15158 | static PyMethodDef unicodeiter_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15159 | {"__length_hint__", (PyCFunction)unicodeiter_len, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 15160 | length_hint_doc}, |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 15161 | {"__reduce__", (PyCFunction)unicodeiter_reduce, METH_NOARGS, |
| 15162 | reduce_doc}, |
| 15163 | {"__setstate__", (PyCFunction)unicodeiter_setstate, METH_O, |
| 15164 | setstate_doc}, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15165 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15166 | }; |
| 15167 | |
| 15168 | PyTypeObject PyUnicodeIter_Type = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15169 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 15170 | "str_iterator", /* tp_name */ |
| 15171 | sizeof(unicodeiterobject), /* tp_basicsize */ |
| 15172 | 0, /* tp_itemsize */ |
| 15173 | /* methods */ |
| 15174 | (destructor)unicodeiter_dealloc, /* tp_dealloc */ |
| 15175 | 0, /* tp_print */ |
| 15176 | 0, /* tp_getattr */ |
| 15177 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 15178 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15179 | 0, /* tp_repr */ |
| 15180 | 0, /* tp_as_number */ |
| 15181 | 0, /* tp_as_sequence */ |
| 15182 | 0, /* tp_as_mapping */ |
| 15183 | 0, /* tp_hash */ |
| 15184 | 0, /* tp_call */ |
| 15185 | 0, /* tp_str */ |
| 15186 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 15187 | 0, /* tp_setattro */ |
| 15188 | 0, /* tp_as_buffer */ |
| 15189 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 15190 | 0, /* tp_doc */ |
| 15191 | (traverseproc)unicodeiter_traverse, /* tp_traverse */ |
| 15192 | 0, /* tp_clear */ |
| 15193 | 0, /* tp_richcompare */ |
| 15194 | 0, /* tp_weaklistoffset */ |
| 15195 | PyObject_SelfIter, /* tp_iter */ |
| 15196 | (iternextfunc)unicodeiter_next, /* tp_iternext */ |
| 15197 | unicodeiter_methods, /* tp_methods */ |
| 15198 | 0, |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15199 | }; |
| 15200 | |
| 15201 | static PyObject * |
| 15202 | unicode_iter(PyObject *seq) |
| 15203 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15204 | unicodeiterobject *it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15205 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15206 | if (!PyUnicode_Check(seq)) { |
| 15207 | PyErr_BadInternalCall(); |
| 15208 | return NULL; |
| 15209 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15210 | if (PyUnicode_READY(seq) == -1) |
| 15211 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15212 | it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type); |
| 15213 | if (it == NULL) |
| 15214 | return NULL; |
| 15215 | it->it_index = 0; |
| 15216 | Py_INCREF(seq); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 15217 | it->it_seq = seq; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15218 | _PyObject_GC_TRACK(it); |
| 15219 | return (PyObject *)it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15220 | } |
| 15221 | |
Martin v. Löwis | 0d3072e | 2011-10-31 08:40:56 +0100 | [diff] [blame] | 15222 | |
| 15223 | size_t |
| 15224 | Py_UNICODE_strlen(const Py_UNICODE *u) |
| 15225 | { |
| 15226 | int res = 0; |
| 15227 | while(*u++) |
| 15228 | res++; |
| 15229 | return res; |
| 15230 | } |
| 15231 | |
| 15232 | Py_UNICODE* |
| 15233 | Py_UNICODE_strcpy(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 15234 | { |
| 15235 | Py_UNICODE *u = s1; |
| 15236 | while ((*u++ = *s2++)); |
| 15237 | return s1; |
| 15238 | } |
| 15239 | |
| 15240 | Py_UNICODE* |
| 15241 | Py_UNICODE_strncpy(Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 15242 | { |
| 15243 | Py_UNICODE *u = s1; |
| 15244 | while ((*u++ = *s2++)) |
| 15245 | if (n-- == 0) |
| 15246 | break; |
| 15247 | return s1; |
| 15248 | } |
| 15249 | |
| 15250 | Py_UNICODE* |
| 15251 | Py_UNICODE_strcat(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 15252 | { |
| 15253 | Py_UNICODE *u1 = s1; |
| 15254 | u1 += Py_UNICODE_strlen(u1); |
| 15255 | Py_UNICODE_strcpy(u1, s2); |
| 15256 | return s1; |
| 15257 | } |
| 15258 | |
| 15259 | int |
| 15260 | Py_UNICODE_strcmp(const Py_UNICODE *s1, const Py_UNICODE *s2) |
| 15261 | { |
| 15262 | while (*s1 && *s2 && *s1 == *s2) |
| 15263 | s1++, s2++; |
| 15264 | if (*s1 && *s2) |
| 15265 | return (*s1 < *s2) ? -1 : +1; |
| 15266 | if (*s1) |
| 15267 | return 1; |
| 15268 | if (*s2) |
| 15269 | return -1; |
| 15270 | return 0; |
| 15271 | } |
| 15272 | |
| 15273 | int |
| 15274 | Py_UNICODE_strncmp(const Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 15275 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 15276 | Py_UNICODE u1, u2; |
Martin v. Löwis | 0d3072e | 2011-10-31 08:40:56 +0100 | [diff] [blame] | 15277 | for (; n != 0; n--) { |
| 15278 | u1 = *s1; |
| 15279 | u2 = *s2; |
| 15280 | if (u1 != u2) |
| 15281 | return (u1 < u2) ? -1 : +1; |
| 15282 | if (u1 == '\0') |
| 15283 | return 0; |
| 15284 | s1++; |
| 15285 | s2++; |
| 15286 | } |
| 15287 | return 0; |
| 15288 | } |
| 15289 | |
| 15290 | Py_UNICODE* |
| 15291 | Py_UNICODE_strchr(const Py_UNICODE *s, Py_UNICODE c) |
| 15292 | { |
| 15293 | const Py_UNICODE *p; |
| 15294 | for (p = s; *p; p++) |
| 15295 | if (*p == c) |
| 15296 | return (Py_UNICODE*)p; |
| 15297 | return NULL; |
| 15298 | } |
| 15299 | |
| 15300 | Py_UNICODE* |
| 15301 | Py_UNICODE_strrchr(const Py_UNICODE *s, Py_UNICODE c) |
| 15302 | { |
| 15303 | const Py_UNICODE *p; |
| 15304 | p = s + Py_UNICODE_strlen(s); |
| 15305 | while (p != s) { |
| 15306 | p--; |
| 15307 | if (*p == c) |
| 15308 | return (Py_UNICODE*)p; |
| 15309 | } |
| 15310 | return NULL; |
| 15311 | } |
Victor Stinner | 331ea92 | 2010-08-10 16:37:20 +0000 | [diff] [blame] | 15312 | |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 15313 | Py_UNICODE* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 15314 | PyUnicode_AsUnicodeCopy(PyObject *unicode) |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 15315 | { |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 15316 | Py_UNICODE *u, *copy; |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 15317 | Py_ssize_t len, size; |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 15318 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15319 | if (!PyUnicode_Check(unicode)) { |
| 15320 | PyErr_BadArgument(); |
| 15321 | return NULL; |
| 15322 | } |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 15323 | u = PyUnicode_AsUnicodeAndSize(unicode, &len); |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 15324 | if (u == NULL) |
| 15325 | return NULL; |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 15326 | /* Ensure we won't overflow the size. */ |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 15327 | if (len > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 15328 | PyErr_NoMemory(); |
| 15329 | return NULL; |
| 15330 | } |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 15331 | size = len + 1; /* copy the null character */ |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 15332 | size *= sizeof(Py_UNICODE); |
| 15333 | copy = PyMem_Malloc(size); |
| 15334 | if (copy == NULL) { |
| 15335 | PyErr_NoMemory(); |
| 15336 | return NULL; |
| 15337 | } |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 15338 | memcpy(copy, u, size); |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 15339 | return copy; |
| 15340 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 15341 | |
Georg Brandl | 66c221e | 2010-10-14 07:04:07 +0000 | [diff] [blame] | 15342 | /* A _string module, to export formatter_parser and formatter_field_name_split |
| 15343 | to the string.Formatter class implemented in Python. */ |
| 15344 | |
| 15345 | static PyMethodDef _string_methods[] = { |
| 15346 | {"formatter_field_name_split", (PyCFunction) formatter_field_name_split, |
| 15347 | METH_O, PyDoc_STR("split the argument as a field name")}, |
| 15348 | {"formatter_parser", (PyCFunction) formatter_parser, |
| 15349 | METH_O, PyDoc_STR("parse the argument as a format string")}, |
| 15350 | {NULL, NULL} |
| 15351 | }; |
| 15352 | |
| 15353 | static struct PyModuleDef _string_module = { |
| 15354 | PyModuleDef_HEAD_INIT, |
| 15355 | "_string", |
| 15356 | PyDoc_STR("string helper module"), |
| 15357 | 0, |
| 15358 | _string_methods, |
| 15359 | NULL, |
| 15360 | NULL, |
| 15361 | NULL, |
| 15362 | NULL |
| 15363 | }; |
| 15364 | |
| 15365 | PyMODINIT_FUNC |
| 15366 | PyInit__string(void) |
| 15367 | { |
| 15368 | return PyModule_Create(&_string_module); |
| 15369 | } |
| 15370 | |
| 15371 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 15372 | #ifdef __cplusplus |
| 15373 | } |
| 15374 | #endif |