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" |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 44 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 45 | #ifdef MS_WINDOWS |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 46 | #include <windows.h> |
| 47 | #endif |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 48 | |
Victor Stinner | ce5faf6 | 2011-10-05 00:42:43 +0200 | [diff] [blame] | 49 | #ifdef Py_DEBUG |
| 50 | # define DONT_MAKE_RESULT_READY |
| 51 | #endif |
| 52 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 53 | /* Limit for the Unicode object free list */ |
| 54 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 55 | #define PyUnicode_MAXFREELIST 1024 |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 56 | |
| 57 | /* Limit for the Unicode object free list stay alive optimization. |
| 58 | |
| 59 | The implementation will keep allocated Unicode memory intact for |
| 60 | all objects on the free list having a size less than this |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 61 | limit. This reduces malloc() overhead for small Unicode objects. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 62 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 63 | At worst this will result in PyUnicode_MAXFREELIST * |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 64 | (sizeof(PyUnicodeObject) + KEEPALIVE_SIZE_LIMIT + |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 65 | malloc()-overhead) bytes of unused garbage. |
| 66 | |
| 67 | Setting the limit to 0 effectively turns the feature off. |
| 68 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 69 | Note: This is an experimental feature ! If you get core dumps when |
| 70 | using Unicode objects, turn this feature off. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 71 | |
| 72 | */ |
| 73 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 74 | #define KEEPALIVE_SIZE_LIMIT 9 |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 75 | |
| 76 | /* Endianness switches; defaults to little endian */ |
| 77 | |
| 78 | #ifdef WORDS_BIGENDIAN |
| 79 | # define BYTEORDER_IS_BIG_ENDIAN |
| 80 | #else |
| 81 | # define BYTEORDER_IS_LITTLE_ENDIAN |
| 82 | #endif |
| 83 | |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 84 | /* --- Globals ------------------------------------------------------------ |
| 85 | |
| 86 | The globals are initialized by the _PyUnicode_Init() API and should |
| 87 | not be used before calling that API. |
| 88 | |
| 89 | */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 90 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 91 | |
| 92 | #ifdef __cplusplus |
| 93 | extern "C" { |
| 94 | #endif |
| 95 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 96 | #ifdef Py_DEBUG |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 97 | # define _PyUnicode_CHECK(op) _PyUnicode_CheckConsistency(op, 0) |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 98 | #else |
| 99 | # define _PyUnicode_CHECK(op) PyUnicode_Check(op) |
| 100 | #endif |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 101 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 102 | #define _PyUnicode_UTF8(op) \ |
| 103 | (((PyCompactUnicodeObject*)(op))->utf8) |
| 104 | #define PyUnicode_UTF8(op) \ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 105 | (assert(_PyUnicode_CHECK(op)), \ |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 106 | assert(PyUnicode_IS_READY(op)), \ |
| 107 | PyUnicode_IS_COMPACT_ASCII(op) ? \ |
| 108 | ((char*)((PyASCIIObject*)(op) + 1)) : \ |
| 109 | _PyUnicode_UTF8(op)) |
Victor Stinner | bc8b81b | 2011-09-29 19:31:34 +0200 | [diff] [blame] | 110 | #define _PyUnicode_UTF8_LENGTH(op) \ |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 111 | (((PyCompactUnicodeObject*)(op))->utf8_length) |
| 112 | #define PyUnicode_UTF8_LENGTH(op) \ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 113 | (assert(_PyUnicode_CHECK(op)), \ |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 114 | assert(PyUnicode_IS_READY(op)), \ |
| 115 | PyUnicode_IS_COMPACT_ASCII(op) ? \ |
| 116 | ((PyASCIIObject*)(op))->length : \ |
| 117 | _PyUnicode_UTF8_LENGTH(op)) |
Victor Stinner | a5f9163 | 2011-10-04 01:07:11 +0200 | [diff] [blame] | 118 | #define _PyUnicode_WSTR(op) \ |
| 119 | (((PyASCIIObject*)(op))->wstr) |
| 120 | #define _PyUnicode_WSTR_LENGTH(op) \ |
| 121 | (((PyCompactUnicodeObject*)(op))->wstr_length) |
| 122 | #define _PyUnicode_LENGTH(op) \ |
| 123 | (((PyASCIIObject *)(op))->length) |
| 124 | #define _PyUnicode_STATE(op) \ |
| 125 | (((PyASCIIObject *)(op))->state) |
| 126 | #define _PyUnicode_HASH(op) \ |
| 127 | (((PyASCIIObject *)(op))->hash) |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 128 | #define _PyUnicode_KIND(op) \ |
| 129 | (assert(_PyUnicode_CHECK(op)), \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 130 | ((PyASCIIObject *)(op))->state.kind) |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 131 | #define _PyUnicode_GET_LENGTH(op) \ |
| 132 | (assert(_PyUnicode_CHECK(op)), \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 133 | ((PyASCIIObject *)(op))->length) |
Victor Stinner | a5f9163 | 2011-10-04 01:07:11 +0200 | [diff] [blame] | 134 | #define _PyUnicode_DATA_ANY(op) \ |
| 135 | (((PyUnicodeObject*)(op))->data.any) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 136 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 137 | #undef PyUnicode_READY |
| 138 | #define PyUnicode_READY(op) \ |
| 139 | (assert(_PyUnicode_CHECK(op)), \ |
| 140 | (PyUnicode_IS_READY(op) ? \ |
Victor Stinner | a5f9163 | 2011-10-04 01:07:11 +0200 | [diff] [blame] | 141 | 0 : \ |
| 142 | _PyUnicode_Ready((PyObject *)(op)))) |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 143 | |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 144 | #define _PyUnicode_READY_REPLACE(p_obj) \ |
| 145 | (assert(_PyUnicode_CHECK(*p_obj)), \ |
| 146 | (PyUnicode_IS_READY(*p_obj) ? \ |
| 147 | 0 : _PyUnicode_ReadyReplace((PyObject **)(p_obj)))) |
| 148 | |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 149 | #define _PyUnicode_SHARE_UTF8(op) \ |
| 150 | (assert(_PyUnicode_CHECK(op)), \ |
| 151 | assert(!PyUnicode_IS_COMPACT_ASCII(op)), \ |
| 152 | (_PyUnicode_UTF8(op) == PyUnicode_DATA(op))) |
| 153 | #define _PyUnicode_SHARE_WSTR(op) \ |
| 154 | (assert(_PyUnicode_CHECK(op)), \ |
| 155 | (_PyUnicode_WSTR(unicode) == PyUnicode_DATA(op))) |
| 156 | |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 157 | /* true if the Unicode object has an allocated UTF-8 memory block |
| 158 | (not shared with other data) */ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 159 | #define _PyUnicode_HAS_UTF8_MEMORY(op) \ |
| 160 | (assert(_PyUnicode_CHECK(op)), \ |
| 161 | (!PyUnicode_IS_COMPACT_ASCII(op) \ |
| 162 | && _PyUnicode_UTF8(op) \ |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 163 | && _PyUnicode_UTF8(op) != PyUnicode_DATA(op))) |
| 164 | |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 165 | /* true if the Unicode object has an allocated wstr memory block |
| 166 | (not shared with other data) */ |
| 167 | #define _PyUnicode_HAS_WSTR_MEMORY(op) \ |
| 168 | (assert(_PyUnicode_CHECK(op)), \ |
| 169 | (_PyUnicode_WSTR(op) && \ |
| 170 | (!PyUnicode_IS_READY(op) || \ |
| 171 | _PyUnicode_WSTR(op) != PyUnicode_DATA(op)))) |
| 172 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 173 | /* Generic helper macro to convert characters of different types. |
| 174 | from_type and to_type have to be valid type names, begin and end |
| 175 | are pointers to the source characters which should be of type |
| 176 | "from_type *". to is a pointer of type "to_type *" and points to the |
| 177 | buffer where the result characters are written to. */ |
| 178 | #define _PyUnicode_CONVERT_BYTES(from_type, to_type, begin, end, to) \ |
| 179 | do { \ |
Antoine Pitrou | e459a08 | 2011-10-11 20:58:41 +0200 | [diff] [blame] | 180 | to_type *_to = (to_type *) to; \ |
| 181 | const from_type *_iter = (begin); \ |
| 182 | const from_type *_end = (end); \ |
| 183 | Py_ssize_t n = (_end) - (_iter); \ |
| 184 | const from_type *_unrolled_end = \ |
| 185 | _iter + (n & ~ (Py_ssize_t) 3); \ |
| 186 | while (_iter < (_unrolled_end)) { \ |
| 187 | _to[0] = (to_type) _iter[0]; \ |
| 188 | _to[1] = (to_type) _iter[1]; \ |
| 189 | _to[2] = (to_type) _iter[2]; \ |
| 190 | _to[3] = (to_type) _iter[3]; \ |
| 191 | _iter += 4; _to += 4; \ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 192 | } \ |
Antoine Pitrou | e459a08 | 2011-10-11 20:58:41 +0200 | [diff] [blame] | 193 | while (_iter < (_end)) \ |
| 194 | *_to++ = (to_type) *_iter++; \ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 195 | } while (0) |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 196 | |
Victor Stinner | b15d4d8 | 2011-09-28 23:59:20 +0200 | [diff] [blame] | 197 | /* The Unicode string has been modified: reset the hash */ |
| 198 | #define _PyUnicode_DIRTY(op) do { _PyUnicode_HASH(op) = -1; } while (0) |
| 199 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 200 | /* This dictionary holds all interned unicode strings. Note that references |
| 201 | to strings in this dictionary are *not* counted in the string's ob_refcnt. |
| 202 | When the interned string reaches a refcnt of 0 the string deallocation |
| 203 | function will delete the reference from this dictionary. |
| 204 | |
| 205 | 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] | 206 | 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] | 207 | */ |
| 208 | static PyObject *interned; |
| 209 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 210 | /* The empty Unicode object is shared to improve performance. */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 211 | static PyObject *unicode_empty; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 212 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 213 | /* List of static strings. */ |
| 214 | static _Py_Identifier *static_strings; |
| 215 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 216 | /* Single character Unicode strings in the Latin-1 range are being |
| 217 | shared as well. */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 218 | static PyObject *unicode_latin1[256]; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 219 | |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 220 | /* Fast detection of the most frequent whitespace characters */ |
| 221 | const unsigned char _Py_ascii_whitespace[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 222 | 0, 0, 0, 0, 0, 0, 0, 0, |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 223 | /* case 0x0009: * CHARACTER TABULATION */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 224 | /* case 0x000A: * LINE FEED */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 225 | /* case 0x000B: * LINE TABULATION */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 226 | /* case 0x000C: * FORM FEED */ |
| 227 | /* case 0x000D: * CARRIAGE RETURN */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 228 | 0, 1, 1, 1, 1, 1, 0, 0, |
| 229 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 230 | /* case 0x001C: * FILE SEPARATOR */ |
| 231 | /* case 0x001D: * GROUP SEPARATOR */ |
| 232 | /* case 0x001E: * RECORD SEPARATOR */ |
| 233 | /* case 0x001F: * UNIT SEPARATOR */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 234 | 0, 0, 0, 0, 1, 1, 1, 1, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 235 | /* case 0x0020: * SPACE */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 236 | 1, 0, 0, 0, 0, 0, 0, 0, |
| 237 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 238 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 239 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 240 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 241 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 242 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 243 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 244 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 245 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 246 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 247 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 248 | 0, 0, 0, 0, 0, 0, 0, 0 |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 249 | }; |
| 250 | |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 251 | /* forward */ |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 252 | static PyUnicodeObject *_PyUnicode_New(Py_ssize_t length); |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 253 | static PyObject* get_latin1_char(unsigned char ch); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 254 | static void copy_characters( |
| 255 | PyObject *to, Py_ssize_t to_start, |
| 256 | PyObject *from, Py_ssize_t from_start, |
| 257 | Py_ssize_t how_many); |
Victor Stinner | c729b8e | 2011-10-06 02:36:59 +0200 | [diff] [blame] | 258 | #ifdef Py_DEBUG |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 259 | static int unicode_is_singleton(PyObject *unicode); |
Victor Stinner | c729b8e | 2011-10-06 02:36:59 +0200 | [diff] [blame] | 260 | #endif |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 261 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 262 | static PyObject * |
| 263 | unicode_encode_call_errorhandler(const char *errors, |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 264 | PyObject **errorHandler,const char *encoding, const char *reason, |
| 265 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 266 | Py_ssize_t startpos, Py_ssize_t endpos, Py_ssize_t *newpos); |
| 267 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 268 | static void |
| 269 | raise_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 270 | const char *encoding, |
| 271 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 272 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 273 | const char *reason); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 274 | |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 275 | /* Same for linebreaks */ |
| 276 | static unsigned char ascii_linebreak[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 277 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 278 | /* 0x000A, * LINE FEED */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 279 | /* 0x000B, * LINE TABULATION */ |
| 280 | /* 0x000C, * FORM FEED */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 281 | /* 0x000D, * CARRIAGE RETURN */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 282 | 0, 0, 1, 1, 1, 1, 0, 0, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 283 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 284 | /* 0x001C, * FILE SEPARATOR */ |
| 285 | /* 0x001D, * GROUP SEPARATOR */ |
| 286 | /* 0x001E, * RECORD SEPARATOR */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 287 | 0, 0, 0, 0, 1, 1, 1, 0, |
| 288 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 289 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 290 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 291 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 292 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 293 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 294 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 295 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 296 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 297 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 298 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 299 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 300 | 0, 0, 0, 0, 0, 0, 0, 0 |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 301 | }; |
| 302 | |
Ezio Melotti | 48a2f8f | 2011-09-29 00:18:19 +0300 | [diff] [blame] | 303 | /* The max unicode value is always 0x10FFFF while using the PEP-393 API. |
| 304 | 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] | 305 | Py_UNICODE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 306 | PyUnicode_GetMax(void) |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 307 | { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 308 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 309 | return 0x10FFFF; |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 310 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 311 | /* This is actually an illegal character, so it should |
| 312 | not be passed to unichr. */ |
| 313 | return 0xFFFF; |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 314 | #endif |
| 315 | } |
| 316 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 317 | #ifdef Py_DEBUG |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 318 | int |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 319 | /* FIXME: use PyObject* type for op */ |
| 320 | _PyUnicode_CheckConsistency(void *op, int check_content) |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 321 | { |
| 322 | PyASCIIObject *ascii; |
| 323 | unsigned int kind; |
| 324 | |
| 325 | assert(PyUnicode_Check(op)); |
| 326 | |
| 327 | ascii = (PyASCIIObject *)op; |
| 328 | kind = ascii->state.kind; |
| 329 | |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 330 | if (ascii->state.ascii == 1 && ascii->state.compact == 1) { |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 331 | assert(kind == PyUnicode_1BYTE_KIND); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 332 | assert(ascii->state.ready == 1); |
| 333 | } |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 334 | else { |
Victor Stinner | 85041a5 | 2011-10-03 14:42:39 +0200 | [diff] [blame] | 335 | PyCompactUnicodeObject *compact = (PyCompactUnicodeObject *)op; |
Victor Stinner | 7f11ad4 | 2011-10-04 00:00:20 +0200 | [diff] [blame] | 336 | void *data; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 337 | |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 338 | if (ascii->state.compact == 1) { |
| 339 | data = compact + 1; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 340 | assert(kind == PyUnicode_1BYTE_KIND |
| 341 | || kind == PyUnicode_2BYTE_KIND |
| 342 | || kind == PyUnicode_4BYTE_KIND); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 343 | assert(ascii->state.ascii == 0); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 344 | assert(ascii->state.ready == 1); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 345 | assert (compact->utf8 != data); |
| 346 | } else { |
| 347 | PyUnicodeObject *unicode = (PyUnicodeObject *)op; |
| 348 | |
| 349 | data = unicode->data.any; |
| 350 | if (kind == PyUnicode_WCHAR_KIND) { |
| 351 | assert(ascii->state.compact == 0); |
| 352 | assert(ascii->state.ascii == 0); |
| 353 | assert(ascii->state.ready == 0); |
| 354 | assert(ascii->wstr != NULL); |
| 355 | assert(data == NULL); |
| 356 | assert(compact->utf8 == NULL); |
| 357 | assert(ascii->state.interned == SSTATE_NOT_INTERNED); |
| 358 | } |
| 359 | else { |
| 360 | assert(kind == PyUnicode_1BYTE_KIND |
| 361 | || kind == PyUnicode_2BYTE_KIND |
| 362 | || kind == PyUnicode_4BYTE_KIND); |
| 363 | assert(ascii->state.compact == 0); |
| 364 | assert(ascii->state.ready == 1); |
| 365 | assert(data != NULL); |
| 366 | if (ascii->state.ascii) { |
| 367 | assert (compact->utf8 == data); |
| 368 | assert (compact->utf8_length == ascii->length); |
| 369 | } |
| 370 | else |
| 371 | assert (compact->utf8 != data); |
| 372 | } |
| 373 | } |
| 374 | if (kind != PyUnicode_WCHAR_KIND) { |
Victor Stinner | 7f11ad4 | 2011-10-04 00:00:20 +0200 | [diff] [blame] | 375 | if ( |
| 376 | #if SIZEOF_WCHAR_T == 2 |
| 377 | kind == PyUnicode_2BYTE_KIND |
| 378 | #else |
| 379 | kind == PyUnicode_4BYTE_KIND |
| 380 | #endif |
| 381 | ) |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 382 | { |
| 383 | assert(ascii->wstr == data); |
| 384 | assert(compact->wstr_length == ascii->length); |
| 385 | } else |
| 386 | assert(ascii->wstr != data); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 387 | } |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 388 | |
| 389 | if (compact->utf8 == NULL) |
| 390 | assert(compact->utf8_length == 0); |
| 391 | if (ascii->wstr == NULL) |
| 392 | assert(compact->wstr_length == 0); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 393 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 394 | /* check that the best kind is used */ |
| 395 | if (check_content && kind != PyUnicode_WCHAR_KIND) |
| 396 | { |
| 397 | Py_ssize_t i; |
| 398 | Py_UCS4 maxchar = 0; |
| 399 | void *data = PyUnicode_DATA(ascii); |
| 400 | for (i=0; i < ascii->length; i++) |
| 401 | { |
| 402 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 403 | if (ch > maxchar) |
| 404 | maxchar = ch; |
| 405 | } |
| 406 | if (kind == PyUnicode_1BYTE_KIND) { |
| 407 | if (ascii->state.ascii == 0) |
| 408 | assert(maxchar >= 128); |
| 409 | else |
| 410 | assert(maxchar < 128); |
| 411 | } |
| 412 | else if (kind == PyUnicode_2BYTE_KIND) |
| 413 | assert(maxchar >= 0x100); |
| 414 | else |
| 415 | assert(maxchar >= 0x10000); |
| 416 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 417 | if (check_content && !unicode_is_singleton((PyObject*)ascii)) |
| 418 | assert(ascii->hash == -1); |
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 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 423 | /* --- Bloom Filters ----------------------------------------------------- */ |
| 424 | |
| 425 | /* stuff to implement simple "bloom filters" for Unicode characters. |
| 426 | to keep things simple, we use a single bitmask, using the least 5 |
| 427 | bits from each unicode characters as the bit index. */ |
| 428 | |
| 429 | /* the linebreak mask is set up by Unicode_Init below */ |
| 430 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 431 | #if LONG_BIT >= 128 |
| 432 | #define BLOOM_WIDTH 128 |
| 433 | #elif LONG_BIT >= 64 |
| 434 | #define BLOOM_WIDTH 64 |
| 435 | #elif LONG_BIT >= 32 |
| 436 | #define BLOOM_WIDTH 32 |
| 437 | #else |
| 438 | #error "LONG_BIT is smaller than 32" |
| 439 | #endif |
| 440 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 441 | #define BLOOM_MASK unsigned long |
| 442 | |
| 443 | static BLOOM_MASK bloom_linebreak; |
| 444 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 445 | #define BLOOM_ADD(mask, ch) ((mask |= (1UL << ((ch) & (BLOOM_WIDTH - 1))))) |
| 446 | #define BLOOM(mask, ch) ((mask & (1UL << ((ch) & (BLOOM_WIDTH - 1))))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 447 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 448 | #define BLOOM_LINEBREAK(ch) \ |
| 449 | ((ch) < 128U ? ascii_linebreak[(ch)] : \ |
| 450 | (BLOOM(bloom_linebreak, (ch)) && Py_UNICODE_ISLINEBREAK(ch))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 451 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 452 | Py_LOCAL_INLINE(BLOOM_MASK) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 453 | make_bloom_mask(int kind, void* ptr, Py_ssize_t len) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 454 | { |
| 455 | /* calculate simple bloom-style bitmask for a given unicode string */ |
| 456 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 457 | BLOOM_MASK mask; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 458 | Py_ssize_t i; |
| 459 | |
| 460 | mask = 0; |
| 461 | for (i = 0; i < len; i++) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 462 | BLOOM_ADD(mask, PyUnicode_READ(kind, ptr, i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 463 | |
| 464 | return mask; |
| 465 | } |
| 466 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 467 | #define BLOOM_MEMBER(mask, chr, str) \ |
| 468 | (BLOOM(mask, chr) \ |
| 469 | && (PyUnicode_FindChar(str, chr, 0, PyUnicode_GET_LENGTH(str), 1) >= 0)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 470 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 471 | /* --- Unicode Object ----------------------------------------------------- */ |
| 472 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 473 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 474 | fixup(PyObject *self, Py_UCS4 (*fixfct)(PyObject *s)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 475 | |
| 476 | Py_LOCAL_INLINE(char *) findchar(void *s, int kind, |
| 477 | Py_ssize_t size, Py_UCS4 ch, |
| 478 | int direction) |
| 479 | { |
| 480 | /* like wcschr, but doesn't stop at NULL characters */ |
| 481 | Py_ssize_t i; |
| 482 | if (direction == 1) { |
| 483 | for(i = 0; i < size; i++) |
| 484 | if (PyUnicode_READ(kind, s, i) == ch) |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 485 | return (char*)s + kind * i; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 486 | } |
| 487 | else { |
| 488 | for(i = size-1; i >= 0; i--) |
| 489 | if (PyUnicode_READ(kind, s, i) == ch) |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 490 | return (char*)s + kind * i; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 491 | } |
| 492 | return NULL; |
| 493 | } |
| 494 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 495 | static PyObject* |
| 496 | resize_compact(PyObject *unicode, Py_ssize_t length) |
| 497 | { |
| 498 | Py_ssize_t char_size; |
| 499 | Py_ssize_t struct_size; |
| 500 | Py_ssize_t new_size; |
| 501 | int share_wstr; |
| 502 | |
| 503 | assert(PyUnicode_IS_READY(unicode)); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 504 | char_size = PyUnicode_KIND(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 505 | if (PyUnicode_IS_COMPACT_ASCII(unicode)) |
| 506 | struct_size = sizeof(PyASCIIObject); |
| 507 | else |
| 508 | struct_size = sizeof(PyCompactUnicodeObject); |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 509 | share_wstr = _PyUnicode_SHARE_WSTR(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 510 | |
| 511 | _Py_DEC_REFTOTAL; |
| 512 | _Py_ForgetReference(unicode); |
| 513 | |
| 514 | if (length > ((PY_SSIZE_T_MAX - struct_size) / char_size - 1)) { |
| 515 | PyErr_NoMemory(); |
| 516 | return NULL; |
| 517 | } |
| 518 | new_size = (struct_size + (length + 1) * char_size); |
| 519 | |
| 520 | unicode = (PyObject *)PyObject_REALLOC((char *)unicode, new_size); |
| 521 | if (unicode == NULL) { |
| 522 | PyObject_Del(unicode); |
| 523 | PyErr_NoMemory(); |
| 524 | return NULL; |
| 525 | } |
| 526 | _Py_NewReference(unicode); |
| 527 | _PyUnicode_LENGTH(unicode) = length; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 528 | if (share_wstr) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 529 | _PyUnicode_WSTR(unicode) = PyUnicode_DATA(unicode); |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 530 | if (!PyUnicode_IS_COMPACT_ASCII(unicode)) |
| 531 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 532 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 533 | PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), |
| 534 | length, 0); |
| 535 | return unicode; |
| 536 | } |
| 537 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 538 | static int |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 539 | resize_inplace(PyUnicodeObject *unicode, Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 540 | { |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 541 | wchar_t *wstr; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 542 | assert(!PyUnicode_IS_COMPACT(unicode)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 543 | assert(Py_REFCNT(unicode) == 1); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 544 | |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 545 | _PyUnicode_DIRTY(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 546 | |
| 547 | if (PyUnicode_IS_READY(unicode)) { |
| 548 | Py_ssize_t char_size; |
| 549 | Py_ssize_t new_size; |
Victor Stinner | 1c8d0c7 | 2011-10-03 12:11:00 +0200 | [diff] [blame] | 550 | int share_wstr, share_utf8; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 551 | void *data; |
| 552 | |
| 553 | data = _PyUnicode_DATA_ANY(unicode); |
| 554 | assert(data != NULL); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 555 | char_size = PyUnicode_KIND(unicode); |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 556 | share_wstr = _PyUnicode_SHARE_WSTR(unicode); |
| 557 | share_utf8 = _PyUnicode_SHARE_UTF8(unicode); |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 558 | if (!share_utf8 && _PyUnicode_HAS_UTF8_MEMORY(unicode)) |
| 559 | { |
| 560 | PyObject_DEL(_PyUnicode_UTF8(unicode)); |
| 561 | _PyUnicode_UTF8(unicode) = NULL; |
| 562 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
| 563 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 564 | |
| 565 | if (length > (PY_SSIZE_T_MAX / char_size - 1)) { |
| 566 | PyErr_NoMemory(); |
| 567 | return -1; |
| 568 | } |
| 569 | new_size = (length + 1) * char_size; |
| 570 | |
| 571 | data = (PyObject *)PyObject_REALLOC(data, new_size); |
| 572 | if (data == NULL) { |
| 573 | PyErr_NoMemory(); |
| 574 | return -1; |
| 575 | } |
| 576 | _PyUnicode_DATA_ANY(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 577 | if (share_wstr) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 578 | _PyUnicode_WSTR(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 579 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 580 | } |
| 581 | if (share_utf8) { |
Victor Stinner | 1c8d0c7 | 2011-10-03 12:11:00 +0200 | [diff] [blame] | 582 | _PyUnicode_UTF8(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 583 | _PyUnicode_UTF8_LENGTH(unicode) = length; |
| 584 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 585 | _PyUnicode_LENGTH(unicode) = length; |
| 586 | PyUnicode_WRITE(PyUnicode_KIND(unicode), data, length, 0); |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 587 | if (share_wstr || _PyUnicode_WSTR(unicode) == NULL) { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 588 | assert(_PyUnicode_CheckConsistency(unicode, 0)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 589 | return 0; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 590 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 591 | } |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 592 | assert(_PyUnicode_WSTR(unicode) != NULL); |
| 593 | |
| 594 | /* check for integer overflow */ |
| 595 | if (length > PY_SSIZE_T_MAX / sizeof(wchar_t) - 1) { |
| 596 | PyErr_NoMemory(); |
| 597 | return -1; |
| 598 | } |
| 599 | wstr = _PyUnicode_WSTR(unicode); |
| 600 | wstr = PyObject_REALLOC(wstr, sizeof(wchar_t) * (length + 1)); |
| 601 | if (!wstr) { |
| 602 | PyErr_NoMemory(); |
| 603 | return -1; |
| 604 | } |
| 605 | _PyUnicode_WSTR(unicode) = wstr; |
| 606 | _PyUnicode_WSTR(unicode)[length] = 0; |
| 607 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 608 | assert(_PyUnicode_CheckConsistency(unicode, 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 609 | return 0; |
| 610 | } |
| 611 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 612 | static PyObject* |
| 613 | resize_copy(PyObject *unicode, Py_ssize_t length) |
| 614 | { |
| 615 | Py_ssize_t copy_length; |
| 616 | if (PyUnicode_IS_COMPACT(unicode)) { |
| 617 | PyObject *copy; |
| 618 | assert(PyUnicode_IS_READY(unicode)); |
| 619 | |
| 620 | copy = PyUnicode_New(length, PyUnicode_MAX_CHAR_VALUE(unicode)); |
| 621 | if (copy == NULL) |
| 622 | return NULL; |
| 623 | |
| 624 | copy_length = Py_MIN(length, PyUnicode_GET_LENGTH(unicode)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 625 | copy_characters(copy, 0, unicode, 0, copy_length); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 626 | return copy; |
Victor Stinner | 8cfcbed | 2011-10-03 23:19:21 +0200 | [diff] [blame] | 627 | } |
| 628 | else { |
Victor Stinner | 2fd8227 | 2011-10-03 04:06:05 +0200 | [diff] [blame] | 629 | PyUnicodeObject *w; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 630 | assert(_PyUnicode_WSTR(unicode) != NULL); |
| 631 | assert(_PyUnicode_DATA_ANY(unicode) == NULL); |
Victor Stinner | 2fd8227 | 2011-10-03 04:06:05 +0200 | [diff] [blame] | 632 | w = _PyUnicode_New(length); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 633 | if (w == NULL) |
| 634 | return NULL; |
| 635 | copy_length = _PyUnicode_WSTR_LENGTH(unicode); |
| 636 | copy_length = Py_MIN(copy_length, length); |
| 637 | Py_UNICODE_COPY(_PyUnicode_WSTR(w), _PyUnicode_WSTR(unicode), |
| 638 | copy_length); |
| 639 | return (PyObject*)w; |
| 640 | } |
| 641 | } |
| 642 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 643 | /* 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] | 644 | Ux0000 terminated; some code (e.g. new_identifier) |
| 645 | relies on that. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 646 | |
| 647 | XXX This allocator could further be enhanced by assuring that the |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 648 | free list never reduces its size below 1. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 649 | |
| 650 | */ |
| 651 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 652 | #ifdef Py_DEBUG |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 653 | static int unicode_old_new_calls = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 654 | #endif |
| 655 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 656 | static PyUnicodeObject * |
| 657 | _PyUnicode_New(Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 658 | { |
| 659 | register PyUnicodeObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 660 | size_t new_size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 661 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 662 | /* Optimization for empty strings */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 663 | if (length == 0 && unicode_empty != NULL) { |
| 664 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 665 | return (PyUnicodeObject*)unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 666 | } |
| 667 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 668 | /* Ensure we won't overflow the size. */ |
| 669 | if (length > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
| 670 | return (PyUnicodeObject *)PyErr_NoMemory(); |
| 671 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 672 | if (length < 0) { |
| 673 | PyErr_SetString(PyExc_SystemError, |
| 674 | "Negative size passed to _PyUnicode_New"); |
| 675 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 676 | } |
| 677 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 678 | #ifdef Py_DEBUG |
| 679 | ++unicode_old_new_calls; |
| 680 | #endif |
| 681 | |
| 682 | unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type); |
| 683 | if (unicode == NULL) |
| 684 | return NULL; |
| 685 | new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
| 686 | _PyUnicode_WSTR(unicode) = (Py_UNICODE*) PyObject_MALLOC(new_size); |
| 687 | if (!_PyUnicode_WSTR(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 688 | PyErr_NoMemory(); |
| 689 | goto onError; |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 690 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 691 | |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 692 | /* Initialize the first element to guard against cases where |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 693 | * the caller fails before initializing str -- unicode_resize() |
| 694 | * reads str[0], and the Keep-Alive optimization can keep memory |
| 695 | * allocated for str alive across a call to unicode_dealloc(unicode). |
| 696 | * We don't want unicode_resize to read uninitialized memory in |
| 697 | * that case. |
| 698 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 699 | _PyUnicode_WSTR(unicode)[0] = 0; |
| 700 | _PyUnicode_WSTR(unicode)[length] = 0; |
| 701 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 702 | _PyUnicode_HASH(unicode) = -1; |
| 703 | _PyUnicode_STATE(unicode).interned = 0; |
| 704 | _PyUnicode_STATE(unicode).kind = 0; |
| 705 | _PyUnicode_STATE(unicode).compact = 0; |
| 706 | _PyUnicode_STATE(unicode).ready = 0; |
| 707 | _PyUnicode_STATE(unicode).ascii = 0; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 708 | _PyUnicode_DATA_ANY(unicode) = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 709 | _PyUnicode_LENGTH(unicode) = 0; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 710 | _PyUnicode_UTF8(unicode) = NULL; |
| 711 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 712 | return unicode; |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 713 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 714 | onError: |
Amaury Forgeot d'Arc | 7888d08 | 2008-08-01 01:06:32 +0000 | [diff] [blame] | 715 | /* XXX UNREF/NEWREF interface should be more symmetrical */ |
| 716 | _Py_DEC_REFTOTAL; |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 717 | _Py_ForgetReference((PyObject *)unicode); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 718 | PyObject_Del(unicode); |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 719 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 720 | } |
| 721 | |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 722 | static const char* |
| 723 | unicode_kind_name(PyObject *unicode) |
| 724 | { |
Victor Stinner | 42dfd71 | 2011-10-03 14:41:45 +0200 | [diff] [blame] | 725 | /* don't check consistency: unicode_kind_name() is called from |
| 726 | _PyUnicode_Dump() */ |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 727 | if (!PyUnicode_IS_COMPACT(unicode)) |
| 728 | { |
| 729 | if (!PyUnicode_IS_READY(unicode)) |
| 730 | return "wstr"; |
| 731 | switch(PyUnicode_KIND(unicode)) |
| 732 | { |
| 733 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 734 | if (PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 735 | return "legacy ascii"; |
| 736 | else |
| 737 | return "legacy latin1"; |
| 738 | case PyUnicode_2BYTE_KIND: |
| 739 | return "legacy UCS2"; |
| 740 | case PyUnicode_4BYTE_KIND: |
| 741 | return "legacy UCS4"; |
| 742 | default: |
| 743 | return "<legacy invalid kind>"; |
| 744 | } |
| 745 | } |
| 746 | assert(PyUnicode_IS_READY(unicode)); |
| 747 | switch(PyUnicode_KIND(unicode)) |
| 748 | { |
| 749 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 750 | if (PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 751 | return "ascii"; |
| 752 | else |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 753 | return "latin1"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 754 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 755 | return "UCS2"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 756 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 757 | return "UCS4"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 758 | default: |
| 759 | return "<invalid compact kind>"; |
| 760 | } |
| 761 | } |
| 762 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 763 | #ifdef Py_DEBUG |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 764 | static int unicode_new_new_calls = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 765 | |
| 766 | /* Functions wrapping macros for use in debugger */ |
| 767 | char *_PyUnicode_utf8(void *unicode){ |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 768 | return PyUnicode_UTF8(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 769 | } |
| 770 | |
| 771 | void *_PyUnicode_compact_data(void *unicode) { |
| 772 | return _PyUnicode_COMPACT_DATA(unicode); |
| 773 | } |
| 774 | void *_PyUnicode_data(void *unicode){ |
| 775 | printf("obj %p\n", unicode); |
| 776 | printf("compact %d\n", PyUnicode_IS_COMPACT(unicode)); |
| 777 | printf("compact ascii %d\n", PyUnicode_IS_COMPACT_ASCII(unicode)); |
| 778 | printf("ascii op %p\n", ((void*)((PyASCIIObject*)(unicode) + 1))); |
| 779 | printf("compact op %p\n", ((void*)((PyCompactUnicodeObject*)(unicode) + 1))); |
| 780 | printf("compact data %p\n", _PyUnicode_COMPACT_DATA(unicode)); |
| 781 | return PyUnicode_DATA(unicode); |
| 782 | } |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 783 | |
| 784 | void |
| 785 | _PyUnicode_Dump(PyObject *op) |
| 786 | { |
| 787 | PyASCIIObject *ascii = (PyASCIIObject *)op; |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 788 | PyCompactUnicodeObject *compact = (PyCompactUnicodeObject *)op; |
| 789 | PyUnicodeObject *unicode = (PyUnicodeObject *)op; |
| 790 | void *data; |
| 791 | printf("%s: len=%zu, ",unicode_kind_name(op), ascii->length); |
| 792 | if (ascii->state.compact) |
| 793 | data = (compact + 1); |
| 794 | else |
| 795 | data = unicode->data.any; |
| 796 | if (ascii->wstr == data) |
| 797 | printf("shared "); |
| 798 | printf("wstr=%p", ascii->wstr); |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 799 | if (!(ascii->state.ascii == 1 && ascii->state.compact == 1)) { |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 800 | printf(" (%zu), ", compact->wstr_length); |
| 801 | if (!ascii->state.compact && compact->utf8 == unicode->data.any) |
| 802 | printf("shared "); |
| 803 | printf("utf8=%p (%zu)", compact->utf8, compact->utf8_length); |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 804 | } |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 805 | printf(", data=%p\n", data); |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 806 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 807 | #endif |
| 808 | |
| 809 | PyObject * |
| 810 | PyUnicode_New(Py_ssize_t size, Py_UCS4 maxchar) |
| 811 | { |
| 812 | PyObject *obj; |
| 813 | PyCompactUnicodeObject *unicode; |
| 814 | void *data; |
| 815 | int kind_state; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 816 | int is_sharing, is_ascii; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 817 | Py_ssize_t char_size; |
| 818 | Py_ssize_t struct_size; |
| 819 | |
| 820 | /* Optimization for empty strings */ |
| 821 | if (size == 0 && unicode_empty != NULL) { |
| 822 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 823 | return unicode_empty; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 824 | } |
| 825 | |
| 826 | #ifdef Py_DEBUG |
| 827 | ++unicode_new_new_calls; |
| 828 | #endif |
| 829 | |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 830 | is_ascii = 0; |
| 831 | is_sharing = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 832 | struct_size = sizeof(PyCompactUnicodeObject); |
| 833 | if (maxchar < 128) { |
| 834 | kind_state = PyUnicode_1BYTE_KIND; |
| 835 | char_size = 1; |
| 836 | is_ascii = 1; |
| 837 | struct_size = sizeof(PyASCIIObject); |
| 838 | } |
| 839 | else if (maxchar < 256) { |
| 840 | kind_state = PyUnicode_1BYTE_KIND; |
| 841 | char_size = 1; |
| 842 | } |
| 843 | else if (maxchar < 65536) { |
| 844 | kind_state = PyUnicode_2BYTE_KIND; |
| 845 | char_size = 2; |
| 846 | if (sizeof(wchar_t) == 2) |
| 847 | is_sharing = 1; |
| 848 | } |
| 849 | else { |
| 850 | kind_state = PyUnicode_4BYTE_KIND; |
| 851 | char_size = 4; |
| 852 | if (sizeof(wchar_t) == 4) |
| 853 | is_sharing = 1; |
| 854 | } |
| 855 | |
| 856 | /* Ensure we won't overflow the size. */ |
| 857 | if (size < 0) { |
| 858 | PyErr_SetString(PyExc_SystemError, |
| 859 | "Negative size passed to PyUnicode_New"); |
| 860 | return NULL; |
| 861 | } |
| 862 | if (size > ((PY_SSIZE_T_MAX - struct_size) / char_size - 1)) |
| 863 | return PyErr_NoMemory(); |
| 864 | |
| 865 | /* Duplicated allocation code from _PyObject_New() instead of a call to |
| 866 | * PyObject_New() so we are able to allocate space for the object and |
| 867 | * it's data buffer. |
| 868 | */ |
| 869 | obj = (PyObject *) PyObject_MALLOC(struct_size + (size + 1) * char_size); |
| 870 | if (obj == NULL) |
| 871 | return PyErr_NoMemory(); |
| 872 | obj = PyObject_INIT(obj, &PyUnicode_Type); |
| 873 | if (obj == NULL) |
| 874 | return NULL; |
| 875 | |
| 876 | unicode = (PyCompactUnicodeObject *)obj; |
| 877 | if (is_ascii) |
| 878 | data = ((PyASCIIObject*)obj) + 1; |
| 879 | else |
| 880 | data = unicode + 1; |
| 881 | _PyUnicode_LENGTH(unicode) = size; |
| 882 | _PyUnicode_HASH(unicode) = -1; |
| 883 | _PyUnicode_STATE(unicode).interned = 0; |
| 884 | _PyUnicode_STATE(unicode).kind = kind_state; |
| 885 | _PyUnicode_STATE(unicode).compact = 1; |
| 886 | _PyUnicode_STATE(unicode).ready = 1; |
| 887 | _PyUnicode_STATE(unicode).ascii = is_ascii; |
| 888 | if (is_ascii) { |
| 889 | ((char*)data)[size] = 0; |
| 890 | _PyUnicode_WSTR(unicode) = NULL; |
| 891 | } |
| 892 | else if (kind_state == PyUnicode_1BYTE_KIND) { |
| 893 | ((char*)data)[size] = 0; |
| 894 | _PyUnicode_WSTR(unicode) = NULL; |
| 895 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 896 | unicode->utf8 = NULL; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 897 | unicode->utf8_length = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 898 | } |
| 899 | else { |
| 900 | unicode->utf8 = NULL; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 901 | unicode->utf8_length = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 902 | if (kind_state == PyUnicode_2BYTE_KIND) |
| 903 | ((Py_UCS2*)data)[size] = 0; |
| 904 | else /* kind_state == PyUnicode_4BYTE_KIND */ |
| 905 | ((Py_UCS4*)data)[size] = 0; |
| 906 | if (is_sharing) { |
| 907 | _PyUnicode_WSTR_LENGTH(unicode) = size; |
| 908 | _PyUnicode_WSTR(unicode) = (wchar_t *)data; |
| 909 | } |
| 910 | else { |
| 911 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 912 | _PyUnicode_WSTR(unicode) = NULL; |
| 913 | } |
| 914 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 915 | assert(_PyUnicode_CheckConsistency(unicode, 0)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 916 | return obj; |
| 917 | } |
| 918 | |
| 919 | #if SIZEOF_WCHAR_T == 2 |
| 920 | /* Helper function to convert a 16-bits wchar_t representation to UCS4, this |
| 921 | will decode surrogate pairs, the other conversions are implemented as macros |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 922 | for efficiency. |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 923 | |
| 924 | This function assumes that unicode can hold one more code point than wstr |
| 925 | characters for a terminating null character. */ |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 926 | static void |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 927 | unicode_convert_wchar_to_ucs4(const wchar_t *begin, const wchar_t *end, |
| 928 | PyUnicodeObject *unicode) |
| 929 | { |
| 930 | const wchar_t *iter; |
| 931 | Py_UCS4 *ucs4_out; |
| 932 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 933 | assert(unicode != NULL); |
| 934 | assert(_PyUnicode_CHECK(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 935 | assert(_PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND); |
| 936 | ucs4_out = PyUnicode_4BYTE_DATA(unicode); |
| 937 | |
| 938 | for (iter = begin; iter < end; ) { |
| 939 | assert(ucs4_out < (PyUnicode_4BYTE_DATA(unicode) + |
| 940 | _PyUnicode_GET_LENGTH(unicode))); |
| 941 | if (*iter >= 0xD800 && *iter <= 0xDBFF |
| 942 | && (iter+1) < end && iter[1] >= 0xDC00 && iter[1] <= 0xDFFF) |
| 943 | { |
| 944 | *ucs4_out++ = (((iter[0] & 0x3FF)<<10) | (iter[1] & 0x3FF)) + 0x10000; |
| 945 | iter += 2; |
| 946 | } |
| 947 | else { |
| 948 | *ucs4_out++ = *iter; |
| 949 | iter++; |
| 950 | } |
| 951 | } |
| 952 | assert(ucs4_out == (PyUnicode_4BYTE_DATA(unicode) + |
| 953 | _PyUnicode_GET_LENGTH(unicode))); |
| 954 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 955 | } |
| 956 | #endif |
| 957 | |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 958 | static int |
| 959 | _PyUnicode_Dirty(PyObject *unicode) |
| 960 | { |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 961 | assert(_PyUnicode_CHECK(unicode)); |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 962 | if (Py_REFCNT(unicode) != 1) { |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 963 | PyErr_SetString(PyExc_SystemError, |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 964 | "Cannot modify a string having more than 1 reference"); |
| 965 | return -1; |
| 966 | } |
| 967 | _PyUnicode_DIRTY(unicode); |
| 968 | return 0; |
| 969 | } |
| 970 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 971 | static int |
| 972 | _copy_characters(PyObject *to, Py_ssize_t to_start, |
| 973 | PyObject *from, Py_ssize_t from_start, |
| 974 | Py_ssize_t how_many, int check_maxchar) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 975 | { |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 976 | unsigned int from_kind, to_kind; |
| 977 | void *from_data, *to_data; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 978 | int fast; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 979 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 980 | assert(PyUnicode_Check(from)); |
| 981 | assert(PyUnicode_Check(to)); |
| 982 | assert(PyUnicode_IS_READY(from)); |
| 983 | assert(PyUnicode_IS_READY(to)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 984 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 985 | assert(PyUnicode_GET_LENGTH(from) >= how_many); |
| 986 | assert(to_start + how_many <= PyUnicode_GET_LENGTH(to)); |
| 987 | assert(0 <= how_many); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 988 | |
Victor Stinner | f5ca1a2 | 2011-09-28 23:54:59 +0200 | [diff] [blame] | 989 | if (how_many == 0) |
| 990 | return 0; |
| 991 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 992 | from_kind = PyUnicode_KIND(from); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 993 | from_data = PyUnicode_DATA(from); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 994 | to_kind = PyUnicode_KIND(to); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 995 | to_data = PyUnicode_DATA(to); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 996 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 997 | #ifdef Py_DEBUG |
| 998 | if (!check_maxchar |
| 999 | && (from_kind > to_kind |
| 1000 | || (!PyUnicode_IS_ASCII(from) && PyUnicode_IS_ASCII(to)))) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1001 | { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1002 | const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to); |
| 1003 | Py_UCS4 ch; |
| 1004 | Py_ssize_t i; |
| 1005 | for (i=0; i < how_many; i++) { |
| 1006 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
| 1007 | assert(ch <= to_maxchar); |
| 1008 | } |
| 1009 | } |
| 1010 | #endif |
| 1011 | fast = (from_kind == to_kind); |
| 1012 | if (check_maxchar |
| 1013 | && (!PyUnicode_IS_ASCII(from) && PyUnicode_IS_ASCII(to))) |
| 1014 | { |
| 1015 | /* deny latin1 => ascii */ |
| 1016 | fast = 0; |
| 1017 | } |
| 1018 | |
| 1019 | if (fast) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 1020 | Py_MEMCPY((char*)to_data + to_kind * to_start, |
| 1021 | (char*)from_data + from_kind * from_start, |
| 1022 | to_kind * how_many); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1023 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1024 | else if (from_kind == PyUnicode_1BYTE_KIND |
| 1025 | && to_kind == PyUnicode_2BYTE_KIND) |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1026 | { |
| 1027 | _PyUnicode_CONVERT_BYTES( |
| 1028 | Py_UCS1, Py_UCS2, |
| 1029 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 1030 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 1031 | PyUnicode_2BYTE_DATA(to) + to_start |
| 1032 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1033 | } |
Victor Stinner | 157f83f | 2011-09-28 21:41:31 +0200 | [diff] [blame] | 1034 | else if (from_kind == PyUnicode_1BYTE_KIND |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1035 | && to_kind == PyUnicode_4BYTE_KIND) |
| 1036 | { |
| 1037 | _PyUnicode_CONVERT_BYTES( |
| 1038 | Py_UCS1, Py_UCS4, |
| 1039 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 1040 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 1041 | PyUnicode_4BYTE_DATA(to) + to_start |
| 1042 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1043 | } |
| 1044 | else if (from_kind == PyUnicode_2BYTE_KIND |
| 1045 | && to_kind == PyUnicode_4BYTE_KIND) |
| 1046 | { |
| 1047 | _PyUnicode_CONVERT_BYTES( |
| 1048 | Py_UCS2, Py_UCS4, |
| 1049 | PyUnicode_2BYTE_DATA(from) + from_start, |
| 1050 | PyUnicode_2BYTE_DATA(from) + from_start + how_many, |
| 1051 | PyUnicode_4BYTE_DATA(to) + to_start |
| 1052 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1053 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1054 | else { |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1055 | /* check if max_char(from substring) <= max_char(to) */ |
| 1056 | if (from_kind > to_kind |
| 1057 | /* latin1 => ascii */ |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1058 | || (!PyUnicode_IS_ASCII(from) && PyUnicode_IS_ASCII(to))) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1059 | { |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1060 | /* slow path to check for character overflow */ |
| 1061 | const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1062 | Py_UCS4 ch; |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1063 | Py_ssize_t i; |
| 1064 | |
Victor Stinner | 56c161a | 2011-10-06 02:47:11 +0200 | [diff] [blame] | 1065 | #ifdef Py_DEBUG |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1066 | for (i=0; i < how_many; i++) { |
| 1067 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
Victor Stinner | 56c161a | 2011-10-06 02:47:11 +0200 | [diff] [blame] | 1068 | assert(ch <= to_maxchar); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1069 | PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); |
| 1070 | } |
Victor Stinner | 56c161a | 2011-10-06 02:47:11 +0200 | [diff] [blame] | 1071 | #else |
| 1072 | if (!check_maxchar) { |
| 1073 | for (i=0; i < how_many; i++) { |
| 1074 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
| 1075 | PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); |
| 1076 | } |
| 1077 | } |
| 1078 | else { |
| 1079 | for (i=0; i < how_many; i++) { |
| 1080 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
| 1081 | if (ch > to_maxchar) |
| 1082 | return 1; |
| 1083 | PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); |
| 1084 | } |
| 1085 | } |
| 1086 | #endif |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1087 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1088 | else { |
Victor Stinner | 56c161a | 2011-10-06 02:47:11 +0200 | [diff] [blame] | 1089 | assert(0 && "inconsistent state"); |
| 1090 | return 1; |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1091 | } |
| 1092 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1093 | return 0; |
| 1094 | } |
| 1095 | |
| 1096 | static void |
| 1097 | copy_characters(PyObject *to, Py_ssize_t to_start, |
| 1098 | PyObject *from, Py_ssize_t from_start, |
| 1099 | Py_ssize_t how_many) |
| 1100 | { |
| 1101 | (void)_copy_characters(to, to_start, from, from_start, how_many, 0); |
| 1102 | } |
| 1103 | |
| 1104 | Py_ssize_t |
| 1105 | PyUnicode_CopyCharacters(PyObject *to, Py_ssize_t to_start, |
| 1106 | PyObject *from, Py_ssize_t from_start, |
| 1107 | Py_ssize_t how_many) |
| 1108 | { |
| 1109 | int err; |
| 1110 | |
| 1111 | if (!PyUnicode_Check(from) || !PyUnicode_Check(to)) { |
| 1112 | PyErr_BadInternalCall(); |
| 1113 | return -1; |
| 1114 | } |
| 1115 | |
| 1116 | if (PyUnicode_READY(from)) |
| 1117 | return -1; |
| 1118 | if (PyUnicode_READY(to)) |
| 1119 | return -1; |
| 1120 | |
| 1121 | how_many = Py_MIN(PyUnicode_GET_LENGTH(from), how_many); |
| 1122 | if (to_start + how_many > PyUnicode_GET_LENGTH(to)) { |
| 1123 | PyErr_Format(PyExc_SystemError, |
| 1124 | "Cannot write %zi characters at %zi " |
| 1125 | "in a string of %zi characters", |
| 1126 | how_many, to_start, PyUnicode_GET_LENGTH(to)); |
| 1127 | return -1; |
| 1128 | } |
| 1129 | |
| 1130 | if (how_many == 0) |
| 1131 | return 0; |
| 1132 | |
| 1133 | if (_PyUnicode_Dirty(to)) |
| 1134 | return -1; |
| 1135 | |
| 1136 | err = _copy_characters(to, to_start, from, from_start, how_many, 1); |
| 1137 | if (err) { |
| 1138 | PyErr_Format(PyExc_SystemError, |
| 1139 | "Cannot copy %s characters " |
| 1140 | "into a string of %s characters", |
| 1141 | unicode_kind_name(from), |
| 1142 | unicode_kind_name(to)); |
| 1143 | return -1; |
| 1144 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1145 | return how_many; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1146 | } |
| 1147 | |
Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 1148 | /* Find the maximum code point and count the number of surrogate pairs so a |
| 1149 | correct string length can be computed before converting a string to UCS4. |
| 1150 | This function counts single surrogates as a character and not as a pair. |
| 1151 | |
| 1152 | Return 0 on success, or -1 on error. */ |
| 1153 | static int |
| 1154 | find_maxchar_surrogates(const wchar_t *begin, const wchar_t *end, |
| 1155 | Py_UCS4 *maxchar, Py_ssize_t *num_surrogates) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1156 | { |
| 1157 | const wchar_t *iter; |
| 1158 | |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1159 | assert(num_surrogates != NULL && maxchar != NULL); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1160 | *num_surrogates = 0; |
| 1161 | *maxchar = 0; |
| 1162 | |
| 1163 | for (iter = begin; iter < end; ) { |
Victor Stinner | ae86485 | 2011-10-05 14:02:44 +0200 | [diff] [blame] | 1164 | if (*iter > *maxchar) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1165 | *maxchar = *iter; |
Victor Stinner | ae86485 | 2011-10-05 14:02:44 +0200 | [diff] [blame] | 1166 | #if SIZEOF_WCHAR_T != 2 |
| 1167 | if (*maxchar >= 0x10000) |
| 1168 | return 0; |
| 1169 | #endif |
| 1170 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1171 | #if SIZEOF_WCHAR_T == 2 |
| 1172 | if (*iter >= 0xD800 && *iter <= 0xDBFF |
| 1173 | && (iter+1) < end && iter[1] >= 0xDC00 && iter[1] <= 0xDFFF) |
| 1174 | { |
| 1175 | Py_UCS4 surrogate_val; |
| 1176 | surrogate_val = (((iter[0] & 0x3FF)<<10) |
| 1177 | | (iter[1] & 0x3FF)) + 0x10000; |
| 1178 | ++(*num_surrogates); |
| 1179 | if (surrogate_val > *maxchar) |
| 1180 | *maxchar = surrogate_val; |
| 1181 | iter += 2; |
| 1182 | } |
| 1183 | else |
| 1184 | iter++; |
| 1185 | #else |
| 1186 | iter++; |
| 1187 | #endif |
| 1188 | } |
| 1189 | return 0; |
| 1190 | } |
| 1191 | |
| 1192 | #ifdef Py_DEBUG |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 1193 | static int unicode_ready_calls = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1194 | #endif |
| 1195 | |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 1196 | static int |
| 1197 | unicode_ready(PyObject **p_obj, int replace) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1198 | { |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 1199 | PyUnicodeObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1200 | wchar_t *end; |
| 1201 | Py_UCS4 maxchar = 0; |
| 1202 | Py_ssize_t num_surrogates; |
| 1203 | #if SIZEOF_WCHAR_T == 2 |
| 1204 | Py_ssize_t length_wo_surrogates; |
| 1205 | #endif |
| 1206 | |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 1207 | assert(p_obj != NULL); |
| 1208 | unicode = (PyUnicodeObject *)*p_obj; |
| 1209 | |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 1210 | /* _PyUnicode_Ready() is only intended for old-style API usage where |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1211 | strings were created using _PyObject_New() and where no canonical |
| 1212 | representation (the str field) has been set yet aka strings |
| 1213 | which are not yet ready. */ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1214 | assert(_PyUnicode_CHECK(unicode)); |
| 1215 | assert(_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1216 | assert(_PyUnicode_WSTR(unicode) != NULL); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1217 | assert(_PyUnicode_DATA_ANY(unicode) == NULL); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1218 | assert(_PyUnicode_UTF8(unicode) == NULL); |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1219 | /* Actually, it should neither be interned nor be anything else: */ |
| 1220 | assert(_PyUnicode_STATE(unicode).interned == SSTATE_NOT_INTERNED); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1221 | |
| 1222 | #ifdef Py_DEBUG |
| 1223 | ++unicode_ready_calls; |
| 1224 | #endif |
| 1225 | |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 1226 | #ifdef Py_DEBUG |
| 1227 | assert(!replace || Py_REFCNT(unicode) == 1); |
| 1228 | #else |
| 1229 | if (replace && Py_REFCNT(unicode) != 1) |
| 1230 | replace = 0; |
| 1231 | #endif |
| 1232 | if (replace) { |
| 1233 | Py_ssize_t len = _PyUnicode_WSTR_LENGTH(unicode); |
| 1234 | wchar_t *wstr = _PyUnicode_WSTR(unicode); |
| 1235 | /* Optimization for empty strings */ |
| 1236 | if (len == 0) { |
| 1237 | Py_INCREF(unicode_empty); |
| 1238 | Py_DECREF(*p_obj); |
| 1239 | *p_obj = unicode_empty; |
| 1240 | return 0; |
| 1241 | } |
| 1242 | if (len == 1 && wstr[0] < 256) { |
| 1243 | PyObject *latin1_char = get_latin1_char((unsigned char)wstr[0]); |
| 1244 | if (latin1_char == NULL) |
| 1245 | return -1; |
| 1246 | Py_DECREF(*p_obj); |
| 1247 | *p_obj = latin1_char; |
| 1248 | return 0; |
| 1249 | } |
| 1250 | } |
| 1251 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1252 | end = _PyUnicode_WSTR(unicode) + _PyUnicode_WSTR_LENGTH(unicode); |
Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 1253 | if (find_maxchar_surrogates(_PyUnicode_WSTR(unicode), end, |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1254 | &maxchar, &num_surrogates) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1255 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1256 | |
| 1257 | if (maxchar < 256) { |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1258 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(_PyUnicode_WSTR_LENGTH(unicode) + 1); |
| 1259 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1260 | PyErr_NoMemory(); |
| 1261 | return -1; |
| 1262 | } |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1263 | _PyUnicode_CONVERT_BYTES(wchar_t, unsigned char, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1264 | _PyUnicode_WSTR(unicode), end, |
| 1265 | PyUnicode_1BYTE_DATA(unicode)); |
| 1266 | PyUnicode_1BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1267 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1268 | _PyUnicode_STATE(unicode).kind = PyUnicode_1BYTE_KIND; |
| 1269 | if (maxchar < 128) { |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1270 | _PyUnicode_STATE(unicode).ascii = 1; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1271 | _PyUnicode_UTF8(unicode) = _PyUnicode_DATA_ANY(unicode); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1272 | _PyUnicode_UTF8_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1273 | } |
| 1274 | else { |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1275 | _PyUnicode_STATE(unicode).ascii = 0; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1276 | _PyUnicode_UTF8(unicode) = NULL; |
| 1277 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1278 | } |
| 1279 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1280 | _PyUnicode_WSTR(unicode) = NULL; |
| 1281 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1282 | } |
| 1283 | /* In this case we might have to convert down from 4-byte native |
| 1284 | wchar_t to 2-byte unicode. */ |
| 1285 | else if (maxchar < 65536) { |
| 1286 | assert(num_surrogates == 0 && |
| 1287 | "FindMaxCharAndNumSurrogatePairs() messed up"); |
| 1288 | |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1289 | #if SIZEOF_WCHAR_T == 2 |
| 1290 | /* We can share representations and are done. */ |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1291 | _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1292 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1293 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1294 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1295 | _PyUnicode_UTF8(unicode) = NULL; |
| 1296 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1297 | #else |
| 1298 | /* sizeof(wchar_t) == 4 */ |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1299 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC( |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1300 | 2 * (_PyUnicode_WSTR_LENGTH(unicode) + 1)); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1301 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1302 | PyErr_NoMemory(); |
| 1303 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1304 | } |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1305 | _PyUnicode_CONVERT_BYTES(wchar_t, Py_UCS2, |
| 1306 | _PyUnicode_WSTR(unicode), end, |
| 1307 | PyUnicode_2BYTE_DATA(unicode)); |
| 1308 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1309 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1310 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1311 | _PyUnicode_UTF8(unicode) = NULL; |
| 1312 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1313 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1314 | _PyUnicode_WSTR(unicode) = NULL; |
| 1315 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1316 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1317 | } |
| 1318 | /* maxchar exeeds 16 bit, wee need 4 bytes for unicode characters */ |
| 1319 | else { |
| 1320 | #if SIZEOF_WCHAR_T == 2 |
| 1321 | /* in case the native representation is 2-bytes, we need to allocate a |
| 1322 | new normalized 4-byte version. */ |
| 1323 | length_wo_surrogates = _PyUnicode_WSTR_LENGTH(unicode) - num_surrogates; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1324 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(4 * (length_wo_surrogates + 1)); |
| 1325 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1326 | PyErr_NoMemory(); |
| 1327 | return -1; |
| 1328 | } |
| 1329 | _PyUnicode_LENGTH(unicode) = length_wo_surrogates; |
| 1330 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1331 | _PyUnicode_UTF8(unicode) = NULL; |
| 1332 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 126c559 | 2011-10-03 04:17:10 +0200 | [diff] [blame] | 1333 | /* unicode_convert_wchar_to_ucs4() requires a ready string */ |
| 1334 | _PyUnicode_STATE(unicode).ready = 1; |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1335 | unicode_convert_wchar_to_ucs4(_PyUnicode_WSTR(unicode), end, unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1336 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1337 | _PyUnicode_WSTR(unicode) = NULL; |
| 1338 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1339 | #else |
| 1340 | assert(num_surrogates == 0); |
| 1341 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1342 | _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1343 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1344 | _PyUnicode_UTF8(unicode) = NULL; |
| 1345 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1346 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; |
| 1347 | #endif |
| 1348 | PyUnicode_4BYTE_DATA(unicode)[_PyUnicode_LENGTH(unicode)] = '\0'; |
| 1349 | } |
| 1350 | _PyUnicode_STATE(unicode).ready = 1; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1351 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1352 | return 0; |
| 1353 | } |
| 1354 | |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 1355 | int |
| 1356 | _PyUnicode_ReadyReplace(PyObject **op) |
| 1357 | { |
| 1358 | return unicode_ready(op, 1); |
| 1359 | } |
| 1360 | |
| 1361 | int |
| 1362 | _PyUnicode_Ready(PyObject *op) |
| 1363 | { |
| 1364 | return unicode_ready(&op, 0); |
| 1365 | } |
| 1366 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1367 | static void |
| 1368 | unicode_dealloc(register PyUnicodeObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1369 | { |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1370 | switch (PyUnicode_CHECK_INTERNED(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1371 | case SSTATE_NOT_INTERNED: |
| 1372 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1373 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1374 | case SSTATE_INTERNED_MORTAL: |
| 1375 | /* revive dead object temporarily for DelItem */ |
| 1376 | Py_REFCNT(unicode) = 3; |
| 1377 | if (PyDict_DelItem(interned, (PyObject *)unicode) != 0) |
| 1378 | Py_FatalError( |
| 1379 | "deletion of interned string failed"); |
| 1380 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1381 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1382 | case SSTATE_INTERNED_IMMORTAL: |
| 1383 | Py_FatalError("Immortal interned string died."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1384 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1385 | default: |
| 1386 | Py_FatalError("Inconsistent interned string state."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1387 | } |
| 1388 | |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 1389 | if (_PyUnicode_HAS_WSTR_MEMORY(unicode)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1390 | PyObject_DEL(_PyUnicode_WSTR(unicode)); |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 1391 | if (_PyUnicode_HAS_UTF8_MEMORY(unicode)) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1392 | PyObject_DEL(_PyUnicode_UTF8(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1393 | |
| 1394 | if (PyUnicode_IS_COMPACT(unicode)) { |
| 1395 | Py_TYPE(unicode)->tp_free((PyObject *)unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1396 | } |
| 1397 | else { |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1398 | if (_PyUnicode_DATA_ANY(unicode)) |
| 1399 | PyObject_DEL(_PyUnicode_DATA_ANY(unicode)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1400 | Py_TYPE(unicode)->tp_free((PyObject *)unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1401 | } |
| 1402 | } |
| 1403 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1404 | #ifdef Py_DEBUG |
| 1405 | static int |
| 1406 | unicode_is_singleton(PyObject *unicode) |
| 1407 | { |
| 1408 | PyASCIIObject *ascii = (PyASCIIObject *)unicode; |
| 1409 | if (unicode == unicode_empty) |
| 1410 | return 1; |
| 1411 | if (ascii->state.kind != PyUnicode_WCHAR_KIND && ascii->length == 1) |
| 1412 | { |
| 1413 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, 0); |
| 1414 | if (ch < 256 && unicode_latin1[ch] == unicode) |
| 1415 | return 1; |
| 1416 | } |
| 1417 | return 0; |
| 1418 | } |
| 1419 | #endif |
| 1420 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1421 | static int |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1422 | unicode_resizable(PyObject *unicode) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1423 | { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1424 | if (Py_REFCNT(unicode) != 1) |
| 1425 | return 0; |
| 1426 | if (PyUnicode_CHECK_INTERNED(unicode)) |
| 1427 | return 0; |
Victor Stinner | 77bb47b | 2011-10-03 20:06:05 +0200 | [diff] [blame] | 1428 | #ifdef Py_DEBUG |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1429 | /* singleton refcount is greater than 1 */ |
| 1430 | assert(!unicode_is_singleton(unicode)); |
Victor Stinner | 77bb47b | 2011-10-03 20:06:05 +0200 | [diff] [blame] | 1431 | #endif |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1432 | return 1; |
| 1433 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1434 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1435 | static int |
| 1436 | unicode_resize(PyObject **p_unicode, Py_ssize_t length) |
| 1437 | { |
| 1438 | PyObject *unicode; |
| 1439 | Py_ssize_t old_length; |
| 1440 | |
| 1441 | assert(p_unicode != NULL); |
| 1442 | unicode = *p_unicode; |
| 1443 | |
| 1444 | assert(unicode != NULL); |
| 1445 | assert(PyUnicode_Check(unicode)); |
| 1446 | assert(0 <= length); |
| 1447 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1448 | if (_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1449 | old_length = PyUnicode_WSTR_LENGTH(unicode); |
| 1450 | else |
| 1451 | old_length = PyUnicode_GET_LENGTH(unicode); |
| 1452 | if (old_length == length) |
| 1453 | return 0; |
| 1454 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1455 | if (!unicode_resizable(unicode)) { |
| 1456 | PyObject *copy = resize_copy(unicode, length); |
| 1457 | if (copy == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1458 | return -1; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1459 | Py_DECREF(*p_unicode); |
| 1460 | *p_unicode = copy; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1461 | return 0; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1462 | } |
| 1463 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1464 | if (PyUnicode_IS_COMPACT(unicode)) { |
| 1465 | *p_unicode = resize_compact(unicode, length); |
| 1466 | if (*p_unicode == NULL) |
| 1467 | return -1; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1468 | assert(_PyUnicode_CheckConsistency(*p_unicode, 0)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1469 | return 0; |
Benjamin Peterson | 4bfce8f | 2011-10-03 19:35:07 -0400 | [diff] [blame] | 1470 | } |
| 1471 | return resize_inplace((PyUnicodeObject*)unicode, length); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1472 | } |
| 1473 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1474 | int |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1475 | PyUnicode_Resize(PyObject **p_unicode, Py_ssize_t length) |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1476 | { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1477 | PyObject *unicode; |
| 1478 | if (p_unicode == NULL) { |
| 1479 | PyErr_BadInternalCall(); |
| 1480 | return -1; |
| 1481 | } |
| 1482 | unicode = *p_unicode; |
| 1483 | if (unicode == NULL || !PyUnicode_Check(unicode) || length < 0 |
| 1484 | || _PyUnicode_KIND(unicode) != PyUnicode_WCHAR_KIND) |
| 1485 | { |
| 1486 | PyErr_BadInternalCall(); |
| 1487 | return -1; |
| 1488 | } |
| 1489 | return unicode_resize(p_unicode, length); |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1490 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1491 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1492 | static PyObject* |
| 1493 | get_latin1_char(unsigned char ch) |
| 1494 | { |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1495 | PyObject *unicode = unicode_latin1[ch]; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1496 | if (!unicode) { |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1497 | unicode = PyUnicode_New(1, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1498 | if (!unicode) |
| 1499 | return NULL; |
| 1500 | PyUnicode_1BYTE_DATA(unicode)[0] = ch; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1501 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1502 | unicode_latin1[ch] = unicode; |
| 1503 | } |
| 1504 | Py_INCREF(unicode); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1505 | return unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1506 | } |
| 1507 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1508 | PyObject * |
| 1509 | PyUnicode_FromUnicode(const Py_UNICODE *u, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1510 | { |
| 1511 | PyUnicodeObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1512 | Py_UCS4 maxchar = 0; |
| 1513 | Py_ssize_t num_surrogates; |
| 1514 | |
| 1515 | if (u == NULL) |
| 1516 | return (PyObject*)_PyUnicode_New(size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1517 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1518 | /* If the Unicode data is known at construction time, we can apply |
| 1519 | some optimizations which share commonly used objects. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1520 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1521 | /* Optimization for empty strings */ |
| 1522 | if (size == 0 && unicode_empty != NULL) { |
| 1523 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1524 | return unicode_empty; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1525 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1526 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1527 | /* Single character Unicode objects in the Latin-1 range are |
| 1528 | shared when using this constructor */ |
| 1529 | if (size == 1 && *u < 256) |
| 1530 | return get_latin1_char((unsigned char)*u); |
| 1531 | |
| 1532 | /* If not empty and not single character, copy the Unicode data |
| 1533 | into the new object */ |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1534 | if (find_maxchar_surrogates(u, u + size, |
| 1535 | &maxchar, &num_surrogates) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1536 | return NULL; |
| 1537 | |
| 1538 | unicode = (PyUnicodeObject *) PyUnicode_New(size - num_surrogates, |
| 1539 | maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1540 | if (!unicode) |
| 1541 | return NULL; |
| 1542 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1543 | switch (PyUnicode_KIND(unicode)) { |
| 1544 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1545 | _PyUnicode_CONVERT_BYTES(Py_UNICODE, unsigned char, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1546 | u, u + size, PyUnicode_1BYTE_DATA(unicode)); |
| 1547 | break; |
| 1548 | case PyUnicode_2BYTE_KIND: |
| 1549 | #if Py_UNICODE_SIZE == 2 |
| 1550 | Py_MEMCPY(PyUnicode_2BYTE_DATA(unicode), u, size * 2); |
| 1551 | #else |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1552 | _PyUnicode_CONVERT_BYTES(Py_UNICODE, Py_UCS2, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1553 | u, u + size, PyUnicode_2BYTE_DATA(unicode)); |
| 1554 | #endif |
| 1555 | break; |
| 1556 | case PyUnicode_4BYTE_KIND: |
| 1557 | #if SIZEOF_WCHAR_T == 2 |
| 1558 | /* This is the only case which has to process surrogates, thus |
| 1559 | a simple copy loop is not enough and we need a function. */ |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1560 | unicode_convert_wchar_to_ucs4(u, u + size, unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1561 | #else |
| 1562 | assert(num_surrogates == 0); |
| 1563 | Py_MEMCPY(PyUnicode_4BYTE_DATA(unicode), u, size * 4); |
| 1564 | #endif |
| 1565 | break; |
| 1566 | default: |
| 1567 | assert(0 && "Impossible state"); |
| 1568 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1569 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1570 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1571 | return (PyObject *)unicode; |
| 1572 | } |
| 1573 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1574 | PyObject * |
| 1575 | PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size) |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1576 | { |
| 1577 | PyUnicodeObject *unicode; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1578 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1579 | if (size < 0) { |
| 1580 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1581 | "Negative size passed to PyUnicode_FromStringAndSize"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1582 | return NULL; |
| 1583 | } |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1584 | |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1585 | /* If the Unicode data is known at construction time, we can apply |
Martin v. Löwis | 9c12106 | 2007-08-05 20:26:11 +0000 | [diff] [blame] | 1586 | some optimizations which share commonly used objects. |
| 1587 | Also, this means the input must be UTF-8, so fall back to the |
| 1588 | UTF-8 decoder at the end. */ |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1589 | if (u != NULL) { |
| 1590 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1591 | /* Optimization for empty strings */ |
| 1592 | if (size == 0 && unicode_empty != NULL) { |
| 1593 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1594 | return unicode_empty; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1595 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1596 | |
| 1597 | /* Single characters are shared when using this constructor. |
| 1598 | Restrict to ASCII, since the input must be UTF-8. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1599 | if (size == 1 && Py_CHARMASK(*u) < 128) |
| 1600 | return get_latin1_char(Py_CHARMASK(*u)); |
Martin v. Löwis | 9c12106 | 2007-08-05 20:26:11 +0000 | [diff] [blame] | 1601 | |
| 1602 | return PyUnicode_DecodeUTF8(u, size, NULL); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1603 | } |
| 1604 | |
Walter Dörwald | 5550731 | 2007-05-18 13:12:10 +0000 | [diff] [blame] | 1605 | unicode = _PyUnicode_New(size); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1606 | if (!unicode) |
| 1607 | return NULL; |
| 1608 | |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1609 | return (PyObject *)unicode; |
| 1610 | } |
| 1611 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1612 | PyObject * |
| 1613 | PyUnicode_FromString(const char *u) |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1614 | { |
| 1615 | size_t size = strlen(u); |
| 1616 | if (size > PY_SSIZE_T_MAX) { |
| 1617 | PyErr_SetString(PyExc_OverflowError, "input too long"); |
| 1618 | return NULL; |
| 1619 | } |
| 1620 | |
| 1621 | return PyUnicode_FromStringAndSize(u, size); |
| 1622 | } |
| 1623 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1624 | PyObject * |
| 1625 | _PyUnicode_FromId(_Py_Identifier *id) |
| 1626 | { |
| 1627 | if (!id->object) { |
| 1628 | id->object = PyUnicode_FromString(id->string); |
| 1629 | if (!id->object) |
| 1630 | return NULL; |
| 1631 | PyUnicode_InternInPlace(&id->object); |
| 1632 | assert(!id->next); |
| 1633 | id->next = static_strings; |
| 1634 | static_strings = id; |
| 1635 | } |
| 1636 | Py_INCREF(id->object); |
| 1637 | return id->object; |
| 1638 | } |
| 1639 | |
| 1640 | void |
| 1641 | _PyUnicode_ClearStaticStrings() |
| 1642 | { |
| 1643 | _Py_Identifier *i; |
| 1644 | for (i = static_strings; i; i = i->next) { |
| 1645 | Py_DECREF(i->object); |
| 1646 | i->object = NULL; |
| 1647 | i->next = NULL; |
| 1648 | } |
| 1649 | } |
| 1650 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1651 | static PyObject* |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1652 | unicode_fromascii(const unsigned char* s, Py_ssize_t size) |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1653 | { |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1654 | PyObject *res; |
| 1655 | #ifdef Py_DEBUG |
| 1656 | const unsigned char *p; |
| 1657 | const unsigned char *end = s + size; |
| 1658 | for (p=s; p < end; p++) { |
| 1659 | assert(*p < 128); |
| 1660 | } |
| 1661 | #endif |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1662 | if (size == 1) |
| 1663 | return get_latin1_char(s[0]); |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1664 | res = PyUnicode_New(size, 127); |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1665 | if (!res) |
| 1666 | return NULL; |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1667 | memcpy(PyUnicode_1BYTE_DATA(res), s, size); |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1668 | return res; |
| 1669 | } |
| 1670 | |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 1671 | static Py_UCS4 |
| 1672 | kind_maxchar_limit(unsigned int kind) |
| 1673 | { |
| 1674 | switch(kind) { |
| 1675 | case PyUnicode_1BYTE_KIND: |
| 1676 | return 0x80; |
| 1677 | case PyUnicode_2BYTE_KIND: |
| 1678 | return 0x100; |
| 1679 | case PyUnicode_4BYTE_KIND: |
| 1680 | return 0x10000; |
| 1681 | default: |
| 1682 | assert(0 && "invalid kind"); |
| 1683 | return 0x10ffff; |
| 1684 | } |
| 1685 | } |
| 1686 | |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1687 | static PyObject* |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1688 | _PyUnicode_FromUCS1(const unsigned char* u, Py_ssize_t size) |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1689 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1690 | PyObject *res; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1691 | unsigned char max_char = 127; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1692 | Py_ssize_t i; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1693 | |
| 1694 | assert(size >= 0); |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1695 | if (size == 1) |
| 1696 | return get_latin1_char(u[0]); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1697 | for (i = 0; i < size; i++) { |
| 1698 | if (u[i] & 0x80) { |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1699 | max_char = 255; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1700 | break; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1701 | } |
| 1702 | } |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1703 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1704 | if (!res) |
| 1705 | return NULL; |
| 1706 | memcpy(PyUnicode_1BYTE_DATA(res), u, size); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1707 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1708 | return res; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1709 | } |
| 1710 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1711 | static PyObject* |
| 1712 | _PyUnicode_FromUCS2(const Py_UCS2 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1713 | { |
| 1714 | PyObject *res; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1715 | Py_UCS2 max_char = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1716 | Py_ssize_t i; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1717 | |
| 1718 | assert(size >= 0); |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1719 | if (size == 1 && u[0] < 256) |
| 1720 | return get_latin1_char(u[0]); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1721 | for (i = 0; i < size; i++) { |
| 1722 | if (u[i] > max_char) { |
| 1723 | max_char = u[i]; |
| 1724 | if (max_char >= 256) |
| 1725 | break; |
| 1726 | } |
| 1727 | } |
| 1728 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1729 | if (!res) |
| 1730 | return NULL; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1731 | if (max_char >= 256) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1732 | memcpy(PyUnicode_2BYTE_DATA(res), u, sizeof(Py_UCS2)*size); |
| 1733 | else |
| 1734 | for (i = 0; i < size; i++) |
| 1735 | PyUnicode_1BYTE_DATA(res)[i] = (Py_UCS1)u[i]; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1736 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1737 | return res; |
| 1738 | } |
| 1739 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1740 | static PyObject* |
| 1741 | _PyUnicode_FromUCS4(const Py_UCS4 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1742 | { |
| 1743 | PyObject *res; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1744 | Py_UCS4 max_char = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1745 | Py_ssize_t i; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1746 | |
| 1747 | assert(size >= 0); |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1748 | if (size == 1 && u[0] < 256) |
| 1749 | return get_latin1_char(u[0]); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1750 | for (i = 0; i < size; i++) { |
| 1751 | if (u[i] > max_char) { |
| 1752 | max_char = u[i]; |
| 1753 | if (max_char >= 0x10000) |
| 1754 | break; |
| 1755 | } |
| 1756 | } |
| 1757 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1758 | if (!res) |
| 1759 | return NULL; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1760 | if (max_char >= 0x10000) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1761 | memcpy(PyUnicode_4BYTE_DATA(res), u, sizeof(Py_UCS4)*size); |
| 1762 | else { |
| 1763 | int kind = PyUnicode_KIND(res); |
| 1764 | void *data = PyUnicode_DATA(res); |
| 1765 | for (i = 0; i < size; i++) |
| 1766 | PyUnicode_WRITE(kind, data, i, u[i]); |
| 1767 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1768 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1769 | return res; |
| 1770 | } |
| 1771 | |
| 1772 | PyObject* |
| 1773 | PyUnicode_FromKindAndData(int kind, const void *buffer, Py_ssize_t size) |
| 1774 | { |
| 1775 | switch(kind) { |
| 1776 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1777 | return _PyUnicode_FromUCS1(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1778 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1779 | return _PyUnicode_FromUCS2(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1780 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1781 | return _PyUnicode_FromUCS4(buffer, size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1782 | default: |
| 1783 | assert(0 && "invalid kind"); |
| 1784 | PyErr_SetString(PyExc_SystemError, "invalid kind"); |
| 1785 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1786 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1787 | } |
| 1788 | |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1789 | /* Ensure that a string uses the most efficient storage, if it is not the |
| 1790 | case: create a new string with of the right kind. Write NULL into *p_unicode |
| 1791 | on error. */ |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 1792 | static void |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1793 | unicode_adjust_maxchar(PyObject **p_unicode) |
| 1794 | { |
| 1795 | PyObject *unicode, *copy; |
| 1796 | Py_UCS4 max_char; |
| 1797 | Py_ssize_t i, len; |
| 1798 | unsigned int kind; |
| 1799 | |
| 1800 | assert(p_unicode != NULL); |
| 1801 | unicode = *p_unicode; |
| 1802 | assert(PyUnicode_IS_READY(unicode)); |
| 1803 | if (PyUnicode_IS_ASCII(unicode)) |
| 1804 | return; |
| 1805 | |
| 1806 | len = PyUnicode_GET_LENGTH(unicode); |
| 1807 | kind = PyUnicode_KIND(unicode); |
| 1808 | if (kind == PyUnicode_1BYTE_KIND) { |
| 1809 | const Py_UCS1 *u = PyUnicode_1BYTE_DATA(unicode); |
| 1810 | for (i = 0; i < len; i++) { |
| 1811 | if (u[i] & 0x80) |
| 1812 | return; |
| 1813 | } |
| 1814 | max_char = 127; |
| 1815 | } |
| 1816 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 1817 | const Py_UCS2 *u = PyUnicode_2BYTE_DATA(unicode); |
| 1818 | max_char = 0; |
| 1819 | for (i = 0; i < len; i++) { |
| 1820 | if (u[i] > max_char) { |
| 1821 | max_char = u[i]; |
| 1822 | if (max_char >= 256) |
| 1823 | return; |
| 1824 | } |
| 1825 | } |
| 1826 | } |
| 1827 | else { |
Antoine Pitrou | 15a66cf | 2011-10-06 15:25:32 +0200 | [diff] [blame] | 1828 | const Py_UCS4 *u; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1829 | assert(kind == PyUnicode_4BYTE_KIND); |
Antoine Pitrou | 15a66cf | 2011-10-06 15:25:32 +0200 | [diff] [blame] | 1830 | u = PyUnicode_4BYTE_DATA(unicode); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1831 | max_char = 0; |
| 1832 | for (i = 0; i < len; i++) { |
| 1833 | if (u[i] > max_char) { |
| 1834 | max_char = u[i]; |
| 1835 | if (max_char >= 0x10000) |
| 1836 | return; |
| 1837 | } |
| 1838 | } |
| 1839 | } |
Victor Stinner | 200f213 | 2011-10-06 13:27:56 +0200 | [diff] [blame] | 1840 | assert(max_char < PyUnicode_MAX_CHAR_VALUE(unicode)); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1841 | copy = PyUnicode_New(len, max_char); |
| 1842 | copy_characters(copy, 0, unicode, 0, len); |
| 1843 | Py_DECREF(unicode); |
| 1844 | *p_unicode = copy; |
| 1845 | } |
| 1846 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 1847 | PyObject* |
| 1848 | PyUnicode_Copy(PyObject *unicode) |
| 1849 | { |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 1850 | Py_ssize_t size; |
| 1851 | PyObject *copy; |
| 1852 | void *data; |
| 1853 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 1854 | if (!PyUnicode_Check(unicode)) { |
| 1855 | PyErr_BadInternalCall(); |
| 1856 | return NULL; |
| 1857 | } |
| 1858 | if (PyUnicode_READY(unicode)) |
| 1859 | return NULL; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 1860 | |
| 1861 | size = PyUnicode_GET_LENGTH(unicode); |
| 1862 | copy = PyUnicode_New(size, PyUnicode_MAX_CHAR_VALUE(unicode)); |
| 1863 | if (!copy) |
| 1864 | return NULL; |
| 1865 | assert(PyUnicode_KIND(copy) == PyUnicode_KIND(unicode)); |
| 1866 | |
| 1867 | data = PyUnicode_DATA(unicode); |
| 1868 | switch (PyUnicode_KIND(unicode)) |
| 1869 | { |
| 1870 | case PyUnicode_1BYTE_KIND: |
| 1871 | memcpy(PyUnicode_1BYTE_DATA(copy), data, size); |
| 1872 | break; |
| 1873 | case PyUnicode_2BYTE_KIND: |
| 1874 | memcpy(PyUnicode_2BYTE_DATA(copy), data, sizeof(Py_UCS2) * size); |
| 1875 | break; |
| 1876 | case PyUnicode_4BYTE_KIND: |
| 1877 | memcpy(PyUnicode_4BYTE_DATA(copy), data, sizeof(Py_UCS4) * size); |
| 1878 | break; |
| 1879 | default: |
| 1880 | assert(0); |
| 1881 | break; |
| 1882 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1883 | assert(_PyUnicode_CheckConsistency(copy, 1)); |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 1884 | return copy; |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 1885 | } |
| 1886 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1887 | |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1888 | /* Widen Unicode objects to larger buffers. Don't write terminating null |
| 1889 | character. Return NULL on error. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1890 | |
| 1891 | void* |
| 1892 | _PyUnicode_AsKind(PyObject *s, unsigned int kind) |
| 1893 | { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1894 | Py_ssize_t len; |
| 1895 | void *result; |
| 1896 | unsigned int skind; |
| 1897 | |
| 1898 | if (PyUnicode_READY(s)) |
| 1899 | return NULL; |
| 1900 | |
| 1901 | len = PyUnicode_GET_LENGTH(s); |
| 1902 | skind = PyUnicode_KIND(s); |
| 1903 | if (skind >= kind) { |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 1904 | PyErr_SetString(PyExc_SystemError, "invalid widening attempt"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1905 | return NULL; |
| 1906 | } |
| 1907 | switch(kind) { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1908 | case PyUnicode_2BYTE_KIND: |
| 1909 | result = PyMem_Malloc(len * sizeof(Py_UCS2)); |
| 1910 | if (!result) |
| 1911 | return PyErr_NoMemory(); |
| 1912 | assert(skind == PyUnicode_1BYTE_KIND); |
| 1913 | _PyUnicode_CONVERT_BYTES( |
| 1914 | Py_UCS1, Py_UCS2, |
| 1915 | PyUnicode_1BYTE_DATA(s), |
| 1916 | PyUnicode_1BYTE_DATA(s) + len, |
| 1917 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1918 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1919 | case PyUnicode_4BYTE_KIND: |
| 1920 | result = PyMem_Malloc(len * sizeof(Py_UCS4)); |
| 1921 | if (!result) |
| 1922 | return PyErr_NoMemory(); |
| 1923 | if (skind == PyUnicode_2BYTE_KIND) { |
| 1924 | _PyUnicode_CONVERT_BYTES( |
| 1925 | Py_UCS2, Py_UCS4, |
| 1926 | PyUnicode_2BYTE_DATA(s), |
| 1927 | PyUnicode_2BYTE_DATA(s) + len, |
| 1928 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1929 | } |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1930 | else { |
| 1931 | assert(skind == PyUnicode_1BYTE_KIND); |
| 1932 | _PyUnicode_CONVERT_BYTES( |
| 1933 | Py_UCS1, Py_UCS4, |
| 1934 | PyUnicode_1BYTE_DATA(s), |
| 1935 | PyUnicode_1BYTE_DATA(s) + len, |
| 1936 | result); |
| 1937 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1938 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1939 | default: |
| 1940 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1941 | } |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 1942 | PyErr_SetString(PyExc_SystemError, "invalid kind"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1943 | return NULL; |
| 1944 | } |
| 1945 | |
| 1946 | static Py_UCS4* |
| 1947 | as_ucs4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 1948 | int copy_null) |
| 1949 | { |
| 1950 | int kind; |
| 1951 | void *data; |
| 1952 | Py_ssize_t len, targetlen; |
| 1953 | if (PyUnicode_READY(string) == -1) |
| 1954 | return NULL; |
| 1955 | kind = PyUnicode_KIND(string); |
| 1956 | data = PyUnicode_DATA(string); |
| 1957 | len = PyUnicode_GET_LENGTH(string); |
| 1958 | targetlen = len; |
| 1959 | if (copy_null) |
| 1960 | targetlen++; |
| 1961 | if (!target) { |
| 1962 | if (PY_SSIZE_T_MAX / sizeof(Py_UCS4) < targetlen) { |
| 1963 | PyErr_NoMemory(); |
| 1964 | return NULL; |
| 1965 | } |
| 1966 | target = PyMem_Malloc(targetlen * sizeof(Py_UCS4)); |
| 1967 | if (!target) { |
| 1968 | PyErr_NoMemory(); |
| 1969 | return NULL; |
| 1970 | } |
| 1971 | } |
| 1972 | else { |
| 1973 | if (targetsize < targetlen) { |
| 1974 | PyErr_Format(PyExc_SystemError, |
| 1975 | "string is longer than the buffer"); |
| 1976 | if (copy_null && 0 < targetsize) |
| 1977 | target[0] = 0; |
| 1978 | return NULL; |
| 1979 | } |
| 1980 | } |
| 1981 | if (kind != PyUnicode_4BYTE_KIND) { |
| 1982 | Py_ssize_t i; |
| 1983 | for (i = 0; i < len; i++) |
| 1984 | target[i] = PyUnicode_READ(kind, data, i); |
| 1985 | } |
| 1986 | else |
| 1987 | Py_MEMCPY(target, data, len * sizeof(Py_UCS4)); |
| 1988 | if (copy_null) |
| 1989 | target[len] = 0; |
| 1990 | return target; |
| 1991 | } |
| 1992 | |
| 1993 | Py_UCS4* |
| 1994 | PyUnicode_AsUCS4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 1995 | int copy_null) |
| 1996 | { |
| 1997 | if (target == NULL || targetsize < 1) { |
| 1998 | PyErr_BadInternalCall(); |
| 1999 | return NULL; |
| 2000 | } |
| 2001 | return as_ucs4(string, target, targetsize, copy_null); |
| 2002 | } |
| 2003 | |
| 2004 | Py_UCS4* |
| 2005 | PyUnicode_AsUCS4Copy(PyObject *string) |
| 2006 | { |
| 2007 | return as_ucs4(string, NULL, 0, 1); |
| 2008 | } |
| 2009 | |
| 2010 | #ifdef HAVE_WCHAR_H |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 2011 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2012 | PyObject * |
| 2013 | PyUnicode_FromWideChar(register const wchar_t *w, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2014 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2015 | if (w == NULL) { |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 2016 | if (size == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2017 | return PyUnicode_New(0, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2018 | PyErr_BadInternalCall(); |
| 2019 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2020 | } |
| 2021 | |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 2022 | if (size == -1) { |
| 2023 | size = wcslen(w); |
| 2024 | } |
| 2025 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2026 | return PyUnicode_FromUnicode(w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2027 | } |
| 2028 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2029 | #endif /* HAVE_WCHAR_H */ |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 2030 | |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2031 | static void |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2032 | makefmt(char *fmt, int longflag, int longlongflag, int size_tflag, |
| 2033 | int zeropad, int width, int precision, char c) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2034 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2035 | *fmt++ = '%'; |
| 2036 | if (width) { |
| 2037 | if (zeropad) |
| 2038 | *fmt++ = '0'; |
| 2039 | fmt += sprintf(fmt, "%d", width); |
| 2040 | } |
| 2041 | if (precision) |
| 2042 | fmt += sprintf(fmt, ".%d", precision); |
| 2043 | if (longflag) |
| 2044 | *fmt++ = 'l'; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2045 | else if (longlongflag) { |
| 2046 | /* longlongflag should only ever be nonzero on machines with |
| 2047 | HAVE_LONG_LONG defined */ |
| 2048 | #ifdef HAVE_LONG_LONG |
| 2049 | char *f = PY_FORMAT_LONG_LONG; |
| 2050 | while (*f) |
| 2051 | *fmt++ = *f++; |
| 2052 | #else |
| 2053 | /* we shouldn't ever get here */ |
| 2054 | assert(0); |
| 2055 | *fmt++ = 'l'; |
| 2056 | #endif |
| 2057 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2058 | else if (size_tflag) { |
| 2059 | char *f = PY_FORMAT_SIZE_T; |
| 2060 | while (*f) |
| 2061 | *fmt++ = *f++; |
| 2062 | } |
| 2063 | *fmt++ = c; |
| 2064 | *fmt = '\0'; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2065 | } |
| 2066 | |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2067 | /* helper for PyUnicode_FromFormatV() */ |
| 2068 | |
| 2069 | static const char* |
| 2070 | parse_format_flags(const char *f, |
| 2071 | int *p_width, int *p_precision, |
| 2072 | int *p_longflag, int *p_longlongflag, int *p_size_tflag) |
| 2073 | { |
| 2074 | int width, precision, longflag, longlongflag, size_tflag; |
| 2075 | |
| 2076 | /* parse the width.precision part, e.g. "%2.5s" => width=2, precision=5 */ |
| 2077 | f++; |
| 2078 | width = 0; |
| 2079 | while (Py_ISDIGIT((unsigned)*f)) |
| 2080 | width = (width*10) + *f++ - '0'; |
| 2081 | precision = 0; |
| 2082 | if (*f == '.') { |
| 2083 | f++; |
| 2084 | while (Py_ISDIGIT((unsigned)*f)) |
| 2085 | precision = (precision*10) + *f++ - '0'; |
| 2086 | if (*f == '%') { |
| 2087 | /* "%.3%s" => f points to "3" */ |
| 2088 | f--; |
| 2089 | } |
| 2090 | } |
| 2091 | if (*f == '\0') { |
| 2092 | /* bogus format "%.1" => go backward, f points to "1" */ |
| 2093 | f--; |
| 2094 | } |
| 2095 | if (p_width != NULL) |
| 2096 | *p_width = width; |
| 2097 | if (p_precision != NULL) |
| 2098 | *p_precision = precision; |
| 2099 | |
| 2100 | /* Handle %ld, %lu, %lld and %llu. */ |
| 2101 | longflag = 0; |
| 2102 | longlongflag = 0; |
Victor Stinner | e7faec1 | 2011-03-02 00:01:53 +0000 | [diff] [blame] | 2103 | size_tflag = 0; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2104 | |
| 2105 | if (*f == 'l') { |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2106 | if (f[1] == 'd' || f[1] == 'u' || f[1] == 'i') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2107 | longflag = 1; |
| 2108 | ++f; |
| 2109 | } |
| 2110 | #ifdef HAVE_LONG_LONG |
| 2111 | else if (f[1] == 'l' && |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2112 | (f[2] == 'd' || f[2] == 'u' || f[2] == 'i')) { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2113 | longlongflag = 1; |
| 2114 | f += 2; |
| 2115 | } |
| 2116 | #endif |
| 2117 | } |
| 2118 | /* handle the size_t flag. */ |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2119 | 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] | 2120 | size_tflag = 1; |
| 2121 | ++f; |
| 2122 | } |
| 2123 | if (p_longflag != NULL) |
| 2124 | *p_longflag = longflag; |
| 2125 | if (p_longlongflag != NULL) |
| 2126 | *p_longlongflag = longlongflag; |
| 2127 | if (p_size_tflag != NULL) |
| 2128 | *p_size_tflag = size_tflag; |
| 2129 | return f; |
| 2130 | } |
| 2131 | |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2132 | /* maximum number of characters required for output of %ld. 21 characters |
| 2133 | allows for 64-bit integers (in decimal) and an optional sign. */ |
| 2134 | #define MAX_LONG_CHARS 21 |
| 2135 | /* maximum number of characters required for output of %lld. |
| 2136 | We need at most ceil(log10(256)*SIZEOF_LONG_LONG) digits, |
| 2137 | plus 1 for the sign. 53/22 is an upper bound for log10(256). */ |
| 2138 | #define MAX_LONG_LONG_CHARS (2 + (SIZEOF_LONG_LONG*53-1) / 22) |
| 2139 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2140 | PyObject * |
| 2141 | PyUnicode_FromFormatV(const char *format, va_list vargs) |
| 2142 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2143 | va_list count; |
| 2144 | Py_ssize_t callcount = 0; |
| 2145 | PyObject **callresults = NULL; |
| 2146 | PyObject **callresult = NULL; |
| 2147 | Py_ssize_t n = 0; |
| 2148 | int width = 0; |
| 2149 | int precision = 0; |
| 2150 | int zeropad; |
| 2151 | const char* f; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2152 | PyObject *string; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2153 | /* used by sprintf */ |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2154 | char fmt[61]; /* should be enough for %0width.precisionlld */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2155 | Py_UCS4 maxchar = 127; /* result is ASCII by default */ |
| 2156 | Py_UCS4 argmaxchar; |
| 2157 | Py_ssize_t numbersize = 0; |
| 2158 | char *numberresults = NULL; |
| 2159 | char *numberresult = NULL; |
| 2160 | Py_ssize_t i; |
| 2161 | int kind; |
| 2162 | void *data; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2163 | |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 2164 | Py_VA_COPY(count, vargs); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2165 | /* step 1: count the number of %S/%R/%A/%s format specifications |
| 2166 | * (we call PyObject_Str()/PyObject_Repr()/PyObject_ASCII()/ |
| 2167 | * PyUnicode_DecodeUTF8() for these objects once during step 3 and put the |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2168 | * result in an array) |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 2169 | * also estimate a upper bound for all the number formats in the string, |
| 2170 | * numbers will be formatted in step 3 and be kept in a '\0'-separated |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2171 | * buffer before putting everything together. */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2172 | for (f = format; *f; f++) { |
| 2173 | if (*f == '%') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2174 | int longlongflag; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2175 | /* skip width or width.precision (eg. "1.2" of "%1.2f") */ |
| 2176 | f = parse_format_flags(f, &width, NULL, NULL, &longlongflag, NULL); |
| 2177 | if (*f == 's' || *f=='S' || *f=='R' || *f=='A' || *f=='V') |
| 2178 | ++callcount; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2179 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2180 | else if (*f == 'd' || *f=='u' || *f=='i' || *f=='x' || *f=='p') { |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2181 | #ifdef HAVE_LONG_LONG |
| 2182 | if (longlongflag) { |
| 2183 | if (width < MAX_LONG_LONG_CHARS) |
| 2184 | width = MAX_LONG_LONG_CHARS; |
| 2185 | } |
| 2186 | else |
| 2187 | #endif |
| 2188 | /* MAX_LONG_CHARS is enough to hold a 64-bit integer, |
| 2189 | including sign. Decimal takes the most space. This |
| 2190 | isn't enough for octal. If a width is specified we |
| 2191 | need more (which we allocate later). */ |
| 2192 | if (width < MAX_LONG_CHARS) |
| 2193 | width = MAX_LONG_CHARS; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2194 | |
| 2195 | /* account for the size + '\0' to separate numbers |
| 2196 | inside of the numberresults buffer */ |
| 2197 | numbersize += (width + 1); |
| 2198 | } |
| 2199 | } |
| 2200 | else if ((unsigned char)*f > 127) { |
| 2201 | PyErr_Format(PyExc_ValueError, |
| 2202 | "PyUnicode_FromFormatV() expects an ASCII-encoded format " |
| 2203 | "string, got a non-ASCII byte: 0x%02x", |
| 2204 | (unsigned char)*f); |
| 2205 | return NULL; |
| 2206 | } |
| 2207 | } |
| 2208 | /* step 2: allocate memory for the results of |
| 2209 | * PyObject_Str()/PyObject_Repr()/PyUnicode_DecodeUTF8() calls */ |
| 2210 | if (callcount) { |
| 2211 | callresults = PyObject_Malloc(sizeof(PyObject *) * callcount); |
| 2212 | if (!callresults) { |
| 2213 | PyErr_NoMemory(); |
| 2214 | return NULL; |
| 2215 | } |
| 2216 | callresult = callresults; |
| 2217 | } |
| 2218 | /* step 2.5: allocate memory for the results of formating numbers */ |
| 2219 | if (numbersize) { |
| 2220 | numberresults = PyObject_Malloc(numbersize); |
| 2221 | if (!numberresults) { |
| 2222 | PyErr_NoMemory(); |
| 2223 | goto fail; |
| 2224 | } |
| 2225 | numberresult = numberresults; |
| 2226 | } |
| 2227 | |
| 2228 | /* step 3: format numbers and figure out how large a buffer we need */ |
| 2229 | for (f = format; *f; f++) { |
| 2230 | if (*f == '%') { |
| 2231 | const char* p; |
| 2232 | int longflag; |
| 2233 | int longlongflag; |
| 2234 | int size_tflag; |
| 2235 | int numprinted; |
| 2236 | |
| 2237 | p = f; |
| 2238 | zeropad = (f[1] == '0'); |
| 2239 | f = parse_format_flags(f, &width, &precision, |
| 2240 | &longflag, &longlongflag, &size_tflag); |
| 2241 | switch (*f) { |
| 2242 | case 'c': |
| 2243 | { |
| 2244 | Py_UCS4 ordinal = va_arg(count, int); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2245 | maxchar = Py_MAX(maxchar, ordinal); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2246 | n++; |
| 2247 | break; |
| 2248 | } |
| 2249 | case '%': |
| 2250 | n++; |
| 2251 | break; |
| 2252 | case 'i': |
| 2253 | case 'd': |
| 2254 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, |
| 2255 | width, precision, *f); |
| 2256 | if (longflag) |
| 2257 | numprinted = sprintf(numberresult, fmt, |
| 2258 | va_arg(count, long)); |
| 2259 | #ifdef HAVE_LONG_LONG |
| 2260 | else if (longlongflag) |
| 2261 | numprinted = sprintf(numberresult, fmt, |
| 2262 | va_arg(count, PY_LONG_LONG)); |
| 2263 | #endif |
| 2264 | else if (size_tflag) |
| 2265 | numprinted = sprintf(numberresult, fmt, |
| 2266 | va_arg(count, Py_ssize_t)); |
| 2267 | else |
| 2268 | numprinted = sprintf(numberresult, fmt, |
| 2269 | va_arg(count, int)); |
| 2270 | n += numprinted; |
| 2271 | /* advance by +1 to skip over the '\0' */ |
| 2272 | numberresult += (numprinted + 1); |
| 2273 | assert(*(numberresult - 1) == '\0'); |
| 2274 | assert(*(numberresult - 2) != '\0'); |
| 2275 | assert(numprinted >= 0); |
| 2276 | assert(numberresult <= numberresults + numbersize); |
| 2277 | break; |
| 2278 | case 'u': |
| 2279 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, |
| 2280 | width, precision, 'u'); |
| 2281 | if (longflag) |
| 2282 | numprinted = sprintf(numberresult, fmt, |
| 2283 | va_arg(count, unsigned long)); |
| 2284 | #ifdef HAVE_LONG_LONG |
| 2285 | else if (longlongflag) |
| 2286 | numprinted = sprintf(numberresult, fmt, |
| 2287 | va_arg(count, unsigned PY_LONG_LONG)); |
| 2288 | #endif |
| 2289 | else if (size_tflag) |
| 2290 | numprinted = sprintf(numberresult, fmt, |
| 2291 | va_arg(count, size_t)); |
| 2292 | else |
| 2293 | numprinted = sprintf(numberresult, fmt, |
| 2294 | va_arg(count, unsigned int)); |
| 2295 | n += numprinted; |
| 2296 | numberresult += (numprinted + 1); |
| 2297 | assert(*(numberresult - 1) == '\0'); |
| 2298 | assert(*(numberresult - 2) != '\0'); |
| 2299 | assert(numprinted >= 0); |
| 2300 | assert(numberresult <= numberresults + numbersize); |
| 2301 | break; |
| 2302 | case 'x': |
| 2303 | makefmt(fmt, 0, 0, 0, zeropad, width, precision, 'x'); |
| 2304 | numprinted = sprintf(numberresult, fmt, va_arg(count, int)); |
| 2305 | n += numprinted; |
| 2306 | numberresult += (numprinted + 1); |
| 2307 | assert(*(numberresult - 1) == '\0'); |
| 2308 | assert(*(numberresult - 2) != '\0'); |
| 2309 | assert(numprinted >= 0); |
| 2310 | assert(numberresult <= numberresults + numbersize); |
| 2311 | break; |
| 2312 | case 'p': |
| 2313 | numprinted = sprintf(numberresult, "%p", va_arg(count, void*)); |
| 2314 | /* %p is ill-defined: ensure leading 0x. */ |
| 2315 | if (numberresult[1] == 'X') |
| 2316 | numberresult[1] = 'x'; |
| 2317 | else if (numberresult[1] != 'x') { |
| 2318 | memmove(numberresult + 2, numberresult, |
| 2319 | strlen(numberresult) + 1); |
| 2320 | numberresult[0] = '0'; |
| 2321 | numberresult[1] = 'x'; |
| 2322 | numprinted += 2; |
| 2323 | } |
| 2324 | n += numprinted; |
| 2325 | numberresult += (numprinted + 1); |
| 2326 | assert(*(numberresult - 1) == '\0'); |
| 2327 | assert(*(numberresult - 2) != '\0'); |
| 2328 | assert(numprinted >= 0); |
| 2329 | assert(numberresult <= numberresults + numbersize); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2330 | break; |
| 2331 | case 's': |
| 2332 | { |
| 2333 | /* UTF-8 */ |
Georg Brandl | 780b2a6 | 2009-05-05 09:19:59 +0000 | [diff] [blame] | 2334 | const char *s = va_arg(count, const char*); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2335 | PyObject *str = PyUnicode_DecodeUTF8(s, strlen(s), "replace"); |
| 2336 | if (!str) |
| 2337 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2338 | /* since PyUnicode_DecodeUTF8 returns already flexible |
| 2339 | unicode objects, there is no need to call ready on them */ |
| 2340 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2341 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2342 | n += PyUnicode_GET_LENGTH(str); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2343 | /* Remember the str and switch to the next slot */ |
| 2344 | *callresult++ = str; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2345 | break; |
| 2346 | } |
| 2347 | case 'U': |
| 2348 | { |
| 2349 | PyObject *obj = va_arg(count, PyObject *); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 2350 | assert(obj && _PyUnicode_CHECK(obj)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2351 | if (PyUnicode_READY(obj) == -1) |
| 2352 | goto fail; |
| 2353 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2354 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2355 | n += PyUnicode_GET_LENGTH(obj); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2356 | break; |
| 2357 | } |
| 2358 | case 'V': |
| 2359 | { |
| 2360 | PyObject *obj = va_arg(count, PyObject *); |
| 2361 | const char *str = va_arg(count, const char *); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2362 | PyObject *str_obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2363 | assert(obj || str); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 2364 | assert(!obj || _PyUnicode_CHECK(obj)); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2365 | if (obj) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2366 | if (PyUnicode_READY(obj) == -1) |
| 2367 | goto fail; |
| 2368 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2369 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2370 | n += PyUnicode_GET_LENGTH(obj); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2371 | *callresult++ = NULL; |
| 2372 | } |
| 2373 | else { |
| 2374 | str_obj = PyUnicode_DecodeUTF8(str, strlen(str), "replace"); |
| 2375 | if (!str_obj) |
| 2376 | goto fail; |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 2377 | if (PyUnicode_READY(str_obj)) { |
| 2378 | Py_DECREF(str_obj); |
| 2379 | goto fail; |
| 2380 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2381 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str_obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2382 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2383 | n += PyUnicode_GET_LENGTH(str_obj); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2384 | *callresult++ = str_obj; |
| 2385 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2386 | break; |
| 2387 | } |
| 2388 | case 'S': |
| 2389 | { |
| 2390 | PyObject *obj = va_arg(count, PyObject *); |
| 2391 | PyObject *str; |
| 2392 | assert(obj); |
| 2393 | str = PyObject_Str(obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2394 | if (!str || PyUnicode_READY(str) == -1) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2395 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2396 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2397 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2398 | n += PyUnicode_GET_LENGTH(str); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2399 | /* Remember the str and switch to the next slot */ |
| 2400 | *callresult++ = str; |
| 2401 | break; |
| 2402 | } |
| 2403 | case 'R': |
| 2404 | { |
| 2405 | PyObject *obj = va_arg(count, PyObject *); |
| 2406 | PyObject *repr; |
| 2407 | assert(obj); |
| 2408 | repr = PyObject_Repr(obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2409 | if (!repr || PyUnicode_READY(repr) == -1) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2410 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2411 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(repr); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2412 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2413 | n += PyUnicode_GET_LENGTH(repr); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2414 | /* Remember the repr and switch to the next slot */ |
| 2415 | *callresult++ = repr; |
| 2416 | break; |
| 2417 | } |
| 2418 | case 'A': |
| 2419 | { |
| 2420 | PyObject *obj = va_arg(count, PyObject *); |
| 2421 | PyObject *ascii; |
| 2422 | assert(obj); |
| 2423 | ascii = PyObject_ASCII(obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2424 | if (!ascii || PyUnicode_READY(ascii) == -1) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2425 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2426 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(ascii); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2427 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2428 | n += PyUnicode_GET_LENGTH(ascii); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2429 | /* Remember the repr and switch to the next slot */ |
| 2430 | *callresult++ = ascii; |
| 2431 | break; |
| 2432 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2433 | default: |
| 2434 | /* if we stumble upon an unknown |
| 2435 | formatting code, copy the rest of |
| 2436 | the format string to the output |
| 2437 | string. (we cannot just skip the |
| 2438 | code, since there's no way to know |
| 2439 | what's in the argument list) */ |
| 2440 | n += strlen(p); |
| 2441 | goto expand; |
| 2442 | } |
| 2443 | } else |
| 2444 | n++; |
| 2445 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2446 | expand: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2447 | /* step 4: fill the buffer */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2448 | /* Since we've analyzed how much space we need, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2449 | we don't have to resize the string. |
| 2450 | There can be no errors beyond this point. */ |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2451 | string = PyUnicode_New(n, maxchar); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2452 | if (!string) |
| 2453 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2454 | kind = PyUnicode_KIND(string); |
| 2455 | data = PyUnicode_DATA(string); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2456 | callresult = callresults; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2457 | numberresult = numberresults; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2458 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2459 | for (i = 0, f = format; *f; f++) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2460 | if (*f == '%') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2461 | const char* p; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2462 | |
| 2463 | p = f; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2464 | f = parse_format_flags(f, NULL, NULL, NULL, NULL, NULL); |
| 2465 | /* checking for == because the last argument could be a empty |
| 2466 | string, which causes i to point to end, the assert at the end of |
| 2467 | the loop */ |
| 2468 | assert(i <= PyUnicode_GET_LENGTH(string)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2469 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2470 | switch (*f) { |
| 2471 | case 'c': |
Victor Stinner | 5ed8b2c | 2011-02-21 21:13:44 +0000 | [diff] [blame] | 2472 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2473 | const int ordinal = va_arg(vargs, int); |
| 2474 | PyUnicode_WRITE(kind, data, i++, ordinal); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2475 | break; |
Victor Stinner | 5ed8b2c | 2011-02-21 21:13:44 +0000 | [diff] [blame] | 2476 | } |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2477 | case 'i': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2478 | case 'd': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2479 | case 'u': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2480 | case 'x': |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2481 | case 'p': |
| 2482 | /* unused, since we already have the result */ |
| 2483 | if (*f == 'p') |
| 2484 | (void) va_arg(vargs, void *); |
| 2485 | else |
| 2486 | (void) va_arg(vargs, int); |
| 2487 | /* extract the result from numberresults and append. */ |
| 2488 | for (; *numberresult; ++i, ++numberresult) |
| 2489 | PyUnicode_WRITE(kind, data, i, *numberresult); |
| 2490 | /* skip over the separating '\0' */ |
| 2491 | assert(*numberresult == '\0'); |
| 2492 | numberresult++; |
| 2493 | assert(numberresult <= numberresults + numbersize); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2494 | break; |
| 2495 | case 's': |
| 2496 | { |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2497 | /* unused, since we already have the result */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2498 | Py_ssize_t size; |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2499 | (void) va_arg(vargs, char *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2500 | size = PyUnicode_GET_LENGTH(*callresult); |
| 2501 | assert(PyUnicode_KIND(*callresult) <= PyUnicode_KIND(string)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2502 | copy_characters(string, i, *callresult, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2503 | i += size; |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2504 | /* We're done with the unicode()/repr() => forget it */ |
| 2505 | Py_DECREF(*callresult); |
| 2506 | /* switch to next unicode()/repr() result */ |
| 2507 | ++callresult; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2508 | break; |
| 2509 | } |
| 2510 | case 'U': |
| 2511 | { |
| 2512 | PyObject *obj = va_arg(vargs, PyObject *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2513 | Py_ssize_t size; |
| 2514 | assert(PyUnicode_KIND(obj) <= PyUnicode_KIND(string)); |
| 2515 | size = PyUnicode_GET_LENGTH(obj); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2516 | copy_characters(string, i, obj, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2517 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2518 | break; |
| 2519 | } |
| 2520 | case 'V': |
| 2521 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2522 | Py_ssize_t size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2523 | PyObject *obj = va_arg(vargs, PyObject *); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2524 | va_arg(vargs, const char *); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2525 | if (obj) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2526 | size = PyUnicode_GET_LENGTH(obj); |
| 2527 | assert(PyUnicode_KIND(obj) <= PyUnicode_KIND(string)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2528 | copy_characters(string, i, obj, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2529 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2530 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2531 | size = PyUnicode_GET_LENGTH(*callresult); |
| 2532 | assert(PyUnicode_KIND(*callresult) <= |
| 2533 | PyUnicode_KIND(string)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2534 | copy_characters(string, i, *callresult, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2535 | i += size; |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2536 | Py_DECREF(*callresult); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2537 | } |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2538 | ++callresult; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2539 | break; |
| 2540 | } |
| 2541 | case 'S': |
| 2542 | case 'R': |
Victor Stinner | 9a90900 | 2010-10-18 20:59:24 +0000 | [diff] [blame] | 2543 | case 'A': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2544 | { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2545 | Py_ssize_t size = PyUnicode_GET_LENGTH(*callresult); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2546 | /* unused, since we already have the result */ |
| 2547 | (void) va_arg(vargs, PyObject *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2548 | assert(PyUnicode_KIND(*callresult) <= PyUnicode_KIND(string)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2549 | copy_characters(string, i, *callresult, 0, size); |
| 2550 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2551 | /* We're done with the unicode()/repr() => forget it */ |
| 2552 | Py_DECREF(*callresult); |
| 2553 | /* switch to next unicode()/repr() result */ |
| 2554 | ++callresult; |
| 2555 | break; |
| 2556 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2557 | case '%': |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2558 | PyUnicode_WRITE(kind, data, i++, '%'); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2559 | break; |
| 2560 | default: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2561 | for (; *p; ++p, ++i) |
| 2562 | PyUnicode_WRITE(kind, data, i, *p); |
| 2563 | assert(i == PyUnicode_GET_LENGTH(string)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2564 | goto end; |
| 2565 | } |
Victor Stinner | 1205f27 | 2010-09-11 00:54:47 +0000 | [diff] [blame] | 2566 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2567 | else { |
| 2568 | assert(i < PyUnicode_GET_LENGTH(string)); |
| 2569 | PyUnicode_WRITE(kind, data, i++, *f); |
| 2570 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2571 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2572 | assert(i == PyUnicode_GET_LENGTH(string)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2573 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2574 | end: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2575 | if (callresults) |
| 2576 | PyObject_Free(callresults); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2577 | if (numberresults) |
| 2578 | PyObject_Free(numberresults); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2579 | assert(_PyUnicode_CheckConsistency(string, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2580 | return (PyObject *)string; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2581 | fail: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2582 | if (callresults) { |
| 2583 | PyObject **callresult2 = callresults; |
| 2584 | while (callresult2 < callresult) { |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2585 | Py_XDECREF(*callresult2); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2586 | ++callresult2; |
| 2587 | } |
| 2588 | PyObject_Free(callresults); |
| 2589 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2590 | if (numberresults) |
| 2591 | PyObject_Free(numberresults); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2592 | return NULL; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2593 | } |
| 2594 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2595 | PyObject * |
| 2596 | PyUnicode_FromFormat(const char *format, ...) |
| 2597 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2598 | PyObject* ret; |
| 2599 | va_list vargs; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2600 | |
| 2601 | #ifdef HAVE_STDARG_PROTOTYPES |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2602 | va_start(vargs, format); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2603 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2604 | va_start(vargs); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2605 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2606 | ret = PyUnicode_FromFormatV(format, vargs); |
| 2607 | va_end(vargs); |
| 2608 | return ret; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2609 | } |
| 2610 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2611 | #ifdef HAVE_WCHAR_H |
| 2612 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2613 | /* Helper function for PyUnicode_AsWideChar() and PyUnicode_AsWideCharString(): |
| 2614 | convert a Unicode object to a wide character string. |
| 2615 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2616 | - 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] | 2617 | character) required to convert the unicode object. Ignore size argument. |
| 2618 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2619 | - Otherwise: return the number of wide characters (excluding the null |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2620 | character) written into w. Write at most size wide characters (including |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2621 | the null character). */ |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2622 | static Py_ssize_t |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2623 | unicode_aswidechar(PyUnicodeObject *unicode, |
| 2624 | wchar_t *w, |
| 2625 | Py_ssize_t size) |
| 2626 | { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2627 | Py_ssize_t res; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2628 | const wchar_t *wstr; |
| 2629 | |
| 2630 | wstr = PyUnicode_AsUnicodeAndSize((PyObject *)unicode, &res); |
| 2631 | if (wstr == NULL) |
| 2632 | return -1; |
| 2633 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2634 | if (w != NULL) { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2635 | if (size > res) |
| 2636 | size = res + 1; |
| 2637 | else |
| 2638 | res = size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2639 | Py_MEMCPY(w, wstr, size * sizeof(wchar_t)); |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2640 | return res; |
| 2641 | } |
| 2642 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2643 | return res + 1; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2644 | } |
| 2645 | |
| 2646 | Py_ssize_t |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2647 | PyUnicode_AsWideChar(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2648 | wchar_t *w, |
| 2649 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2650 | { |
| 2651 | if (unicode == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2652 | PyErr_BadInternalCall(); |
| 2653 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2654 | } |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2655 | return unicode_aswidechar((PyUnicodeObject*)unicode, w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2656 | } |
| 2657 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2658 | wchar_t* |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 2659 | PyUnicode_AsWideCharString(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2660 | Py_ssize_t *size) |
| 2661 | { |
| 2662 | wchar_t* buffer; |
| 2663 | Py_ssize_t buflen; |
| 2664 | |
| 2665 | if (unicode == NULL) { |
| 2666 | PyErr_BadInternalCall(); |
| 2667 | return NULL; |
| 2668 | } |
| 2669 | |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 2670 | buflen = unicode_aswidechar((PyUnicodeObject *)unicode, NULL, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2671 | if (buflen == -1) |
| 2672 | return NULL; |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2673 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < buflen) { |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2674 | PyErr_NoMemory(); |
| 2675 | return NULL; |
| 2676 | } |
| 2677 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2678 | buffer = PyMem_MALLOC(buflen * sizeof(wchar_t)); |
| 2679 | if (buffer == NULL) { |
| 2680 | PyErr_NoMemory(); |
| 2681 | return NULL; |
| 2682 | } |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 2683 | buflen = unicode_aswidechar((PyUnicodeObject *)unicode, buffer, buflen); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2684 | if (buflen == -1) |
| 2685 | return NULL; |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2686 | if (size != NULL) |
| 2687 | *size = buflen; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2688 | return buffer; |
| 2689 | } |
| 2690 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2691 | #endif /* HAVE_WCHAR_H */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2692 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2693 | PyObject * |
| 2694 | PyUnicode_FromOrdinal(int ordinal) |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2695 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2696 | PyObject *v; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2697 | if (ordinal < 0 || ordinal > 0x10ffff) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2698 | PyErr_SetString(PyExc_ValueError, |
| 2699 | "chr() arg not in range(0x110000)"); |
| 2700 | return NULL; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2701 | } |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 2702 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2703 | if (ordinal < 256) |
| 2704 | return get_latin1_char(ordinal); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2705 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2706 | v = PyUnicode_New(1, ordinal); |
| 2707 | if (v == NULL) |
| 2708 | return NULL; |
| 2709 | PyUnicode_WRITE(PyUnicode_KIND(v), PyUnicode_DATA(v), 0, ordinal); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2710 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2711 | return v; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2712 | } |
| 2713 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2714 | PyObject * |
| 2715 | PyUnicode_FromObject(register PyObject *obj) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2716 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2717 | /* XXX Perhaps we should make this API an alias of |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2718 | PyObject_Str() instead ?! */ |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2719 | if (PyUnicode_CheckExact(obj)) { |
Victor Stinner | d3a83d5 | 2011-10-01 03:09:33 +0200 | [diff] [blame] | 2720 | if (PyUnicode_READY(obj)) |
| 2721 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2722 | Py_INCREF(obj); |
| 2723 | return obj; |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2724 | } |
| 2725 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2726 | /* For a Unicode subtype that's not a Unicode object, |
| 2727 | return a true Unicode object with the same data. */ |
Victor Stinner | 2219e0a | 2011-10-01 01:16:59 +0200 | [diff] [blame] | 2728 | return PyUnicode_Copy(obj); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2729 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2730 | PyErr_Format(PyExc_TypeError, |
| 2731 | "Can't convert '%.100s' object to str implicitly", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2732 | Py_TYPE(obj)->tp_name); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2733 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2734 | } |
| 2735 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2736 | PyObject * |
| 2737 | PyUnicode_FromEncodedObject(register PyObject *obj, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2738 | const char *encoding, |
| 2739 | const char *errors) |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2740 | { |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2741 | Py_buffer buffer; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2742 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2743 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2744 | if (obj == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2745 | PyErr_BadInternalCall(); |
| 2746 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2747 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2748 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2749 | /* Decoding bytes objects is the most common case and should be fast */ |
| 2750 | if (PyBytes_Check(obj)) { |
| 2751 | if (PyBytes_GET_SIZE(obj) == 0) { |
| 2752 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 2753 | v = unicode_empty; |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2754 | } |
| 2755 | else { |
| 2756 | v = PyUnicode_Decode( |
| 2757 | PyBytes_AS_STRING(obj), PyBytes_GET_SIZE(obj), |
| 2758 | encoding, errors); |
| 2759 | } |
| 2760 | return v; |
| 2761 | } |
| 2762 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2763 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2764 | PyErr_SetString(PyExc_TypeError, |
| 2765 | "decoding str is not supported"); |
| 2766 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2767 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2768 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2769 | /* Retrieve a bytes buffer view through the PEP 3118 buffer interface */ |
| 2770 | if (PyObject_GetBuffer(obj, &buffer, PyBUF_SIMPLE) < 0) { |
| 2771 | PyErr_Format(PyExc_TypeError, |
| 2772 | "coercing to str: need bytes, bytearray " |
| 2773 | "or buffer-like object, %.80s found", |
| 2774 | Py_TYPE(obj)->tp_name); |
| 2775 | return NULL; |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 2776 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2777 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2778 | if (buffer.len == 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2779 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 2780 | v = unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2781 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2782 | else |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2783 | v = PyUnicode_Decode((char*) buffer.buf, buffer.len, encoding, errors); |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 2784 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2785 | PyBuffer_Release(&buffer); |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2786 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2787 | } |
| 2788 | |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2789 | /* Convert encoding to lower case and replace '_' with '-' in order to |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2790 | catch e.g. UTF_8. Return 0 on error (encoding is longer than lower_len-1), |
| 2791 | 1 on success. */ |
| 2792 | static int |
| 2793 | normalize_encoding(const char *encoding, |
| 2794 | char *lower, |
| 2795 | size_t lower_len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2796 | { |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2797 | const char *e; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2798 | char *l; |
| 2799 | char *l_end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2800 | |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2801 | e = encoding; |
| 2802 | l = lower; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2803 | l_end = &lower[lower_len - 1]; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2804 | while (*e) { |
| 2805 | if (l == l_end) |
| 2806 | return 0; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 2807 | if (Py_ISUPPER(*e)) { |
| 2808 | *l++ = Py_TOLOWER(*e++); |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2809 | } |
| 2810 | else if (*e == '_') { |
| 2811 | *l++ = '-'; |
| 2812 | e++; |
| 2813 | } |
| 2814 | else { |
| 2815 | *l++ = *e++; |
| 2816 | } |
| 2817 | } |
| 2818 | *l = '\0'; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2819 | return 1; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2820 | } |
| 2821 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2822 | PyObject * |
| 2823 | PyUnicode_Decode(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2824 | Py_ssize_t size, |
| 2825 | const char *encoding, |
| 2826 | const char *errors) |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2827 | { |
| 2828 | PyObject *buffer = NULL, *unicode; |
| 2829 | Py_buffer info; |
| 2830 | char lower[11]; /* Enough for any encoding shortcut */ |
| 2831 | |
| 2832 | if (encoding == NULL) |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2833 | return PyUnicode_DecodeUTF8(s, size, errors); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 2834 | |
| 2835 | /* Shortcuts for common default encodings */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2836 | if (normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2837 | if ((strcmp(lower, "utf-8") == 0) || |
| 2838 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2839 | return PyUnicode_DecodeUTF8(s, size, errors); |
| 2840 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2841 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2842 | (strcmp(lower, "iso-8859-1") == 0)) |
| 2843 | return PyUnicode_DecodeLatin1(s, size, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 2844 | #ifdef HAVE_MBCS |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2845 | else if (strcmp(lower, "mbcs") == 0) |
| 2846 | return PyUnicode_DecodeMBCS(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 2847 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2848 | else if (strcmp(lower, "ascii") == 0) |
| 2849 | return PyUnicode_DecodeASCII(s, size, errors); |
| 2850 | else if (strcmp(lower, "utf-16") == 0) |
| 2851 | return PyUnicode_DecodeUTF16(s, size, errors, 0); |
| 2852 | else if (strcmp(lower, "utf-32") == 0) |
| 2853 | return PyUnicode_DecodeUTF32(s, size, errors, 0); |
| 2854 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2855 | |
| 2856 | /* Decode via the codec registry */ |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 2857 | buffer = NULL; |
Antoine Pitrou | c3b3924 | 2009-01-03 16:59:18 +0000 | [diff] [blame] | 2858 | 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] | 2859 | goto onError; |
Antoine Pitrou | ee58fa4 | 2008-08-19 18:22:14 +0000 | [diff] [blame] | 2860 | buffer = PyMemoryView_FromBuffer(&info); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2861 | if (buffer == NULL) |
| 2862 | goto onError; |
| 2863 | unicode = PyCodec_Decode(buffer, encoding, errors); |
| 2864 | if (unicode == NULL) |
| 2865 | goto onError; |
| 2866 | if (!PyUnicode_Check(unicode)) { |
| 2867 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 2868 | "decoder did not return a str object (type=%.400s)", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2869 | Py_TYPE(unicode)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2870 | Py_DECREF(unicode); |
| 2871 | goto onError; |
| 2872 | } |
| 2873 | Py_DECREF(buffer); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 2874 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 2875 | if (_PyUnicode_READY_REPLACE(&unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2876 | Py_DECREF(unicode); |
| 2877 | return NULL; |
| 2878 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 2879 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2880 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2881 | return unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2882 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2883 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2884 | Py_XDECREF(buffer); |
| 2885 | return NULL; |
| 2886 | } |
| 2887 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2888 | PyObject * |
| 2889 | PyUnicode_AsDecodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2890 | const char *encoding, |
| 2891 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2892 | { |
| 2893 | PyObject *v; |
| 2894 | |
| 2895 | if (!PyUnicode_Check(unicode)) { |
| 2896 | PyErr_BadArgument(); |
| 2897 | goto onError; |
| 2898 | } |
| 2899 | |
| 2900 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2901 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2902 | |
| 2903 | /* Decode via the codec registry */ |
| 2904 | v = PyCodec_Decode(unicode, encoding, errors); |
| 2905 | if (v == NULL) |
| 2906 | goto onError; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2907 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2908 | return v; |
| 2909 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2910 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2911 | return NULL; |
| 2912 | } |
| 2913 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2914 | PyObject * |
| 2915 | PyUnicode_AsDecodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2916 | const char *encoding, |
| 2917 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2918 | { |
| 2919 | PyObject *v; |
| 2920 | |
| 2921 | if (!PyUnicode_Check(unicode)) { |
| 2922 | PyErr_BadArgument(); |
| 2923 | goto onError; |
| 2924 | } |
| 2925 | |
| 2926 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2927 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2928 | |
| 2929 | /* Decode via the codec registry */ |
| 2930 | v = PyCodec_Decode(unicode, encoding, errors); |
| 2931 | if (v == NULL) |
| 2932 | goto onError; |
| 2933 | if (!PyUnicode_Check(v)) { |
| 2934 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 2935 | "decoder did not return a str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2936 | Py_TYPE(v)->tp_name); |
| 2937 | Py_DECREF(v); |
| 2938 | goto onError; |
| 2939 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2940 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2941 | return v; |
| 2942 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2943 | onError: |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2944 | return NULL; |
| 2945 | } |
| 2946 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2947 | PyObject * |
| 2948 | PyUnicode_Encode(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2949 | Py_ssize_t size, |
| 2950 | const char *encoding, |
| 2951 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2952 | { |
| 2953 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2954 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2955 | unicode = PyUnicode_FromUnicode(s, size); |
| 2956 | if (unicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2957 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2958 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 2959 | Py_DECREF(unicode); |
| 2960 | return v; |
| 2961 | } |
| 2962 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2963 | PyObject * |
| 2964 | PyUnicode_AsEncodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2965 | const char *encoding, |
| 2966 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2967 | { |
| 2968 | PyObject *v; |
| 2969 | |
| 2970 | if (!PyUnicode_Check(unicode)) { |
| 2971 | PyErr_BadArgument(); |
| 2972 | goto onError; |
| 2973 | } |
| 2974 | |
| 2975 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2976 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2977 | |
| 2978 | /* Encode via the codec registry */ |
| 2979 | v = PyCodec_Encode(unicode, encoding, errors); |
| 2980 | if (v == NULL) |
| 2981 | goto onError; |
| 2982 | return v; |
| 2983 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2984 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2985 | return NULL; |
| 2986 | } |
| 2987 | |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 2988 | PyObject * |
| 2989 | PyUnicode_EncodeFSDefault(PyObject *unicode) |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 2990 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 2991 | #ifdef HAVE_MBCS |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 2992 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
| 2993 | PyUnicode_GET_SIZE(unicode), |
| 2994 | NULL); |
| 2995 | #elif defined(__APPLE__) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2996 | return _PyUnicode_AsUTF8String(unicode, "surrogateescape"); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 2997 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 2998 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 2999 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 3000 | cannot use it to encode and decode filenames before it is loaded. Load |
| 3001 | the Python codec requires to encode at least its own filename. Use the C |
| 3002 | version of the locale codec until the codec registry is initialized and |
| 3003 | the Python codec is loaded. |
| 3004 | |
| 3005 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 3006 | cannot only rely on it: check also interp->fscodec_initialized for |
| 3007 | subinterpreters. */ |
| 3008 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3009 | return PyUnicode_AsEncodedString(unicode, |
| 3010 | Py_FileSystemDefaultEncoding, |
| 3011 | "surrogateescape"); |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 3012 | } |
| 3013 | else { |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3014 | /* locale encoding with surrogateescape */ |
| 3015 | wchar_t *wchar; |
| 3016 | char *bytes; |
| 3017 | PyObject *bytes_obj; |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 3018 | size_t error_pos; |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3019 | |
| 3020 | wchar = PyUnicode_AsWideCharString(unicode, NULL); |
| 3021 | if (wchar == NULL) |
| 3022 | return NULL; |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 3023 | bytes = _Py_wchar2char(wchar, &error_pos); |
| 3024 | if (bytes == NULL) { |
| 3025 | if (error_pos != (size_t)-1) { |
| 3026 | char *errmsg = strerror(errno); |
| 3027 | PyObject *exc = NULL; |
| 3028 | if (errmsg == NULL) |
| 3029 | errmsg = "Py_wchar2char() failed"; |
| 3030 | raise_encode_exception(&exc, |
| 3031 | "filesystemencoding", |
| 3032 | PyUnicode_AS_UNICODE(unicode), PyUnicode_GET_SIZE(unicode), |
| 3033 | error_pos, error_pos+1, |
| 3034 | errmsg); |
| 3035 | Py_XDECREF(exc); |
| 3036 | } |
| 3037 | else |
| 3038 | PyErr_NoMemory(); |
| 3039 | PyMem_Free(wchar); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3040 | return NULL; |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 3041 | } |
| 3042 | PyMem_Free(wchar); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3043 | |
| 3044 | bytes_obj = PyBytes_FromString(bytes); |
| 3045 | PyMem_Free(bytes); |
| 3046 | return bytes_obj; |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 3047 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3048 | #endif |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3049 | } |
| 3050 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3051 | PyObject * |
| 3052 | PyUnicode_AsEncodedString(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3053 | const char *encoding, |
| 3054 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3055 | { |
| 3056 | PyObject *v; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3057 | char lower[11]; /* Enough for any encoding shortcut */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3058 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3059 | if (!PyUnicode_Check(unicode)) { |
| 3060 | PyErr_BadArgument(); |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3061 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3062 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3063 | |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3064 | if (encoding == NULL) { |
| 3065 | if (errors == NULL || strcmp(errors, "strict") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3066 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3067 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3068 | return _PyUnicode_AsUTF8String(unicode, errors); |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3069 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3070 | |
| 3071 | /* Shortcuts for common default encodings */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3072 | if (normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3073 | if ((strcmp(lower, "utf-8") == 0) || |
| 3074 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3075 | { |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3076 | if (errors == NULL || strcmp(errors, "strict") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3077 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3078 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3079 | return _PyUnicode_AsUTF8String(unicode, errors); |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3080 | } |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3081 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3082 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3083 | (strcmp(lower, "iso-8859-1") == 0)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3084 | return _PyUnicode_AsLatin1String(unicode, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3085 | #ifdef HAVE_MBCS |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3086 | else if (strcmp(lower, "mbcs") == 0) |
| 3087 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
| 3088 | PyUnicode_GET_SIZE(unicode), |
| 3089 | errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 3090 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3091 | else if (strcmp(lower, "ascii") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3092 | return _PyUnicode_AsASCIIString(unicode, errors); |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3093 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3094 | |
| 3095 | /* Encode via the codec registry */ |
| 3096 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3097 | if (v == NULL) |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3098 | return NULL; |
| 3099 | |
| 3100 | /* The normal path */ |
| 3101 | if (PyBytes_Check(v)) |
| 3102 | return v; |
| 3103 | |
| 3104 | /* 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] | 3105 | if (PyByteArray_Check(v)) { |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3106 | int error; |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3107 | PyObject *b; |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3108 | |
| 3109 | error = PyErr_WarnFormat(PyExc_RuntimeWarning, 1, |
| 3110 | "encoder %s returned bytearray instead of bytes", |
| 3111 | encoding); |
| 3112 | if (error) { |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3113 | Py_DECREF(v); |
| 3114 | return NULL; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3115 | } |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3116 | |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3117 | b = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(v), Py_SIZE(v)); |
| 3118 | Py_DECREF(v); |
| 3119 | return b; |
| 3120 | } |
| 3121 | |
| 3122 | PyErr_Format(PyExc_TypeError, |
| 3123 | "encoder did not return a bytes object (type=%.400s)", |
| 3124 | Py_TYPE(v)->tp_name); |
| 3125 | Py_DECREF(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3126 | return NULL; |
| 3127 | } |
| 3128 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3129 | PyObject * |
| 3130 | PyUnicode_AsEncodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3131 | const char *encoding, |
| 3132 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3133 | { |
| 3134 | PyObject *v; |
| 3135 | |
| 3136 | if (!PyUnicode_Check(unicode)) { |
| 3137 | PyErr_BadArgument(); |
| 3138 | goto onError; |
| 3139 | } |
| 3140 | |
| 3141 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3142 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3143 | |
| 3144 | /* Encode via the codec registry */ |
| 3145 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3146 | if (v == NULL) |
| 3147 | goto onError; |
| 3148 | if (!PyUnicode_Check(v)) { |
| 3149 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3150 | "encoder did not return an str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3151 | Py_TYPE(v)->tp_name); |
| 3152 | Py_DECREF(v); |
| 3153 | goto onError; |
| 3154 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3155 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3156 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3157 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3158 | return NULL; |
| 3159 | } |
| 3160 | |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3161 | PyObject* |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3162 | PyUnicode_DecodeFSDefault(const char *s) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3163 | Py_ssize_t size = (Py_ssize_t)strlen(s); |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3164 | return PyUnicode_DecodeFSDefaultAndSize(s, size); |
| 3165 | } |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3166 | |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3167 | PyObject* |
| 3168 | PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size) |
| 3169 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3170 | #ifdef HAVE_MBCS |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3171 | return PyUnicode_DecodeMBCS(s, size, NULL); |
| 3172 | #elif defined(__APPLE__) |
| 3173 | return PyUnicode_DecodeUTF8(s, size, "surrogateescape"); |
| 3174 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 3175 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 3176 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 3177 | cannot use it to encode and decode filenames before it is loaded. Load |
| 3178 | the Python codec requires to encode at least its own filename. Use the C |
| 3179 | version of the locale codec until the codec registry is initialized and |
| 3180 | the Python codec is loaded. |
| 3181 | |
| 3182 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 3183 | cannot only rely on it: check also interp->fscodec_initialized for |
| 3184 | subinterpreters. */ |
| 3185 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3186 | return PyUnicode_Decode(s, size, |
| 3187 | Py_FileSystemDefaultEncoding, |
Victor Stinner | b9a20ad | 2010-04-30 16:37:52 +0000 | [diff] [blame] | 3188 | "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3189 | } |
| 3190 | else { |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3191 | /* locale encoding with surrogateescape */ |
| 3192 | wchar_t *wchar; |
| 3193 | PyObject *unicode; |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 3194 | size_t len; |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3195 | |
| 3196 | if (s[size] != '\0' || size != strlen(s)) { |
| 3197 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 3198 | return NULL; |
| 3199 | } |
| 3200 | |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 3201 | wchar = _Py_char2wchar(s, &len); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3202 | if (wchar == NULL) |
Victor Stinner | d5af0a5 | 2010-11-08 23:34:29 +0000 | [diff] [blame] | 3203 | return PyErr_NoMemory(); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3204 | |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 3205 | unicode = PyUnicode_FromWideChar(wchar, len); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3206 | PyMem_Free(wchar); |
| 3207 | return unicode; |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3208 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3209 | #endif |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3210 | } |
| 3211 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3212 | |
| 3213 | int |
| 3214 | PyUnicode_FSConverter(PyObject* arg, void* addr) |
| 3215 | { |
| 3216 | PyObject *output = NULL; |
| 3217 | Py_ssize_t size; |
| 3218 | void *data; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3219 | if (arg == NULL) { |
| 3220 | Py_DECREF(*(PyObject**)addr); |
| 3221 | return 1; |
| 3222 | } |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 3223 | if (PyBytes_Check(arg)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3224 | output = arg; |
| 3225 | Py_INCREF(output); |
| 3226 | } |
| 3227 | else { |
| 3228 | arg = PyUnicode_FromObject(arg); |
| 3229 | if (!arg) |
| 3230 | return 0; |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3231 | output = PyUnicode_EncodeFSDefault(arg); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3232 | Py_DECREF(arg); |
| 3233 | if (!output) |
| 3234 | return 0; |
| 3235 | if (!PyBytes_Check(output)) { |
| 3236 | Py_DECREF(output); |
| 3237 | PyErr_SetString(PyExc_TypeError, "encoder failed to return bytes"); |
| 3238 | return 0; |
| 3239 | } |
| 3240 | } |
Victor Stinner | 0ea2a46 | 2010-04-30 00:22:08 +0000 | [diff] [blame] | 3241 | size = PyBytes_GET_SIZE(output); |
| 3242 | data = PyBytes_AS_STRING(output); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3243 | if (size != strlen(data)) { |
Benjamin Peterson | 7a6b44a | 2011-08-18 13:51:47 -0500 | [diff] [blame] | 3244 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3245 | Py_DECREF(output); |
| 3246 | return 0; |
| 3247 | } |
| 3248 | *(PyObject**)addr = output; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3249 | return Py_CLEANUP_SUPPORTED; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3250 | } |
| 3251 | |
| 3252 | |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3253 | int |
| 3254 | PyUnicode_FSDecoder(PyObject* arg, void* addr) |
| 3255 | { |
| 3256 | PyObject *output = NULL; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3257 | if (arg == NULL) { |
| 3258 | Py_DECREF(*(PyObject**)addr); |
| 3259 | return 1; |
| 3260 | } |
| 3261 | if (PyUnicode_Check(arg)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3262 | if (PyUnicode_READY(arg)) |
| 3263 | return 0; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3264 | output = arg; |
| 3265 | Py_INCREF(output); |
| 3266 | } |
| 3267 | else { |
| 3268 | arg = PyBytes_FromObject(arg); |
| 3269 | if (!arg) |
| 3270 | return 0; |
| 3271 | output = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AS_STRING(arg), |
| 3272 | PyBytes_GET_SIZE(arg)); |
| 3273 | Py_DECREF(arg); |
| 3274 | if (!output) |
| 3275 | return 0; |
| 3276 | if (!PyUnicode_Check(output)) { |
| 3277 | Py_DECREF(output); |
| 3278 | PyErr_SetString(PyExc_TypeError, "decoder failed to return unicode"); |
| 3279 | return 0; |
| 3280 | } |
| 3281 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3282 | if (findchar(PyUnicode_DATA(output), PyUnicode_KIND(output), |
| 3283 | PyUnicode_GET_LENGTH(output), 0, 1)) { |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3284 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 3285 | Py_DECREF(output); |
| 3286 | return 0; |
| 3287 | } |
| 3288 | *(PyObject**)addr = output; |
| 3289 | return Py_CLEANUP_SUPPORTED; |
| 3290 | } |
| 3291 | |
| 3292 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3293 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3294 | PyUnicode_AsUTF8AndSize(PyObject *unicode, Py_ssize_t *psize) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3295 | { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 3296 | PyObject *bytes; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3297 | PyUnicodeObject *u = (PyUnicodeObject *)unicode; |
| 3298 | |
Neal Norwitz | e0a0a6e | 2007-08-25 01:04:21 +0000 | [diff] [blame] | 3299 | if (!PyUnicode_Check(unicode)) { |
| 3300 | PyErr_BadArgument(); |
| 3301 | return NULL; |
| 3302 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3303 | if (PyUnicode_READY(u) == -1) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3304 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3305 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3306 | if (PyUnicode_UTF8(unicode) == NULL) { |
| 3307 | assert(!PyUnicode_IS_COMPACT_ASCII(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3308 | bytes = _PyUnicode_AsUTF8String(unicode, "strict"); |
| 3309 | if (bytes == NULL) |
| 3310 | return NULL; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3311 | _PyUnicode_UTF8(u) = PyObject_MALLOC(PyBytes_GET_SIZE(bytes) + 1); |
| 3312 | if (_PyUnicode_UTF8(u) == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3313 | Py_DECREF(bytes); |
| 3314 | return NULL; |
| 3315 | } |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3316 | _PyUnicode_UTF8_LENGTH(u) = PyBytes_GET_SIZE(bytes); |
| 3317 | Py_MEMCPY(_PyUnicode_UTF8(u), PyBytes_AS_STRING(bytes), _PyUnicode_UTF8_LENGTH(u) + 1); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3318 | Py_DECREF(bytes); |
| 3319 | } |
| 3320 | |
| 3321 | if (psize) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3322 | *psize = PyUnicode_UTF8_LENGTH(unicode); |
| 3323 | return PyUnicode_UTF8(unicode); |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3324 | } |
| 3325 | |
| 3326 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3327 | PyUnicode_AsUTF8(PyObject *unicode) |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3328 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3329 | return PyUnicode_AsUTF8AndSize(unicode, NULL); |
| 3330 | } |
| 3331 | |
| 3332 | #ifdef Py_DEBUG |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 3333 | static int unicode_as_unicode_calls = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3334 | #endif |
| 3335 | |
| 3336 | |
| 3337 | Py_UNICODE * |
| 3338 | PyUnicode_AsUnicodeAndSize(PyObject *unicode, Py_ssize_t *size) |
| 3339 | { |
| 3340 | PyUnicodeObject *u; |
| 3341 | const unsigned char *one_byte; |
| 3342 | #if SIZEOF_WCHAR_T == 4 |
| 3343 | const Py_UCS2 *two_bytes; |
| 3344 | #else |
| 3345 | const Py_UCS4 *four_bytes; |
| 3346 | const Py_UCS4 *ucs4_end; |
| 3347 | Py_ssize_t num_surrogates; |
| 3348 | #endif |
| 3349 | wchar_t *w; |
| 3350 | wchar_t *wchar_end; |
| 3351 | |
| 3352 | if (!PyUnicode_Check(unicode)) { |
| 3353 | PyErr_BadArgument(); |
| 3354 | return NULL; |
| 3355 | } |
| 3356 | u = (PyUnicodeObject*)unicode; |
| 3357 | if (_PyUnicode_WSTR(u) == NULL) { |
| 3358 | /* Non-ASCII compact unicode object */ |
| 3359 | assert(_PyUnicode_KIND(u) != 0); |
| 3360 | assert(PyUnicode_IS_READY(u)); |
| 3361 | |
| 3362 | #ifdef Py_DEBUG |
| 3363 | ++unicode_as_unicode_calls; |
| 3364 | #endif |
| 3365 | |
| 3366 | if (PyUnicode_KIND(u) == PyUnicode_4BYTE_KIND) { |
| 3367 | #if SIZEOF_WCHAR_T == 2 |
| 3368 | four_bytes = PyUnicode_4BYTE_DATA(u); |
| 3369 | ucs4_end = four_bytes + _PyUnicode_LENGTH(u); |
| 3370 | num_surrogates = 0; |
| 3371 | |
| 3372 | for (; four_bytes < ucs4_end; ++four_bytes) { |
| 3373 | if (*four_bytes > 0xFFFF) |
| 3374 | ++num_surrogates; |
| 3375 | } |
| 3376 | |
| 3377 | _PyUnicode_WSTR(u) = (wchar_t *) PyObject_MALLOC( |
| 3378 | sizeof(wchar_t) * (_PyUnicode_LENGTH(u) + 1 + num_surrogates)); |
| 3379 | if (!_PyUnicode_WSTR(u)) { |
| 3380 | PyErr_NoMemory(); |
| 3381 | return NULL; |
| 3382 | } |
| 3383 | _PyUnicode_WSTR_LENGTH(u) = _PyUnicode_LENGTH(u) + num_surrogates; |
| 3384 | |
| 3385 | w = _PyUnicode_WSTR(u); |
| 3386 | wchar_end = w + _PyUnicode_WSTR_LENGTH(u); |
| 3387 | four_bytes = PyUnicode_4BYTE_DATA(u); |
| 3388 | for (; four_bytes < ucs4_end; ++four_bytes, ++w) { |
| 3389 | if (*four_bytes > 0xFFFF) { |
| 3390 | /* encode surrogate pair in this case */ |
| 3391 | *w++ = 0xD800 | ((*four_bytes - 0x10000) >> 10); |
| 3392 | *w = 0xDC00 | ((*four_bytes - 0x10000) & 0x3FF); |
| 3393 | } |
| 3394 | else |
| 3395 | *w = *four_bytes; |
| 3396 | |
| 3397 | if (w > wchar_end) { |
| 3398 | assert(0 && "Miscalculated string end"); |
| 3399 | } |
| 3400 | } |
| 3401 | *w = 0; |
| 3402 | #else |
| 3403 | /* sizeof(wchar_t) == 4 */ |
| 3404 | Py_FatalError("Impossible unicode object state, wstr and str " |
| 3405 | "should share memory already."); |
| 3406 | return NULL; |
| 3407 | #endif |
| 3408 | } |
| 3409 | else { |
| 3410 | _PyUnicode_WSTR(u) = (wchar_t *) PyObject_MALLOC(sizeof(wchar_t) * |
| 3411 | (_PyUnicode_LENGTH(u) + 1)); |
| 3412 | if (!_PyUnicode_WSTR(u)) { |
| 3413 | PyErr_NoMemory(); |
| 3414 | return NULL; |
| 3415 | } |
| 3416 | if (!PyUnicode_IS_COMPACT_ASCII(u)) |
| 3417 | _PyUnicode_WSTR_LENGTH(u) = _PyUnicode_LENGTH(u); |
| 3418 | w = _PyUnicode_WSTR(u); |
| 3419 | wchar_end = w + _PyUnicode_LENGTH(u); |
| 3420 | |
| 3421 | if (PyUnicode_KIND(u) == PyUnicode_1BYTE_KIND) { |
| 3422 | one_byte = PyUnicode_1BYTE_DATA(u); |
| 3423 | for (; w < wchar_end; ++one_byte, ++w) |
| 3424 | *w = *one_byte; |
| 3425 | /* null-terminate the wstr */ |
| 3426 | *w = 0; |
| 3427 | } |
| 3428 | else if (PyUnicode_KIND(u) == PyUnicode_2BYTE_KIND) { |
| 3429 | #if SIZEOF_WCHAR_T == 4 |
| 3430 | two_bytes = PyUnicode_2BYTE_DATA(u); |
| 3431 | for (; w < wchar_end; ++two_bytes, ++w) |
| 3432 | *w = *two_bytes; |
| 3433 | /* null-terminate the wstr */ |
| 3434 | *w = 0; |
| 3435 | #else |
| 3436 | /* sizeof(wchar_t) == 2 */ |
| 3437 | PyObject_FREE(_PyUnicode_WSTR(u)); |
| 3438 | _PyUnicode_WSTR(u) = NULL; |
| 3439 | Py_FatalError("Impossible unicode object state, wstr " |
| 3440 | "and str should share memory already."); |
| 3441 | return NULL; |
| 3442 | #endif |
| 3443 | } |
| 3444 | else { |
| 3445 | assert(0 && "This should never happen."); |
| 3446 | } |
| 3447 | } |
| 3448 | } |
| 3449 | if (size != NULL) |
| 3450 | *size = PyUnicode_WSTR_LENGTH(u); |
| 3451 | return _PyUnicode_WSTR(u); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3452 | } |
| 3453 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3454 | Py_UNICODE * |
| 3455 | PyUnicode_AsUnicode(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3456 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3457 | return PyUnicode_AsUnicodeAndSize(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3458 | } |
| 3459 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3460 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3461 | Py_ssize_t |
| 3462 | PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3463 | { |
| 3464 | if (!PyUnicode_Check(unicode)) { |
| 3465 | PyErr_BadArgument(); |
| 3466 | goto onError; |
| 3467 | } |
| 3468 | return PyUnicode_GET_SIZE(unicode); |
| 3469 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3470 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3471 | return -1; |
| 3472 | } |
| 3473 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3474 | Py_ssize_t |
| 3475 | PyUnicode_GetLength(PyObject *unicode) |
| 3476 | { |
Victor Stinner | 5a706cf | 2011-10-02 00:36:53 +0200 | [diff] [blame] | 3477 | if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3478 | PyErr_BadArgument(); |
| 3479 | return -1; |
| 3480 | } |
| 3481 | |
| 3482 | return PyUnicode_GET_LENGTH(unicode); |
| 3483 | } |
| 3484 | |
| 3485 | Py_UCS4 |
| 3486 | PyUnicode_ReadChar(PyObject *unicode, Py_ssize_t index) |
| 3487 | { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 3488 | if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) { |
| 3489 | PyErr_BadArgument(); |
| 3490 | return (Py_UCS4)-1; |
| 3491 | } |
| 3492 | if (index < 0 || index >= _PyUnicode_LENGTH(unicode)) { |
| 3493 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3494 | return (Py_UCS4)-1; |
| 3495 | } |
| 3496 | return PyUnicode_READ_CHAR(unicode, index); |
| 3497 | } |
| 3498 | |
| 3499 | int |
| 3500 | PyUnicode_WriteChar(PyObject *unicode, Py_ssize_t index, Py_UCS4 ch) |
| 3501 | { |
| 3502 | if (!PyUnicode_Check(unicode) || !PyUnicode_IS_COMPACT(unicode)) { |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3503 | PyErr_BadArgument(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3504 | return -1; |
| 3505 | } |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3506 | if (index < 0 || index >= _PyUnicode_LENGTH(unicode)) { |
| 3507 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 3508 | return -1; |
| 3509 | } |
| 3510 | if (_PyUnicode_Dirty(unicode)) |
| 3511 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3512 | PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), |
| 3513 | index, ch); |
| 3514 | return 0; |
| 3515 | } |
| 3516 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3517 | const char * |
| 3518 | PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3519 | { |
Victor Stinner | 42cb462 | 2010-09-01 19:39:01 +0000 | [diff] [blame] | 3520 | return "utf-8"; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3521 | } |
| 3522 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 3523 | /* create or adjust a UnicodeDecodeError */ |
| 3524 | static void |
| 3525 | make_decode_exception(PyObject **exceptionObject, |
| 3526 | const char *encoding, |
| 3527 | const char *input, Py_ssize_t length, |
| 3528 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 3529 | const char *reason) |
| 3530 | { |
| 3531 | if (*exceptionObject == NULL) { |
| 3532 | *exceptionObject = PyUnicodeDecodeError_Create( |
| 3533 | encoding, input, length, startpos, endpos, reason); |
| 3534 | } |
| 3535 | else { |
| 3536 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, startpos)) |
| 3537 | goto onError; |
| 3538 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, endpos)) |
| 3539 | goto onError; |
| 3540 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 3541 | goto onError; |
| 3542 | } |
| 3543 | return; |
| 3544 | |
| 3545 | onError: |
| 3546 | Py_DECREF(*exceptionObject); |
| 3547 | *exceptionObject = NULL; |
| 3548 | } |
| 3549 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3550 | /* error handling callback helper: |
| 3551 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 3552 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3553 | and adjust various state variables. |
| 3554 | return 0 on success, -1 on error |
| 3555 | */ |
| 3556 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3557 | static int |
| 3558 | unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3559 | const char *encoding, const char *reason, |
| 3560 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 3561 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
| 3562 | PyUnicodeObject **output, Py_ssize_t *outpos, Py_UNICODE **outptr) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3563 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3564 | 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] | 3565 | |
| 3566 | PyObject *restuple = NULL; |
| 3567 | PyObject *repunicode = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3568 | Py_ssize_t outsize = PyUnicode_GET_SIZE(*output); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3569 | Py_ssize_t insize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3570 | Py_ssize_t requiredsize; |
| 3571 | Py_ssize_t newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3572 | const Py_UNICODE *repptr; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3573 | PyObject *inputobj = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3574 | Py_ssize_t repsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3575 | int res = -1; |
| 3576 | |
| 3577 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3578 | *errorHandler = PyCodec_LookupError(errors); |
| 3579 | if (*errorHandler == NULL) |
| 3580 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3581 | } |
| 3582 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 3583 | make_decode_exception(exceptionObject, |
| 3584 | encoding, |
| 3585 | *input, *inend - *input, |
| 3586 | *startinpos, *endinpos, |
| 3587 | reason); |
| 3588 | if (*exceptionObject == NULL) |
| 3589 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3590 | |
| 3591 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 3592 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3593 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3594 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 3595 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3596 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3597 | } |
| 3598 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3599 | goto onError; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3600 | |
| 3601 | /* Copy back the bytes variables, which might have been modified by the |
| 3602 | callback */ |
| 3603 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 3604 | if (!inputobj) |
| 3605 | goto onError; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3606 | if (!PyBytes_Check(inputobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3607 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3608 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3609 | *input = PyBytes_AS_STRING(inputobj); |
| 3610 | insize = PyBytes_GET_SIZE(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3611 | *inend = *input + insize; |
Walter Dörwald | 36f938f | 2007-08-10 10:11:43 +0000 | [diff] [blame] | 3612 | /* we can DECREF safely, as the exception has another reference, |
| 3613 | so the object won't go away. */ |
| 3614 | Py_DECREF(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3615 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3616 | if (newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3617 | newpos = insize+newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 3618 | if (newpos<0 || newpos>insize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3619 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos); |
| 3620 | goto onError; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 3621 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3622 | |
| 3623 | /* need more space? (at least enough for what we |
| 3624 | have+the replacement+the rest of the string (starting |
| 3625 | at the new input position), so we won't have to check space |
| 3626 | when there are no errors in the rest of the string) */ |
| 3627 | repptr = PyUnicode_AS_UNICODE(repunicode); |
| 3628 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 3629 | requiredsize = *outpos + repsize + insize-newpos; |
| 3630 | if (requiredsize > outsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3631 | if (requiredsize<2*outsize) |
| 3632 | requiredsize = 2*outsize; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 3633 | if (PyUnicode_Resize((PyObject**)output, requiredsize) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3634 | goto onError; |
| 3635 | *outptr = PyUnicode_AS_UNICODE(*output) + *outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3636 | } |
| 3637 | *endinpos = newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3638 | *inptr = *input + newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3639 | Py_UNICODE_COPY(*outptr, repptr, repsize); |
| 3640 | *outptr += repsize; |
| 3641 | *outpos += repsize; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3642 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3643 | /* we made it! */ |
| 3644 | res = 0; |
| 3645 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3646 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3647 | Py_XDECREF(restuple); |
| 3648 | return res; |
| 3649 | } |
| 3650 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3651 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 3652 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3653 | /* See RFC2152 for details. We encode conservatively and decode liberally. */ |
| 3654 | |
| 3655 | /* Three simple macros defining base-64. */ |
| 3656 | |
| 3657 | /* Is c a base-64 character? */ |
| 3658 | |
| 3659 | #define IS_BASE64(c) \ |
| 3660 | (((c) >= 'A' && (c) <= 'Z') || \ |
| 3661 | ((c) >= 'a' && (c) <= 'z') || \ |
| 3662 | ((c) >= '0' && (c) <= '9') || \ |
| 3663 | (c) == '+' || (c) == '/') |
| 3664 | |
| 3665 | /* given that c is a base-64 character, what is its base-64 value? */ |
| 3666 | |
| 3667 | #define FROM_BASE64(c) \ |
| 3668 | (((c) >= 'A' && (c) <= 'Z') ? (c) - 'A' : \ |
| 3669 | ((c) >= 'a' && (c) <= 'z') ? (c) - 'a' + 26 : \ |
| 3670 | ((c) >= '0' && (c) <= '9') ? (c) - '0' + 52 : \ |
| 3671 | (c) == '+' ? 62 : 63) |
| 3672 | |
| 3673 | /* What is the base-64 character of the bottom 6 bits of n? */ |
| 3674 | |
| 3675 | #define TO_BASE64(n) \ |
| 3676 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 3677 | |
| 3678 | /* DECODE_DIRECT: this byte encountered in a UTF-7 string should be |
| 3679 | * decoded as itself. We are permissive on decoding; the only ASCII |
| 3680 | * byte not decoding to itself is the + which begins a base64 |
| 3681 | * string. */ |
| 3682 | |
| 3683 | #define DECODE_DIRECT(c) \ |
| 3684 | ((c) <= 127 && (c) != '+') |
| 3685 | |
| 3686 | /* The UTF-7 encoder treats ASCII characters differently according to |
| 3687 | * whether they are Set D, Set O, Whitespace, or special (i.e. none of |
| 3688 | * the above). See RFC2152. This array identifies these different |
| 3689 | * sets: |
| 3690 | * 0 : "Set D" |
| 3691 | * alphanumeric and '(),-./:? |
| 3692 | * 1 : "Set O" |
| 3693 | * !"#$%&*;<=>@[]^_`{|} |
| 3694 | * 2 : "whitespace" |
| 3695 | * ht nl cr sp |
| 3696 | * 3 : special (must be base64 encoded) |
| 3697 | * everything else (i.e. +\~ and non-printing codes 0-8 11-12 14-31 127) |
| 3698 | */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3699 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3700 | static |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3701 | char utf7_category[128] = { |
| 3702 | /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */ |
| 3703 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 3, 3, 2, 3, 3, |
| 3704 | /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */ |
| 3705 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 3706 | /* sp ! " # $ % & ' ( ) * + , - . / */ |
| 3707 | 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 3, 0, 0, 0, 0, |
| 3708 | /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */ |
| 3709 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, |
| 3710 | /* @ A B C D E F G H I J K L M N O */ |
| 3711 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 3712 | /* P Q R S T U V W X Y Z [ \ ] ^ _ */ |
| 3713 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 1, 1, 1, |
| 3714 | /* ` a b c d e f g h i j k l m n o */ |
| 3715 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 3716 | /* p q r s t u v w x y z { | } ~ del */ |
| 3717 | 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] | 3718 | }; |
| 3719 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3720 | /* ENCODE_DIRECT: this character should be encoded as itself. The |
| 3721 | * answer depends on whether we are encoding set O as itself, and also |
| 3722 | * on whether we are encoding whitespace as itself. RFC2152 makes it |
| 3723 | * clear that the answers to these questions vary between |
| 3724 | * applications, so this code needs to be flexible. */ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 3725 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3726 | #define ENCODE_DIRECT(c, directO, directWS) \ |
| 3727 | ((c) < 128 && (c) > 0 && \ |
| 3728 | ((utf7_category[(c)] == 0) || \ |
| 3729 | (directWS && (utf7_category[(c)] == 2)) || \ |
| 3730 | (directO && (utf7_category[(c)] == 1)))) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3731 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3732 | PyObject * |
| 3733 | PyUnicode_DecodeUTF7(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3734 | Py_ssize_t size, |
| 3735 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3736 | { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3737 | return PyUnicode_DecodeUTF7Stateful(s, size, errors, NULL); |
| 3738 | } |
| 3739 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3740 | /* The decoder. The only state we preserve is our read position, |
| 3741 | * i.e. how many characters we have consumed. So if we end in the |
| 3742 | * middle of a shift sequence we have to back off the read position |
| 3743 | * and the output to the beginning of the sequence, otherwise we lose |
| 3744 | * all the shift state (seen bits, number of bits seen, high |
| 3745 | * surrogate). */ |
| 3746 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3747 | PyObject * |
| 3748 | PyUnicode_DecodeUTF7Stateful(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3749 | Py_ssize_t size, |
| 3750 | const char *errors, |
| 3751 | Py_ssize_t *consumed) |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3752 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3753 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3754 | Py_ssize_t startinpos; |
| 3755 | Py_ssize_t endinpos; |
| 3756 | Py_ssize_t outpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3757 | const char *e; |
| 3758 | PyUnicodeObject *unicode; |
| 3759 | Py_UNICODE *p; |
| 3760 | const char *errmsg = ""; |
| 3761 | int inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3762 | Py_UNICODE *shiftOutStart; |
| 3763 | unsigned int base64bits = 0; |
| 3764 | unsigned long base64buffer = 0; |
| 3765 | Py_UNICODE surrogate = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3766 | PyObject *errorHandler = NULL; |
| 3767 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3768 | |
| 3769 | unicode = _PyUnicode_New(size); |
| 3770 | if (!unicode) |
| 3771 | return NULL; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3772 | if (size == 0) { |
| 3773 | if (consumed) |
| 3774 | *consumed = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3775 | return (PyObject *)unicode; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3776 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3777 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3778 | p = PyUnicode_AS_UNICODE(unicode); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3779 | shiftOutStart = p; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3780 | e = s + size; |
| 3781 | |
| 3782 | while (s < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3783 | Py_UNICODE ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3784 | restart: |
Antoine Pitrou | 5ffd9e9 | 2008-07-25 18:05:24 +0000 | [diff] [blame] | 3785 | ch = (unsigned char) *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3786 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3787 | if (inShift) { /* in a base-64 section */ |
| 3788 | if (IS_BASE64(ch)) { /* consume a base-64 character */ |
| 3789 | base64buffer = (base64buffer << 6) | FROM_BASE64(ch); |
| 3790 | base64bits += 6; |
| 3791 | s++; |
| 3792 | if (base64bits >= 16) { |
| 3793 | /* we have enough bits for a UTF-16 value */ |
| 3794 | Py_UNICODE outCh = (Py_UNICODE) |
| 3795 | (base64buffer >> (base64bits-16)); |
| 3796 | base64bits -= 16; |
| 3797 | base64buffer &= (1 << base64bits) - 1; /* clear high bits */ |
| 3798 | if (surrogate) { |
| 3799 | /* expecting a second surrogate */ |
| 3800 | if (outCh >= 0xDC00 && outCh <= 0xDFFF) { |
| 3801 | #ifdef Py_UNICODE_WIDE |
| 3802 | *p++ = (((surrogate & 0x3FF)<<10) |
| 3803 | | (outCh & 0x3FF)) + 0x10000; |
| 3804 | #else |
| 3805 | *p++ = surrogate; |
| 3806 | *p++ = outCh; |
| 3807 | #endif |
| 3808 | surrogate = 0; |
| 3809 | } |
| 3810 | else { |
| 3811 | surrogate = 0; |
| 3812 | errmsg = "second surrogate missing"; |
| 3813 | goto utf7Error; |
| 3814 | } |
| 3815 | } |
| 3816 | else if (outCh >= 0xD800 && outCh <= 0xDBFF) { |
| 3817 | /* first surrogate */ |
| 3818 | surrogate = outCh; |
| 3819 | } |
| 3820 | else if (outCh >= 0xDC00 && outCh <= 0xDFFF) { |
| 3821 | errmsg = "unexpected second surrogate"; |
| 3822 | goto utf7Error; |
| 3823 | } |
| 3824 | else { |
| 3825 | *p++ = outCh; |
| 3826 | } |
| 3827 | } |
| 3828 | } |
| 3829 | else { /* now leaving a base-64 section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3830 | inShift = 0; |
| 3831 | s++; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3832 | if (surrogate) { |
| 3833 | errmsg = "second surrogate missing at end of shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3834 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3835 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3836 | if (base64bits > 0) { /* left-over bits */ |
| 3837 | if (base64bits >= 6) { |
| 3838 | /* We've seen at least one base-64 character */ |
| 3839 | errmsg = "partial character in shift sequence"; |
| 3840 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3841 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3842 | else { |
| 3843 | /* Some bits remain; they should be zero */ |
| 3844 | if (base64buffer != 0) { |
| 3845 | errmsg = "non-zero padding bits in shift sequence"; |
| 3846 | goto utf7Error; |
| 3847 | } |
| 3848 | } |
| 3849 | } |
| 3850 | if (ch != '-') { |
| 3851 | /* '-' is absorbed; other terminating |
| 3852 | characters are preserved */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3853 | *p++ = ch; |
| 3854 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3855 | } |
| 3856 | } |
| 3857 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3858 | startinpos = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3859 | s++; /* consume '+' */ |
| 3860 | if (s < e && *s == '-') { /* '+-' encodes '+' */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3861 | s++; |
| 3862 | *p++ = '+'; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3863 | } |
| 3864 | else { /* begin base64-encoded section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3865 | inShift = 1; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3866 | shiftOutStart = p; |
| 3867 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3868 | } |
| 3869 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3870 | else if (DECODE_DIRECT(ch)) { /* character decodes as itself */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3871 | *p++ = ch; |
| 3872 | s++; |
| 3873 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3874 | else { |
| 3875 | startinpos = s-starts; |
| 3876 | s++; |
| 3877 | errmsg = "unexpected special character"; |
| 3878 | goto utf7Error; |
| 3879 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3880 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3881 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3882 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 3883 | endinpos = s-starts; |
| 3884 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3885 | errors, &errorHandler, |
| 3886 | "utf7", errmsg, |
| 3887 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 3888 | &unicode, &outpos, &p)) |
| 3889 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3890 | } |
| 3891 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3892 | /* end of string */ |
| 3893 | |
| 3894 | if (inShift && !consumed) { /* in shift sequence, no more to follow */ |
| 3895 | /* if we're in an inconsistent state, that's an error */ |
| 3896 | if (surrogate || |
| 3897 | (base64bits >= 6) || |
| 3898 | (base64bits > 0 && base64buffer != 0)) { |
| 3899 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 3900 | endinpos = size; |
| 3901 | if (unicode_decode_call_errorhandler( |
| 3902 | errors, &errorHandler, |
| 3903 | "utf7", "unterminated shift sequence", |
| 3904 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 3905 | &unicode, &outpos, &p)) |
| 3906 | goto onError; |
| 3907 | if (s < e) |
| 3908 | goto restart; |
| 3909 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3910 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3911 | |
| 3912 | /* return state */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3913 | if (consumed) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3914 | if (inShift) { |
| 3915 | p = shiftOutStart; /* back off output */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3916 | *consumed = startinpos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3917 | } |
| 3918 | else { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3919 | *consumed = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3920 | } |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3921 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3922 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 3923 | if (PyUnicode_Resize((PyObject**)&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3924 | goto onError; |
| 3925 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3926 | Py_XDECREF(errorHandler); |
| 3927 | Py_XDECREF(exc); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 3928 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 3929 | if (_PyUnicode_READY_REPLACE(&unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3930 | Py_DECREF(unicode); |
| 3931 | return NULL; |
| 3932 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 3933 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 3934 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3935 | return (PyObject *)unicode; |
| 3936 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3937 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3938 | Py_XDECREF(errorHandler); |
| 3939 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3940 | Py_DECREF(unicode); |
| 3941 | return NULL; |
| 3942 | } |
| 3943 | |
| 3944 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3945 | PyObject * |
| 3946 | PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3947 | Py_ssize_t size, |
| 3948 | int base64SetO, |
| 3949 | int base64WhiteSpace, |
| 3950 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3951 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3952 | PyObject *v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3953 | /* It might be possible to tighten this worst case */ |
Alexandre Vassalotti | e85bd98 | 2009-07-21 00:39:03 +0000 | [diff] [blame] | 3954 | Py_ssize_t allocated = 8 * size; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3955 | int inShift = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3956 | Py_ssize_t i = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3957 | unsigned int base64bits = 0; |
| 3958 | unsigned long base64buffer = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3959 | char * out; |
| 3960 | char * start; |
| 3961 | |
| 3962 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3963 | return PyBytes_FromStringAndSize(NULL, 0); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3964 | |
Alexandre Vassalotti | e85bd98 | 2009-07-21 00:39:03 +0000 | [diff] [blame] | 3965 | if (allocated / 8 != size) |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3966 | return PyErr_NoMemory(); |
| 3967 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3968 | v = PyBytes_FromStringAndSize(NULL, allocated); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3969 | if (v == NULL) |
| 3970 | return NULL; |
| 3971 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3972 | start = out = PyBytes_AS_STRING(v); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3973 | for (;i < size; ++i) { |
| 3974 | Py_UNICODE ch = s[i]; |
| 3975 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3976 | if (inShift) { |
| 3977 | if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 3978 | /* shifting out */ |
| 3979 | if (base64bits) { /* output remaining bits */ |
| 3980 | *out++ = TO_BASE64(base64buffer << (6-base64bits)); |
| 3981 | base64buffer = 0; |
| 3982 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3983 | } |
| 3984 | inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3985 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 3986 | so no '-' is required, except if the character is itself a '-' */ |
| 3987 | if (IS_BASE64(ch) || ch == '-') { |
| 3988 | *out++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3989 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3990 | *out++ = (char) ch; |
| 3991 | } |
| 3992 | else { |
| 3993 | goto encode_char; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3994 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3995 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3996 | else { /* not in a shift sequence */ |
| 3997 | if (ch == '+') { |
| 3998 | *out++ = '+'; |
| 3999 | *out++ = '-'; |
| 4000 | } |
| 4001 | else if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 4002 | *out++ = (char) ch; |
| 4003 | } |
| 4004 | else { |
| 4005 | *out++ = '+'; |
| 4006 | inShift = 1; |
| 4007 | goto encode_char; |
| 4008 | } |
| 4009 | } |
| 4010 | continue; |
| 4011 | encode_char: |
| 4012 | #ifdef Py_UNICODE_WIDE |
| 4013 | if (ch >= 0x10000) { |
| 4014 | /* code first surrogate */ |
| 4015 | base64bits += 16; |
| 4016 | base64buffer = (base64buffer << 16) | 0xd800 | ((ch-0x10000) >> 10); |
| 4017 | while (base64bits >= 6) { |
| 4018 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 4019 | base64bits -= 6; |
| 4020 | } |
| 4021 | /* prepare second surrogate */ |
| 4022 | ch = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 4023 | } |
| 4024 | #endif |
| 4025 | base64bits += 16; |
| 4026 | base64buffer = (base64buffer << 16) | ch; |
| 4027 | while (base64bits >= 6) { |
| 4028 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 4029 | base64bits -= 6; |
| 4030 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 4031 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4032 | if (base64bits) |
| 4033 | *out++= TO_BASE64(base64buffer << (6-base64bits) ); |
| 4034 | if (inShift) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4035 | *out++ = '-'; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4036 | if (_PyBytes_Resize(&v, out - start) < 0) |
| 4037 | return NULL; |
| 4038 | return v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4039 | } |
| 4040 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4041 | #undef IS_BASE64 |
| 4042 | #undef FROM_BASE64 |
| 4043 | #undef TO_BASE64 |
| 4044 | #undef DECODE_DIRECT |
| 4045 | #undef ENCODE_DIRECT |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4046 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4047 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 4048 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4049 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4050 | char utf8_code_length[256] = { |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4051 | /* Map UTF-8 encoded prefix byte to sequence length. Zero means |
| 4052 | illegal prefix. See RFC 3629 for details */ |
| 4053 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 00-0F */ |
| 4054 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 4055 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4056 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 4057 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 4058 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 4059 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4060 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 70-7F */ |
| 4061 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 80-8F */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4062 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4063 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4064 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* B0-BF */ |
| 4065 | 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* C0-C1 + C2-CF */ |
| 4066 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* D0-DF */ |
| 4067 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* E0-EF */ |
| 4068 | 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 /* F0-F4 + F5-FF */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4069 | }; |
| 4070 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4071 | PyObject * |
| 4072 | PyUnicode_DecodeUTF8(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4073 | Py_ssize_t size, |
| 4074 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4075 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4076 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 4077 | } |
| 4078 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4079 | /* Mask to check or force alignment of a pointer to C 'long' boundaries */ |
| 4080 | #define LONG_PTR_MASK (size_t) (SIZEOF_LONG - 1) |
| 4081 | |
| 4082 | /* Mask to quickly check whether a C 'long' contains a |
| 4083 | non-ASCII, UTF8-encoded char. */ |
| 4084 | #if (SIZEOF_LONG == 8) |
| 4085 | # define ASCII_CHAR_MASK 0x8080808080808080L |
| 4086 | #elif (SIZEOF_LONG == 4) |
| 4087 | # define ASCII_CHAR_MASK 0x80808080L |
| 4088 | #else |
| 4089 | # error C 'long' size should be either 4 or 8! |
| 4090 | #endif |
| 4091 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4092 | /* Scans a UTF-8 string and returns the maximum character to be expected, |
| 4093 | the size of the decoded unicode string and if any major errors were |
| 4094 | encountered. |
| 4095 | |
| 4096 | This function does check basic UTF-8 sanity, it does however NOT CHECK |
| 4097 | if the string contains surrogates, and if all continuation bytes are |
| 4098 | within the correct ranges, these checks are performed in |
| 4099 | PyUnicode_DecodeUTF8Stateful. |
| 4100 | |
| 4101 | If it sets has_errors to 1, it means the value of unicode_size and max_char |
| 4102 | will be bogus and you should not rely on useful information in them. |
| 4103 | */ |
| 4104 | static Py_UCS4 |
| 4105 | utf8_max_char_size_and_has_errors(const char *s, Py_ssize_t string_size, |
| 4106 | Py_ssize_t *unicode_size, Py_ssize_t* consumed, |
| 4107 | int *has_errors) |
| 4108 | { |
| 4109 | Py_ssize_t n; |
| 4110 | Py_ssize_t char_count = 0; |
| 4111 | Py_UCS4 max_char = 127, new_max; |
| 4112 | Py_UCS4 upper_bound; |
| 4113 | const unsigned char *p = (const unsigned char *)s; |
| 4114 | const unsigned char *end = p + string_size; |
| 4115 | const unsigned char *aligned_end = (const unsigned char *) ((size_t) end & ~LONG_PTR_MASK); |
| 4116 | int err = 0; |
| 4117 | |
| 4118 | for (; p < end && !err; ++p, ++char_count) { |
| 4119 | /* Only check value if it's not a ASCII char... */ |
| 4120 | if (*p < 0x80) { |
| 4121 | /* Fast path, see below in PyUnicode_DecodeUTF8Stateful for |
| 4122 | an explanation. */ |
| 4123 | if (!((size_t) p & LONG_PTR_MASK)) { |
| 4124 | /* Help register allocation */ |
| 4125 | register const unsigned char *_p = p; |
| 4126 | while (_p < aligned_end) { |
| 4127 | unsigned long value = *(unsigned long *) _p; |
| 4128 | if (value & ASCII_CHAR_MASK) |
| 4129 | break; |
| 4130 | _p += SIZEOF_LONG; |
| 4131 | char_count += SIZEOF_LONG; |
| 4132 | } |
| 4133 | p = _p; |
| 4134 | if (p == end) |
| 4135 | break; |
| 4136 | } |
| 4137 | } |
| 4138 | if (*p >= 0x80) { |
| 4139 | n = utf8_code_length[*p]; |
| 4140 | new_max = max_char; |
| 4141 | switch (n) { |
| 4142 | /* invalid start byte */ |
| 4143 | case 0: |
| 4144 | err = 1; |
| 4145 | break; |
| 4146 | case 2: |
| 4147 | /* Code points between 0x00FF and 0x07FF inclusive. |
| 4148 | Approximate the upper bound of the code point, |
| 4149 | if this flips over 255 we can be sure it will be more |
| 4150 | than 255 and the string will need 2 bytes per code coint, |
| 4151 | if it stays under or equal to 255, we can be sure 1 byte |
| 4152 | is enough. |
| 4153 | ((*p & 0b00011111) << 6) | 0b00111111 */ |
| 4154 | upper_bound = ((*p & 0x1F) << 6) | 0x3F; |
| 4155 | if (max_char < upper_bound) |
| 4156 | new_max = upper_bound; |
| 4157 | /* Ensure we track at least that we left ASCII space. */ |
| 4158 | if (new_max < 128) |
| 4159 | new_max = 128; |
| 4160 | break; |
| 4161 | case 3: |
| 4162 | /* Between 0x0FFF and 0xFFFF inclusive, so values are |
| 4163 | always > 255 and <= 65535 and will always need 2 bytes. */ |
| 4164 | if (max_char < 65535) |
| 4165 | new_max = 65535; |
| 4166 | break; |
| 4167 | case 4: |
| 4168 | /* Code point will be above 0xFFFF for sure in this case. */ |
| 4169 | new_max = 65537; |
| 4170 | break; |
| 4171 | /* Internal error, this should be caught by the first if */ |
| 4172 | case 1: |
| 4173 | default: |
| 4174 | assert(0 && "Impossible case in utf8_max_char_and_size"); |
| 4175 | err = 1; |
| 4176 | } |
| 4177 | /* Instead of number of overall bytes for this code point, |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 4178 | n contains the number of following bytes: */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4179 | --n; |
| 4180 | /* Check if the follow up chars are all valid continuation bytes */ |
| 4181 | if (n >= 1) { |
| 4182 | const unsigned char *cont; |
| 4183 | if ((p + n) >= end) { |
| 4184 | if (consumed == 0) |
| 4185 | /* incomplete data, non-incremental decoding */ |
| 4186 | err = 1; |
| 4187 | break; |
| 4188 | } |
| 4189 | for (cont = p + 1; cont < (p + n); ++cont) { |
| 4190 | if ((*cont & 0xc0) != 0x80) { |
| 4191 | err = 1; |
| 4192 | break; |
| 4193 | } |
| 4194 | } |
| 4195 | p += n; |
| 4196 | } |
| 4197 | else |
| 4198 | err = 1; |
| 4199 | max_char = new_max; |
| 4200 | } |
| 4201 | } |
| 4202 | |
| 4203 | if (unicode_size) |
| 4204 | *unicode_size = char_count; |
| 4205 | if (has_errors) |
| 4206 | *has_errors = err; |
| 4207 | return max_char; |
| 4208 | } |
| 4209 | |
| 4210 | /* Similar to PyUnicode_WRITE but can also write into wstr field |
| 4211 | of the legacy unicode representation */ |
| 4212 | #define WRITE_FLEXIBLE_OR_WSTR(kind, buf, index, value) \ |
| 4213 | do { \ |
| 4214 | const int k_ = (kind); \ |
| 4215 | if (k_ == PyUnicode_WCHAR_KIND) \ |
| 4216 | ((Py_UNICODE *)(buf))[(index)] = (Py_UNICODE)(value); \ |
| 4217 | else if (k_ == PyUnicode_1BYTE_KIND) \ |
| 4218 | ((unsigned char *)(buf))[(index)] = (unsigned char)(value); \ |
| 4219 | else if (k_ == PyUnicode_2BYTE_KIND) \ |
| 4220 | ((Py_UCS2 *)(buf))[(index)] = (Py_UCS2)(value); \ |
| 4221 | else \ |
| 4222 | ((Py_UCS4 *)(buf))[(index)] = (Py_UCS4)(value); \ |
| 4223 | } while (0) |
| 4224 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4225 | PyObject * |
| 4226 | PyUnicode_DecodeUTF8Stateful(const char *s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4227 | Py_ssize_t size, |
| 4228 | const char *errors, |
| 4229 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4230 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4231 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4232 | int n; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4233 | int k; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4234 | Py_ssize_t startinpos; |
| 4235 | Py_ssize_t endinpos; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4236 | const char *e, *aligned_end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4237 | PyUnicodeObject *unicode; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4238 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4239 | PyObject *errorHandler = NULL; |
| 4240 | PyObject *exc = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4241 | Py_UCS4 maxchar = 0; |
| 4242 | Py_ssize_t unicode_size; |
| 4243 | Py_ssize_t i; |
| 4244 | int kind; |
| 4245 | void *data; |
| 4246 | int has_errors; |
| 4247 | Py_UNICODE *error_outptr; |
| 4248 | #if SIZEOF_WCHAR_T == 2 |
| 4249 | Py_ssize_t wchar_offset = 0; |
| 4250 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4251 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4252 | if (size == 0) { |
| 4253 | if (consumed) |
| 4254 | *consumed = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4255 | return (PyObject *)PyUnicode_New(0, 0); |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4256 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4257 | maxchar = utf8_max_char_size_and_has_errors(s, size, &unicode_size, |
| 4258 | consumed, &has_errors); |
| 4259 | if (has_errors) { |
| 4260 | unicode = _PyUnicode_New(size); |
| 4261 | if (!unicode) |
| 4262 | return NULL; |
| 4263 | kind = PyUnicode_WCHAR_KIND; |
| 4264 | data = PyUnicode_AS_UNICODE(unicode); |
| 4265 | assert(data != NULL); |
| 4266 | } |
| 4267 | else { |
| 4268 | unicode = (PyUnicodeObject *)PyUnicode_New(unicode_size, maxchar); |
| 4269 | if (!unicode) |
| 4270 | return NULL; |
| 4271 | /* When the string is ASCII only, just use memcpy and return. |
| 4272 | unicode_size may be != size if there is an incomplete UTF-8 |
| 4273 | sequence at the end of the ASCII block. */ |
| 4274 | if (maxchar < 128 && size == unicode_size) { |
| 4275 | Py_MEMCPY(PyUnicode_1BYTE_DATA(unicode), s, unicode_size); |
| 4276 | return (PyObject *)unicode; |
| 4277 | } |
| 4278 | kind = PyUnicode_KIND(unicode); |
| 4279 | data = PyUnicode_DATA(unicode); |
| 4280 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4281 | /* Unpack UTF-8 encoded data */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4282 | i = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4283 | e = s + size; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4284 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4285 | |
| 4286 | while (s < e) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4287 | Py_UCS4 ch = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4288 | |
| 4289 | if (ch < 0x80) { |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4290 | /* Fast path for runs of ASCII characters. Given that common UTF-8 |
| 4291 | input will consist of an overwhelming majority of ASCII |
| 4292 | characters, we try to optimize for this case by checking |
| 4293 | as many characters as a C 'long' can contain. |
| 4294 | First, check if we can do an aligned read, as most CPUs have |
| 4295 | a penalty for unaligned reads. |
| 4296 | */ |
| 4297 | if (!((size_t) s & LONG_PTR_MASK)) { |
| 4298 | /* Help register allocation */ |
| 4299 | register const char *_s = s; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4300 | register Py_ssize_t _i = i; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4301 | while (_s < aligned_end) { |
| 4302 | /* Read a whole long at a time (either 4 or 8 bytes), |
| 4303 | and do a fast unrolled copy if it only contains ASCII |
| 4304 | characters. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4305 | unsigned long value = *(unsigned long *) _s; |
| 4306 | if (value & ASCII_CHAR_MASK) |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4307 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4308 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+0, _s[0]); |
| 4309 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+1, _s[1]); |
| 4310 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+2, _s[2]); |
| 4311 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+3, _s[3]); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4312 | #if (SIZEOF_LONG == 8) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4313 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+4, _s[4]); |
| 4314 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+5, _s[5]); |
| 4315 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+6, _s[6]); |
| 4316 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+7, _s[7]); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4317 | #endif |
| 4318 | _s += SIZEOF_LONG; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4319 | _i += SIZEOF_LONG; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4320 | } |
| 4321 | s = _s; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4322 | i = _i; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4323 | if (s == e) |
| 4324 | break; |
| 4325 | ch = (unsigned char)*s; |
| 4326 | } |
| 4327 | } |
| 4328 | |
| 4329 | if (ch < 0x80) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4330 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4331 | s++; |
| 4332 | continue; |
| 4333 | } |
| 4334 | |
| 4335 | n = utf8_code_length[ch]; |
| 4336 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4337 | if (s + n > e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4338 | if (consumed) |
| 4339 | break; |
| 4340 | else { |
| 4341 | errmsg = "unexpected end of data"; |
| 4342 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4343 | endinpos = startinpos+1; |
| 4344 | for (k=1; (k < size-startinpos) && ((s[k]&0xC0) == 0x80); k++) |
| 4345 | endinpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4346 | goto utf8Error; |
| 4347 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4348 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4349 | |
| 4350 | switch (n) { |
| 4351 | |
| 4352 | case 0: |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4353 | errmsg = "invalid start byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4354 | startinpos = s-starts; |
| 4355 | endinpos = startinpos+1; |
| 4356 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4357 | |
| 4358 | case 1: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4359 | errmsg = "internal error"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4360 | startinpos = s-starts; |
| 4361 | endinpos = startinpos+1; |
| 4362 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4363 | |
| 4364 | case 2: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4365 | if ((s[1] & 0xc0) != 0x80) { |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4366 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4367 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4368 | endinpos = startinpos + 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4369 | goto utf8Error; |
| 4370 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4371 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4372 | assert ((ch > 0x007F) && (ch <= 0x07FF)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4373 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4374 | break; |
| 4375 | |
| 4376 | case 3: |
Ezio Melotti | 9bf2b3a | 2010-07-03 04:52:19 +0000 | [diff] [blame] | 4377 | /* Decoding UTF-8 sequences in range \xed\xa0\x80-\xed\xbf\xbf |
| 4378 | will result in surrogates in range d800-dfff. Surrogates are |
| 4379 | not valid UTF-8 so they are rejected. |
| 4380 | See http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf |
| 4381 | (table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4382 | if ((s[1] & 0xc0) != 0x80 || |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4383 | (s[2] & 0xc0) != 0x80 || |
| 4384 | ((unsigned char)s[0] == 0xE0 && |
| 4385 | (unsigned char)s[1] < 0xA0) || |
| 4386 | ((unsigned char)s[0] == 0xED && |
| 4387 | (unsigned char)s[1] > 0x9F)) { |
| 4388 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4389 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4390 | endinpos = startinpos + 1; |
| 4391 | |
| 4392 | /* if s[1] first two bits are 1 and 0, then the invalid |
| 4393 | continuation byte is s[2], so increment endinpos by 1, |
| 4394 | if not, s[1] is invalid and endinpos doesn't need to |
| 4395 | be incremented. */ |
| 4396 | if ((s[1] & 0xC0) == 0x80) |
| 4397 | endinpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4398 | goto utf8Error; |
| 4399 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4400 | ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4401 | assert ((ch > 0x07FF) && (ch <= 0xFFFF)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4402 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, ch); |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4403 | break; |
| 4404 | |
| 4405 | case 4: |
| 4406 | if ((s[1] & 0xc0) != 0x80 || |
| 4407 | (s[2] & 0xc0) != 0x80 || |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4408 | (s[3] & 0xc0) != 0x80 || |
| 4409 | ((unsigned char)s[0] == 0xF0 && |
| 4410 | (unsigned char)s[1] < 0x90) || |
| 4411 | ((unsigned char)s[0] == 0xF4 && |
| 4412 | (unsigned char)s[1] > 0x8F)) { |
| 4413 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4414 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4415 | endinpos = startinpos + 1; |
| 4416 | if ((s[1] & 0xC0) == 0x80) { |
| 4417 | endinpos++; |
| 4418 | if ((s[2] & 0xC0) == 0x80) |
| 4419 | endinpos++; |
| 4420 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4421 | goto utf8Error; |
| 4422 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4423 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4424 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
| 4425 | assert ((ch > 0xFFFF) && (ch <= 0x10ffff)); |
| 4426 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4427 | /* If the string is flexible or we have native UCS-4, write |
| 4428 | directly.. */ |
| 4429 | if (sizeof(Py_UNICODE) > 2 || kind != PyUnicode_WCHAR_KIND) |
| 4430 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, ch); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4431 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4432 | else { |
| 4433 | /* compute and append the two surrogates: */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4434 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4435 | /* translate from 10000..10FFFF to 0..FFFF */ |
| 4436 | ch -= 0x10000; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4437 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4438 | /* high surrogate = top 10 bits added to D800 */ |
| 4439 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, |
| 4440 | (Py_UNICODE)(0xD800 + (ch >> 10))); |
| 4441 | |
| 4442 | /* low surrogate = bottom 10 bits added to DC00 */ |
| 4443 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, |
| 4444 | (Py_UNICODE)(0xDC00 + (ch & 0x03FF))); |
| 4445 | } |
| 4446 | #if SIZEOF_WCHAR_T == 2 |
| 4447 | wchar_offset++; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 4448 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4449 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4450 | } |
| 4451 | s += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4452 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4453 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4454 | utf8Error: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4455 | /* If this is not yet a resizable string, make it one.. */ |
| 4456 | if (kind != PyUnicode_WCHAR_KIND) { |
| 4457 | const Py_UNICODE *u; |
| 4458 | PyUnicodeObject *new_unicode = _PyUnicode_New(size); |
| 4459 | if (!new_unicode) |
| 4460 | goto onError; |
| 4461 | u = PyUnicode_AsUnicode((PyObject *)unicode); |
| 4462 | if (!u) |
| 4463 | goto onError; |
| 4464 | #if SIZEOF_WCHAR_T == 2 |
| 4465 | i += wchar_offset; |
| 4466 | #endif |
| 4467 | Py_UNICODE_COPY(PyUnicode_AS_UNICODE(new_unicode), u, i); |
| 4468 | Py_DECREF(unicode); |
| 4469 | unicode = new_unicode; |
| 4470 | kind = 0; |
| 4471 | data = PyUnicode_AS_UNICODE(new_unicode); |
| 4472 | assert(data != NULL); |
| 4473 | } |
| 4474 | error_outptr = PyUnicode_AS_UNICODE(unicode) + i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4475 | if (unicode_decode_call_errorhandler( |
| 4476 | errors, &errorHandler, |
| 4477 | "utf8", errmsg, |
| 4478 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4479 | &unicode, &i, &error_outptr)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4480 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4481 | /* Update data because unicode_decode_call_errorhandler might have |
| 4482 | re-created or resized the unicode object. */ |
| 4483 | data = PyUnicode_AS_UNICODE(unicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4484 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4485 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4486 | /* Ensure the unicode_size calculation above was correct: */ |
| 4487 | assert(kind == PyUnicode_WCHAR_KIND || i == unicode_size); |
| 4488 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4489 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4490 | *consumed = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4491 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4492 | /* Adjust length and ready string when it contained errors and |
| 4493 | is of the old resizable kind. */ |
| 4494 | if (kind == PyUnicode_WCHAR_KIND) { |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 4495 | if (PyUnicode_Resize((PyObject**)&unicode, i) < 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4496 | goto onError; |
| 4497 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4498 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4499 | Py_XDECREF(errorHandler); |
| 4500 | Py_XDECREF(exc); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 4501 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 4502 | if (_PyUnicode_READY_REPLACE(&unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4503 | Py_DECREF(unicode); |
| 4504 | return NULL; |
| 4505 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 4506 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 4507 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4508 | return (PyObject *)unicode; |
| 4509 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4510 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4511 | Py_XDECREF(errorHandler); |
| 4512 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4513 | Py_DECREF(unicode); |
| 4514 | return NULL; |
| 4515 | } |
| 4516 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4517 | #undef WRITE_FLEXIBLE_OR_WSTR |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4518 | |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4519 | #ifdef __APPLE__ |
| 4520 | |
| 4521 | /* Simplified UTF-8 decoder using surrogateescape error handler, |
| 4522 | used to decode the command line arguments on Mac OS X. */ |
| 4523 | |
| 4524 | wchar_t* |
| 4525 | _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size) |
| 4526 | { |
| 4527 | int n; |
| 4528 | const char *e; |
| 4529 | wchar_t *unicode, *p; |
| 4530 | |
| 4531 | /* Note: size will always be longer than the resulting Unicode |
| 4532 | character count */ |
| 4533 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < (size + 1)) { |
| 4534 | PyErr_NoMemory(); |
| 4535 | return NULL; |
| 4536 | } |
| 4537 | unicode = PyMem_Malloc((size + 1) * sizeof(wchar_t)); |
| 4538 | if (!unicode) |
| 4539 | return NULL; |
| 4540 | |
| 4541 | /* Unpack UTF-8 encoded data */ |
| 4542 | p = unicode; |
| 4543 | e = s + size; |
| 4544 | while (s < e) { |
| 4545 | Py_UCS4 ch = (unsigned char)*s; |
| 4546 | |
| 4547 | if (ch < 0x80) { |
| 4548 | *p++ = (wchar_t)ch; |
| 4549 | s++; |
| 4550 | continue; |
| 4551 | } |
| 4552 | |
| 4553 | n = utf8_code_length[ch]; |
| 4554 | if (s + n > e) { |
| 4555 | goto surrogateescape; |
| 4556 | } |
| 4557 | |
| 4558 | switch (n) { |
| 4559 | case 0: |
| 4560 | case 1: |
| 4561 | goto surrogateescape; |
| 4562 | |
| 4563 | case 2: |
| 4564 | if ((s[1] & 0xc0) != 0x80) |
| 4565 | goto surrogateescape; |
| 4566 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
| 4567 | assert ((ch > 0x007F) && (ch <= 0x07FF)); |
| 4568 | *p++ = (wchar_t)ch; |
| 4569 | break; |
| 4570 | |
| 4571 | case 3: |
| 4572 | /* Decoding UTF-8 sequences in range \xed\xa0\x80-\xed\xbf\xbf |
| 4573 | will result in surrogates in range d800-dfff. Surrogates are |
| 4574 | not valid UTF-8 so they are rejected. |
| 4575 | See http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf |
| 4576 | (table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt */ |
| 4577 | if ((s[1] & 0xc0) != 0x80 || |
| 4578 | (s[2] & 0xc0) != 0x80 || |
| 4579 | ((unsigned char)s[0] == 0xE0 && |
| 4580 | (unsigned char)s[1] < 0xA0) || |
| 4581 | ((unsigned char)s[0] == 0xED && |
| 4582 | (unsigned char)s[1] > 0x9F)) { |
| 4583 | |
| 4584 | goto surrogateescape; |
| 4585 | } |
| 4586 | ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); |
| 4587 | assert ((ch > 0x07FF) && (ch <= 0xFFFF)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4588 | *p++ = (wchar_t)ch; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4589 | break; |
| 4590 | |
| 4591 | case 4: |
| 4592 | if ((s[1] & 0xc0) != 0x80 || |
| 4593 | (s[2] & 0xc0) != 0x80 || |
| 4594 | (s[3] & 0xc0) != 0x80 || |
| 4595 | ((unsigned char)s[0] == 0xF0 && |
| 4596 | (unsigned char)s[1] < 0x90) || |
| 4597 | ((unsigned char)s[0] == 0xF4 && |
| 4598 | (unsigned char)s[1] > 0x8F)) { |
| 4599 | goto surrogateescape; |
| 4600 | } |
| 4601 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
| 4602 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
| 4603 | assert ((ch > 0xFFFF) && (ch <= 0x10ffff)); |
| 4604 | |
| 4605 | #if SIZEOF_WCHAR_T == 4 |
| 4606 | *p++ = (wchar_t)ch; |
| 4607 | #else |
| 4608 | /* compute and append the two surrogates: */ |
| 4609 | |
| 4610 | /* translate from 10000..10FFFF to 0..FFFF */ |
| 4611 | ch -= 0x10000; |
| 4612 | |
| 4613 | /* high surrogate = top 10 bits added to D800 */ |
| 4614 | *p++ = (wchar_t)(0xD800 + (ch >> 10)); |
| 4615 | |
| 4616 | /* low surrogate = bottom 10 bits added to DC00 */ |
| 4617 | *p++ = (wchar_t)(0xDC00 + (ch & 0x03FF)); |
| 4618 | #endif |
| 4619 | break; |
| 4620 | } |
| 4621 | s += n; |
| 4622 | continue; |
| 4623 | |
| 4624 | surrogateescape: |
| 4625 | *p++ = 0xDC00 + ch; |
| 4626 | s++; |
| 4627 | } |
| 4628 | *p = L'\0'; |
| 4629 | return unicode; |
| 4630 | } |
| 4631 | |
| 4632 | #endif /* __APPLE__ */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4633 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4634 | /* Primary internal function which creates utf8 encoded bytes objects. |
| 4635 | |
| 4636 | Allocation strategy: if the string is short, convert into a stack buffer |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4637 | and allocate exactly as much space needed at the end. Else allocate the |
| 4638 | maximum possible needed (4 result bytes per Unicode character), and return |
| 4639 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4640 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 4641 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4642 | _PyUnicode_AsUTF8String(PyObject *obj, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4643 | { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4644 | #define MAX_SHORT_UNICHARS 300 /* largest size we'll do on the stack */ |
Tim Peters | 0eca65c | 2002-04-21 17:28:06 +0000 | [diff] [blame] | 4645 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4646 | Py_ssize_t i; /* index into s of next input byte */ |
| 4647 | PyObject *result; /* result string object */ |
| 4648 | char *p; /* next free byte in output buffer */ |
| 4649 | Py_ssize_t nallocated; /* number of result bytes allocated */ |
| 4650 | Py_ssize_t nneeded; /* number of result bytes needed */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4651 | char stackbuf[MAX_SHORT_UNICHARS * 4]; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4652 | PyObject *errorHandler = NULL; |
| 4653 | PyObject *exc = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4654 | int kind; |
| 4655 | void *data; |
| 4656 | Py_ssize_t size; |
| 4657 | PyUnicodeObject *unicode = (PyUnicodeObject *)obj; |
| 4658 | #if SIZEOF_WCHAR_T == 2 |
| 4659 | Py_ssize_t wchar_offset = 0; |
| 4660 | #endif |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 4661 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4662 | if (!PyUnicode_Check(unicode)) { |
| 4663 | PyErr_BadArgument(); |
| 4664 | return NULL; |
| 4665 | } |
| 4666 | |
| 4667 | if (PyUnicode_READY(unicode) == -1) |
| 4668 | return NULL; |
| 4669 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 4670 | if (PyUnicode_UTF8(unicode)) |
| 4671 | return PyBytes_FromStringAndSize(PyUnicode_UTF8(unicode), |
| 4672 | PyUnicode_UTF8_LENGTH(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4673 | |
| 4674 | kind = PyUnicode_KIND(unicode); |
| 4675 | data = PyUnicode_DATA(unicode); |
| 4676 | size = PyUnicode_GET_LENGTH(unicode); |
| 4677 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4678 | assert(size >= 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4679 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4680 | if (size <= MAX_SHORT_UNICHARS) { |
| 4681 | /* Write into the stack buffer; nallocated can't overflow. |
| 4682 | * At the end, we'll allocate exactly as much heap space as it |
| 4683 | * turns out we need. |
| 4684 | */ |
| 4685 | nallocated = Py_SAFE_DOWNCAST(sizeof(stackbuf), size_t, int); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4686 | result = NULL; /* will allocate after we're done */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4687 | p = stackbuf; |
| 4688 | } |
| 4689 | else { |
| 4690 | /* Overallocate on the heap, and give the excess back at the end. */ |
| 4691 | nallocated = size * 4; |
| 4692 | if (nallocated / 4 != size) /* overflow! */ |
| 4693 | return PyErr_NoMemory(); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4694 | result = PyBytes_FromStringAndSize(NULL, nallocated); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4695 | if (result == NULL) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4696 | return NULL; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4697 | p = PyBytes_AS_STRING(result); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4698 | } |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4699 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4700 | for (i = 0; i < size;) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4701 | Py_UCS4 ch = PyUnicode_READ(kind, data, i++); |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 4702 | |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4703 | if (ch < 0x80) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4704 | /* Encode ASCII */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4705 | *p++ = (char) ch; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 4706 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4707 | else if (ch < 0x0800) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4708 | /* Encode Latin-1 */ |
Marc-André Lemburg | dc724d6 | 2002-02-06 18:20:19 +0000 | [diff] [blame] | 4709 | *p++ = (char)(0xc0 | (ch >> 6)); |
| 4710 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4711 | } else if (0xD800 <= ch && ch <= 0xDFFF) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4712 | Py_ssize_t newpos; |
| 4713 | PyObject *rep; |
| 4714 | Py_ssize_t repsize, k, startpos; |
| 4715 | startpos = i-1; |
| 4716 | #if SIZEOF_WCHAR_T == 2 |
| 4717 | startpos += wchar_offset; |
Victor Stinner | 445a623 | 2010-04-22 20:01:57 +0000 | [diff] [blame] | 4718 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4719 | rep = unicode_encode_call_errorhandler( |
| 4720 | errors, &errorHandler, "utf-8", "surrogates not allowed", |
| 4721 | PyUnicode_AS_UNICODE(unicode), PyUnicode_GET_SIZE(unicode), |
| 4722 | &exc, startpos, startpos+1, &newpos); |
| 4723 | if (!rep) |
| 4724 | goto error; |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4725 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4726 | if (PyBytes_Check(rep)) |
| 4727 | repsize = PyBytes_GET_SIZE(rep); |
| 4728 | else |
| 4729 | repsize = PyUnicode_GET_SIZE(rep); |
| 4730 | |
| 4731 | if (repsize > 4) { |
| 4732 | Py_ssize_t offset; |
| 4733 | |
| 4734 | if (result == NULL) |
| 4735 | offset = p - stackbuf; |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4736 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4737 | offset = p - PyBytes_AS_STRING(result); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4738 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4739 | if (nallocated > PY_SSIZE_T_MAX - repsize + 4) { |
| 4740 | /* integer overflow */ |
| 4741 | PyErr_NoMemory(); |
| 4742 | goto error; |
| 4743 | } |
| 4744 | nallocated += repsize - 4; |
| 4745 | if (result != NULL) { |
| 4746 | if (_PyBytes_Resize(&result, nallocated) < 0) |
| 4747 | goto error; |
| 4748 | } else { |
| 4749 | result = PyBytes_FromStringAndSize(NULL, nallocated); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4750 | if (result == NULL) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4751 | goto error; |
| 4752 | Py_MEMCPY(PyBytes_AS_STRING(result), stackbuf, offset); |
| 4753 | } |
| 4754 | p = PyBytes_AS_STRING(result) + offset; |
| 4755 | } |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4756 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4757 | if (PyBytes_Check(rep)) { |
| 4758 | char *prep = PyBytes_AS_STRING(rep); |
| 4759 | for(k = repsize; k > 0; k--) |
| 4760 | *p++ = *prep++; |
| 4761 | } else /* rep is unicode */ { |
| 4762 | const Py_UNICODE *prep = PyUnicode_AS_UNICODE(rep); |
| 4763 | Py_UNICODE c; |
| 4764 | |
| 4765 | for(k=0; k<repsize; k++) { |
| 4766 | c = prep[k]; |
| 4767 | if (0x80 <= c) { |
| 4768 | raise_encode_exception(&exc, "utf-8", |
| 4769 | PyUnicode_AS_UNICODE(unicode), |
| 4770 | size, i-1, i, |
| 4771 | "surrogates not allowed"); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4772 | goto error; |
| 4773 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4774 | *p++ = (char)prep[k]; |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4775 | } |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4776 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4777 | Py_DECREF(rep); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4778 | } else if (ch < 0x10000) { |
| 4779 | *p++ = (char)(0xe0 | (ch >> 12)); |
| 4780 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 4781 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 4782 | } else /* ch >= 0x10000 */ { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4783 | /* Encode UCS4 Unicode ordinals */ |
| 4784 | *p++ = (char)(0xf0 | (ch >> 18)); |
| 4785 | *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); |
| 4786 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 4787 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4788 | #if SIZEOF_WCHAR_T == 2 |
| 4789 | wchar_offset++; |
| 4790 | #endif |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4791 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4792 | } |
Tim Peters | 0eca65c | 2002-04-21 17:28:06 +0000 | [diff] [blame] | 4793 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4794 | if (result == NULL) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4795 | /* This was stack allocated. */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4796 | nneeded = p - stackbuf; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4797 | assert(nneeded <= nallocated); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4798 | result = PyBytes_FromStringAndSize(stackbuf, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4799 | } |
| 4800 | else { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 4801 | /* Cut back to size actually needed. */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4802 | nneeded = p - PyBytes_AS_STRING(result); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4803 | assert(nneeded <= nallocated); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4804 | _PyBytes_Resize(&result, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4805 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4806 | |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4807 | Py_XDECREF(errorHandler); |
| 4808 | Py_XDECREF(exc); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4809 | return result; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4810 | error: |
| 4811 | Py_XDECREF(errorHandler); |
| 4812 | Py_XDECREF(exc); |
| 4813 | Py_XDECREF(result); |
| 4814 | return NULL; |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4815 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4816 | #undef MAX_SHORT_UNICHARS |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4817 | } |
| 4818 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4819 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4820 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
| 4821 | Py_ssize_t size, |
| 4822 | const char *errors) |
| 4823 | { |
| 4824 | PyObject *v, *unicode; |
| 4825 | |
| 4826 | unicode = PyUnicode_FromUnicode(s, size); |
| 4827 | if (unicode == NULL) |
| 4828 | return NULL; |
| 4829 | v = _PyUnicode_AsUTF8String(unicode, errors); |
| 4830 | Py_DECREF(unicode); |
| 4831 | return v; |
| 4832 | } |
| 4833 | |
| 4834 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4835 | PyUnicode_AsUTF8String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4836 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4837 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4838 | } |
| 4839 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4840 | /* --- UTF-32 Codec ------------------------------------------------------- */ |
| 4841 | |
| 4842 | PyObject * |
| 4843 | PyUnicode_DecodeUTF32(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4844 | Py_ssize_t size, |
| 4845 | const char *errors, |
| 4846 | int *byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4847 | { |
| 4848 | return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); |
| 4849 | } |
| 4850 | |
| 4851 | PyObject * |
| 4852 | PyUnicode_DecodeUTF32Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4853 | Py_ssize_t size, |
| 4854 | const char *errors, |
| 4855 | int *byteorder, |
| 4856 | Py_ssize_t *consumed) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4857 | { |
| 4858 | const char *starts = s; |
| 4859 | Py_ssize_t startinpos; |
| 4860 | Py_ssize_t endinpos; |
| 4861 | Py_ssize_t outpos; |
| 4862 | PyUnicodeObject *unicode; |
| 4863 | Py_UNICODE *p; |
| 4864 | #ifndef Py_UNICODE_WIDE |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 4865 | int pairs = 0; |
Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 4866 | const unsigned char *qq; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4867 | #else |
| 4868 | const int pairs = 0; |
| 4869 | #endif |
Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 4870 | const unsigned char *q, *e; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4871 | int bo = 0; /* assume native ordering by default */ |
| 4872 | const char *errmsg = ""; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4873 | /* Offsets from q for retrieving bytes in the right order. */ |
| 4874 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 4875 | int iorder[] = {0, 1, 2, 3}; |
| 4876 | #else |
| 4877 | int iorder[] = {3, 2, 1, 0}; |
| 4878 | #endif |
| 4879 | PyObject *errorHandler = NULL; |
| 4880 | PyObject *exc = NULL; |
Victor Stinner | 313a120 | 2010-06-11 23:56:51 +0000 | [diff] [blame] | 4881 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4882 | q = (unsigned char *)s; |
| 4883 | e = q + size; |
| 4884 | |
| 4885 | if (byteorder) |
| 4886 | bo = *byteorder; |
| 4887 | |
| 4888 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 4889 | byte order setting accordingly. In native mode, the leading BOM |
| 4890 | mark is skipped, in all other modes, it is copied to the output |
| 4891 | stream as-is (giving a ZWNBSP character). */ |
| 4892 | if (bo == 0) { |
| 4893 | if (size >= 4) { |
| 4894 | const Py_UCS4 bom = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4895 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4896 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4897 | if (bom == 0x0000FEFF) { |
| 4898 | q += 4; |
| 4899 | bo = -1; |
| 4900 | } |
| 4901 | else if (bom == 0xFFFE0000) { |
| 4902 | q += 4; |
| 4903 | bo = 1; |
| 4904 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4905 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4906 | if (bom == 0x0000FEFF) { |
| 4907 | q += 4; |
| 4908 | bo = 1; |
| 4909 | } |
| 4910 | else if (bom == 0xFFFE0000) { |
| 4911 | q += 4; |
| 4912 | bo = -1; |
| 4913 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4914 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4915 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4916 | } |
| 4917 | |
| 4918 | if (bo == -1) { |
| 4919 | /* force LE */ |
| 4920 | iorder[0] = 0; |
| 4921 | iorder[1] = 1; |
| 4922 | iorder[2] = 2; |
| 4923 | iorder[3] = 3; |
| 4924 | } |
| 4925 | else if (bo == 1) { |
| 4926 | /* force BE */ |
| 4927 | iorder[0] = 3; |
| 4928 | iorder[1] = 2; |
| 4929 | iorder[2] = 1; |
| 4930 | iorder[3] = 0; |
| 4931 | } |
| 4932 | |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 4933 | /* On narrow builds we split characters outside the BMP into two |
| 4934 | codepoints => count how much extra space we need. */ |
| 4935 | #ifndef Py_UNICODE_WIDE |
| 4936 | for (qq = q; qq < e; qq += 4) |
| 4937 | if (qq[iorder[2]] != 0 || qq[iorder[3]] != 0) |
| 4938 | pairs++; |
| 4939 | #endif |
| 4940 | |
| 4941 | /* This might be one to much, because of a BOM */ |
| 4942 | unicode = _PyUnicode_New((size+3)/4+pairs); |
| 4943 | if (!unicode) |
| 4944 | return NULL; |
| 4945 | if (size == 0) |
| 4946 | return (PyObject *)unicode; |
| 4947 | |
| 4948 | /* Unpack UTF-32 encoded data */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4949 | p = PyUnicode_AS_UNICODE(unicode); |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 4950 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4951 | while (q < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4952 | Py_UCS4 ch; |
| 4953 | /* remaining bytes at the end? (size should be divisible by 4) */ |
| 4954 | if (e-q<4) { |
| 4955 | if (consumed) |
| 4956 | break; |
| 4957 | errmsg = "truncated data"; |
| 4958 | startinpos = ((const char *)q)-starts; |
| 4959 | endinpos = ((const char *)e)-starts; |
| 4960 | goto utf32Error; |
| 4961 | /* The remaining input chars are ignored if the callback |
| 4962 | chooses to skip the input */ |
| 4963 | } |
| 4964 | ch = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
| 4965 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4966 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4967 | if (ch >= 0x110000) |
| 4968 | { |
| 4969 | errmsg = "codepoint not in range(0x110000)"; |
| 4970 | startinpos = ((const char *)q)-starts; |
| 4971 | endinpos = startinpos+4; |
| 4972 | goto utf32Error; |
| 4973 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4974 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4975 | if (ch >= 0x10000) |
| 4976 | { |
| 4977 | *p++ = 0xD800 | ((ch-0x10000) >> 10); |
| 4978 | *p++ = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 4979 | } |
| 4980 | else |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4981 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4982 | *p++ = ch; |
| 4983 | q += 4; |
| 4984 | continue; |
| 4985 | utf32Error: |
| 4986 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 4987 | if (unicode_decode_call_errorhandler( |
| 4988 | errors, &errorHandler, |
| 4989 | "utf32", errmsg, |
| 4990 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, |
| 4991 | &unicode, &outpos, &p)) |
| 4992 | goto onError; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4993 | } |
| 4994 | |
| 4995 | if (byteorder) |
| 4996 | *byteorder = bo; |
| 4997 | |
| 4998 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4999 | *consumed = (const char *)q-starts; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5000 | |
| 5001 | /* Adjust length */ |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 5002 | if (PyUnicode_Resize((PyObject**)&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5003 | goto onError; |
| 5004 | |
| 5005 | Py_XDECREF(errorHandler); |
| 5006 | Py_XDECREF(exc); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 5007 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 5008 | if (_PyUnicode_READY_REPLACE(&unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5009 | Py_DECREF(unicode); |
| 5010 | return NULL; |
| 5011 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 5012 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 5013 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5014 | return (PyObject *)unicode; |
| 5015 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5016 | onError: |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5017 | Py_DECREF(unicode); |
| 5018 | Py_XDECREF(errorHandler); |
| 5019 | Py_XDECREF(exc); |
| 5020 | return NULL; |
| 5021 | } |
| 5022 | |
| 5023 | PyObject * |
| 5024 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5025 | Py_ssize_t size, |
| 5026 | const char *errors, |
| 5027 | int byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5028 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5029 | PyObject *v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5030 | unsigned char *p; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5031 | Py_ssize_t nsize, bytesize; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5032 | #ifndef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5033 | Py_ssize_t i, pairs; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5034 | #else |
| 5035 | const int pairs = 0; |
| 5036 | #endif |
| 5037 | /* Offsets from p for storing byte pairs in the right order. */ |
| 5038 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5039 | int iorder[] = {0, 1, 2, 3}; |
| 5040 | #else |
| 5041 | int iorder[] = {3, 2, 1, 0}; |
| 5042 | #endif |
| 5043 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5044 | #define STORECHAR(CH) \ |
| 5045 | do { \ |
| 5046 | p[iorder[3]] = ((CH) >> 24) & 0xff; \ |
| 5047 | p[iorder[2]] = ((CH) >> 16) & 0xff; \ |
| 5048 | p[iorder[1]] = ((CH) >> 8) & 0xff; \ |
| 5049 | p[iorder[0]] = (CH) & 0xff; \ |
| 5050 | p += 4; \ |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5051 | } while(0) |
| 5052 | |
| 5053 | /* In narrow builds we can output surrogate pairs as one codepoint, |
| 5054 | so we need less space. */ |
| 5055 | #ifndef Py_UNICODE_WIDE |
| 5056 | for (i = pairs = 0; i < size-1; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5057 | if (0xD800 <= s[i] && s[i] <= 0xDBFF && |
| 5058 | 0xDC00 <= s[i+1] && s[i+1] <= 0xDFFF) |
| 5059 | pairs++; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5060 | #endif |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5061 | nsize = (size - pairs + (byteorder == 0)); |
| 5062 | bytesize = nsize * 4; |
| 5063 | if (bytesize / 4 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5064 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5065 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5066 | if (v == NULL) |
| 5067 | return NULL; |
| 5068 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5069 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5070 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5071 | STORECHAR(0xFEFF); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5072 | if (size == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5073 | goto done; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5074 | |
| 5075 | if (byteorder == -1) { |
| 5076 | /* force LE */ |
| 5077 | iorder[0] = 0; |
| 5078 | iorder[1] = 1; |
| 5079 | iorder[2] = 2; |
| 5080 | iorder[3] = 3; |
| 5081 | } |
| 5082 | else if (byteorder == 1) { |
| 5083 | /* force BE */ |
| 5084 | iorder[0] = 3; |
| 5085 | iorder[1] = 2; |
| 5086 | iorder[2] = 1; |
| 5087 | iorder[3] = 0; |
| 5088 | } |
| 5089 | |
| 5090 | while (size-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5091 | Py_UCS4 ch = *s++; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5092 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5093 | if (0xD800 <= ch && ch <= 0xDBFF && size > 0) { |
| 5094 | Py_UCS4 ch2 = *s; |
| 5095 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
| 5096 | ch = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
| 5097 | s++; |
| 5098 | size--; |
| 5099 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5100 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5101 | #endif |
| 5102 | STORECHAR(ch); |
| 5103 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5104 | |
| 5105 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5106 | return v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5107 | #undef STORECHAR |
| 5108 | } |
| 5109 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5110 | PyObject * |
| 5111 | PyUnicode_AsUTF32String(PyObject *unicode) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5112 | { |
| 5113 | if (!PyUnicode_Check(unicode)) { |
| 5114 | PyErr_BadArgument(); |
| 5115 | return NULL; |
| 5116 | } |
| 5117 | return PyUnicode_EncodeUTF32(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5118 | PyUnicode_GET_SIZE(unicode), |
| 5119 | NULL, |
| 5120 | 0); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5121 | } |
| 5122 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5123 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 5124 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5125 | PyObject * |
| 5126 | PyUnicode_DecodeUTF16(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5127 | Py_ssize_t size, |
| 5128 | const char *errors, |
| 5129 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5130 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5131 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 5132 | } |
| 5133 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5134 | /* Two masks for fast checking of whether a C 'long' may contain |
| 5135 | UTF16-encoded surrogate characters. This is an efficient heuristic, |
| 5136 | assuming that non-surrogate characters with a code point >= 0x8000 are |
| 5137 | rare in most input. |
| 5138 | FAST_CHAR_MASK is used when the input is in native byte ordering, |
| 5139 | SWAPPED_FAST_CHAR_MASK when the input is in byteswapped ordering. |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5140 | */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5141 | #if (SIZEOF_LONG == 8) |
| 5142 | # define FAST_CHAR_MASK 0x8000800080008000L |
| 5143 | # define SWAPPED_FAST_CHAR_MASK 0x0080008000800080L |
| 5144 | #elif (SIZEOF_LONG == 4) |
| 5145 | # define FAST_CHAR_MASK 0x80008000L |
| 5146 | # define SWAPPED_FAST_CHAR_MASK 0x00800080L |
| 5147 | #else |
| 5148 | # error C 'long' size should be either 4 or 8! |
| 5149 | #endif |
| 5150 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5151 | PyObject * |
| 5152 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5153 | Py_ssize_t size, |
| 5154 | const char *errors, |
| 5155 | int *byteorder, |
| 5156 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5157 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5158 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5159 | Py_ssize_t startinpos; |
| 5160 | Py_ssize_t endinpos; |
| 5161 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5162 | PyUnicodeObject *unicode; |
| 5163 | Py_UNICODE *p; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5164 | const unsigned char *q, *e, *aligned_end; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5165 | int bo = 0; /* assume native ordering by default */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5166 | int native_ordering = 0; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 5167 | const char *errmsg = ""; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5168 | /* Offsets from q for retrieving byte pairs in the right order. */ |
| 5169 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5170 | int ihi = 1, ilo = 0; |
| 5171 | #else |
| 5172 | int ihi = 0, ilo = 1; |
| 5173 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5174 | PyObject *errorHandler = NULL; |
| 5175 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5176 | |
| 5177 | /* Note: size will always be longer than the resulting Unicode |
| 5178 | character count */ |
| 5179 | unicode = _PyUnicode_New(size); |
| 5180 | if (!unicode) |
| 5181 | return NULL; |
| 5182 | if (size == 0) |
| 5183 | return (PyObject *)unicode; |
| 5184 | |
| 5185 | /* Unpack UTF-16 encoded data */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5186 | p = PyUnicode_AS_UNICODE(unicode); |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5187 | q = (unsigned char *)s; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5188 | e = q + size - 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5189 | |
| 5190 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5191 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5192 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5193 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 5194 | byte order setting accordingly. In native mode, the leading BOM |
| 5195 | mark is skipped, in all other modes, it is copied to the output |
| 5196 | stream as-is (giving a ZWNBSP character). */ |
| 5197 | if (bo == 0) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5198 | if (size >= 2) { |
| 5199 | const Py_UNICODE bom = (q[ihi] << 8) | q[ilo]; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5200 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5201 | if (bom == 0xFEFF) { |
| 5202 | q += 2; |
| 5203 | bo = -1; |
| 5204 | } |
| 5205 | else if (bom == 0xFFFE) { |
| 5206 | q += 2; |
| 5207 | bo = 1; |
| 5208 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5209 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5210 | if (bom == 0xFEFF) { |
| 5211 | q += 2; |
| 5212 | bo = 1; |
| 5213 | } |
| 5214 | else if (bom == 0xFFFE) { |
| 5215 | q += 2; |
| 5216 | bo = -1; |
| 5217 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5218 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5219 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5220 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5221 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5222 | if (bo == -1) { |
| 5223 | /* force LE */ |
| 5224 | ihi = 1; |
| 5225 | ilo = 0; |
| 5226 | } |
| 5227 | else if (bo == 1) { |
| 5228 | /* force BE */ |
| 5229 | ihi = 0; |
| 5230 | ilo = 1; |
| 5231 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5232 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5233 | native_ordering = ilo < ihi; |
| 5234 | #else |
| 5235 | native_ordering = ilo > ihi; |
| 5236 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5237 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5238 | aligned_end = (const unsigned char *) ((size_t) e & ~LONG_PTR_MASK); |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5239 | while (q < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5240 | Py_UNICODE ch; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5241 | /* First check for possible aligned read of a C 'long'. Unaligned |
| 5242 | reads are more expensive, better to defer to another iteration. */ |
| 5243 | if (!((size_t) q & LONG_PTR_MASK)) { |
| 5244 | /* Fast path for runs of non-surrogate chars. */ |
| 5245 | register const unsigned char *_q = q; |
| 5246 | Py_UNICODE *_p = p; |
| 5247 | if (native_ordering) { |
| 5248 | /* Native ordering is simple: as long as the input cannot |
| 5249 | possibly contain a surrogate char, do an unrolled copy |
| 5250 | of several 16-bit code points to the target object. |
| 5251 | The non-surrogate check is done on several input bytes |
| 5252 | at a time (as many as a C 'long' can contain). */ |
| 5253 | while (_q < aligned_end) { |
| 5254 | unsigned long data = * (unsigned long *) _q; |
| 5255 | if (data & FAST_CHAR_MASK) |
| 5256 | break; |
| 5257 | _p[0] = ((unsigned short *) _q)[0]; |
| 5258 | _p[1] = ((unsigned short *) _q)[1]; |
| 5259 | #if (SIZEOF_LONG == 8) |
| 5260 | _p[2] = ((unsigned short *) _q)[2]; |
| 5261 | _p[3] = ((unsigned short *) _q)[3]; |
| 5262 | #endif |
| 5263 | _q += SIZEOF_LONG; |
| 5264 | _p += SIZEOF_LONG / 2; |
| 5265 | } |
| 5266 | } |
| 5267 | else { |
| 5268 | /* Byteswapped ordering is similar, but we must decompose |
| 5269 | the copy bytewise, and take care of zero'ing out the |
| 5270 | upper bytes if the target object is in 32-bit units |
| 5271 | (that is, in UCS-4 builds). */ |
| 5272 | while (_q < aligned_end) { |
| 5273 | unsigned long data = * (unsigned long *) _q; |
| 5274 | if (data & SWAPPED_FAST_CHAR_MASK) |
| 5275 | break; |
| 5276 | /* Zero upper bytes in UCS-4 builds */ |
| 5277 | #if (Py_UNICODE_SIZE > 2) |
| 5278 | _p[0] = 0; |
| 5279 | _p[1] = 0; |
| 5280 | #if (SIZEOF_LONG == 8) |
| 5281 | _p[2] = 0; |
| 5282 | _p[3] = 0; |
| 5283 | #endif |
| 5284 | #endif |
Antoine Pitrou | d6e8de1 | 2009-01-11 23:56:55 +0000 | [diff] [blame] | 5285 | /* Issue #4916; UCS-4 builds on big endian machines must |
| 5286 | fill the two last bytes of each 4-byte unit. */ |
| 5287 | #if (!defined(BYTEORDER_IS_LITTLE_ENDIAN) && Py_UNICODE_SIZE > 2) |
| 5288 | # define OFF 2 |
| 5289 | #else |
| 5290 | # define OFF 0 |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5291 | #endif |
Antoine Pitrou | d6e8de1 | 2009-01-11 23:56:55 +0000 | [diff] [blame] | 5292 | ((unsigned char *) _p)[OFF + 1] = _q[0]; |
| 5293 | ((unsigned char *) _p)[OFF + 0] = _q[1]; |
| 5294 | ((unsigned char *) _p)[OFF + 1 + Py_UNICODE_SIZE] = _q[2]; |
| 5295 | ((unsigned char *) _p)[OFF + 0 + Py_UNICODE_SIZE] = _q[3]; |
| 5296 | #if (SIZEOF_LONG == 8) |
| 5297 | ((unsigned char *) _p)[OFF + 1 + 2 * Py_UNICODE_SIZE] = _q[4]; |
| 5298 | ((unsigned char *) _p)[OFF + 0 + 2 * Py_UNICODE_SIZE] = _q[5]; |
| 5299 | ((unsigned char *) _p)[OFF + 1 + 3 * Py_UNICODE_SIZE] = _q[6]; |
| 5300 | ((unsigned char *) _p)[OFF + 0 + 3 * Py_UNICODE_SIZE] = _q[7]; |
| 5301 | #endif |
| 5302 | #undef OFF |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5303 | _q += SIZEOF_LONG; |
| 5304 | _p += SIZEOF_LONG / 2; |
| 5305 | } |
| 5306 | } |
| 5307 | p = _p; |
| 5308 | q = _q; |
| 5309 | if (q >= e) |
| 5310 | break; |
| 5311 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5312 | ch = (q[ihi] << 8) | q[ilo]; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5313 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5314 | q += 2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5315 | |
| 5316 | if (ch < 0xD800 || ch > 0xDFFF) { |
| 5317 | *p++ = ch; |
| 5318 | continue; |
| 5319 | } |
| 5320 | |
| 5321 | /* UTF-16 code pair: */ |
| 5322 | if (q > e) { |
| 5323 | errmsg = "unexpected end of data"; |
| 5324 | startinpos = (((const char *)q) - 2) - starts; |
| 5325 | endinpos = ((const char *)e) + 1 - starts; |
| 5326 | goto utf16Error; |
| 5327 | } |
| 5328 | if (0xD800 <= ch && ch <= 0xDBFF) { |
| 5329 | Py_UNICODE ch2 = (q[ihi] << 8) | q[ilo]; |
| 5330 | q += 2; |
| 5331 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 5332 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5333 | *p++ = ch; |
| 5334 | *p++ = ch2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5335 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5336 | *p++ = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5337 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5338 | continue; |
| 5339 | } |
| 5340 | else { |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5341 | errmsg = "illegal UTF-16 surrogate"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5342 | startinpos = (((const char *)q)-4)-starts; |
| 5343 | endinpos = startinpos+2; |
| 5344 | goto utf16Error; |
| 5345 | } |
| 5346 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5347 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5348 | errmsg = "illegal encoding"; |
| 5349 | startinpos = (((const char *)q)-2)-starts; |
| 5350 | endinpos = startinpos+2; |
| 5351 | /* Fall through to report the error */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5352 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5353 | utf16Error: |
| 5354 | outpos = p - PyUnicode_AS_UNICODE(unicode); |
| 5355 | if (unicode_decode_call_errorhandler( |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5356 | errors, |
| 5357 | &errorHandler, |
| 5358 | "utf16", errmsg, |
| 5359 | &starts, |
| 5360 | (const char **)&e, |
| 5361 | &startinpos, |
| 5362 | &endinpos, |
| 5363 | &exc, |
| 5364 | (const char **)&q, |
| 5365 | &unicode, |
| 5366 | &outpos, |
| 5367 | &p)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5368 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5369 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5370 | /* remaining byte at the end? (size should be even) */ |
| 5371 | if (e == q) { |
| 5372 | if (!consumed) { |
| 5373 | errmsg = "truncated data"; |
| 5374 | startinpos = ((const char *)q) - starts; |
| 5375 | endinpos = ((const char *)e) + 1 - starts; |
| 5376 | outpos = p - PyUnicode_AS_UNICODE(unicode); |
| 5377 | if (unicode_decode_call_errorhandler( |
| 5378 | errors, |
| 5379 | &errorHandler, |
| 5380 | "utf16", errmsg, |
| 5381 | &starts, |
| 5382 | (const char **)&e, |
| 5383 | &startinpos, |
| 5384 | &endinpos, |
| 5385 | &exc, |
| 5386 | (const char **)&q, |
| 5387 | &unicode, |
| 5388 | &outpos, |
| 5389 | &p)) |
| 5390 | goto onError; |
| 5391 | /* The remaining input chars are ignored if the callback |
| 5392 | chooses to skip the input */ |
| 5393 | } |
| 5394 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5395 | |
| 5396 | if (byteorder) |
| 5397 | *byteorder = bo; |
| 5398 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5399 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5400 | *consumed = (const char *)q-starts; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5401 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5402 | /* Adjust length */ |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 5403 | if (PyUnicode_Resize((PyObject**)&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5404 | goto onError; |
| 5405 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5406 | Py_XDECREF(errorHandler); |
| 5407 | Py_XDECREF(exc); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 5408 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 5409 | if (_PyUnicode_READY_REPLACE(&unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5410 | Py_DECREF(unicode); |
| 5411 | return NULL; |
| 5412 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 5413 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 5414 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5415 | return (PyObject *)unicode; |
| 5416 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5417 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5418 | Py_DECREF(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5419 | Py_XDECREF(errorHandler); |
| 5420 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5421 | return NULL; |
| 5422 | } |
| 5423 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5424 | #undef FAST_CHAR_MASK |
| 5425 | #undef SWAPPED_FAST_CHAR_MASK |
| 5426 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5427 | PyObject * |
| 5428 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5429 | Py_ssize_t size, |
| 5430 | const char *errors, |
| 5431 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5432 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5433 | PyObject *v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5434 | unsigned char *p; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5435 | Py_ssize_t nsize, bytesize; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 5436 | #ifdef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5437 | Py_ssize_t i, pairs; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 5438 | #else |
| 5439 | const int pairs = 0; |
| 5440 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5441 | /* Offsets from p for storing byte pairs in the right order. */ |
| 5442 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5443 | int ihi = 1, ilo = 0; |
| 5444 | #else |
| 5445 | int ihi = 0, ilo = 1; |
| 5446 | #endif |
| 5447 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5448 | #define STORECHAR(CH) \ |
| 5449 | do { \ |
| 5450 | p[ihi] = ((CH) >> 8) & 0xff; \ |
| 5451 | p[ilo] = (CH) & 0xff; \ |
| 5452 | p += 2; \ |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5453 | } while(0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5454 | |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 5455 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5456 | for (i = pairs = 0; i < size; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5457 | if (s[i] >= 0x10000) |
| 5458 | pairs++; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 5459 | #endif |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5460 | /* 2 * (size + pairs + (byteorder == 0)) */ |
| 5461 | if (size > PY_SSIZE_T_MAX || |
| 5462 | size > PY_SSIZE_T_MAX - pairs - (byteorder == 0)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5463 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5464 | nsize = size + pairs + (byteorder == 0); |
| 5465 | bytesize = nsize * 2; |
| 5466 | if (bytesize / 2 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5467 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5468 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5469 | if (v == NULL) |
| 5470 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5471 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5472 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5473 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5474 | STORECHAR(0xFEFF); |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 5475 | if (size == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5476 | goto done; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5477 | |
| 5478 | if (byteorder == -1) { |
| 5479 | /* force LE */ |
| 5480 | ihi = 1; |
| 5481 | ilo = 0; |
| 5482 | } |
| 5483 | else if (byteorder == 1) { |
| 5484 | /* force BE */ |
| 5485 | ihi = 0; |
| 5486 | ilo = 1; |
| 5487 | } |
| 5488 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5489 | while (size-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5490 | Py_UNICODE ch = *s++; |
| 5491 | Py_UNICODE ch2 = 0; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 5492 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5493 | if (ch >= 0x10000) { |
| 5494 | ch2 = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 5495 | ch = 0xD800 | ((ch-0x10000) >> 10); |
| 5496 | } |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 5497 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5498 | STORECHAR(ch); |
| 5499 | if (ch2) |
| 5500 | STORECHAR(ch2); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5501 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5502 | |
| 5503 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5504 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5505 | #undef STORECHAR |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5506 | } |
| 5507 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5508 | PyObject * |
| 5509 | PyUnicode_AsUTF16String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5510 | { |
| 5511 | if (!PyUnicode_Check(unicode)) { |
| 5512 | PyErr_BadArgument(); |
| 5513 | return NULL; |
| 5514 | } |
| 5515 | return PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5516 | PyUnicode_GET_SIZE(unicode), |
| 5517 | NULL, |
| 5518 | 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5519 | } |
| 5520 | |
| 5521 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 5522 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5523 | /* Helper function for PyUnicode_DecodeUnicodeEscape, determines |
| 5524 | if all the escapes in the string make it still a valid ASCII string. |
| 5525 | Returns -1 if any escapes were found which cause the string to |
| 5526 | pop out of ASCII range. Otherwise returns the length of the |
| 5527 | required buffer to hold the string. |
| 5528 | */ |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 5529 | static Py_ssize_t |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5530 | length_of_escaped_ascii_string(const char *s, Py_ssize_t size) |
| 5531 | { |
| 5532 | const unsigned char *p = (const unsigned char *)s; |
| 5533 | const unsigned char *end = p + size; |
| 5534 | Py_ssize_t length = 0; |
| 5535 | |
| 5536 | if (size < 0) |
| 5537 | return -1; |
| 5538 | |
| 5539 | for (; p < end; ++p) { |
| 5540 | if (*p > 127) { |
| 5541 | /* Non-ASCII */ |
| 5542 | return -1; |
| 5543 | } |
| 5544 | else if (*p != '\\') { |
| 5545 | /* Normal character */ |
| 5546 | ++length; |
| 5547 | } |
| 5548 | else { |
| 5549 | /* Backslash-escape, check next char */ |
| 5550 | ++p; |
| 5551 | /* Escape sequence reaches till end of string or |
| 5552 | non-ASCII follow-up. */ |
| 5553 | if (p >= end || *p > 127) |
| 5554 | return -1; |
| 5555 | switch (*p) { |
| 5556 | case '\n': |
| 5557 | /* backslash + \n result in zero characters */ |
| 5558 | break; |
| 5559 | case '\\': case '\'': case '\"': |
| 5560 | case 'b': case 'f': case 't': |
| 5561 | case 'n': case 'r': case 'v': case 'a': |
| 5562 | ++length; |
| 5563 | break; |
| 5564 | case '0': case '1': case '2': case '3': |
| 5565 | case '4': case '5': case '6': case '7': |
| 5566 | case 'x': case 'u': case 'U': case 'N': |
| 5567 | /* these do not guarantee ASCII characters */ |
| 5568 | return -1; |
| 5569 | default: |
| 5570 | /* count the backslash + the other character */ |
| 5571 | length += 2; |
| 5572 | } |
| 5573 | } |
| 5574 | } |
| 5575 | return length; |
| 5576 | } |
| 5577 | |
| 5578 | /* Similar to PyUnicode_WRITE but either write into wstr field |
| 5579 | or treat string as ASCII. */ |
| 5580 | #define WRITE_ASCII_OR_WSTR(kind, buf, index, value) \ |
| 5581 | do { \ |
| 5582 | if ((kind) != PyUnicode_WCHAR_KIND) \ |
| 5583 | ((unsigned char *)(buf))[(index)] = (unsigned char)(value); \ |
| 5584 | else \ |
| 5585 | ((Py_UNICODE *)(buf))[(index)] = (Py_UNICODE)(value); \ |
| 5586 | } while (0) |
| 5587 | |
| 5588 | #define WRITE_WSTR(buf, index, value) \ |
| 5589 | assert(kind == PyUnicode_WCHAR_KIND), \ |
| 5590 | ((Py_UNICODE *)(buf))[(index)] = (Py_UNICODE)(value) |
| 5591 | |
| 5592 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5593 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 5594 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5595 | PyObject * |
| 5596 | PyUnicode_DecodeUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5597 | Py_ssize_t size, |
Victor Stinner | c17f540 | 2011-09-29 00:16:58 +0200 | [diff] [blame] | 5598 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5599 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5600 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5601 | Py_ssize_t startinpos; |
| 5602 | Py_ssize_t endinpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5603 | int j; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5604 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5605 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5606 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5607 | char* message; |
| 5608 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5609 | PyObject *errorHandler = NULL; |
| 5610 | PyObject *exc = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5611 | Py_ssize_t ascii_length; |
| 5612 | Py_ssize_t i; |
| 5613 | int kind; |
| 5614 | void *data; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5615 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5616 | ascii_length = length_of_escaped_ascii_string(s, size); |
| 5617 | |
| 5618 | /* After length_of_escaped_ascii_string() there are two alternatives, |
| 5619 | either the string is pure ASCII with named escapes like \n, etc. |
| 5620 | and we determined it's exact size (common case) |
| 5621 | or it contains \x, \u, ... escape sequences. then we create a |
| 5622 | legacy wchar string and resize it at the end of this function. */ |
| 5623 | if (ascii_length >= 0) { |
| 5624 | v = (PyUnicodeObject *)PyUnicode_New(ascii_length, 127); |
| 5625 | if (!v) |
| 5626 | goto onError; |
| 5627 | assert(PyUnicode_KIND(v) == PyUnicode_1BYTE_KIND); |
| 5628 | kind = PyUnicode_1BYTE_KIND; |
| 5629 | data = PyUnicode_DATA(v); |
| 5630 | } |
| 5631 | else { |
| 5632 | /* Escaped strings will always be longer than the resulting |
| 5633 | Unicode string, so we start with size here and then reduce the |
| 5634 | length after conversion to the true value. |
| 5635 | (but if the error callback returns a long replacement string |
| 5636 | we'll have to allocate more space) */ |
| 5637 | v = _PyUnicode_New(size); |
| 5638 | if (!v) |
| 5639 | goto onError; |
| 5640 | kind = PyUnicode_WCHAR_KIND; |
| 5641 | data = PyUnicode_AS_UNICODE(v); |
| 5642 | } |
| 5643 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5644 | if (size == 0) |
| 5645 | return (PyObject *)v; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5646 | i = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5647 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5648 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5649 | while (s < end) { |
| 5650 | unsigned char c; |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 5651 | Py_UNICODE x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5652 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5653 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5654 | if (kind == PyUnicode_WCHAR_KIND) { |
| 5655 | assert(i < _PyUnicode_WSTR_LENGTH(v)); |
| 5656 | } |
| 5657 | else { |
| 5658 | /* The only case in which i == ascii_length is a backslash |
| 5659 | followed by a newline. */ |
| 5660 | assert(i <= ascii_length); |
| 5661 | } |
| 5662 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5663 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 5664 | if (*s != '\\') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5665 | WRITE_ASCII_OR_WSTR(kind, data, i++, (unsigned char) *s++); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5666 | continue; |
| 5667 | } |
| 5668 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5669 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5670 | /* \ - Escapes */ |
| 5671 | s++; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5672 | c = *s++; |
| 5673 | if (s > end) |
| 5674 | c = '\0'; /* Invalid after \ */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5675 | |
| 5676 | if (kind == PyUnicode_WCHAR_KIND) { |
| 5677 | assert(i < _PyUnicode_WSTR_LENGTH(v)); |
| 5678 | } |
| 5679 | else { |
| 5680 | /* The only case in which i == ascii_length is a backslash |
| 5681 | followed by a newline. */ |
| 5682 | assert(i < ascii_length || (i == ascii_length && c == '\n')); |
| 5683 | } |
| 5684 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5685 | switch (c) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5686 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5687 | /* \x escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5688 | case '\n': break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5689 | case '\\': WRITE_ASCII_OR_WSTR(kind, data, i++, '\\'); break; |
| 5690 | case '\'': WRITE_ASCII_OR_WSTR(kind, data, i++, '\''); break; |
| 5691 | case '\"': WRITE_ASCII_OR_WSTR(kind, data, i++, '\"'); break; |
| 5692 | case 'b': WRITE_ASCII_OR_WSTR(kind, data, i++, '\b'); break; |
| 5693 | /* FF */ |
| 5694 | case 'f': WRITE_ASCII_OR_WSTR(kind, data, i++, '\014'); break; |
| 5695 | case 't': WRITE_ASCII_OR_WSTR(kind, data, i++, '\t'); break; |
| 5696 | case 'n': WRITE_ASCII_OR_WSTR(kind, data, i++, '\n'); break; |
| 5697 | case 'r': WRITE_ASCII_OR_WSTR(kind, data, i++, '\r'); break; |
| 5698 | /* VT */ |
| 5699 | case 'v': WRITE_ASCII_OR_WSTR(kind, data, i++, '\013'); break; |
| 5700 | /* BEL, not classic C */ |
| 5701 | case 'a': WRITE_ASCII_OR_WSTR(kind, data, i++, '\007'); break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5702 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5703 | /* \OOO (octal) escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5704 | case '0': case '1': case '2': case '3': |
| 5705 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5706 | x = s[-1] - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5707 | if (s < end && '0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5708 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5709 | if (s < end && '0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5710 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5711 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5712 | WRITE_WSTR(data, i++, x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5713 | break; |
| 5714 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5715 | /* hex escapes */ |
| 5716 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5717 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5718 | digits = 2; |
| 5719 | message = "truncated \\xXX escape"; |
| 5720 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5721 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5722 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5723 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5724 | digits = 4; |
| 5725 | message = "truncated \\uXXXX escape"; |
| 5726 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5727 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5728 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5729 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5730 | digits = 8; |
| 5731 | message = "truncated \\UXXXXXXXX escape"; |
| 5732 | hexescape: |
| 5733 | chr = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5734 | p = PyUnicode_AS_UNICODE(v) + i; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5735 | if (s+digits>end) { |
| 5736 | endinpos = size; |
| 5737 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5738 | errors, &errorHandler, |
| 5739 | "unicodeescape", "end of string in escape sequence", |
| 5740 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5741 | &v, &i, &p)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5742 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5743 | data = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5744 | goto nextByte; |
| 5745 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5746 | for (j = 0; j < digits; ++j) { |
| 5747 | c = (unsigned char) s[j]; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 5748 | if (!Py_ISXDIGIT(c)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5749 | endinpos = (s+j+1)-starts; |
| 5750 | p = PyUnicode_AS_UNICODE(v) + i; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5751 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5752 | errors, &errorHandler, |
| 5753 | "unicodeescape", message, |
| 5754 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5755 | &v, &i, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5756 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5757 | data = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5758 | goto nextByte; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5759 | } |
| 5760 | chr = (chr<<4) & ~0xF; |
| 5761 | if (c >= '0' && c <= '9') |
| 5762 | chr += c - '0'; |
| 5763 | else if (c >= 'a' && c <= 'f') |
| 5764 | chr += 10 + c - 'a'; |
| 5765 | else |
| 5766 | chr += 10 + c - 'A'; |
| 5767 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5768 | s += j; |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 5769 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5770 | /* _decoding_error will have already written into the |
| 5771 | target buffer. */ |
| 5772 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5773 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5774 | /* when we get here, chr is a 32-bit unicode character */ |
| 5775 | if (chr <= 0xffff) |
| 5776 | /* UCS-2 character */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5777 | WRITE_WSTR(data, i++, chr); |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5778 | else if (chr <= 0x10ffff) { |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5779 | /* UCS-4 character. Either store directly, or as |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5780 | surrogate pair. */ |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 5781 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5782 | WRITE_WSTR(data, i++, chr); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5783 | #else |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5784 | chr -= 0x10000L; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5785 | WRITE_WSTR(data, i++, 0xD800 + (Py_UNICODE) (chr >> 10)); |
| 5786 | WRITE_WSTR(data, i++, 0xDC00 + (Py_UNICODE) (chr & 0x03FF)); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5787 | #endif |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5788 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5789 | endinpos = s-starts; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5790 | p = PyUnicode_AS_UNICODE(v) + i; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5791 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5792 | errors, &errorHandler, |
| 5793 | "unicodeescape", "illegal Unicode character", |
| 5794 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5795 | &v, &i, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5796 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5797 | data = PyUnicode_AS_UNICODE(v); |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5798 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5799 | break; |
| 5800 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5801 | /* \N{name} */ |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5802 | case 'N': |
| 5803 | message = "malformed \\N character escape"; |
| 5804 | if (ucnhash_CAPI == NULL) { |
| 5805 | /* load the unicode data module */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5806 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCapsule_Import( |
| 5807 | PyUnicodeData_CAPSULE_NAME, 1); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5808 | if (ucnhash_CAPI == NULL) |
| 5809 | goto ucnhashError; |
| 5810 | } |
| 5811 | if (*s == '{') { |
| 5812 | const char *start = s+1; |
| 5813 | /* look for the closing brace */ |
| 5814 | while (*s != '}' && s < end) |
| 5815 | s++; |
| 5816 | if (s > start && s < end && *s == '}') { |
| 5817 | /* found a name. look it up in the unicode database */ |
| 5818 | message = "unknown Unicode character name"; |
| 5819 | s++; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5820 | if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), |
| 5821 | &chr)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5822 | goto store; |
| 5823 | } |
| 5824 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5825 | endinpos = s-starts; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5826 | p = PyUnicode_AS_UNICODE(v) + i; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5827 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5828 | errors, &errorHandler, |
| 5829 | "unicodeescape", message, |
| 5830 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5831 | &v, &i, &p)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5832 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5833 | data = PyUnicode_AS_UNICODE(v); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5834 | break; |
| 5835 | |
| 5836 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5837 | if (s > end) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5838 | assert(kind == PyUnicode_WCHAR_KIND); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5839 | message = "\\ at end of string"; |
| 5840 | s--; |
| 5841 | endinpos = s-starts; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5842 | p = PyUnicode_AS_UNICODE(v) + i; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5843 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5844 | errors, &errorHandler, |
| 5845 | "unicodeescape", message, |
| 5846 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5847 | &v, &i, &p)) |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5848 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5849 | data = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5850 | } |
| 5851 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5852 | WRITE_ASCII_OR_WSTR(kind, data, i++, '\\'); |
| 5853 | WRITE_ASCII_OR_WSTR(kind, data, i++, (unsigned char)s[-1]); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5854 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5855 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5856 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5857 | nextByte: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5858 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5859 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5860 | /* Ensure the length prediction worked in case of ASCII strings */ |
| 5861 | assert(kind == PyUnicode_WCHAR_KIND || i == ascii_length); |
| 5862 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 5863 | if (kind == PyUnicode_WCHAR_KIND) |
| 5864 | { |
| 5865 | if (PyUnicode_Resize((PyObject**)&v, i) < 0) |
| 5866 | goto onError; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 5867 | } |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 5868 | Py_XDECREF(errorHandler); |
| 5869 | Py_XDECREF(exc); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 5870 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 5871 | if (_PyUnicode_READY_REPLACE(&v)) { |
| 5872 | Py_DECREF(v); |
| 5873 | return NULL; |
| 5874 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 5875 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 5876 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5877 | return (PyObject *)v; |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5878 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5879 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5880 | PyErr_SetString( |
| 5881 | PyExc_UnicodeError, |
| 5882 | "\\N escapes not supported (can't load unicodedata module)" |
| 5883 | ); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 5884 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5885 | Py_XDECREF(errorHandler); |
| 5886 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 5887 | return NULL; |
| 5888 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5889 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5890 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5891 | Py_XDECREF(errorHandler); |
| 5892 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5893 | return NULL; |
| 5894 | } |
| 5895 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5896 | #undef WRITE_ASCII_OR_WSTR |
| 5897 | #undef WRITE_WSTR |
| 5898 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5899 | /* Return a Unicode-Escape string version of the Unicode object. |
| 5900 | |
| 5901 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 5902 | appropriate. |
| 5903 | |
| 5904 | */ |
| 5905 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5906 | static const char *hexdigits = "0123456789abcdef"; |
| 5907 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5908 | PyObject * |
| 5909 | PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5910 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5911 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5912 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5913 | char *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5914 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5915 | #ifdef Py_UNICODE_WIDE |
| 5916 | const Py_ssize_t expandsize = 10; |
| 5917 | #else |
| 5918 | const Py_ssize_t expandsize = 6; |
| 5919 | #endif |
| 5920 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5921 | /* XXX(nnorwitz): rather than over-allocating, it would be |
| 5922 | better to choose a different scheme. Perhaps scan the |
| 5923 | first N-chars of the string and allocate based on that size. |
| 5924 | */ |
| 5925 | /* Initial allocation is based on the longest-possible unichr |
| 5926 | escape. |
| 5927 | |
| 5928 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 5929 | unichr, so in this case it's the longest unichr escape. In |
| 5930 | narrow (UTF-16) builds this is five chars per source unichr |
| 5931 | since there are two unichrs in the surrogate pair, so in narrow |
| 5932 | (UTF-16) builds it's not the longest unichr escape. |
| 5933 | |
| 5934 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 5935 | so in the narrow (UTF-16) build case it's the longest unichr |
| 5936 | escape. |
| 5937 | */ |
| 5938 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5939 | if (size == 0) |
| 5940 | return PyBytes_FromStringAndSize(NULL, 0); |
| 5941 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5942 | if (size > (PY_SSIZE_T_MAX - 2 - 1) / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5943 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5944 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5945 | repr = PyBytes_FromStringAndSize(NULL, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5946 | 2 |
| 5947 | + expandsize*size |
| 5948 | + 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5949 | if (repr == NULL) |
| 5950 | return NULL; |
| 5951 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5952 | p = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5953 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5954 | while (size-- > 0) { |
| 5955 | Py_UNICODE ch = *s++; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5956 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5957 | /* Escape backslashes */ |
| 5958 | if (ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5959 | *p++ = '\\'; |
| 5960 | *p++ = (char) ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5961 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5962 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5963 | |
Guido van Rossum | 0d42e0c | 2001-07-20 16:36:21 +0000 | [diff] [blame] | 5964 | #ifdef Py_UNICODE_WIDE |
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) { |
| 5967 | *p++ = '\\'; |
| 5968 | *p++ = 'U'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5969 | *p++ = hexdigits[(ch >> 28) & 0x0000000F]; |
| 5970 | *p++ = hexdigits[(ch >> 24) & 0x0000000F]; |
| 5971 | *p++ = hexdigits[(ch >> 20) & 0x0000000F]; |
| 5972 | *p++ = hexdigits[(ch >> 16) & 0x0000000F]; |
| 5973 | *p++ = hexdigits[(ch >> 12) & 0x0000000F]; |
| 5974 | *p++ = hexdigits[(ch >> 8) & 0x0000000F]; |
| 5975 | *p++ = hexdigits[(ch >> 4) & 0x0000000F]; |
| 5976 | *p++ = hexdigits[ch & 0x0000000F]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5977 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5978 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5979 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5980 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 5981 | else if (ch >= 0xD800 && ch < 0xDC00) { |
| 5982 | Py_UNICODE ch2; |
| 5983 | Py_UCS4 ucs; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5984 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5985 | ch2 = *s++; |
| 5986 | size--; |
Georg Brandl | 78eef3de | 2010-08-01 20:51:02 +0000 | [diff] [blame] | 5987 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5988 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 5989 | *p++ = '\\'; |
| 5990 | *p++ = 'U'; |
| 5991 | *p++ = hexdigits[(ucs >> 28) & 0x0000000F]; |
| 5992 | *p++ = hexdigits[(ucs >> 24) & 0x0000000F]; |
| 5993 | *p++ = hexdigits[(ucs >> 20) & 0x0000000F]; |
| 5994 | *p++ = hexdigits[(ucs >> 16) & 0x0000000F]; |
| 5995 | *p++ = hexdigits[(ucs >> 12) & 0x0000000F]; |
| 5996 | *p++ = hexdigits[(ucs >> 8) & 0x0000000F]; |
| 5997 | *p++ = hexdigits[(ucs >> 4) & 0x0000000F]; |
| 5998 | *p++ = hexdigits[ucs & 0x0000000F]; |
| 5999 | continue; |
| 6000 | } |
| 6001 | /* Fall through: isolated surrogates are copied as-is */ |
| 6002 | s--; |
| 6003 | size++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6004 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6005 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 6006 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6007 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 6008 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6009 | *p++ = '\\'; |
| 6010 | *p++ = 'u'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 6011 | *p++ = hexdigits[(ch >> 12) & 0x000F]; |
| 6012 | *p++ = hexdigits[(ch >> 8) & 0x000F]; |
| 6013 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 6014 | *p++ = hexdigits[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6015 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 6016 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 6017 | /* Map special whitespace to '\t', \n', '\r' */ |
| 6018 | else if (ch == '\t') { |
| 6019 | *p++ = '\\'; |
| 6020 | *p++ = 't'; |
| 6021 | } |
| 6022 | else if (ch == '\n') { |
| 6023 | *p++ = '\\'; |
| 6024 | *p++ = 'n'; |
| 6025 | } |
| 6026 | else if (ch == '\r') { |
| 6027 | *p++ = '\\'; |
| 6028 | *p++ = 'r'; |
| 6029 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 6030 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 6031 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 6032 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6033 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 6034 | *p++ = 'x'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 6035 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 6036 | *p++ = hexdigits[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6037 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 6038 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6039 | /* Copy everything else as-is */ |
| 6040 | else |
| 6041 | *p++ = (char) ch; |
| 6042 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6043 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6044 | assert(p - PyBytes_AS_STRING(repr) > 0); |
| 6045 | if (_PyBytes_Resize(&repr, p - PyBytes_AS_STRING(repr)) < 0) |
| 6046 | return NULL; |
| 6047 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6048 | } |
| 6049 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6050 | PyObject * |
| 6051 | PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6052 | { |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 6053 | PyObject *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6054 | if (!PyUnicode_Check(unicode)) { |
| 6055 | PyErr_BadArgument(); |
| 6056 | return NULL; |
| 6057 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 6058 | s = PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 6059 | PyUnicode_GET_SIZE(unicode)); |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 6060 | return s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6061 | } |
| 6062 | |
| 6063 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 6064 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6065 | PyObject * |
| 6066 | PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6067 | Py_ssize_t size, |
| 6068 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6069 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6070 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6071 | Py_ssize_t startinpos; |
| 6072 | Py_ssize_t endinpos; |
| 6073 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6074 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6075 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6076 | const char *end; |
| 6077 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6078 | PyObject *errorHandler = NULL; |
| 6079 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6080 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6081 | /* Escaped strings will always be longer than the resulting |
| 6082 | 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] | 6083 | length after conversion to the true value. (But decoding error |
| 6084 | handler might have to resize the string) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6085 | v = _PyUnicode_New(size); |
| 6086 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6087 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6088 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6089 | return (PyObject *)v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6090 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6091 | end = s + size; |
| 6092 | while (s < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6093 | unsigned char c; |
| 6094 | Py_UCS4 x; |
| 6095 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6096 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6097 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6098 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 6099 | if (*s != '\\') { |
| 6100 | *p++ = (unsigned char)*s++; |
| 6101 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6102 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6103 | startinpos = s-starts; |
| 6104 | |
| 6105 | /* \u-escapes are only interpreted iff the number of leading |
| 6106 | backslashes if odd */ |
| 6107 | bs = s; |
| 6108 | for (;s < end;) { |
| 6109 | if (*s != '\\') |
| 6110 | break; |
| 6111 | *p++ = (unsigned char)*s++; |
| 6112 | } |
| 6113 | if (((s - bs) & 1) == 0 || |
| 6114 | s >= end || |
| 6115 | (*s != 'u' && *s != 'U')) { |
| 6116 | continue; |
| 6117 | } |
| 6118 | p--; |
| 6119 | count = *s=='u' ? 4 : 8; |
| 6120 | s++; |
| 6121 | |
| 6122 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
| 6123 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 6124 | for (x = 0, i = 0; i < count; ++i, ++s) { |
| 6125 | c = (unsigned char)*s; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 6126 | if (!Py_ISXDIGIT(c)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6127 | endinpos = s-starts; |
| 6128 | if (unicode_decode_call_errorhandler( |
| 6129 | errors, &errorHandler, |
| 6130 | "rawunicodeescape", "truncated \\uXXXX", |
| 6131 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 6132 | &v, &outpos, &p)) |
| 6133 | goto onError; |
| 6134 | goto nextByte; |
| 6135 | } |
| 6136 | x = (x<<4) & ~0xF; |
| 6137 | if (c >= '0' && c <= '9') |
| 6138 | x += c - '0'; |
| 6139 | else if (c >= 'a' && c <= 'f') |
| 6140 | x += 10 + c - 'a'; |
| 6141 | else |
| 6142 | x += 10 + c - 'A'; |
| 6143 | } |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 6144 | if (x <= 0xffff) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6145 | /* UCS-2 character */ |
| 6146 | *p++ = (Py_UNICODE) x; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 6147 | else if (x <= 0x10ffff) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6148 | /* UCS-4 character. Either store directly, or as |
| 6149 | surrogate pair. */ |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 6150 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6151 | *p++ = (Py_UNICODE) x; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 6152 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6153 | x -= 0x10000L; |
| 6154 | *p++ = 0xD800 + (Py_UNICODE) (x >> 10); |
| 6155 | *p++ = 0xDC00 + (Py_UNICODE) (x & 0x03FF); |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 6156 | #endif |
| 6157 | } else { |
| 6158 | endinpos = s-starts; |
| 6159 | outpos = p-PyUnicode_AS_UNICODE(v); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6160 | if (unicode_decode_call_errorhandler( |
| 6161 | errors, &errorHandler, |
| 6162 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6163 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 6164 | &v, &outpos, &p)) |
| 6165 | goto onError; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6166 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6167 | nextByte: |
| 6168 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6169 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 6170 | if (PyUnicode_Resize((PyObject**)&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6171 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6172 | Py_XDECREF(errorHandler); |
| 6173 | Py_XDECREF(exc); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 6174 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 6175 | if (_PyUnicode_READY_REPLACE(&v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6176 | Py_DECREF(v); |
| 6177 | return NULL; |
| 6178 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 6179 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 6180 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6181 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6182 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6183 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6184 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6185 | Py_XDECREF(errorHandler); |
| 6186 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6187 | return NULL; |
| 6188 | } |
| 6189 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6190 | PyObject * |
| 6191 | PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6192 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6193 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6194 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6195 | char *p; |
| 6196 | char *q; |
| 6197 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6198 | #ifdef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 6199 | const Py_ssize_t expandsize = 10; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6200 | #else |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 6201 | const Py_ssize_t expandsize = 6; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6202 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6203 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 6204 | if (size > PY_SSIZE_T_MAX / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6205 | return PyErr_NoMemory(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6206 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6207 | repr = PyBytes_FromStringAndSize(NULL, expandsize * size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6208 | if (repr == NULL) |
| 6209 | return NULL; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 6210 | if (size == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6211 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6212 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6213 | p = q = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6214 | while (size-- > 0) { |
| 6215 | Py_UNICODE ch = *s++; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6216 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6217 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 6218 | if (ch >= 0x10000) { |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6219 | *p++ = '\\'; |
| 6220 | *p++ = 'U'; |
Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 6221 | *p++ = hexdigits[(ch >> 28) & 0xf]; |
| 6222 | *p++ = hexdigits[(ch >> 24) & 0xf]; |
| 6223 | *p++ = hexdigits[(ch >> 20) & 0xf]; |
| 6224 | *p++ = hexdigits[(ch >> 16) & 0xf]; |
| 6225 | *p++ = hexdigits[(ch >> 12) & 0xf]; |
| 6226 | *p++ = hexdigits[(ch >> 8) & 0xf]; |
| 6227 | *p++ = hexdigits[(ch >> 4) & 0xf]; |
| 6228 | *p++ = hexdigits[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6229 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6230 | else |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 6231 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6232 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 6233 | if (ch >= 0xD800 && ch < 0xDC00) { |
| 6234 | Py_UNICODE ch2; |
| 6235 | Py_UCS4 ucs; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 6236 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6237 | ch2 = *s++; |
| 6238 | size--; |
Georg Brandl | 78eef3de | 2010-08-01 20:51:02 +0000 | [diff] [blame] | 6239 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6240 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 6241 | *p++ = '\\'; |
| 6242 | *p++ = 'U'; |
| 6243 | *p++ = hexdigits[(ucs >> 28) & 0xf]; |
| 6244 | *p++ = hexdigits[(ucs >> 24) & 0xf]; |
| 6245 | *p++ = hexdigits[(ucs >> 20) & 0xf]; |
| 6246 | *p++ = hexdigits[(ucs >> 16) & 0xf]; |
| 6247 | *p++ = hexdigits[(ucs >> 12) & 0xf]; |
| 6248 | *p++ = hexdigits[(ucs >> 8) & 0xf]; |
| 6249 | *p++ = hexdigits[(ucs >> 4) & 0xf]; |
| 6250 | *p++ = hexdigits[ucs & 0xf]; |
| 6251 | continue; |
| 6252 | } |
| 6253 | /* Fall through: isolated surrogates are copied as-is */ |
| 6254 | s--; |
| 6255 | size++; |
| 6256 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6257 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6258 | /* Map 16-bit characters to '\uxxxx' */ |
| 6259 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6260 | *p++ = '\\'; |
| 6261 | *p++ = 'u'; |
Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 6262 | *p++ = hexdigits[(ch >> 12) & 0xf]; |
| 6263 | *p++ = hexdigits[(ch >> 8) & 0xf]; |
| 6264 | *p++ = hexdigits[(ch >> 4) & 0xf]; |
| 6265 | *p++ = hexdigits[ch & 15]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6266 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6267 | /* Copy everything else as-is */ |
| 6268 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6269 | *p++ = (char) ch; |
| 6270 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6271 | size = p - q; |
| 6272 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6273 | assert(size > 0); |
| 6274 | if (_PyBytes_Resize(&repr, size) < 0) |
| 6275 | return NULL; |
| 6276 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6277 | } |
| 6278 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6279 | PyObject * |
| 6280 | PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6281 | { |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 6282 | PyObject *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6283 | if (!PyUnicode_Check(unicode)) { |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 6284 | PyErr_BadArgument(); |
| 6285 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6286 | } |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 6287 | s = PyUnicode_EncodeRawUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 6288 | PyUnicode_GET_SIZE(unicode)); |
| 6289 | |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 6290 | return s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6291 | } |
| 6292 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6293 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 6294 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6295 | PyObject * |
| 6296 | _PyUnicode_DecodeUnicodeInternal(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6297 | Py_ssize_t size, |
| 6298 | const char *errors) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6299 | { |
| 6300 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6301 | Py_ssize_t startinpos; |
| 6302 | Py_ssize_t endinpos; |
| 6303 | Py_ssize_t outpos; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6304 | PyUnicodeObject *v; |
| 6305 | Py_UNICODE *p; |
| 6306 | const char *end; |
| 6307 | const char *reason; |
| 6308 | PyObject *errorHandler = NULL; |
| 6309 | PyObject *exc = NULL; |
| 6310 | |
Neal Norwitz | d43069c | 2006-01-08 01:12:10 +0000 | [diff] [blame] | 6311 | #ifdef Py_UNICODE_WIDE |
| 6312 | Py_UNICODE unimax = PyUnicode_GetMax(); |
| 6313 | #endif |
| 6314 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6315 | /* XXX overflow detection missing */ |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6316 | v = _PyUnicode_New((size+Py_UNICODE_SIZE-1)/ Py_UNICODE_SIZE); |
| 6317 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6318 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6319 | /* Intentionally PyUnicode_GET_SIZE instead of PyUnicode_GET_LENGTH |
| 6320 | as string was created with the old API. */ |
| 6321 | if (PyUnicode_GET_SIZE(v) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6322 | return (PyObject *)v; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6323 | p = PyUnicode_AS_UNICODE(v); |
| 6324 | end = s + size; |
| 6325 | |
| 6326 | while (s < end) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6327 | memcpy(p, s, sizeof(Py_UNICODE)); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6328 | /* We have to sanity check the raw data, otherwise doom looms for |
| 6329 | some malformed UCS-4 data. */ |
| 6330 | if ( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6331 | #ifdef Py_UNICODE_WIDE |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6332 | *p > unimax || *p < 0 || |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6333 | #endif |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6334 | end-s < Py_UNICODE_SIZE |
| 6335 | ) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6336 | { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6337 | startinpos = s - starts; |
| 6338 | if (end-s < Py_UNICODE_SIZE) { |
| 6339 | endinpos = end-starts; |
| 6340 | reason = "truncated input"; |
| 6341 | } |
| 6342 | else { |
| 6343 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 6344 | reason = "illegal code point (> 0x10FFFF)"; |
| 6345 | } |
| 6346 | outpos = p - PyUnicode_AS_UNICODE(v); |
| 6347 | if (unicode_decode_call_errorhandler( |
| 6348 | errors, &errorHandler, |
| 6349 | "unicode_internal", reason, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 6350 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 6351 | &v, &outpos, &p)) { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6352 | goto onError; |
| 6353 | } |
| 6354 | } |
| 6355 | else { |
| 6356 | p++; |
| 6357 | s += Py_UNICODE_SIZE; |
| 6358 | } |
| 6359 | } |
| 6360 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 6361 | if (PyUnicode_Resize((PyObject**)&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6362 | goto onError; |
| 6363 | Py_XDECREF(errorHandler); |
| 6364 | Py_XDECREF(exc); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 6365 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 6366 | if (_PyUnicode_READY_REPLACE(&v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6367 | Py_DECREF(v); |
| 6368 | return NULL; |
| 6369 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 6370 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 6371 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6372 | return (PyObject *)v; |
| 6373 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6374 | onError: |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6375 | Py_XDECREF(v); |
| 6376 | Py_XDECREF(errorHandler); |
| 6377 | Py_XDECREF(exc); |
| 6378 | return NULL; |
| 6379 | } |
| 6380 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6381 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 6382 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6383 | PyObject * |
| 6384 | PyUnicode_DecodeLatin1(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6385 | Py_ssize_t size, |
| 6386 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6387 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6388 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 6389 | return _PyUnicode_FromUCS1((unsigned char*)s, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6390 | } |
| 6391 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6392 | /* create or adjust a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6393 | static void |
| 6394 | make_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6395 | const char *encoding, |
| 6396 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 6397 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6398 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6399 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6400 | if (*exceptionObject == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6401 | *exceptionObject = PyUnicodeEncodeError_Create( |
| 6402 | encoding, unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6403 | } |
| 6404 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6405 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 6406 | goto onError; |
| 6407 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 6408 | goto onError; |
| 6409 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 6410 | goto onError; |
| 6411 | return; |
| 6412 | onError: |
| 6413 | Py_DECREF(*exceptionObject); |
| 6414 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6415 | } |
| 6416 | } |
| 6417 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6418 | /* raises a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6419 | static void |
| 6420 | raise_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6421 | const char *encoding, |
| 6422 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 6423 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6424 | const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6425 | { |
| 6426 | make_encode_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6427 | encoding, unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6428 | if (*exceptionObject != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6429 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6430 | } |
| 6431 | |
| 6432 | /* error handling callback helper: |
| 6433 | build arguments, call the callback and check the arguments, |
| 6434 | put the result into newpos and return the replacement string, which |
| 6435 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6436 | static PyObject * |
| 6437 | unicode_encode_call_errorhandler(const char *errors, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6438 | PyObject **errorHandler, |
| 6439 | const char *encoding, const char *reason, |
| 6440 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 6441 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6442 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6443 | { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6444 | static char *argparse = "On;encoding error handler must return (str/bytes, int) tuple"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6445 | |
| 6446 | PyObject *restuple; |
| 6447 | PyObject *resunicode; |
| 6448 | |
| 6449 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6450 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6451 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6452 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6453 | } |
| 6454 | |
| 6455 | make_encode_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6456 | encoding, unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6457 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6458 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6459 | |
| 6460 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6461 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6462 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6463 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6464 | if (!PyTuple_Check(restuple)) { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6465 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6466 | Py_DECREF(restuple); |
| 6467 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6468 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6469 | if (!PyArg_ParseTuple(restuple, argparse, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6470 | &resunicode, newpos)) { |
| 6471 | Py_DECREF(restuple); |
| 6472 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6473 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6474 | if (!PyUnicode_Check(resunicode) && !PyBytes_Check(resunicode)) { |
| 6475 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
| 6476 | Py_DECREF(restuple); |
| 6477 | return NULL; |
| 6478 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6479 | if (*newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6480 | *newpos = size+*newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 6481 | if (*newpos<0 || *newpos>size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6482 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 6483 | Py_DECREF(restuple); |
| 6484 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 6485 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6486 | Py_INCREF(resunicode); |
| 6487 | Py_DECREF(restuple); |
| 6488 | return resunicode; |
| 6489 | } |
| 6490 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6491 | static PyObject * |
| 6492 | unicode_encode_ucs1(const Py_UNICODE *p, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6493 | Py_ssize_t size, |
| 6494 | const char *errors, |
| 6495 | int limit) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6496 | { |
| 6497 | /* output object */ |
| 6498 | PyObject *res; |
| 6499 | /* pointers to the beginning and end+1 of input */ |
| 6500 | const Py_UNICODE *startp = p; |
| 6501 | const Py_UNICODE *endp = p + size; |
| 6502 | /* pointer to the beginning of the unencodable characters */ |
| 6503 | /* const Py_UNICODE *badp = NULL; */ |
| 6504 | /* pointer into the output */ |
| 6505 | char *str; |
| 6506 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6507 | Py_ssize_t ressize; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6508 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 6509 | 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] | 6510 | PyObject *errorHandler = NULL; |
| 6511 | PyObject *exc = NULL; |
| 6512 | /* the following variable is used for caching string comparisons |
| 6513 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 6514 | int known_errorHandler = -1; |
| 6515 | |
| 6516 | /* allocate enough for a simple encoding without |
| 6517 | replacements, if we need more, we'll resize */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6518 | if (size == 0) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6519 | return PyBytes_FromStringAndSize(NULL, 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6520 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6521 | if (res == NULL) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6522 | return NULL; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6523 | str = PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6524 | ressize = size; |
| 6525 | |
| 6526 | while (p<endp) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6527 | Py_UNICODE c = *p; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6528 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6529 | /* can we encode this? */ |
| 6530 | if (c<limit) { |
| 6531 | /* no overflow check, because we know that the space is enough */ |
| 6532 | *str++ = (char)c; |
| 6533 | ++p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6534 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6535 | else { |
| 6536 | Py_ssize_t unicodepos = p-startp; |
| 6537 | Py_ssize_t requiredsize; |
| 6538 | PyObject *repunicode; |
| 6539 | Py_ssize_t repsize; |
| 6540 | Py_ssize_t newpos; |
| 6541 | Py_ssize_t respos; |
| 6542 | Py_UNICODE *uni2; |
| 6543 | /* startpos for collecting unencodable chars */ |
| 6544 | const Py_UNICODE *collstart = p; |
| 6545 | const Py_UNICODE *collend = p; |
| 6546 | /* find all unecodable characters */ |
| 6547 | while ((collend < endp) && ((*collend)>=limit)) |
| 6548 | ++collend; |
| 6549 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 6550 | if (known_errorHandler==-1) { |
| 6551 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 6552 | known_errorHandler = 1; |
| 6553 | else if (!strcmp(errors, "replace")) |
| 6554 | known_errorHandler = 2; |
| 6555 | else if (!strcmp(errors, "ignore")) |
| 6556 | known_errorHandler = 3; |
| 6557 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 6558 | known_errorHandler = 4; |
| 6559 | else |
| 6560 | known_errorHandler = 0; |
| 6561 | } |
| 6562 | switch (known_errorHandler) { |
| 6563 | case 1: /* strict */ |
| 6564 | raise_encode_exception(&exc, encoding, startp, size, collstart-startp, collend-startp, reason); |
| 6565 | goto onError; |
| 6566 | case 2: /* replace */ |
| 6567 | while (collstart++<collend) |
| 6568 | *str++ = '?'; /* fall through */ |
| 6569 | case 3: /* ignore */ |
| 6570 | p = collend; |
| 6571 | break; |
| 6572 | case 4: /* xmlcharrefreplace */ |
| 6573 | respos = str - PyBytes_AS_STRING(res); |
| 6574 | /* determine replacement size (temporarily (mis)uses p) */ |
| 6575 | for (p = collstart, repsize = 0; p < collend; ++p) { |
| 6576 | if (*p<10) |
| 6577 | repsize += 2+1+1; |
| 6578 | else if (*p<100) |
| 6579 | repsize += 2+2+1; |
| 6580 | else if (*p<1000) |
| 6581 | repsize += 2+3+1; |
| 6582 | else if (*p<10000) |
| 6583 | repsize += 2+4+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 6584 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6585 | else |
| 6586 | repsize += 2+5+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 6587 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6588 | else if (*p<100000) |
| 6589 | repsize += 2+5+1; |
| 6590 | else if (*p<1000000) |
| 6591 | repsize += 2+6+1; |
| 6592 | else |
| 6593 | repsize += 2+7+1; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 6594 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6595 | } |
| 6596 | requiredsize = respos+repsize+(endp-collend); |
| 6597 | if (requiredsize > ressize) { |
| 6598 | if (requiredsize<2*ressize) |
| 6599 | requiredsize = 2*ressize; |
| 6600 | if (_PyBytes_Resize(&res, requiredsize)) |
| 6601 | goto onError; |
| 6602 | str = PyBytes_AS_STRING(res) + respos; |
| 6603 | ressize = requiredsize; |
| 6604 | } |
| 6605 | /* generate replacement (temporarily (mis)uses p) */ |
| 6606 | for (p = collstart; p < collend; ++p) { |
| 6607 | str += sprintf(str, "&#%d;", (int)*p); |
| 6608 | } |
| 6609 | p = collend; |
| 6610 | break; |
| 6611 | default: |
| 6612 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 6613 | encoding, reason, startp, size, &exc, |
| 6614 | collstart-startp, collend-startp, &newpos); |
| 6615 | if (repunicode == NULL) |
| 6616 | goto onError; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6617 | if (PyBytes_Check(repunicode)) { |
| 6618 | /* Directly copy bytes result to output. */ |
| 6619 | repsize = PyBytes_Size(repunicode); |
| 6620 | if (repsize > 1) { |
| 6621 | /* Make room for all additional bytes. */ |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6622 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6623 | if (_PyBytes_Resize(&res, ressize+repsize-1)) { |
| 6624 | Py_DECREF(repunicode); |
| 6625 | goto onError; |
| 6626 | } |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6627 | str = PyBytes_AS_STRING(res) + respos; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6628 | ressize += repsize-1; |
| 6629 | } |
| 6630 | memcpy(str, PyBytes_AsString(repunicode), repsize); |
| 6631 | str += repsize; |
| 6632 | p = startp + newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6633 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6634 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6635 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6636 | /* need more space? (at least enough for what we |
| 6637 | have+the replacement+the rest of the string, so |
| 6638 | we won't have to check space for encodable characters) */ |
| 6639 | respos = str - PyBytes_AS_STRING(res); |
| 6640 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 6641 | requiredsize = respos+repsize+(endp-collend); |
| 6642 | if (requiredsize > ressize) { |
| 6643 | if (requiredsize<2*ressize) |
| 6644 | requiredsize = 2*ressize; |
| 6645 | if (_PyBytes_Resize(&res, requiredsize)) { |
| 6646 | Py_DECREF(repunicode); |
| 6647 | goto onError; |
| 6648 | } |
| 6649 | str = PyBytes_AS_STRING(res) + respos; |
| 6650 | ressize = requiredsize; |
| 6651 | } |
| 6652 | /* check if there is anything unencodable in the replacement |
| 6653 | and copy it to the output */ |
| 6654 | for (uni2 = PyUnicode_AS_UNICODE(repunicode);repsize-->0; ++uni2, ++str) { |
| 6655 | c = *uni2; |
| 6656 | if (c >= limit) { |
| 6657 | raise_encode_exception(&exc, encoding, startp, size, |
| 6658 | unicodepos, unicodepos+1, reason); |
| 6659 | Py_DECREF(repunicode); |
| 6660 | goto onError; |
| 6661 | } |
| 6662 | *str = (char)c; |
| 6663 | } |
| 6664 | p = startp + newpos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6665 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6666 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6667 | } |
| 6668 | } |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6669 | /* Resize if we allocated to much */ |
| 6670 | size = str - PyBytes_AS_STRING(res); |
| 6671 | if (size < ressize) { /* If this falls res will be NULL */ |
Alexandre Vassalotti | bad1b92 | 2008-12-27 09:49:09 +0000 | [diff] [blame] | 6672 | assert(size >= 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6673 | if (_PyBytes_Resize(&res, size) < 0) |
| 6674 | goto onError; |
| 6675 | } |
| 6676 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6677 | Py_XDECREF(errorHandler); |
| 6678 | Py_XDECREF(exc); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6679 | return res; |
| 6680 | |
| 6681 | onError: |
| 6682 | Py_XDECREF(res); |
| 6683 | Py_XDECREF(errorHandler); |
| 6684 | Py_XDECREF(exc); |
| 6685 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6686 | } |
| 6687 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6688 | PyObject * |
| 6689 | PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6690 | Py_ssize_t size, |
| 6691 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6692 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6693 | return unicode_encode_ucs1(p, size, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6694 | } |
| 6695 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6696 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6697 | _PyUnicode_AsLatin1String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6698 | { |
| 6699 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6700 | PyErr_BadArgument(); |
| 6701 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6702 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6703 | if (PyUnicode_READY(unicode) == -1) |
| 6704 | return NULL; |
| 6705 | /* Fast path: if it is a one-byte string, construct |
| 6706 | bytes object directly. */ |
| 6707 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) |
| 6708 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6709 | PyUnicode_GET_LENGTH(unicode)); |
| 6710 | /* Non-Latin-1 characters present. Defer to above function to |
| 6711 | raise the exception. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6712 | return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6713 | PyUnicode_GET_SIZE(unicode), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6714 | errors); |
| 6715 | } |
| 6716 | |
| 6717 | PyObject* |
| 6718 | PyUnicode_AsLatin1String(PyObject *unicode) |
| 6719 | { |
| 6720 | return _PyUnicode_AsLatin1String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6721 | } |
| 6722 | |
| 6723 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 6724 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6725 | PyObject * |
| 6726 | PyUnicode_DecodeASCII(const char *s, |
| 6727 | Py_ssize_t size, |
| 6728 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6729 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6730 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6731 | PyUnicodeObject *v; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6732 | Py_UNICODE *u; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6733 | Py_ssize_t startinpos; |
| 6734 | Py_ssize_t endinpos; |
| 6735 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6736 | const char *e; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6737 | int has_error; |
| 6738 | const unsigned char *p = (const unsigned char *)s; |
| 6739 | const unsigned char *end = p + size; |
| 6740 | const unsigned char *aligned_end = (const unsigned char *) ((size_t) end & ~LONG_PTR_MASK); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6741 | PyObject *errorHandler = NULL; |
| 6742 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6743 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6744 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6745 | if (size == 1 && (unsigned char)s[0] < 128) |
| 6746 | return get_latin1_char((unsigned char)s[0]); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6747 | |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6748 | has_error = 0; |
| 6749 | while (p < end && !has_error) { |
| 6750 | /* Fast path, see below in PyUnicode_DecodeUTF8Stateful for |
| 6751 | an explanation. */ |
| 6752 | if (!((size_t) p & LONG_PTR_MASK)) { |
| 6753 | /* Help register allocation */ |
| 6754 | register const unsigned char *_p = p; |
| 6755 | while (_p < aligned_end) { |
| 6756 | unsigned long value = *(unsigned long *) _p; |
| 6757 | if (value & ASCII_CHAR_MASK) { |
| 6758 | has_error = 1; |
| 6759 | break; |
| 6760 | } |
| 6761 | _p += SIZEOF_LONG; |
| 6762 | } |
| 6763 | if (_p == end) |
| 6764 | break; |
| 6765 | if (has_error) |
| 6766 | break; |
| 6767 | p = _p; |
| 6768 | } |
| 6769 | if (*p & 0x80) { |
| 6770 | has_error = 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6771 | break; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6772 | } |
| 6773 | else { |
| 6774 | ++p; |
| 6775 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 6776 | } |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6777 | if (!has_error) |
| 6778 | return unicode_fromascii((const unsigned char *)s, size); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6779 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6780 | v = _PyUnicode_New(size); |
| 6781 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6782 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6783 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6784 | return (PyObject *)v; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6785 | u = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6786 | e = s + size; |
| 6787 | while (s < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6788 | register unsigned char c = (unsigned char)*s; |
| 6789 | if (c < 128) { |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6790 | *u++ = c; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6791 | ++s; |
| 6792 | } |
| 6793 | else { |
| 6794 | startinpos = s-starts; |
| 6795 | endinpos = startinpos + 1; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6796 | outpos = u - (Py_UNICODE *)PyUnicode_AS_UNICODE(v); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6797 | if (unicode_decode_call_errorhandler( |
| 6798 | errors, &errorHandler, |
| 6799 | "ascii", "ordinal not in range(128)", |
| 6800 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6801 | &v, &outpos, &u)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6802 | goto onError; |
| 6803 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6804 | } |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6805 | if (u - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
| 6806 | if (PyUnicode_Resize((PyObject**)&v, u - PyUnicode_AS_UNICODE(v)) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6807 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6808 | Py_XDECREF(errorHandler); |
| 6809 | Py_XDECREF(exc); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 6810 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 6811 | if (_PyUnicode_READY_REPLACE(&v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6812 | Py_DECREF(v); |
| 6813 | return NULL; |
| 6814 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 6815 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 6816 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6817 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6818 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6819 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6820 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6821 | Py_XDECREF(errorHandler); |
| 6822 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6823 | return NULL; |
| 6824 | } |
| 6825 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6826 | PyObject * |
| 6827 | PyUnicode_EncodeASCII(const Py_UNICODE *p, |
| 6828 | Py_ssize_t size, |
| 6829 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6830 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6831 | return unicode_encode_ucs1(p, size, errors, 128); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6832 | } |
| 6833 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6834 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6835 | _PyUnicode_AsASCIIString(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6836 | { |
| 6837 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6838 | PyErr_BadArgument(); |
| 6839 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6840 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6841 | if (PyUnicode_READY(unicode) == -1) |
| 6842 | return NULL; |
| 6843 | /* Fast path: if it is an ASCII-only string, construct bytes object |
| 6844 | directly. Else defer to above function to raise the exception. */ |
| 6845 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) |
| 6846 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6847 | PyUnicode_GET_LENGTH(unicode)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6848 | return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6849 | PyUnicode_GET_SIZE(unicode), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6850 | errors); |
| 6851 | } |
| 6852 | |
| 6853 | PyObject * |
| 6854 | PyUnicode_AsASCIIString(PyObject *unicode) |
| 6855 | { |
| 6856 | return _PyUnicode_AsASCIIString(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6857 | } |
| 6858 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 6859 | #ifdef HAVE_MBCS |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6860 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6861 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6862 | |
Hirokazu Yamamoto | 3530246 | 2009-03-21 13:23:27 +0000 | [diff] [blame] | 6863 | #if SIZEOF_INT < SIZEOF_SIZE_T |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6864 | #define NEED_RETRY |
| 6865 | #endif |
| 6866 | |
| 6867 | /* XXX This code is limited to "true" double-byte encodings, as |
| 6868 | a) it assumes an incomplete character consists of a single byte, and |
| 6869 | b) IsDBCSLeadByte (probably) does not work for non-DBCS multi-byte |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6870 | encodings, see IsDBCSLeadByteEx documentation. */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6871 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6872 | static int |
| 6873 | is_dbcs_lead_byte(const char *s, int offset) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6874 | { |
| 6875 | const char *curr = s + offset; |
| 6876 | |
| 6877 | if (IsDBCSLeadByte(*curr)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6878 | const char *prev = CharPrev(s, curr); |
| 6879 | return (prev == curr) || !IsDBCSLeadByte(*prev) || (curr - prev == 2); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6880 | } |
| 6881 | return 0; |
| 6882 | } |
| 6883 | |
| 6884 | /* |
| 6885 | * Decode MBCS string into unicode object. If 'final' is set, converts |
| 6886 | * trailing lead-byte too. Returns consumed size if succeed, -1 otherwise. |
| 6887 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6888 | static int |
| 6889 | decode_mbcs(PyUnicodeObject **v, |
| 6890 | const char *s, /* MBCS string */ |
| 6891 | int size, /* sizeof MBCS string */ |
| 6892 | int final, |
| 6893 | const char *errors) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6894 | { |
| 6895 | Py_UNICODE *p; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6896 | Py_ssize_t n; |
| 6897 | DWORD usize; |
| 6898 | DWORD flags; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6899 | |
| 6900 | assert(size >= 0); |
| 6901 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6902 | /* check and handle 'errors' arg */ |
| 6903 | if (errors==NULL || strcmp(errors, "strict")==0) |
| 6904 | flags = MB_ERR_INVALID_CHARS; |
| 6905 | else if (strcmp(errors, "ignore")==0) |
| 6906 | flags = 0; |
| 6907 | else { |
| 6908 | PyErr_Format(PyExc_ValueError, |
| 6909 | "mbcs encoding does not support errors='%s'", |
| 6910 | errors); |
| 6911 | return -1; |
| 6912 | } |
| 6913 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6914 | /* Skip trailing lead-byte unless 'final' is set */ |
| 6915 | if (!final && size >= 1 && is_dbcs_lead_byte(s, size - 1)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6916 | --size; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6917 | |
| 6918 | /* First get the size of the result */ |
| 6919 | if (size > 0) { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6920 | usize = MultiByteToWideChar(CP_ACP, flags, s, size, NULL, 0); |
| 6921 | if (usize==0) |
| 6922 | goto mbcs_decode_error; |
| 6923 | } else |
| 6924 | usize = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6925 | |
| 6926 | if (*v == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6927 | /* Create unicode object */ |
| 6928 | *v = _PyUnicode_New(usize); |
| 6929 | if (*v == NULL) |
| 6930 | return -1; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6931 | n = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6932 | } |
| 6933 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6934 | /* Extend unicode object */ |
| 6935 | n = PyUnicode_GET_SIZE(*v); |
Victor Stinner | 2fd8227 | 2011-10-03 04:06:05 +0200 | [diff] [blame] | 6936 | if (PyUnicode_Resize((PyObject**)v, n + usize) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6937 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6938 | } |
| 6939 | |
| 6940 | /* Do the conversion */ |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6941 | if (usize > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6942 | p = PyUnicode_AS_UNICODE(*v) + n; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6943 | if (0 == MultiByteToWideChar(CP_ACP, flags, s, size, p, usize)) { |
| 6944 | goto mbcs_decode_error; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6945 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6946 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6947 | return size; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6948 | |
| 6949 | mbcs_decode_error: |
| 6950 | /* If the last error was ERROR_NO_UNICODE_TRANSLATION, then |
| 6951 | we raise a UnicodeDecodeError - else it is a 'generic' |
| 6952 | windows error |
| 6953 | */ |
| 6954 | if (GetLastError()==ERROR_NO_UNICODE_TRANSLATION) { |
| 6955 | /* Ideally, we should get reason from FormatMessage - this |
| 6956 | is the Windows 2000 English version of the message |
| 6957 | */ |
| 6958 | PyObject *exc = NULL; |
| 6959 | const char *reason = "No mapping for the Unicode character exists " |
| 6960 | "in the target multi-byte code page."; |
| 6961 | make_decode_exception(&exc, "mbcs", s, size, 0, 0, reason); |
| 6962 | if (exc != NULL) { |
| 6963 | PyCodec_StrictErrors(exc); |
| 6964 | Py_DECREF(exc); |
| 6965 | } |
| 6966 | } else { |
| 6967 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 6968 | } |
| 6969 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6970 | } |
| 6971 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6972 | PyObject * |
| 6973 | PyUnicode_DecodeMBCSStateful(const char *s, |
| 6974 | Py_ssize_t size, |
| 6975 | const char *errors, |
| 6976 | Py_ssize_t *consumed) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6977 | { |
| 6978 | PyUnicodeObject *v = NULL; |
| 6979 | int done; |
| 6980 | |
| 6981 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6982 | *consumed = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6983 | |
| 6984 | #ifdef NEED_RETRY |
| 6985 | retry: |
| 6986 | if (size > INT_MAX) |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6987 | done = decode_mbcs(&v, s, INT_MAX, 0, errors); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6988 | else |
| 6989 | #endif |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6990 | done = decode_mbcs(&v, s, (int)size, !consumed, errors); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6991 | |
| 6992 | if (done < 0) { |
| 6993 | Py_XDECREF(v); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6994 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6995 | } |
| 6996 | |
| 6997 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6998 | *consumed += done; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6999 | |
| 7000 | #ifdef NEED_RETRY |
| 7001 | if (size > INT_MAX) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7002 | s += done; |
| 7003 | size -= done; |
| 7004 | goto retry; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7005 | } |
| 7006 | #endif |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 7007 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 7008 | if (_PyUnicode_READY_REPLACE(&v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7009 | Py_DECREF(v); |
| 7010 | return NULL; |
| 7011 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 7012 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 7013 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7014 | return (PyObject *)v; |
| 7015 | } |
| 7016 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7017 | PyObject * |
| 7018 | PyUnicode_DecodeMBCS(const char *s, |
| 7019 | Py_ssize_t size, |
| 7020 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7021 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7022 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 7023 | } |
| 7024 | |
| 7025 | /* |
| 7026 | * Convert unicode into string object (MBCS). |
| 7027 | * Returns 0 if succeed, -1 otherwise. |
| 7028 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7029 | static int |
| 7030 | encode_mbcs(PyObject **repr, |
| 7031 | const Py_UNICODE *p, /* unicode */ |
| 7032 | int size, /* size of unicode */ |
| 7033 | const char* errors) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7034 | { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7035 | BOOL usedDefaultChar = FALSE; |
| 7036 | BOOL *pusedDefaultChar; |
| 7037 | int mbcssize; |
| 7038 | Py_ssize_t n; |
| 7039 | PyObject *exc = NULL; |
| 7040 | DWORD flags; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7041 | |
| 7042 | assert(size >= 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7043 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7044 | /* check and handle 'errors' arg */ |
| 7045 | if (errors==NULL || strcmp(errors, "strict")==0) { |
| 7046 | flags = WC_NO_BEST_FIT_CHARS; |
| 7047 | pusedDefaultChar = &usedDefaultChar; |
| 7048 | } else if (strcmp(errors, "replace")==0) { |
| 7049 | flags = 0; |
| 7050 | pusedDefaultChar = NULL; |
| 7051 | } else { |
| 7052 | PyErr_Format(PyExc_ValueError, |
| 7053 | "mbcs encoding does not support errors='%s'", |
| 7054 | errors); |
| 7055 | return -1; |
| 7056 | } |
| 7057 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7058 | /* First get the size of the result */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7059 | if (size > 0) { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7060 | mbcssize = WideCharToMultiByte(CP_ACP, flags, p, size, NULL, 0, |
| 7061 | NULL, pusedDefaultChar); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7062 | if (mbcssize == 0) { |
| 7063 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 7064 | return -1; |
| 7065 | } |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7066 | /* If we used a default char, then we failed! */ |
| 7067 | if (pusedDefaultChar && *pusedDefaultChar) |
| 7068 | goto mbcs_encode_error; |
| 7069 | } else { |
| 7070 | mbcssize = 0; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7071 | } |
| 7072 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7073 | if (*repr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7074 | /* Create string object */ |
| 7075 | *repr = PyBytes_FromStringAndSize(NULL, mbcssize); |
| 7076 | if (*repr == NULL) |
| 7077 | return -1; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7078 | n = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7079 | } |
| 7080 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7081 | /* Extend string object */ |
| 7082 | n = PyBytes_Size(*repr); |
| 7083 | if (_PyBytes_Resize(repr, n + mbcssize) < 0) |
| 7084 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7085 | } |
| 7086 | |
| 7087 | /* Do the conversion */ |
| 7088 | if (size > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7089 | char *s = PyBytes_AS_STRING(*repr) + n; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7090 | if (0 == WideCharToMultiByte(CP_ACP, flags, p, size, s, mbcssize, |
| 7091 | NULL, pusedDefaultChar)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7092 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 7093 | return -1; |
| 7094 | } |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7095 | if (pusedDefaultChar && *pusedDefaultChar) |
| 7096 | goto mbcs_encode_error; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7097 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7098 | return 0; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7099 | |
| 7100 | mbcs_encode_error: |
| 7101 | raise_encode_exception(&exc, "mbcs", p, size, 0, 0, "invalid character"); |
| 7102 | Py_XDECREF(exc); |
| 7103 | return -1; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7104 | } |
| 7105 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7106 | PyObject * |
| 7107 | PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
| 7108 | Py_ssize_t size, |
| 7109 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7110 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7111 | PyObject *repr = NULL; |
| 7112 | int ret; |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 7113 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7114 | #ifdef NEED_RETRY |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7115 | retry: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7116 | if (size > INT_MAX) |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7117 | ret = encode_mbcs(&repr, p, INT_MAX, errors); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7118 | else |
| 7119 | #endif |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7120 | ret = encode_mbcs(&repr, p, (int)size, errors); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7121 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7122 | if (ret < 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7123 | Py_XDECREF(repr); |
| 7124 | return NULL; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7125 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7126 | |
| 7127 | #ifdef NEED_RETRY |
| 7128 | if (size > INT_MAX) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7129 | p += INT_MAX; |
| 7130 | size -= INT_MAX; |
| 7131 | goto retry; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7132 | } |
| 7133 | #endif |
| 7134 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7135 | return repr; |
| 7136 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 7137 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7138 | PyObject * |
| 7139 | PyUnicode_AsMBCSString(PyObject *unicode) |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7140 | { |
| 7141 | if (!PyUnicode_Check(unicode)) { |
| 7142 | PyErr_BadArgument(); |
| 7143 | return NULL; |
| 7144 | } |
| 7145 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7146 | PyUnicode_GET_SIZE(unicode), |
| 7147 | NULL); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7148 | } |
| 7149 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7150 | #undef NEED_RETRY |
| 7151 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 7152 | #endif /* HAVE_MBCS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7153 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7154 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 7155 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7156 | PyObject * |
| 7157 | PyUnicode_DecodeCharmap(const char *s, |
| 7158 | Py_ssize_t size, |
| 7159 | PyObject *mapping, |
| 7160 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7161 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7162 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7163 | Py_ssize_t startinpos; |
| 7164 | Py_ssize_t endinpos; |
| 7165 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7166 | const char *e; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7167 | PyUnicodeObject *v; |
| 7168 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7169 | Py_ssize_t extrachars = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7170 | PyObject *errorHandler = NULL; |
| 7171 | PyObject *exc = NULL; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7172 | Py_UNICODE *mapstring = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7173 | Py_ssize_t maplen = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7174 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7175 | /* Default to Latin-1 */ |
| 7176 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7177 | return PyUnicode_DecodeLatin1(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7178 | |
| 7179 | v = _PyUnicode_New(size); |
| 7180 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7181 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7182 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7183 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7184 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7185 | e = s + size; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7186 | if (PyUnicode_CheckExact(mapping)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7187 | mapstring = PyUnicode_AS_UNICODE(mapping); |
| 7188 | maplen = PyUnicode_GET_SIZE(mapping); |
| 7189 | while (s < e) { |
| 7190 | unsigned char ch = *s; |
| 7191 | Py_UNICODE x = 0xfffe; /* illegal value */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7192 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7193 | if (ch < maplen) |
| 7194 | x = mapstring[ch]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7195 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7196 | if (x == 0xfffe) { |
| 7197 | /* undefined mapping */ |
| 7198 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 7199 | startinpos = s-starts; |
| 7200 | endinpos = startinpos+1; |
| 7201 | if (unicode_decode_call_errorhandler( |
| 7202 | errors, &errorHandler, |
| 7203 | "charmap", "character maps to <undefined>", |
| 7204 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 7205 | &v, &outpos, &p)) { |
| 7206 | goto onError; |
| 7207 | } |
| 7208 | continue; |
| 7209 | } |
| 7210 | *p++ = x; |
| 7211 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7212 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7213 | } |
| 7214 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7215 | while (s < e) { |
| 7216 | unsigned char ch = *s; |
| 7217 | PyObject *w, *x; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7218 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7219 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 7220 | w = PyLong_FromLong((long)ch); |
| 7221 | if (w == NULL) |
| 7222 | goto onError; |
| 7223 | x = PyObject_GetItem(mapping, w); |
| 7224 | Py_DECREF(w); |
| 7225 | if (x == NULL) { |
| 7226 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 7227 | /* No mapping found means: mapping is undefined. */ |
| 7228 | PyErr_Clear(); |
| 7229 | x = Py_None; |
| 7230 | Py_INCREF(x); |
| 7231 | } else |
| 7232 | goto onError; |
| 7233 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7234 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7235 | /* Apply mapping */ |
| 7236 | if (PyLong_Check(x)) { |
| 7237 | long value = PyLong_AS_LONG(x); |
| 7238 | if (value < 0 || value > 65535) { |
| 7239 | PyErr_SetString(PyExc_TypeError, |
| 7240 | "character mapping must be in range(65536)"); |
| 7241 | Py_DECREF(x); |
| 7242 | goto onError; |
| 7243 | } |
| 7244 | *p++ = (Py_UNICODE)value; |
| 7245 | } |
| 7246 | else if (x == Py_None) { |
| 7247 | /* undefined mapping */ |
| 7248 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 7249 | startinpos = s-starts; |
| 7250 | endinpos = startinpos+1; |
| 7251 | if (unicode_decode_call_errorhandler( |
| 7252 | errors, &errorHandler, |
| 7253 | "charmap", "character maps to <undefined>", |
| 7254 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 7255 | &v, &outpos, &p)) { |
| 7256 | Py_DECREF(x); |
| 7257 | goto onError; |
| 7258 | } |
| 7259 | Py_DECREF(x); |
| 7260 | continue; |
| 7261 | } |
| 7262 | else if (PyUnicode_Check(x)) { |
| 7263 | Py_ssize_t targetsize = PyUnicode_GET_SIZE(x); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7264 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7265 | if (targetsize == 1) |
| 7266 | /* 1-1 mapping */ |
| 7267 | *p++ = *PyUnicode_AS_UNICODE(x); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7268 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7269 | else if (targetsize > 1) { |
| 7270 | /* 1-n mapping */ |
| 7271 | if (targetsize > extrachars) { |
| 7272 | /* resize first */ |
| 7273 | Py_ssize_t oldpos = p - PyUnicode_AS_UNICODE(v); |
| 7274 | Py_ssize_t needed = (targetsize - extrachars) + \ |
| 7275 | (targetsize << 2); |
| 7276 | extrachars += needed; |
| 7277 | /* XXX overflow detection missing */ |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 7278 | if (PyUnicode_Resize((PyObject**)&v, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7279 | PyUnicode_GET_SIZE(v) + needed) < 0) { |
| 7280 | Py_DECREF(x); |
| 7281 | goto onError; |
| 7282 | } |
| 7283 | p = PyUnicode_AS_UNICODE(v) + oldpos; |
| 7284 | } |
| 7285 | Py_UNICODE_COPY(p, |
| 7286 | PyUnicode_AS_UNICODE(x), |
| 7287 | targetsize); |
| 7288 | p += targetsize; |
| 7289 | extrachars -= targetsize; |
| 7290 | } |
| 7291 | /* 1-0 mapping: skip the character */ |
| 7292 | } |
| 7293 | else { |
| 7294 | /* wrong return value */ |
| 7295 | PyErr_SetString(PyExc_TypeError, |
| 7296 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7297 | Py_DECREF(x); |
| 7298 | goto onError; |
| 7299 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7300 | Py_DECREF(x); |
| 7301 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7302 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7303 | } |
| 7304 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 7305 | if (PyUnicode_Resize((PyObject**)&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7306 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7307 | Py_XDECREF(errorHandler); |
| 7308 | Py_XDECREF(exc); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 7309 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 7310 | if (_PyUnicode_READY_REPLACE(&v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7311 | Py_DECREF(v); |
| 7312 | return NULL; |
| 7313 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 7314 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 7315 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7316 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7317 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7318 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7319 | Py_XDECREF(errorHandler); |
| 7320 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7321 | Py_XDECREF(v); |
| 7322 | return NULL; |
| 7323 | } |
| 7324 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7325 | /* Charmap encoding: the lookup table */ |
| 7326 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7327 | struct encoding_map { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7328 | PyObject_HEAD |
| 7329 | unsigned char level1[32]; |
| 7330 | int count2, count3; |
| 7331 | unsigned char level23[1]; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7332 | }; |
| 7333 | |
| 7334 | static PyObject* |
| 7335 | encoding_map_size(PyObject *obj, PyObject* args) |
| 7336 | { |
| 7337 | struct encoding_map *map = (struct encoding_map*)obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7338 | return PyLong_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7339 | 128*map->count3); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7340 | } |
| 7341 | |
| 7342 | static PyMethodDef encoding_map_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7343 | {"size", encoding_map_size, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7344 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 7345 | { 0 } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7346 | }; |
| 7347 | |
| 7348 | static void |
| 7349 | encoding_map_dealloc(PyObject* o) |
| 7350 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7351 | PyObject_FREE(o); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7352 | } |
| 7353 | |
| 7354 | static PyTypeObject EncodingMapType = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7355 | PyVarObject_HEAD_INIT(NULL, 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7356 | "EncodingMap", /*tp_name*/ |
| 7357 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 7358 | 0, /*tp_itemsize*/ |
| 7359 | /* methods */ |
| 7360 | encoding_map_dealloc, /*tp_dealloc*/ |
| 7361 | 0, /*tp_print*/ |
| 7362 | 0, /*tp_getattr*/ |
| 7363 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 7364 | 0, /*tp_reserved*/ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7365 | 0, /*tp_repr*/ |
| 7366 | 0, /*tp_as_number*/ |
| 7367 | 0, /*tp_as_sequence*/ |
| 7368 | 0, /*tp_as_mapping*/ |
| 7369 | 0, /*tp_hash*/ |
| 7370 | 0, /*tp_call*/ |
| 7371 | 0, /*tp_str*/ |
| 7372 | 0, /*tp_getattro*/ |
| 7373 | 0, /*tp_setattro*/ |
| 7374 | 0, /*tp_as_buffer*/ |
| 7375 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 7376 | 0, /*tp_doc*/ |
| 7377 | 0, /*tp_traverse*/ |
| 7378 | 0, /*tp_clear*/ |
| 7379 | 0, /*tp_richcompare*/ |
| 7380 | 0, /*tp_weaklistoffset*/ |
| 7381 | 0, /*tp_iter*/ |
| 7382 | 0, /*tp_iternext*/ |
| 7383 | encoding_map_methods, /*tp_methods*/ |
| 7384 | 0, /*tp_members*/ |
| 7385 | 0, /*tp_getset*/ |
| 7386 | 0, /*tp_base*/ |
| 7387 | 0, /*tp_dict*/ |
| 7388 | 0, /*tp_descr_get*/ |
| 7389 | 0, /*tp_descr_set*/ |
| 7390 | 0, /*tp_dictoffset*/ |
| 7391 | 0, /*tp_init*/ |
| 7392 | 0, /*tp_alloc*/ |
| 7393 | 0, /*tp_new*/ |
| 7394 | 0, /*tp_free*/ |
| 7395 | 0, /*tp_is_gc*/ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7396 | }; |
| 7397 | |
| 7398 | PyObject* |
| 7399 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 7400 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7401 | PyObject *result; |
| 7402 | struct encoding_map *mresult; |
| 7403 | int i; |
| 7404 | int need_dict = 0; |
| 7405 | unsigned char level1[32]; |
| 7406 | unsigned char level2[512]; |
| 7407 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 7408 | int count2 = 0, count3 = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7409 | int kind; |
| 7410 | void *data; |
| 7411 | Py_UCS4 ch; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7412 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7413 | if (!PyUnicode_Check(string) || PyUnicode_GET_LENGTH(string) != 256) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7414 | PyErr_BadArgument(); |
| 7415 | return NULL; |
| 7416 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7417 | kind = PyUnicode_KIND(string); |
| 7418 | data = PyUnicode_DATA(string); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7419 | memset(level1, 0xFF, sizeof level1); |
| 7420 | memset(level2, 0xFF, sizeof level2); |
| 7421 | |
| 7422 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 7423 | or if there are non-BMP characters, we need to use |
| 7424 | a mapping dictionary. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7425 | if (PyUnicode_READ(kind, data, 0) != 0) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7426 | need_dict = 1; |
| 7427 | for (i = 1; i < 256; i++) { |
| 7428 | int l1, l2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7429 | ch = PyUnicode_READ(kind, data, i); |
| 7430 | if (ch == 0 || ch > 0xFFFF) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7431 | need_dict = 1; |
| 7432 | break; |
| 7433 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7434 | if (ch == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7435 | /* unmapped character */ |
| 7436 | continue; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7437 | l1 = ch >> 11; |
| 7438 | l2 = ch >> 7; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7439 | if (level1[l1] == 0xFF) |
| 7440 | level1[l1] = count2++; |
| 7441 | if (level2[l2] == 0xFF) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7442 | level2[l2] = count3++; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7443 | } |
| 7444 | |
| 7445 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 7446 | need_dict = 1; |
| 7447 | |
| 7448 | if (need_dict) { |
| 7449 | PyObject *result = PyDict_New(); |
| 7450 | PyObject *key, *value; |
| 7451 | if (!result) |
| 7452 | return NULL; |
| 7453 | for (i = 0; i < 256; i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7454 | key = PyLong_FromLong(PyUnicode_READ(kind, data, i)); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7455 | value = PyLong_FromLong(i); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7456 | if (!key || !value) |
| 7457 | goto failed1; |
| 7458 | if (PyDict_SetItem(result, key, value) == -1) |
| 7459 | goto failed1; |
| 7460 | Py_DECREF(key); |
| 7461 | Py_DECREF(value); |
| 7462 | } |
| 7463 | return result; |
| 7464 | failed1: |
| 7465 | Py_XDECREF(key); |
| 7466 | Py_XDECREF(value); |
| 7467 | Py_DECREF(result); |
| 7468 | return NULL; |
| 7469 | } |
| 7470 | |
| 7471 | /* Create a three-level trie */ |
| 7472 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 7473 | 16*count2 + 128*count3 - 1); |
| 7474 | if (!result) |
| 7475 | return PyErr_NoMemory(); |
| 7476 | PyObject_Init(result, &EncodingMapType); |
| 7477 | mresult = (struct encoding_map*)result; |
| 7478 | mresult->count2 = count2; |
| 7479 | mresult->count3 = count3; |
| 7480 | mlevel1 = mresult->level1; |
| 7481 | mlevel2 = mresult->level23; |
| 7482 | mlevel3 = mresult->level23 + 16*count2; |
| 7483 | memcpy(mlevel1, level1, 32); |
| 7484 | memset(mlevel2, 0xFF, 16*count2); |
| 7485 | memset(mlevel3, 0, 128*count3); |
| 7486 | count3 = 0; |
| 7487 | for (i = 1; i < 256; i++) { |
| 7488 | int o1, o2, o3, i2, i3; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7489 | if (PyUnicode_READ(kind, data, i) == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7490 | /* unmapped character */ |
| 7491 | continue; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7492 | o1 = PyUnicode_READ(kind, data, i)>>11; |
| 7493 | o2 = (PyUnicode_READ(kind, data, i)>>7) & 0xF; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7494 | i2 = 16*mlevel1[o1] + o2; |
| 7495 | if (mlevel2[i2] == 0xFF) |
| 7496 | mlevel2[i2] = count3++; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7497 | o3 = PyUnicode_READ(kind, data, i) & 0x7F; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7498 | i3 = 128*mlevel2[i2] + o3; |
| 7499 | mlevel3[i3] = i; |
| 7500 | } |
| 7501 | return result; |
| 7502 | } |
| 7503 | |
| 7504 | static int |
| 7505 | encoding_map_lookup(Py_UNICODE c, PyObject *mapping) |
| 7506 | { |
| 7507 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 7508 | int l1 = c>>11; |
| 7509 | int l2 = (c>>7) & 0xF; |
| 7510 | int l3 = c & 0x7F; |
| 7511 | int i; |
| 7512 | |
| 7513 | #ifdef Py_UNICODE_WIDE |
| 7514 | if (c > 0xFFFF) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7515 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7516 | } |
| 7517 | #endif |
| 7518 | if (c == 0) |
| 7519 | return 0; |
| 7520 | /* level 1*/ |
| 7521 | i = map->level1[l1]; |
| 7522 | if (i == 0xFF) { |
| 7523 | return -1; |
| 7524 | } |
| 7525 | /* level 2*/ |
| 7526 | i = map->level23[16*i+l2]; |
| 7527 | if (i == 0xFF) { |
| 7528 | return -1; |
| 7529 | } |
| 7530 | /* level 3 */ |
| 7531 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 7532 | if (i == 0) { |
| 7533 | return -1; |
| 7534 | } |
| 7535 | return i; |
| 7536 | } |
| 7537 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7538 | /* Lookup the character ch in the mapping. If the character |
| 7539 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 7540 | error occurred). */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7541 | static PyObject * |
| 7542 | charmapencode_lookup(Py_UNICODE c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7543 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7544 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7545 | PyObject *x; |
| 7546 | |
| 7547 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7548 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7549 | x = PyObject_GetItem(mapping, w); |
| 7550 | Py_DECREF(w); |
| 7551 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7552 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 7553 | /* No mapping found means: mapping is undefined. */ |
| 7554 | PyErr_Clear(); |
| 7555 | x = Py_None; |
| 7556 | Py_INCREF(x); |
| 7557 | return x; |
| 7558 | } else |
| 7559 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7560 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 7561 | else if (x == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7562 | return x; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7563 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7564 | long value = PyLong_AS_LONG(x); |
| 7565 | if (value < 0 || value > 255) { |
| 7566 | PyErr_SetString(PyExc_TypeError, |
| 7567 | "character mapping must be in range(256)"); |
| 7568 | Py_DECREF(x); |
| 7569 | return NULL; |
| 7570 | } |
| 7571 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7572 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7573 | else if (PyBytes_Check(x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7574 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7575 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7576 | /* wrong return value */ |
| 7577 | PyErr_Format(PyExc_TypeError, |
| 7578 | "character mapping must return integer, bytes or None, not %.400s", |
| 7579 | x->ob_type->tp_name); |
| 7580 | Py_DECREF(x); |
| 7581 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7582 | } |
| 7583 | } |
| 7584 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7585 | static int |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 7586 | charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7587 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7588 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
| 7589 | /* exponentially overallocate to minimize reallocations */ |
| 7590 | if (requiredsize < 2*outsize) |
| 7591 | requiredsize = 2*outsize; |
| 7592 | if (_PyBytes_Resize(outobj, requiredsize)) |
| 7593 | return -1; |
| 7594 | return 0; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7595 | } |
| 7596 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7597 | typedef enum charmapencode_result { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7598 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7599 | } charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7600 | /* 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] | 7601 | various state variables. Resize the output bytes object if not enough |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7602 | space is available. Return a new reference to the object that |
| 7603 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 7604 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 7605 | reallocation error occurred. The caller must decref the result */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7606 | static charmapencode_result |
| 7607 | charmapencode_output(Py_UNICODE c, PyObject *mapping, |
| 7608 | PyObject **outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7609 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7610 | PyObject *rep; |
| 7611 | char *outstart; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7612 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7613 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7614 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7615 | int res = encoding_map_lookup(c, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7616 | Py_ssize_t requiredsize = *outpos+1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7617 | if (res == -1) |
| 7618 | return enc_FAILED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7619 | if (outsize<requiredsize) |
| 7620 | if (charmapencode_resize(outobj, outpos, requiredsize)) |
| 7621 | return enc_EXCEPTION; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7622 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7623 | outstart[(*outpos)++] = (char)res; |
| 7624 | return enc_SUCCESS; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7625 | } |
| 7626 | |
| 7627 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7628 | if (rep==NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7629 | return enc_EXCEPTION; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7630 | else if (rep==Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7631 | Py_DECREF(rep); |
| 7632 | return enc_FAILED; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7633 | } else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7634 | if (PyLong_Check(rep)) { |
| 7635 | Py_ssize_t requiredsize = *outpos+1; |
| 7636 | if (outsize<requiredsize) |
| 7637 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 7638 | Py_DECREF(rep); |
| 7639 | return enc_EXCEPTION; |
| 7640 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7641 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7642 | outstart[(*outpos)++] = (char)PyLong_AS_LONG(rep); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7643 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7644 | else { |
| 7645 | const char *repchars = PyBytes_AS_STRING(rep); |
| 7646 | Py_ssize_t repsize = PyBytes_GET_SIZE(rep); |
| 7647 | Py_ssize_t requiredsize = *outpos+repsize; |
| 7648 | if (outsize<requiredsize) |
| 7649 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 7650 | Py_DECREF(rep); |
| 7651 | return enc_EXCEPTION; |
| 7652 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7653 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7654 | memcpy(outstart + *outpos, repchars, repsize); |
| 7655 | *outpos += repsize; |
| 7656 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7657 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7658 | Py_DECREF(rep); |
| 7659 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7660 | } |
| 7661 | |
| 7662 | /* handle an error in PyUnicode_EncodeCharmap |
| 7663 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7664 | static int |
| 7665 | charmap_encoding_error( |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7666 | const Py_UNICODE *p, Py_ssize_t size, Py_ssize_t *inpos, PyObject *mapping, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7667 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 7668 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 7669 | PyObject **res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7670 | { |
| 7671 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7672 | Py_ssize_t repsize; |
| 7673 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7674 | Py_UNICODE *uni2; |
| 7675 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7676 | Py_ssize_t collstartpos = *inpos; |
| 7677 | Py_ssize_t collendpos = *inpos+1; |
| 7678 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7679 | char *encoding = "charmap"; |
| 7680 | char *reason = "character maps to <undefined>"; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7681 | charmapencode_result x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7682 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7683 | /* find all unencodable characters */ |
| 7684 | while (collendpos < size) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7685 | PyObject *rep; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7686 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7687 | int res = encoding_map_lookup(p[collendpos], mapping); |
| 7688 | if (res != -1) |
| 7689 | break; |
| 7690 | ++collendpos; |
| 7691 | continue; |
| 7692 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7693 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7694 | rep = charmapencode_lookup(p[collendpos], mapping); |
| 7695 | if (rep==NULL) |
| 7696 | return -1; |
| 7697 | else if (rep!=Py_None) { |
| 7698 | Py_DECREF(rep); |
| 7699 | break; |
| 7700 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7701 | Py_DECREF(rep); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7702 | ++collendpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7703 | } |
| 7704 | /* cache callback name lookup |
| 7705 | * (if not done yet, i.e. it's the first error) */ |
| 7706 | if (*known_errorHandler==-1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7707 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 7708 | *known_errorHandler = 1; |
| 7709 | else if (!strcmp(errors, "replace")) |
| 7710 | *known_errorHandler = 2; |
| 7711 | else if (!strcmp(errors, "ignore")) |
| 7712 | *known_errorHandler = 3; |
| 7713 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 7714 | *known_errorHandler = 4; |
| 7715 | else |
| 7716 | *known_errorHandler = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7717 | } |
| 7718 | switch (*known_errorHandler) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7719 | case 1: /* strict */ |
| 7720 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 7721 | return -1; |
| 7722 | case 2: /* replace */ |
| 7723 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7724 | x = charmapencode_output('?', mapping, res, respos); |
| 7725 | if (x==enc_EXCEPTION) { |
| 7726 | return -1; |
| 7727 | } |
| 7728 | else if (x==enc_FAILED) { |
| 7729 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 7730 | return -1; |
| 7731 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7732 | } |
| 7733 | /* fall through */ |
| 7734 | case 3: /* ignore */ |
| 7735 | *inpos = collendpos; |
| 7736 | break; |
| 7737 | case 4: /* xmlcharrefreplace */ |
| 7738 | /* generate replacement (temporarily (mis)uses p) */ |
| 7739 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7740 | char buffer[2+29+1+1]; |
| 7741 | char *cp; |
| 7742 | sprintf(buffer, "&#%d;", (int)p[collpos]); |
| 7743 | for (cp = buffer; *cp; ++cp) { |
| 7744 | x = charmapencode_output(*cp, mapping, res, respos); |
| 7745 | if (x==enc_EXCEPTION) |
| 7746 | return -1; |
| 7747 | else if (x==enc_FAILED) { |
| 7748 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 7749 | return -1; |
| 7750 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7751 | } |
| 7752 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7753 | *inpos = collendpos; |
| 7754 | break; |
| 7755 | default: |
| 7756 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7757 | encoding, reason, p, size, exceptionObject, |
| 7758 | collstartpos, collendpos, &newpos); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7759 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7760 | return -1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 7761 | if (PyBytes_Check(repunicode)) { |
| 7762 | /* Directly copy bytes result to output. */ |
| 7763 | Py_ssize_t outsize = PyBytes_Size(*res); |
| 7764 | Py_ssize_t requiredsize; |
| 7765 | repsize = PyBytes_Size(repunicode); |
| 7766 | requiredsize = *respos + repsize; |
| 7767 | if (requiredsize > outsize) |
| 7768 | /* Make room for all additional bytes. */ |
| 7769 | if (charmapencode_resize(res, respos, requiredsize)) { |
| 7770 | Py_DECREF(repunicode); |
| 7771 | return -1; |
| 7772 | } |
| 7773 | memcpy(PyBytes_AsString(*res) + *respos, |
| 7774 | PyBytes_AsString(repunicode), repsize); |
| 7775 | *respos += repsize; |
| 7776 | *inpos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 7777 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 7778 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 7779 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7780 | /* generate replacement */ |
| 7781 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 7782 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7783 | x = charmapencode_output(*uni2, mapping, res, respos); |
| 7784 | if (x==enc_EXCEPTION) { |
| 7785 | return -1; |
| 7786 | } |
| 7787 | else if (x==enc_FAILED) { |
| 7788 | Py_DECREF(repunicode); |
| 7789 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 7790 | return -1; |
| 7791 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7792 | } |
| 7793 | *inpos = newpos; |
| 7794 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7795 | } |
| 7796 | return 0; |
| 7797 | } |
| 7798 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7799 | PyObject * |
| 7800 | PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
| 7801 | Py_ssize_t size, |
| 7802 | PyObject *mapping, |
| 7803 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7804 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7805 | /* output object */ |
| 7806 | PyObject *res = NULL; |
| 7807 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7808 | Py_ssize_t inpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7809 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7810 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7811 | PyObject *errorHandler = NULL; |
| 7812 | PyObject *exc = NULL; |
| 7813 | /* the following variable is used for caching string comparisons |
| 7814 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 7815 | * 3=ignore, 4=xmlcharrefreplace */ |
| 7816 | int known_errorHandler = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7817 | |
| 7818 | /* Default to Latin-1 */ |
| 7819 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7820 | return PyUnicode_EncodeLatin1(p, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7821 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7822 | /* allocate enough for a simple encoding without |
| 7823 | replacements, if we need more, we'll resize */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7824 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7825 | if (res == NULL) |
| 7826 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 7827 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7828 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7829 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7830 | while (inpos<size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7831 | /* try to encode it */ |
| 7832 | charmapencode_result x = charmapencode_output(p[inpos], mapping, &res, &respos); |
| 7833 | if (x==enc_EXCEPTION) /* error */ |
| 7834 | goto onError; |
| 7835 | if (x==enc_FAILED) { /* unencodable character */ |
| 7836 | if (charmap_encoding_error(p, size, &inpos, mapping, |
| 7837 | &exc, |
| 7838 | &known_errorHandler, &errorHandler, errors, |
| 7839 | &res, &respos)) { |
| 7840 | goto onError; |
| 7841 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7842 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7843 | else |
| 7844 | /* done with this character => adjust input position */ |
| 7845 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7846 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7847 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7848 | /* Resize if we allocated to much */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7849 | if (respos<PyBytes_GET_SIZE(res)) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 7850 | if (_PyBytes_Resize(&res, respos) < 0) |
| 7851 | goto onError; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 7852 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7853 | Py_XDECREF(exc); |
| 7854 | Py_XDECREF(errorHandler); |
| 7855 | return res; |
| 7856 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7857 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7858 | Py_XDECREF(res); |
| 7859 | Py_XDECREF(exc); |
| 7860 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7861 | return NULL; |
| 7862 | } |
| 7863 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7864 | PyObject * |
| 7865 | PyUnicode_AsCharmapString(PyObject *unicode, |
| 7866 | PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7867 | { |
| 7868 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7869 | PyErr_BadArgument(); |
| 7870 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7871 | } |
| 7872 | return PyUnicode_EncodeCharmap(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7873 | PyUnicode_GET_SIZE(unicode), |
| 7874 | mapping, |
| 7875 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7876 | } |
| 7877 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7878 | /* create or adjust a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7879 | static void |
| 7880 | make_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7881 | PyObject *unicode, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7882 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 7883 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7884 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7885 | if (*exceptionObject == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7886 | *exceptionObject = _PyUnicodeTranslateError_Create( |
| 7887 | unicode, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7888 | } |
| 7889 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7890 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 7891 | goto onError; |
| 7892 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 7893 | goto onError; |
| 7894 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 7895 | goto onError; |
| 7896 | return; |
| 7897 | onError: |
| 7898 | Py_DECREF(*exceptionObject); |
| 7899 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7900 | } |
| 7901 | } |
| 7902 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7903 | /* raises a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7904 | static void |
| 7905 | raise_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7906 | PyObject *unicode, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7907 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 7908 | const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7909 | { |
| 7910 | make_translate_exception(exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7911 | unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7912 | if (*exceptionObject != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7913 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7914 | } |
| 7915 | |
| 7916 | /* error handling callback helper: |
| 7917 | build arguments, call the callback and check the arguments, |
| 7918 | put the result into newpos and return the replacement string, which |
| 7919 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7920 | static PyObject * |
| 7921 | unicode_translate_call_errorhandler(const char *errors, |
| 7922 | PyObject **errorHandler, |
| 7923 | const char *reason, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7924 | PyObject *unicode, PyObject **exceptionObject, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7925 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 7926 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7927 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 7928 | 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] | 7929 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7930 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7931 | PyObject *restuple; |
| 7932 | PyObject *resunicode; |
| 7933 | |
| 7934 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7935 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7936 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7937 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7938 | } |
| 7939 | |
| 7940 | make_translate_exception(exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7941 | unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7942 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7943 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7944 | |
| 7945 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7946 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7947 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7948 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7949 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 7950 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7951 | Py_DECREF(restuple); |
| 7952 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7953 | } |
| 7954 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7955 | &resunicode, &i_newpos)) { |
| 7956 | Py_DECREF(restuple); |
| 7957 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7958 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7959 | if (i_newpos<0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7960 | *newpos = PyUnicode_GET_LENGTH(unicode)+i_newpos; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7961 | else |
| 7962 | *newpos = i_newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7963 | if (*newpos<0 || *newpos>PyUnicode_GET_LENGTH(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7964 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 7965 | Py_DECREF(restuple); |
| 7966 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 7967 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7968 | Py_INCREF(resunicode); |
| 7969 | Py_DECREF(restuple); |
| 7970 | return resunicode; |
| 7971 | } |
| 7972 | |
| 7973 | /* Lookup the character ch in the mapping and put the result in result, |
| 7974 | which must be decrefed by the caller. |
| 7975 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7976 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7977 | charmaptranslate_lookup(Py_UCS4 c, PyObject *mapping, PyObject **result) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7978 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7979 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7980 | PyObject *x; |
| 7981 | |
| 7982 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7983 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7984 | x = PyObject_GetItem(mapping, w); |
| 7985 | Py_DECREF(w); |
| 7986 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7987 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 7988 | /* No mapping found means: use 1:1 mapping. */ |
| 7989 | PyErr_Clear(); |
| 7990 | *result = NULL; |
| 7991 | return 0; |
| 7992 | } else |
| 7993 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7994 | } |
| 7995 | else if (x == Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7996 | *result = x; |
| 7997 | return 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7998 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7999 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8000 | long value = PyLong_AS_LONG(x); |
| 8001 | long max = PyUnicode_GetMax(); |
| 8002 | if (value < 0 || value > max) { |
| 8003 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5a2f7e60 | 2007-10-24 21:13:09 +0000 | [diff] [blame] | 8004 | "character mapping must be in range(0x%x)", max+1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8005 | Py_DECREF(x); |
| 8006 | return -1; |
| 8007 | } |
| 8008 | *result = x; |
| 8009 | return 0; |
| 8010 | } |
| 8011 | else if (PyUnicode_Check(x)) { |
| 8012 | *result = x; |
| 8013 | return 0; |
| 8014 | } |
| 8015 | else { |
| 8016 | /* wrong return value */ |
| 8017 | PyErr_SetString(PyExc_TypeError, |
| 8018 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8019 | Py_DECREF(x); |
| 8020 | return -1; |
| 8021 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8022 | } |
| 8023 | /* ensure that *outobj is at least requiredsize characters long, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8024 | if not reallocate and adjust various state variables. |
| 8025 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8026 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8027 | charmaptranslate_makespace(Py_UCS4 **outobj, Py_ssize_t *psize, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8028 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8029 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8030 | Py_ssize_t oldsize = *psize; |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 8031 | if (requiredsize > oldsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8032 | /* exponentially overallocate to minimize reallocations */ |
| 8033 | if (requiredsize < 2 * oldsize) |
| 8034 | requiredsize = 2 * oldsize; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8035 | *outobj = PyMem_Realloc(*outobj, requiredsize * sizeof(Py_UCS4)); |
| 8036 | if (*outobj == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8037 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8038 | *psize = requiredsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8039 | } |
| 8040 | return 0; |
| 8041 | } |
| 8042 | /* lookup the character, put the result in the output string and adjust |
| 8043 | various state variables. Return a new reference to the object that |
| 8044 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 8045 | undefined (in which case no character was written). |
| 8046 | The called must decref result. |
| 8047 | Return 0 on success, -1 on error. */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8048 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8049 | charmaptranslate_output(PyObject *input, Py_ssize_t ipos, |
| 8050 | PyObject *mapping, Py_UCS4 **output, |
| 8051 | Py_ssize_t *osize, Py_ssize_t *opos, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8052 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8053 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8054 | Py_UCS4 curinp = PyUnicode_READ_CHAR(input, ipos); |
| 8055 | if (charmaptranslate_lookup(curinp, mapping, res)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8056 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8057 | if (*res==NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8058 | /* not found => default to 1:1 mapping */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8059 | (*output)[(*opos)++] = curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8060 | } |
| 8061 | else if (*res==Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8062 | ; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8063 | else if (PyLong_Check(*res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8064 | /* 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] | 8065 | (*output)[(*opos)++] = (Py_UCS4)PyLong_AS_LONG(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8066 | } |
| 8067 | else if (PyUnicode_Check(*res)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8068 | Py_ssize_t repsize; |
| 8069 | if (PyUnicode_READY(*res) == -1) |
| 8070 | return -1; |
| 8071 | repsize = PyUnicode_GET_LENGTH(*res); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8072 | if (repsize==1) { |
| 8073 | /* 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] | 8074 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8075 | } |
| 8076 | else if (repsize!=0) { |
| 8077 | /* more than one character */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8078 | Py_ssize_t requiredsize = *opos + |
| 8079 | (PyUnicode_GET_LENGTH(input) - ipos) + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8080 | repsize - 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8081 | Py_ssize_t i; |
| 8082 | if (charmaptranslate_makespace(output, osize, requiredsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8083 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8084 | for(i = 0; i < repsize; i++) |
| 8085 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, i); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8086 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8087 | } |
| 8088 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8089 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8090 | return 0; |
| 8091 | } |
| 8092 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8093 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8094 | _PyUnicode_TranslateCharmap(PyObject *input, |
| 8095 | PyObject *mapping, |
| 8096 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8097 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8098 | /* input object */ |
| 8099 | char *idata; |
| 8100 | Py_ssize_t size, i; |
| 8101 | int kind; |
| 8102 | /* output buffer */ |
| 8103 | Py_UCS4 *output = NULL; |
| 8104 | Py_ssize_t osize; |
| 8105 | PyObject *res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8106 | /* current output position */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8107 | Py_ssize_t opos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8108 | char *reason = "character maps to <undefined>"; |
| 8109 | PyObject *errorHandler = NULL; |
| 8110 | PyObject *exc = NULL; |
| 8111 | /* the following variable is used for caching string comparisons |
| 8112 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 8113 | * 3=ignore, 4=xmlcharrefreplace */ |
| 8114 | int known_errorHandler = -1; |
| 8115 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8116 | if (mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8117 | PyErr_BadArgument(); |
| 8118 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8119 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8120 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8121 | if (PyUnicode_READY(input) == -1) |
| 8122 | return NULL; |
| 8123 | idata = (char*)PyUnicode_DATA(input); |
| 8124 | kind = PyUnicode_KIND(input); |
| 8125 | size = PyUnicode_GET_LENGTH(input); |
| 8126 | i = 0; |
| 8127 | |
| 8128 | if (size == 0) { |
| 8129 | Py_INCREF(input); |
| 8130 | return input; |
| 8131 | } |
| 8132 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8133 | /* allocate enough for a simple 1:1 translation without |
| 8134 | replacements, if we need more, we'll resize */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8135 | osize = size; |
| 8136 | output = PyMem_Malloc(osize * sizeof(Py_UCS4)); |
| 8137 | opos = 0; |
| 8138 | if (output == NULL) { |
| 8139 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8140 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8141 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8142 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8143 | while (i<size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8144 | /* try to encode it */ |
| 8145 | PyObject *x = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8146 | if (charmaptranslate_output(input, i, mapping, |
| 8147 | &output, &osize, &opos, &x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8148 | Py_XDECREF(x); |
| 8149 | goto onError; |
| 8150 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8151 | Py_XDECREF(x); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8152 | if (x!=Py_None) /* it worked => adjust input pointer */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8153 | ++i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8154 | else { /* untranslatable character */ |
| 8155 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
| 8156 | Py_ssize_t repsize; |
| 8157 | Py_ssize_t newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8158 | Py_ssize_t uni2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8159 | /* startpos for collecting untranslatable chars */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8160 | Py_ssize_t collstart = i; |
| 8161 | Py_ssize_t collend = i+1; |
| 8162 | Py_ssize_t coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8163 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8164 | /* find all untranslatable characters */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8165 | while (collend < size) { |
| 8166 | if (charmaptranslate_lookup(PyUnicode_READ(kind,idata, collend), mapping, &x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8167 | goto onError; |
| 8168 | Py_XDECREF(x); |
| 8169 | if (x!=Py_None) |
| 8170 | break; |
| 8171 | ++collend; |
| 8172 | } |
| 8173 | /* cache callback name lookup |
| 8174 | * (if not done yet, i.e. it's the first error) */ |
| 8175 | if (known_errorHandler==-1) { |
| 8176 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 8177 | known_errorHandler = 1; |
| 8178 | else if (!strcmp(errors, "replace")) |
| 8179 | known_errorHandler = 2; |
| 8180 | else if (!strcmp(errors, "ignore")) |
| 8181 | known_errorHandler = 3; |
| 8182 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 8183 | known_errorHandler = 4; |
| 8184 | else |
| 8185 | known_errorHandler = 0; |
| 8186 | } |
| 8187 | switch (known_errorHandler) { |
| 8188 | case 1: /* strict */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8189 | raise_translate_exception(&exc, input, collstart, |
| 8190 | collend, reason); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8191 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8192 | case 2: /* replace */ |
| 8193 | /* 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] | 8194 | for (coll = collstart; coll<collend; coll++) |
| 8195 | output[opos++] = '?'; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8196 | /* fall through */ |
| 8197 | case 3: /* ignore */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8198 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8199 | break; |
| 8200 | case 4: /* xmlcharrefreplace */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8201 | /* generate replacement (temporarily (mis)uses i) */ |
| 8202 | for (i = collstart; i < collend; ++i) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8203 | char buffer[2+29+1+1]; |
| 8204 | char *cp; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8205 | sprintf(buffer, "&#%d;", PyUnicode_READ(kind, idata, i)); |
| 8206 | if (charmaptranslate_makespace(&output, &osize, |
| 8207 | opos+strlen(buffer)+(size-collend))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8208 | goto onError; |
| 8209 | for (cp = buffer; *cp; ++cp) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8210 | output[opos++] = *cp; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8211 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8212 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8213 | break; |
| 8214 | default: |
| 8215 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8216 | reason, input, &exc, |
| 8217 | collstart, collend, &newpos); |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 8218 | if (repunicode == NULL || _PyUnicode_READY_REPLACE(&repunicode)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8219 | goto onError; |
| 8220 | /* generate replacement */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8221 | repsize = PyUnicode_GET_LENGTH(repunicode); |
| 8222 | if (charmaptranslate_makespace(&output, &osize, |
| 8223 | opos+repsize+(size-collend))) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8224 | Py_DECREF(repunicode); |
| 8225 | goto onError; |
| 8226 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8227 | for (uni2 = 0; repsize-->0; ++uni2) |
| 8228 | output[opos++] = PyUnicode_READ_CHAR(repunicode, uni2); |
| 8229 | i = newpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8230 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8231 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8232 | } |
| 8233 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8234 | res = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, output, opos); |
| 8235 | if (!res) |
| 8236 | goto onError; |
| 8237 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8238 | Py_XDECREF(exc); |
| 8239 | Py_XDECREF(errorHandler); |
| 8240 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8241 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8242 | onError: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8243 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8244 | Py_XDECREF(exc); |
| 8245 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8246 | return NULL; |
| 8247 | } |
| 8248 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8249 | /* Deprecated. Use PyUnicode_Translate instead. */ |
| 8250 | PyObject * |
| 8251 | PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
| 8252 | Py_ssize_t size, |
| 8253 | PyObject *mapping, |
| 8254 | const char *errors) |
| 8255 | { |
| 8256 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 8257 | if (!unicode) |
| 8258 | return NULL; |
| 8259 | return _PyUnicode_TranslateCharmap(unicode, mapping, errors); |
| 8260 | } |
| 8261 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8262 | PyObject * |
| 8263 | PyUnicode_Translate(PyObject *str, |
| 8264 | PyObject *mapping, |
| 8265 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8266 | { |
| 8267 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8268 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8269 | str = PyUnicode_FromObject(str); |
| 8270 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8271 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8272 | result = _PyUnicode_TranslateCharmap(str, mapping, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8273 | Py_DECREF(str); |
| 8274 | return result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8275 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8276 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8277 | Py_XDECREF(str); |
| 8278 | return NULL; |
| 8279 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8280 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8281 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8282 | fix_decimal_and_space_to_ascii(PyObject *self) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8283 | { |
| 8284 | /* No need to call PyUnicode_READY(self) because this function is only |
| 8285 | called as a callback from fixup() which does it already. */ |
| 8286 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8287 | const int kind = PyUnicode_KIND(self); |
| 8288 | void *data = PyUnicode_DATA(self); |
| 8289 | Py_UCS4 maxchar = 0, ch, fixed; |
| 8290 | Py_ssize_t i; |
| 8291 | |
| 8292 | for (i = 0; i < len; ++i) { |
| 8293 | ch = PyUnicode_READ(kind, data, i); |
| 8294 | fixed = 0; |
| 8295 | if (ch > 127) { |
| 8296 | if (Py_UNICODE_ISSPACE(ch)) |
| 8297 | fixed = ' '; |
| 8298 | else { |
| 8299 | const int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8300 | if (decimal >= 0) |
| 8301 | fixed = '0' + decimal; |
| 8302 | } |
| 8303 | if (fixed != 0) { |
| 8304 | if (fixed > maxchar) |
| 8305 | maxchar = fixed; |
| 8306 | PyUnicode_WRITE(kind, data, i, fixed); |
| 8307 | } |
| 8308 | else if (ch > maxchar) |
| 8309 | maxchar = ch; |
| 8310 | } |
| 8311 | else if (ch > maxchar) |
| 8312 | maxchar = ch; |
| 8313 | } |
| 8314 | |
| 8315 | return maxchar; |
| 8316 | } |
| 8317 | |
| 8318 | PyObject * |
| 8319 | _PyUnicode_TransformDecimalAndSpaceToASCII(PyObject *unicode) |
| 8320 | { |
| 8321 | if (!PyUnicode_Check(unicode)) { |
| 8322 | PyErr_BadInternalCall(); |
| 8323 | return NULL; |
| 8324 | } |
| 8325 | if (PyUnicode_READY(unicode) == -1) |
| 8326 | return NULL; |
| 8327 | if (PyUnicode_MAX_CHAR_VALUE(unicode) <= 127) { |
| 8328 | /* If the string is already ASCII, just return the same string */ |
| 8329 | Py_INCREF(unicode); |
| 8330 | return unicode; |
| 8331 | } |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8332 | return fixup(unicode, fix_decimal_and_space_to_ascii); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8333 | } |
| 8334 | |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8335 | PyObject * |
| 8336 | PyUnicode_TransformDecimalToASCII(Py_UNICODE *s, |
| 8337 | Py_ssize_t length) |
| 8338 | { |
| 8339 | PyObject *result; |
| 8340 | Py_UNICODE *p; /* write pointer into result */ |
| 8341 | Py_ssize_t i; |
| 8342 | /* Copy to a new string */ |
| 8343 | result = (PyObject *)_PyUnicode_New(length); |
| 8344 | Py_UNICODE_COPY(PyUnicode_AS_UNICODE(result), s, length); |
| 8345 | if (result == NULL) |
| 8346 | return result; |
| 8347 | p = PyUnicode_AS_UNICODE(result); |
| 8348 | /* Iterate over code points */ |
| 8349 | for (i = 0; i < length; i++) { |
| 8350 | Py_UNICODE ch =s[i]; |
| 8351 | if (ch > 127) { |
| 8352 | int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8353 | if (decimal >= 0) |
| 8354 | p[i] = '0' + decimal; |
| 8355 | } |
| 8356 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 8357 | #ifndef DONT_MAKE_RESULT_READY |
| 8358 | if (_PyUnicode_READY_REPLACE(&result)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8359 | Py_DECREF(result); |
| 8360 | return NULL; |
| 8361 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 8362 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 8363 | assert(_PyUnicode_CheckConsistency(result, 1)); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8364 | return result; |
| 8365 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8366 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 8367 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8368 | int |
| 8369 | PyUnicode_EncodeDecimal(Py_UNICODE *s, |
| 8370 | Py_ssize_t length, |
| 8371 | char *output, |
| 8372 | const char *errors) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8373 | { |
| 8374 | Py_UNICODE *p, *end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8375 | PyObject *errorHandler = NULL; |
| 8376 | PyObject *exc = NULL; |
| 8377 | const char *encoding = "decimal"; |
| 8378 | const char *reason = "invalid decimal Unicode string"; |
| 8379 | /* the following variable is used for caching string comparisons |
| 8380 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 8381 | int known_errorHandler = -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8382 | |
| 8383 | if (output == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8384 | PyErr_BadArgument(); |
| 8385 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8386 | } |
| 8387 | |
| 8388 | p = s; |
| 8389 | end = s + length; |
| 8390 | while (p < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8391 | register Py_UNICODE ch = *p; |
| 8392 | int decimal; |
| 8393 | PyObject *repunicode; |
| 8394 | Py_ssize_t repsize; |
| 8395 | Py_ssize_t newpos; |
| 8396 | Py_UNICODE *uni2; |
| 8397 | Py_UNICODE *collstart; |
| 8398 | Py_UNICODE *collend; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8399 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8400 | if (Py_UNICODE_ISSPACE(ch)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8401 | *output++ = ' '; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8402 | ++p; |
| 8403 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8404 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8405 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 8406 | if (decimal >= 0) { |
| 8407 | *output++ = '0' + decimal; |
| 8408 | ++p; |
| 8409 | continue; |
| 8410 | } |
| 8411 | if (0 < ch && ch < 256) { |
| 8412 | *output++ = (char)ch; |
| 8413 | ++p; |
| 8414 | continue; |
| 8415 | } |
| 8416 | /* All other characters are considered unencodable */ |
| 8417 | collstart = p; |
| 8418 | collend = p+1; |
| 8419 | while (collend < end) { |
| 8420 | if ((0 < *collend && *collend < 256) || |
| 8421 | !Py_UNICODE_ISSPACE(*collend) || |
| 8422 | Py_UNICODE_TODECIMAL(*collend)) |
| 8423 | break; |
| 8424 | } |
| 8425 | /* cache callback name lookup |
| 8426 | * (if not done yet, i.e. it's the first error) */ |
| 8427 | if (known_errorHandler==-1) { |
| 8428 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 8429 | known_errorHandler = 1; |
| 8430 | else if (!strcmp(errors, "replace")) |
| 8431 | known_errorHandler = 2; |
| 8432 | else if (!strcmp(errors, "ignore")) |
| 8433 | known_errorHandler = 3; |
| 8434 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 8435 | known_errorHandler = 4; |
| 8436 | else |
| 8437 | known_errorHandler = 0; |
| 8438 | } |
| 8439 | switch (known_errorHandler) { |
| 8440 | case 1: /* strict */ |
| 8441 | raise_encode_exception(&exc, encoding, s, length, collstart-s, collend-s, reason); |
| 8442 | goto onError; |
| 8443 | case 2: /* replace */ |
| 8444 | for (p = collstart; p < collend; ++p) |
| 8445 | *output++ = '?'; |
| 8446 | /* fall through */ |
| 8447 | case 3: /* ignore */ |
| 8448 | p = collend; |
| 8449 | break; |
| 8450 | case 4: /* xmlcharrefreplace */ |
| 8451 | /* generate replacement (temporarily (mis)uses p) */ |
| 8452 | for (p = collstart; p < collend; ++p) |
| 8453 | output += sprintf(output, "&#%d;", (int)*p); |
| 8454 | p = collend; |
| 8455 | break; |
| 8456 | default: |
| 8457 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 8458 | encoding, reason, s, length, &exc, |
| 8459 | collstart-s, collend-s, &newpos); |
| 8460 | if (repunicode == NULL) |
| 8461 | goto onError; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8462 | if (!PyUnicode_Check(repunicode)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8463 | /* Byte results not supported, since they have no decimal property. */ |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8464 | PyErr_SetString(PyExc_TypeError, "error handler should return unicode"); |
| 8465 | Py_DECREF(repunicode); |
| 8466 | goto onError; |
| 8467 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8468 | /* generate replacement */ |
| 8469 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 8470 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 8471 | Py_UNICODE ch = *uni2; |
| 8472 | if (Py_UNICODE_ISSPACE(ch)) |
| 8473 | *output++ = ' '; |
| 8474 | else { |
| 8475 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 8476 | if (decimal >= 0) |
| 8477 | *output++ = '0' + decimal; |
| 8478 | else if (0 < ch && ch < 256) |
| 8479 | *output++ = (char)ch; |
| 8480 | else { |
| 8481 | Py_DECREF(repunicode); |
| 8482 | raise_encode_exception(&exc, encoding, |
| 8483 | s, length, collstart-s, collend-s, reason); |
| 8484 | goto onError; |
| 8485 | } |
| 8486 | } |
| 8487 | } |
| 8488 | p = s + newpos; |
| 8489 | Py_DECREF(repunicode); |
| 8490 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8491 | } |
| 8492 | /* 0-terminate the output string */ |
| 8493 | *output++ = '\0'; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8494 | Py_XDECREF(exc); |
| 8495 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8496 | return 0; |
| 8497 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8498 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8499 | Py_XDECREF(exc); |
| 8500 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8501 | return -1; |
| 8502 | } |
| 8503 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8504 | /* --- Helpers ------------------------------------------------------------ */ |
| 8505 | |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8506 | #include "stringlib/asciilib.h" |
| 8507 | #include "stringlib/fastsearch.h" |
| 8508 | #include "stringlib/partition.h" |
| 8509 | #include "stringlib/split.h" |
| 8510 | #include "stringlib/count.h" |
| 8511 | #include "stringlib/find.h" |
| 8512 | #include "stringlib/localeutil.h" |
| 8513 | #include "stringlib/undef.h" |
| 8514 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8515 | #include "stringlib/ucs1lib.h" |
| 8516 | #include "stringlib/fastsearch.h" |
| 8517 | #include "stringlib/partition.h" |
| 8518 | #include "stringlib/split.h" |
| 8519 | #include "stringlib/count.h" |
| 8520 | #include "stringlib/find.h" |
| 8521 | #include "stringlib/localeutil.h" |
| 8522 | #include "stringlib/undef.h" |
| 8523 | |
| 8524 | #include "stringlib/ucs2lib.h" |
| 8525 | #include "stringlib/fastsearch.h" |
| 8526 | #include "stringlib/partition.h" |
| 8527 | #include "stringlib/split.h" |
| 8528 | #include "stringlib/count.h" |
| 8529 | #include "stringlib/find.h" |
| 8530 | #include "stringlib/localeutil.h" |
| 8531 | #include "stringlib/undef.h" |
| 8532 | |
| 8533 | #include "stringlib/ucs4lib.h" |
| 8534 | #include "stringlib/fastsearch.h" |
| 8535 | #include "stringlib/partition.h" |
| 8536 | #include "stringlib/split.h" |
| 8537 | #include "stringlib/count.h" |
| 8538 | #include "stringlib/find.h" |
| 8539 | #include "stringlib/localeutil.h" |
| 8540 | #include "stringlib/undef.h" |
| 8541 | |
| 8542 | static Py_ssize_t |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 8543 | any_find_slice(int direction, PyObject* s1, PyObject* s2, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8544 | Py_ssize_t start, |
| 8545 | Py_ssize_t end) |
| 8546 | { |
| 8547 | int kind1, kind2, kind; |
| 8548 | void *buf1, *buf2; |
| 8549 | Py_ssize_t len1, len2, result; |
| 8550 | |
| 8551 | kind1 = PyUnicode_KIND(s1); |
| 8552 | kind2 = PyUnicode_KIND(s2); |
| 8553 | kind = kind1 > kind2 ? kind1 : kind2; |
| 8554 | buf1 = PyUnicode_DATA(s1); |
| 8555 | buf2 = PyUnicode_DATA(s2); |
| 8556 | if (kind1 != kind) |
| 8557 | buf1 = _PyUnicode_AsKind(s1, kind); |
| 8558 | if (!buf1) |
| 8559 | return -2; |
| 8560 | if (kind2 != kind) |
| 8561 | buf2 = _PyUnicode_AsKind(s2, kind); |
| 8562 | if (!buf2) { |
| 8563 | if (kind1 != kind) PyMem_Free(buf1); |
| 8564 | return -2; |
| 8565 | } |
| 8566 | len1 = PyUnicode_GET_LENGTH(s1); |
| 8567 | len2 = PyUnicode_GET_LENGTH(s2); |
| 8568 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 8569 | if (direction > 0) { |
| 8570 | switch(kind) { |
| 8571 | case PyUnicode_1BYTE_KIND: |
| 8572 | if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2)) |
| 8573 | result = asciilib_find_slice(buf1, len1, buf2, len2, start, end); |
| 8574 | else |
| 8575 | result = ucs1lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 8576 | break; |
| 8577 | case PyUnicode_2BYTE_KIND: |
| 8578 | result = ucs2lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 8579 | break; |
| 8580 | case PyUnicode_4BYTE_KIND: |
| 8581 | result = ucs4lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 8582 | break; |
| 8583 | default: |
| 8584 | assert(0); result = -2; |
| 8585 | } |
| 8586 | } |
| 8587 | else { |
| 8588 | switch(kind) { |
| 8589 | case PyUnicode_1BYTE_KIND: |
| 8590 | if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2)) |
| 8591 | result = asciilib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 8592 | else |
| 8593 | result = ucs1lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 8594 | break; |
| 8595 | case PyUnicode_2BYTE_KIND: |
| 8596 | result = ucs2lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 8597 | break; |
| 8598 | case PyUnicode_4BYTE_KIND: |
| 8599 | result = ucs4lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 8600 | break; |
| 8601 | default: |
| 8602 | assert(0); result = -2; |
| 8603 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8604 | } |
| 8605 | |
| 8606 | if (kind1 != kind) |
| 8607 | PyMem_Free(buf1); |
| 8608 | if (kind2 != kind) |
| 8609 | PyMem_Free(buf2); |
| 8610 | |
| 8611 | return result; |
| 8612 | } |
| 8613 | |
| 8614 | Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8615 | _PyUnicode_InsertThousandsGrouping(PyObject *unicode, int kind, void *data, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8616 | Py_ssize_t n_buffer, |
| 8617 | void *digits, Py_ssize_t n_digits, |
| 8618 | Py_ssize_t min_width, |
| 8619 | const char *grouping, |
| 8620 | const char *thousands_sep) |
| 8621 | { |
| 8622 | switch(kind) { |
| 8623 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8624 | if (unicode != NULL && PyUnicode_IS_ASCII(unicode)) |
| 8625 | return _PyUnicode_ascii_InsertThousandsGrouping( |
| 8626 | (Py_UCS1*)data, n_buffer, (Py_UCS1*)digits, n_digits, |
| 8627 | min_width, grouping, thousands_sep); |
| 8628 | else |
| 8629 | return _PyUnicode_ucs1_InsertThousandsGrouping( |
| 8630 | (Py_UCS1*)data, n_buffer, (Py_UCS1*)digits, n_digits, |
| 8631 | min_width, grouping, thousands_sep); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8632 | case PyUnicode_2BYTE_KIND: |
| 8633 | return _PyUnicode_ucs2_InsertThousandsGrouping( |
| 8634 | (Py_UCS2*)data, n_buffer, (Py_UCS2*)digits, n_digits, |
| 8635 | min_width, grouping, thousands_sep); |
| 8636 | case PyUnicode_4BYTE_KIND: |
| 8637 | return _PyUnicode_ucs4_InsertThousandsGrouping( |
| 8638 | (Py_UCS4*)data, n_buffer, (Py_UCS4*)digits, n_digits, |
| 8639 | min_width, grouping, thousands_sep); |
| 8640 | } |
| 8641 | assert(0); |
| 8642 | return -1; |
| 8643 | } |
| 8644 | |
| 8645 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 8646 | #include "stringlib/unicodedefs.h" |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8647 | #include "stringlib/fastsearch.h" |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 8648 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8649 | #include "stringlib/count.h" |
| 8650 | #include "stringlib/find.h" |
Eric Smith | 5807c41 | 2008-05-11 21:00:57 +0000 | [diff] [blame] | 8651 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8652 | /* helper macro to fixup start/end slice values */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 8653 | #define ADJUST_INDICES(start, end, len) \ |
| 8654 | if (end > len) \ |
| 8655 | end = len; \ |
| 8656 | else if (end < 0) { \ |
| 8657 | end += len; \ |
| 8658 | if (end < 0) \ |
| 8659 | end = 0; \ |
| 8660 | } \ |
| 8661 | if (start < 0) { \ |
| 8662 | start += len; \ |
| 8663 | if (start < 0) \ |
| 8664 | start = 0; \ |
| 8665 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8666 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8667 | Py_ssize_t |
| 8668 | PyUnicode_Count(PyObject *str, |
| 8669 | PyObject *substr, |
| 8670 | Py_ssize_t start, |
| 8671 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8672 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8673 | Py_ssize_t result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8674 | PyUnicodeObject* str_obj; |
| 8675 | PyUnicodeObject* sub_obj; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8676 | int kind1, kind2, kind; |
| 8677 | void *buf1 = NULL, *buf2 = NULL; |
| 8678 | Py_ssize_t len1, len2; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8679 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8680 | str_obj = (PyUnicodeObject*) PyUnicode_FromObject(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8681 | if (!str_obj || PyUnicode_READY(str_obj) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8682 | return -1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8683 | sub_obj = (PyUnicodeObject*) PyUnicode_FromObject(substr); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 8684 | if (!sub_obj || PyUnicode_READY(sub_obj) == -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8685 | Py_DECREF(str_obj); |
| 8686 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8687 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8688 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8689 | kind1 = PyUnicode_KIND(str_obj); |
| 8690 | kind2 = PyUnicode_KIND(sub_obj); |
| 8691 | kind = kind1 > kind2 ? kind1 : kind2; |
| 8692 | buf1 = PyUnicode_DATA(str_obj); |
| 8693 | if (kind1 != kind) |
| 8694 | buf1 = _PyUnicode_AsKind((PyObject*)str_obj, kind); |
| 8695 | if (!buf1) |
| 8696 | goto onError; |
| 8697 | buf2 = PyUnicode_DATA(sub_obj); |
| 8698 | if (kind2 != kind) |
| 8699 | buf2 = _PyUnicode_AsKind((PyObject*)sub_obj, kind); |
| 8700 | if (!buf2) |
| 8701 | goto onError; |
| 8702 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 8703 | len2 = PyUnicode_GET_LENGTH(sub_obj); |
| 8704 | |
| 8705 | ADJUST_INDICES(start, end, len1); |
| 8706 | switch(kind) { |
| 8707 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8708 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sub_obj)) |
| 8709 | result = asciilib_count( |
| 8710 | ((Py_UCS1*)buf1) + start, end - start, |
| 8711 | buf2, len2, PY_SSIZE_T_MAX |
| 8712 | ); |
| 8713 | else |
| 8714 | result = ucs1lib_count( |
| 8715 | ((Py_UCS1*)buf1) + start, end - start, |
| 8716 | buf2, len2, PY_SSIZE_T_MAX |
| 8717 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8718 | break; |
| 8719 | case PyUnicode_2BYTE_KIND: |
| 8720 | result = ucs2lib_count( |
| 8721 | ((Py_UCS2*)buf1) + start, end - start, |
| 8722 | buf2, len2, PY_SSIZE_T_MAX |
| 8723 | ); |
| 8724 | break; |
| 8725 | case PyUnicode_4BYTE_KIND: |
| 8726 | result = ucs4lib_count( |
| 8727 | ((Py_UCS4*)buf1) + start, end - start, |
| 8728 | buf2, len2, PY_SSIZE_T_MAX |
| 8729 | ); |
| 8730 | break; |
| 8731 | default: |
| 8732 | assert(0); result = 0; |
| 8733 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8734 | |
| 8735 | Py_DECREF(sub_obj); |
| 8736 | Py_DECREF(str_obj); |
| 8737 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8738 | if (kind1 != kind) |
| 8739 | PyMem_Free(buf1); |
| 8740 | if (kind2 != kind) |
| 8741 | PyMem_Free(buf2); |
| 8742 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8743 | return result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8744 | onError: |
| 8745 | Py_DECREF(sub_obj); |
| 8746 | Py_DECREF(str_obj); |
| 8747 | if (kind1 != kind && buf1) |
| 8748 | PyMem_Free(buf1); |
| 8749 | if (kind2 != kind && buf2) |
| 8750 | PyMem_Free(buf2); |
| 8751 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8752 | } |
| 8753 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8754 | Py_ssize_t |
| 8755 | PyUnicode_Find(PyObject *str, |
| 8756 | PyObject *sub, |
| 8757 | Py_ssize_t start, |
| 8758 | Py_ssize_t end, |
| 8759 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8760 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8761 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8762 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8763 | str = PyUnicode_FromObject(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8764 | if (!str || PyUnicode_READY(str) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8765 | return -2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8766 | sub = PyUnicode_FromObject(sub); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8767 | if (!sub || PyUnicode_READY(sub) == -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8768 | Py_DECREF(str); |
| 8769 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8770 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8771 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 8772 | result = any_find_slice(direction, |
| 8773 | str, sub, start, end |
| 8774 | ); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8775 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8776 | Py_DECREF(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8777 | Py_DECREF(sub); |
| 8778 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8779 | return result; |
| 8780 | } |
| 8781 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8782 | Py_ssize_t |
| 8783 | PyUnicode_FindChar(PyObject *str, Py_UCS4 ch, |
| 8784 | Py_ssize_t start, Py_ssize_t end, |
| 8785 | int direction) |
| 8786 | { |
| 8787 | char *result; |
| 8788 | int kind; |
| 8789 | if (PyUnicode_READY(str) == -1) |
| 8790 | return -2; |
Victor Stinner | 267aa24 | 2011-10-02 01:08:37 +0200 | [diff] [blame] | 8791 | if (start < 0 || end < 0) { |
| 8792 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 8793 | return -2; |
| 8794 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8795 | if (end > PyUnicode_GET_LENGTH(str)) |
| 8796 | end = PyUnicode_GET_LENGTH(str); |
| 8797 | kind = PyUnicode_KIND(str); |
| 8798 | result = findchar(PyUnicode_1BYTE_DATA(str) |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 8799 | + kind*start, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8800 | kind, |
| 8801 | end-start, ch, direction); |
| 8802 | if (!result) |
| 8803 | return -1; |
| 8804 | return (result-(char*)PyUnicode_DATA(str)) >> (kind-1); |
| 8805 | } |
| 8806 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8807 | static int |
| 8808 | tailmatch(PyUnicodeObject *self, |
| 8809 | PyUnicodeObject *substring, |
| 8810 | Py_ssize_t start, |
| 8811 | Py_ssize_t end, |
| 8812 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8813 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8814 | int kind_self; |
| 8815 | int kind_sub; |
| 8816 | void *data_self; |
| 8817 | void *data_sub; |
| 8818 | Py_ssize_t offset; |
| 8819 | Py_ssize_t i; |
| 8820 | Py_ssize_t end_sub; |
| 8821 | |
| 8822 | if (PyUnicode_READY(self) == -1 || |
| 8823 | PyUnicode_READY(substring) == -1) |
| 8824 | return 0; |
| 8825 | |
| 8826 | if (PyUnicode_GET_LENGTH(substring) == 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8827 | return 1; |
| 8828 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8829 | ADJUST_INDICES(start, end, PyUnicode_GET_LENGTH(self)); |
| 8830 | end -= PyUnicode_GET_LENGTH(substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8831 | if (end < start) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8832 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8833 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8834 | kind_self = PyUnicode_KIND(self); |
| 8835 | data_self = PyUnicode_DATA(self); |
| 8836 | kind_sub = PyUnicode_KIND(substring); |
| 8837 | data_sub = PyUnicode_DATA(substring); |
| 8838 | end_sub = PyUnicode_GET_LENGTH(substring) - 1; |
| 8839 | |
| 8840 | if (direction > 0) |
| 8841 | offset = end; |
| 8842 | else |
| 8843 | offset = start; |
| 8844 | |
| 8845 | if (PyUnicode_READ(kind_self, data_self, offset) == |
| 8846 | PyUnicode_READ(kind_sub, data_sub, 0) && |
| 8847 | PyUnicode_READ(kind_self, data_self, offset + end_sub) == |
| 8848 | PyUnicode_READ(kind_sub, data_sub, end_sub)) { |
| 8849 | /* If both are of the same kind, memcmp is sufficient */ |
| 8850 | if (kind_self == kind_sub) { |
| 8851 | return ! memcmp((char *)data_self + |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 8852 | (offset * PyUnicode_KIND(substring)), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8853 | data_sub, |
| 8854 | PyUnicode_GET_LENGTH(substring) * |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 8855 | PyUnicode_KIND(substring)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8856 | } |
| 8857 | /* otherwise we have to compare each character by first accesing it */ |
| 8858 | else { |
| 8859 | /* We do not need to compare 0 and len(substring)-1 because |
| 8860 | the if statement above ensured already that they are equal |
| 8861 | when we end up here. */ |
| 8862 | // TODO: honor direction and do a forward or backwards search |
| 8863 | for (i = 1; i < end_sub; ++i) { |
| 8864 | if (PyUnicode_READ(kind_self, data_self, offset + i) != |
| 8865 | PyUnicode_READ(kind_sub, data_sub, i)) |
| 8866 | return 0; |
| 8867 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8868 | return 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8869 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8870 | } |
| 8871 | |
| 8872 | return 0; |
| 8873 | } |
| 8874 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8875 | Py_ssize_t |
| 8876 | PyUnicode_Tailmatch(PyObject *str, |
| 8877 | PyObject *substr, |
| 8878 | Py_ssize_t start, |
| 8879 | Py_ssize_t end, |
| 8880 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8881 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8882 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8883 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8884 | str = PyUnicode_FromObject(str); |
| 8885 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8886 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8887 | substr = PyUnicode_FromObject(substr); |
| 8888 | if (substr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8889 | Py_DECREF(str); |
| 8890 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8891 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8892 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8893 | result = tailmatch((PyUnicodeObject *)str, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8894 | (PyUnicodeObject *)substr, |
| 8895 | start, end, direction); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8896 | Py_DECREF(str); |
| 8897 | Py_DECREF(substr); |
| 8898 | return result; |
| 8899 | } |
| 8900 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8901 | /* Apply fixfct filter to the Unicode object self and return a |
| 8902 | reference to the modified object */ |
| 8903 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8904 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8905 | fixup(PyObject *self, |
| 8906 | Py_UCS4 (*fixfct)(PyObject *s)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8907 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8908 | PyObject *u; |
| 8909 | Py_UCS4 maxchar_old, maxchar_new = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8910 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8911 | if (PyUnicode_READY(self) == -1) |
| 8912 | return NULL; |
| 8913 | maxchar_old = PyUnicode_MAX_CHAR_VALUE(self); |
| 8914 | u = PyUnicode_New(PyUnicode_GET_LENGTH(self), |
| 8915 | maxchar_old); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8916 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8917 | return NULL; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8918 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8919 | Py_MEMCPY(PyUnicode_1BYTE_DATA(u), PyUnicode_1BYTE_DATA(self), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 8920 | PyUnicode_GET_LENGTH(u) * PyUnicode_KIND(u)); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8921 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8922 | /* fix functions return the new maximum character in a string, |
| 8923 | if the kind of the resulting unicode object does not change, |
| 8924 | everything is fine. Otherwise we need to change the string kind |
| 8925 | and re-run the fix function. */ |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8926 | maxchar_new = fixfct(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8927 | if (maxchar_new == 0) |
| 8928 | /* do nothing, keep maxchar_new at 0 which means no changes. */; |
| 8929 | else if (maxchar_new <= 127) |
| 8930 | maxchar_new = 127; |
| 8931 | else if (maxchar_new <= 255) |
| 8932 | maxchar_new = 255; |
| 8933 | else if (maxchar_new <= 65535) |
| 8934 | maxchar_new = 65535; |
| 8935 | else |
| 8936 | maxchar_new = 1114111; /* 0x10ffff */ |
| 8937 | |
| 8938 | if (!maxchar_new && PyUnicode_CheckExact(self)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8939 | /* fixfct should return TRUE if it modified the buffer. If |
| 8940 | FALSE, return a reference to the original buffer instead |
| 8941 | (to save space, not time) */ |
| 8942 | Py_INCREF(self); |
| 8943 | Py_DECREF(u); |
| 8944 | return (PyObject*) self; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8945 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8946 | else if (maxchar_new == maxchar_old) { |
| 8947 | return u; |
| 8948 | } |
| 8949 | else { |
| 8950 | /* In case the maximum character changed, we need to |
| 8951 | convert the string to the new category. */ |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 8952 | PyObject *v = PyUnicode_New(PyUnicode_GET_LENGTH(self), maxchar_new); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8953 | if (v == NULL) { |
| 8954 | Py_DECREF(u); |
| 8955 | return NULL; |
| 8956 | } |
| 8957 | if (maxchar_new > maxchar_old) { |
| 8958 | /* If the maxchar increased so that the kind changed, not all |
| 8959 | characters are representable anymore and we need to fix the |
| 8960 | string again. This only happens in very few cases. */ |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 8961 | copy_characters(v, 0, self, 0, PyUnicode_GET_LENGTH(self)); |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8962 | maxchar_old = fixfct(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8963 | assert(maxchar_old > 0 && maxchar_old <= maxchar_new); |
| 8964 | } |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 8965 | else { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 8966 | copy_characters(v, 0, u, 0, PyUnicode_GET_LENGTH(self)); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 8967 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8968 | |
| 8969 | Py_DECREF(u); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 8970 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8971 | return v; |
| 8972 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8973 | } |
| 8974 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8975 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8976 | fixupper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8977 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8978 | /* No need to call PyUnicode_READY(self) because this function is only |
| 8979 | called as a callback from fixup() which does it already. */ |
| 8980 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8981 | const int kind = PyUnicode_KIND(self); |
| 8982 | void *data = PyUnicode_DATA(self); |
| 8983 | int touched = 0; |
| 8984 | Py_UCS4 maxchar = 0; |
| 8985 | Py_ssize_t i; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8986 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8987 | for (i = 0; i < len; ++i) { |
| 8988 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 8989 | const Py_UCS4 up = Py_UNICODE_TOUPPER(ch); |
| 8990 | if (up != ch) { |
| 8991 | if (up > maxchar) |
| 8992 | maxchar = up; |
| 8993 | PyUnicode_WRITE(kind, data, i, up); |
| 8994 | touched = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8995 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8996 | else if (ch > maxchar) |
| 8997 | maxchar = ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8998 | } |
| 8999 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9000 | if (touched) |
| 9001 | return maxchar; |
| 9002 | else |
| 9003 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9004 | } |
| 9005 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9006 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9007 | fixlower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9008 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9009 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 9010 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9011 | const int kind = PyUnicode_KIND(self); |
| 9012 | void *data = PyUnicode_DATA(self); |
| 9013 | int touched = 0; |
| 9014 | Py_UCS4 maxchar = 0; |
| 9015 | Py_ssize_t i; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9016 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9017 | for(i = 0; i < len; ++i) { |
| 9018 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 9019 | const Py_UCS4 lo = Py_UNICODE_TOLOWER(ch); |
| 9020 | if (lo != ch) { |
| 9021 | if (lo > maxchar) |
| 9022 | maxchar = lo; |
| 9023 | PyUnicode_WRITE(kind, data, i, lo); |
| 9024 | touched = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9025 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9026 | else if (ch > maxchar) |
| 9027 | maxchar = ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9028 | } |
| 9029 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9030 | if (touched) |
| 9031 | return maxchar; |
| 9032 | else |
| 9033 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9034 | } |
| 9035 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9036 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9037 | fixswapcase(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9038 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9039 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 9040 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9041 | const int kind = PyUnicode_KIND(self); |
| 9042 | void *data = PyUnicode_DATA(self); |
| 9043 | int touched = 0; |
| 9044 | Py_UCS4 maxchar = 0; |
| 9045 | Py_ssize_t i; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9046 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9047 | for(i = 0; i < len; ++i) { |
| 9048 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 9049 | Py_UCS4 nu = 0; |
| 9050 | |
| 9051 | if (Py_UNICODE_ISUPPER(ch)) |
| 9052 | nu = Py_UNICODE_TOLOWER(ch); |
| 9053 | else if (Py_UNICODE_ISLOWER(ch)) |
| 9054 | nu = Py_UNICODE_TOUPPER(ch); |
| 9055 | |
| 9056 | if (nu != 0) { |
| 9057 | if (nu > maxchar) |
| 9058 | maxchar = nu; |
| 9059 | PyUnicode_WRITE(kind, data, i, nu); |
| 9060 | touched = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9061 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9062 | else if (ch > maxchar) |
| 9063 | maxchar = ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9064 | } |
| 9065 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9066 | if (touched) |
| 9067 | return maxchar; |
| 9068 | else |
| 9069 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9070 | } |
| 9071 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9072 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9073 | fixcapitalize(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9074 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9075 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 9076 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9077 | const int kind = PyUnicode_KIND(self); |
| 9078 | void *data = PyUnicode_DATA(self); |
| 9079 | int touched = 0; |
| 9080 | Py_UCS4 maxchar = 0; |
| 9081 | Py_ssize_t i = 0; |
| 9082 | Py_UCS4 ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9083 | |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9084 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9085 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9086 | |
| 9087 | ch = PyUnicode_READ(kind, data, i); |
| 9088 | if (!Py_UNICODE_ISUPPER(ch)) { |
| 9089 | maxchar = Py_UNICODE_TOUPPER(ch); |
| 9090 | PyUnicode_WRITE(kind, data, i, maxchar); |
| 9091 | touched = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9092 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9093 | ++i; |
| 9094 | for(; i < len; ++i) { |
| 9095 | ch = PyUnicode_READ(kind, data, i); |
| 9096 | if (!Py_UNICODE_ISLOWER(ch)) { |
| 9097 | const Py_UCS4 lo = Py_UNICODE_TOLOWER(ch); |
| 9098 | if (lo > maxchar) |
| 9099 | maxchar = lo; |
| 9100 | PyUnicode_WRITE(kind, data, i, lo); |
| 9101 | touched = 1; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9102 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9103 | else if (ch > maxchar) |
| 9104 | maxchar = ch; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9105 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9106 | |
| 9107 | if (touched) |
| 9108 | return maxchar; |
| 9109 | else |
| 9110 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9111 | } |
| 9112 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9113 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9114 | fixtitle(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9115 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9116 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 9117 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9118 | const int kind = PyUnicode_KIND(self); |
| 9119 | void *data = PyUnicode_DATA(self); |
| 9120 | Py_UCS4 maxchar = 0; |
| 9121 | Py_ssize_t i = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9122 | int previous_is_cased; |
| 9123 | |
| 9124 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9125 | if (len == 1) { |
| 9126 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 9127 | const Py_UCS4 ti = Py_UNICODE_TOTITLE(ch); |
| 9128 | if (ti != ch) { |
| 9129 | PyUnicode_WRITE(kind, data, i, ti); |
| 9130 | return ti; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9131 | } |
| 9132 | else |
| 9133 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9134 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9135 | previous_is_cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9136 | for(; i < len; ++i) { |
| 9137 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 9138 | Py_UCS4 nu; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9139 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9140 | if (previous_is_cased) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9141 | nu = Py_UNICODE_TOLOWER(ch); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9142 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9143 | nu = Py_UNICODE_TOTITLE(ch); |
| 9144 | |
| 9145 | if (nu > maxchar) |
| 9146 | maxchar = nu; |
| 9147 | PyUnicode_WRITE(kind, data, i, nu); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9148 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9149 | if (Py_UNICODE_ISLOWER(ch) || |
| 9150 | Py_UNICODE_ISUPPER(ch) || |
| 9151 | Py_UNICODE_ISTITLE(ch)) |
| 9152 | previous_is_cased = 1; |
| 9153 | else |
| 9154 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9155 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9156 | return maxchar; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9157 | } |
| 9158 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9159 | PyObject * |
| 9160 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9161 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9162 | PyObject *sep = NULL; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9163 | Py_ssize_t seplen; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9164 | PyObject *res = NULL; /* the result */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9165 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9166 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
| 9167 | PyObject **items; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9168 | PyObject *item; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9169 | Py_ssize_t sz, i, res_offset; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9170 | Py_UCS4 maxchar; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9171 | Py_UCS4 item_maxchar; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9172 | int use_memcpy; |
| 9173 | unsigned char *res_data = NULL, *sep_data = NULL; |
| 9174 | PyObject *last_obj; |
| 9175 | unsigned int kind = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9176 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9177 | fseq = PySequence_Fast(seq, ""); |
| 9178 | if (fseq == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9179 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9180 | } |
| 9181 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9182 | /* NOTE: the following code can't call back into Python code, |
| 9183 | * so we are sure that fseq won't be mutated. |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9184 | */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9185 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9186 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 9187 | /* If empty sequence, return u"". */ |
| 9188 | if (seqlen == 0) { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9189 | Py_DECREF(fseq); |
| 9190 | Py_INCREF(unicode_empty); |
| 9191 | res = unicode_empty; |
| 9192 | return res; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9193 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9194 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9195 | /* If singleton sequence with an exact Unicode, return that. */ |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9196 | last_obj = NULL; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9197 | items = PySequence_Fast_ITEMS(fseq); |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9198 | if (seqlen == 1) { |
| 9199 | if (PyUnicode_CheckExact(items[0])) { |
| 9200 | res = items[0]; |
| 9201 | Py_INCREF(res); |
| 9202 | Py_DECREF(fseq); |
| 9203 | return res; |
| 9204 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9205 | seplen = 0; |
Victor Stinner | c6f0df7 | 2011-10-06 15:58:54 +0200 | [diff] [blame] | 9206 | maxchar = 0; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9207 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9208 | else { |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9209 | /* Set up sep and seplen */ |
| 9210 | if (separator == NULL) { |
| 9211 | /* fall back to a blank space separator */ |
| 9212 | sep = PyUnicode_FromOrdinal(' '); |
| 9213 | if (!sep) |
| 9214 | goto onError; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9215 | seplen = 1; |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9216 | maxchar = 32; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9217 | } |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9218 | else { |
| 9219 | if (!PyUnicode_Check(separator)) { |
| 9220 | PyErr_Format(PyExc_TypeError, |
| 9221 | "separator: expected str instance," |
| 9222 | " %.80s found", |
| 9223 | Py_TYPE(separator)->tp_name); |
| 9224 | goto onError; |
| 9225 | } |
| 9226 | if (PyUnicode_READY(separator)) |
| 9227 | goto onError; |
| 9228 | sep = separator; |
| 9229 | seplen = PyUnicode_GET_LENGTH(separator); |
| 9230 | maxchar = PyUnicode_MAX_CHAR_VALUE(separator); |
| 9231 | /* inc refcount to keep this code path symmetric with the |
| 9232 | above case of a blank separator */ |
| 9233 | Py_INCREF(sep); |
| 9234 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9235 | last_obj = sep; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9236 | } |
| 9237 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9238 | /* There are at least two things to join, or else we have a subclass |
| 9239 | * of str in the sequence. |
| 9240 | * Do a pre-pass to figure out the total amount of space we'll |
| 9241 | * need (sz), and see whether all argument are strings. |
| 9242 | */ |
| 9243 | sz = 0; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9244 | #ifdef Py_DEBUG |
| 9245 | use_memcpy = 0; |
| 9246 | #else |
| 9247 | use_memcpy = 1; |
| 9248 | #endif |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9249 | for (i = 0; i < seqlen; i++) { |
| 9250 | const Py_ssize_t old_sz = sz; |
| 9251 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9252 | if (!PyUnicode_Check(item)) { |
| 9253 | PyErr_Format(PyExc_TypeError, |
| 9254 | "sequence item %zd: expected str instance," |
| 9255 | " %.80s found", |
| 9256 | i, Py_TYPE(item)->tp_name); |
| 9257 | goto onError; |
| 9258 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9259 | if (PyUnicode_READY(item) == -1) |
| 9260 | goto onError; |
| 9261 | sz += PyUnicode_GET_LENGTH(item); |
| 9262 | item_maxchar = PyUnicode_MAX_CHAR_VALUE(item); |
Victor Stinner | c6f0df7 | 2011-10-06 15:58:54 +0200 | [diff] [blame] | 9263 | maxchar = Py_MAX(maxchar, item_maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9264 | if (i != 0) |
| 9265 | sz += seplen; |
| 9266 | if (sz < old_sz || sz > PY_SSIZE_T_MAX) { |
| 9267 | PyErr_SetString(PyExc_OverflowError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9268 | "join() result is too long for a Python string"); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9269 | goto onError; |
| 9270 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9271 | if (use_memcpy && last_obj != NULL) { |
| 9272 | if (PyUnicode_KIND(last_obj) != PyUnicode_KIND(item)) |
| 9273 | use_memcpy = 0; |
| 9274 | } |
| 9275 | last_obj = item; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9276 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9277 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9278 | res = PyUnicode_New(sz, maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9279 | if (res == NULL) |
| 9280 | goto onError; |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9281 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9282 | /* Catenate everything. */ |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9283 | #ifdef Py_DEBUG |
| 9284 | use_memcpy = 0; |
| 9285 | #else |
| 9286 | if (use_memcpy) { |
| 9287 | res_data = PyUnicode_1BYTE_DATA(res); |
| 9288 | kind = PyUnicode_KIND(res); |
| 9289 | if (seplen != 0) |
| 9290 | sep_data = PyUnicode_1BYTE_DATA(sep); |
| 9291 | } |
| 9292 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9293 | for (i = 0, res_offset = 0; i < seqlen; ++i) { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9294 | Py_ssize_t itemlen; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9295 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9296 | /* Copy item, and maybe the separator. */ |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9297 | if (i && seplen != 0) { |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9298 | if (use_memcpy) { |
| 9299 | Py_MEMCPY(res_data, |
| 9300 | sep_data, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9301 | kind * seplen); |
| 9302 | res_data += kind * seplen; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9303 | } |
| 9304 | else { |
| 9305 | copy_characters(res, res_offset, sep, 0, seplen); |
| 9306 | res_offset += seplen; |
| 9307 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9308 | } |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9309 | itemlen = PyUnicode_GET_LENGTH(item); |
| 9310 | if (itemlen != 0) { |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9311 | if (use_memcpy) { |
| 9312 | Py_MEMCPY(res_data, |
| 9313 | PyUnicode_DATA(item), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9314 | kind * itemlen); |
| 9315 | res_data += kind * itemlen; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9316 | } |
| 9317 | else { |
| 9318 | copy_characters(res, res_offset, item, 0, itemlen); |
| 9319 | res_offset += itemlen; |
| 9320 | } |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9321 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9322 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9323 | if (use_memcpy) |
| 9324 | assert(res_data == PyUnicode_1BYTE_DATA(res) |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9325 | + kind * PyUnicode_GET_LENGTH(res)); |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9326 | else |
| 9327 | assert(res_offset == PyUnicode_GET_LENGTH(res)); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9328 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9329 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9330 | Py_XDECREF(sep); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9331 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9332 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9333 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9334 | onError: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9335 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9336 | Py_XDECREF(sep); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9337 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9338 | return NULL; |
| 9339 | } |
| 9340 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9341 | #define FILL(kind, data, value, start, length) \ |
| 9342 | do { \ |
| 9343 | Py_ssize_t i_ = 0; \ |
| 9344 | assert(kind != PyUnicode_WCHAR_KIND); \ |
| 9345 | switch ((kind)) { \ |
| 9346 | case PyUnicode_1BYTE_KIND: { \ |
| 9347 | unsigned char * to_ = (unsigned char *)((data)) + (start); \ |
| 9348 | memset(to_, (unsigned char)value, length); \ |
| 9349 | break; \ |
| 9350 | } \ |
| 9351 | case PyUnicode_2BYTE_KIND: { \ |
| 9352 | Py_UCS2 * to_ = (Py_UCS2 *)((data)) + (start); \ |
| 9353 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 9354 | break; \ |
| 9355 | } \ |
| 9356 | default: { \ |
| 9357 | Py_UCS4 * to_ = (Py_UCS4 *)((data)) + (start); \ |
| 9358 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 9359 | break; \ |
| 9360 | } \ |
| 9361 | } \ |
| 9362 | } while (0) |
| 9363 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9364 | static PyObject * |
| 9365 | pad(PyObject *self, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9366 | Py_ssize_t left, |
| 9367 | Py_ssize_t right, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9368 | Py_UCS4 fill) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9369 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9370 | PyObject *u; |
| 9371 | Py_UCS4 maxchar; |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9372 | int kind; |
| 9373 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9374 | |
| 9375 | if (left < 0) |
| 9376 | left = 0; |
| 9377 | if (right < 0) |
| 9378 | right = 0; |
| 9379 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 9380 | if (left == 0 && right == 0 && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9381 | Py_INCREF(self); |
| 9382 | return self; |
| 9383 | } |
| 9384 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9385 | if (left > PY_SSIZE_T_MAX - _PyUnicode_LENGTH(self) || |
| 9386 | right > PY_SSIZE_T_MAX - (left + _PyUnicode_LENGTH(self))) { |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 9387 | PyErr_SetString(PyExc_OverflowError, "padded string is too long"); |
| 9388 | return NULL; |
| 9389 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9390 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
| 9391 | if (fill > maxchar) |
| 9392 | maxchar = fill; |
| 9393 | u = PyUnicode_New(left + _PyUnicode_LENGTH(self) + right, maxchar); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9394 | if (!u) |
| 9395 | return NULL; |
| 9396 | |
| 9397 | kind = PyUnicode_KIND(u); |
| 9398 | data = PyUnicode_DATA(u); |
| 9399 | if (left) |
| 9400 | FILL(kind, data, fill, 0, left); |
| 9401 | if (right) |
| 9402 | FILL(kind, data, fill, left + _PyUnicode_LENGTH(self), right); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9403 | copy_characters(u, left, self, 0, _PyUnicode_LENGTH(self)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9404 | assert(_PyUnicode_CheckConsistency(u, 1)); |
| 9405 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9406 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9407 | #undef FILL |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9408 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9409 | PyObject * |
| 9410 | PyUnicode_Splitlines(PyObject *string, int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9411 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9412 | PyObject *list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9413 | |
| 9414 | string = PyUnicode_FromObject(string); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9415 | if (string == NULL || PyUnicode_READY(string) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9416 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9417 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9418 | switch(PyUnicode_KIND(string)) { |
| 9419 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9420 | if (PyUnicode_IS_ASCII(string)) |
| 9421 | list = asciilib_splitlines( |
| 9422 | (PyObject*) string, PyUnicode_1BYTE_DATA(string), |
| 9423 | PyUnicode_GET_LENGTH(string), keepends); |
| 9424 | else |
| 9425 | list = ucs1lib_splitlines( |
| 9426 | (PyObject*) string, PyUnicode_1BYTE_DATA(string), |
| 9427 | PyUnicode_GET_LENGTH(string), keepends); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9428 | break; |
| 9429 | case PyUnicode_2BYTE_KIND: |
| 9430 | list = ucs2lib_splitlines( |
| 9431 | (PyObject*) string, PyUnicode_2BYTE_DATA(string), |
| 9432 | PyUnicode_GET_LENGTH(string), keepends); |
| 9433 | break; |
| 9434 | case PyUnicode_4BYTE_KIND: |
| 9435 | list = ucs4lib_splitlines( |
| 9436 | (PyObject*) string, PyUnicode_4BYTE_DATA(string), |
| 9437 | PyUnicode_GET_LENGTH(string), keepends); |
| 9438 | break; |
| 9439 | default: |
| 9440 | assert(0); |
| 9441 | list = 0; |
| 9442 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9443 | Py_DECREF(string); |
| 9444 | return list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9445 | } |
| 9446 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9447 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9448 | split(PyObject *self, |
| 9449 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9450 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9451 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9452 | int kind1, kind2, kind; |
| 9453 | void *buf1, *buf2; |
| 9454 | Py_ssize_t len1, len2; |
| 9455 | PyObject* out; |
| 9456 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9457 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9458 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9459 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9460 | if (PyUnicode_READY(self) == -1) |
| 9461 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9462 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9463 | if (substring == NULL) |
| 9464 | switch(PyUnicode_KIND(self)) { |
| 9465 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9466 | if (PyUnicode_IS_ASCII(self)) |
| 9467 | return asciilib_split_whitespace( |
| 9468 | (PyObject*) self, PyUnicode_1BYTE_DATA(self), |
| 9469 | PyUnicode_GET_LENGTH(self), maxcount |
| 9470 | ); |
| 9471 | else |
| 9472 | return ucs1lib_split_whitespace( |
| 9473 | (PyObject*) self, PyUnicode_1BYTE_DATA(self), |
| 9474 | PyUnicode_GET_LENGTH(self), maxcount |
| 9475 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9476 | case PyUnicode_2BYTE_KIND: |
| 9477 | return ucs2lib_split_whitespace( |
| 9478 | (PyObject*) self, PyUnicode_2BYTE_DATA(self), |
| 9479 | PyUnicode_GET_LENGTH(self), maxcount |
| 9480 | ); |
| 9481 | case PyUnicode_4BYTE_KIND: |
| 9482 | return ucs4lib_split_whitespace( |
| 9483 | (PyObject*) self, PyUnicode_4BYTE_DATA(self), |
| 9484 | PyUnicode_GET_LENGTH(self), maxcount |
| 9485 | ); |
| 9486 | default: |
| 9487 | assert(0); |
| 9488 | return NULL; |
| 9489 | } |
| 9490 | |
| 9491 | if (PyUnicode_READY(substring) == -1) |
| 9492 | return NULL; |
| 9493 | |
| 9494 | kind1 = PyUnicode_KIND(self); |
| 9495 | kind2 = PyUnicode_KIND(substring); |
| 9496 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9497 | buf1 = PyUnicode_DATA(self); |
| 9498 | buf2 = PyUnicode_DATA(substring); |
| 9499 | if (kind1 != kind) |
| 9500 | buf1 = _PyUnicode_AsKind((PyObject*)self, kind); |
| 9501 | if (!buf1) |
| 9502 | return NULL; |
| 9503 | if (kind2 != kind) |
| 9504 | buf2 = _PyUnicode_AsKind((PyObject*)substring, kind); |
| 9505 | if (!buf2) { |
| 9506 | if (kind1 != kind) PyMem_Free(buf1); |
| 9507 | return NULL; |
| 9508 | } |
| 9509 | len1 = PyUnicode_GET_LENGTH(self); |
| 9510 | len2 = PyUnicode_GET_LENGTH(substring); |
| 9511 | |
| 9512 | switch(kind) { |
| 9513 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9514 | if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) |
| 9515 | out = asciilib_split( |
| 9516 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 9517 | else |
| 9518 | out = ucs1lib_split( |
| 9519 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9520 | break; |
| 9521 | case PyUnicode_2BYTE_KIND: |
| 9522 | out = ucs2lib_split( |
| 9523 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 9524 | break; |
| 9525 | case PyUnicode_4BYTE_KIND: |
| 9526 | out = ucs4lib_split( |
| 9527 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 9528 | break; |
| 9529 | default: |
| 9530 | out = NULL; |
| 9531 | } |
| 9532 | if (kind1 != kind) |
| 9533 | PyMem_Free(buf1); |
| 9534 | if (kind2 != kind) |
| 9535 | PyMem_Free(buf2); |
| 9536 | return out; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9537 | } |
| 9538 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9539 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9540 | rsplit(PyObject *self, |
| 9541 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9542 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9543 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9544 | int kind1, kind2, kind; |
| 9545 | void *buf1, *buf2; |
| 9546 | Py_ssize_t len1, len2; |
| 9547 | PyObject* out; |
| 9548 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9549 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9550 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9551 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9552 | if (PyUnicode_READY(self) == -1) |
| 9553 | return NULL; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9554 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9555 | if (substring == NULL) |
| 9556 | switch(PyUnicode_KIND(self)) { |
| 9557 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9558 | if (PyUnicode_IS_ASCII(self)) |
| 9559 | return asciilib_rsplit_whitespace( |
| 9560 | (PyObject*) self, PyUnicode_1BYTE_DATA(self), |
| 9561 | PyUnicode_GET_LENGTH(self), maxcount |
| 9562 | ); |
| 9563 | else |
| 9564 | return ucs1lib_rsplit_whitespace( |
| 9565 | (PyObject*) self, PyUnicode_1BYTE_DATA(self), |
| 9566 | PyUnicode_GET_LENGTH(self), maxcount |
| 9567 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9568 | case PyUnicode_2BYTE_KIND: |
| 9569 | return ucs2lib_rsplit_whitespace( |
| 9570 | (PyObject*) self, PyUnicode_2BYTE_DATA(self), |
| 9571 | PyUnicode_GET_LENGTH(self), maxcount |
| 9572 | ); |
| 9573 | case PyUnicode_4BYTE_KIND: |
| 9574 | return ucs4lib_rsplit_whitespace( |
| 9575 | (PyObject*) self, PyUnicode_4BYTE_DATA(self), |
| 9576 | PyUnicode_GET_LENGTH(self), maxcount |
| 9577 | ); |
| 9578 | default: |
| 9579 | assert(0); |
| 9580 | return NULL; |
| 9581 | } |
| 9582 | |
| 9583 | if (PyUnicode_READY(substring) == -1) |
| 9584 | return NULL; |
| 9585 | |
| 9586 | kind1 = PyUnicode_KIND(self); |
| 9587 | kind2 = PyUnicode_KIND(substring); |
| 9588 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9589 | buf1 = PyUnicode_DATA(self); |
| 9590 | buf2 = PyUnicode_DATA(substring); |
| 9591 | if (kind1 != kind) |
| 9592 | buf1 = _PyUnicode_AsKind((PyObject*)self, kind); |
| 9593 | if (!buf1) |
| 9594 | return NULL; |
| 9595 | if (kind2 != kind) |
| 9596 | buf2 = _PyUnicode_AsKind((PyObject*)substring, kind); |
| 9597 | if (!buf2) { |
| 9598 | if (kind1 != kind) PyMem_Free(buf1); |
| 9599 | return NULL; |
| 9600 | } |
| 9601 | len1 = PyUnicode_GET_LENGTH(self); |
| 9602 | len2 = PyUnicode_GET_LENGTH(substring); |
| 9603 | |
| 9604 | switch(kind) { |
| 9605 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9606 | if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) |
| 9607 | out = asciilib_rsplit( |
| 9608 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 9609 | else |
| 9610 | out = ucs1lib_rsplit( |
| 9611 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9612 | break; |
| 9613 | case PyUnicode_2BYTE_KIND: |
| 9614 | out = ucs2lib_rsplit( |
| 9615 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 9616 | break; |
| 9617 | case PyUnicode_4BYTE_KIND: |
| 9618 | out = ucs4lib_rsplit( |
| 9619 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 9620 | break; |
| 9621 | default: |
| 9622 | out = NULL; |
| 9623 | } |
| 9624 | if (kind1 != kind) |
| 9625 | PyMem_Free(buf1); |
| 9626 | if (kind2 != kind) |
| 9627 | PyMem_Free(buf2); |
| 9628 | return out; |
| 9629 | } |
| 9630 | |
| 9631 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9632 | anylib_find(int kind, PyObject *str1, void *buf1, Py_ssize_t len1, |
| 9633 | 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] | 9634 | { |
| 9635 | switch(kind) { |
| 9636 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9637 | if (PyUnicode_IS_ASCII(str1) && PyUnicode_IS_ASCII(str2)) |
| 9638 | return asciilib_find(buf1, len1, buf2, len2, offset); |
| 9639 | else |
| 9640 | return ucs1lib_find(buf1, len1, buf2, len2, offset); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9641 | case PyUnicode_2BYTE_KIND: |
| 9642 | return ucs2lib_find(buf1, len1, buf2, len2, offset); |
| 9643 | case PyUnicode_4BYTE_KIND: |
| 9644 | return ucs4lib_find(buf1, len1, buf2, len2, offset); |
| 9645 | } |
| 9646 | assert(0); |
| 9647 | return -1; |
| 9648 | } |
| 9649 | |
| 9650 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9651 | anylib_count(int kind, PyObject *sstr, void* sbuf, Py_ssize_t slen, |
| 9652 | 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] | 9653 | { |
| 9654 | switch(kind) { |
| 9655 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9656 | if (PyUnicode_IS_ASCII(sstr) && PyUnicode_IS_ASCII(str1)) |
| 9657 | return asciilib_count(sbuf, slen, buf1, len1, maxcount); |
| 9658 | else |
| 9659 | return ucs1lib_count(sbuf, slen, buf1, len1, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9660 | case PyUnicode_2BYTE_KIND: |
| 9661 | return ucs2lib_count(sbuf, slen, buf1, len1, maxcount); |
| 9662 | case PyUnicode_4BYTE_KIND: |
| 9663 | return ucs4lib_count(sbuf, slen, buf1, len1, maxcount); |
| 9664 | } |
| 9665 | assert(0); |
| 9666 | return 0; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9667 | } |
| 9668 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9669 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9670 | replace(PyObject *self, PyObject *str1, |
| 9671 | PyObject *str2, Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9672 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9673 | PyObject *u; |
| 9674 | char *sbuf = PyUnicode_DATA(self); |
| 9675 | char *buf1 = PyUnicode_DATA(str1); |
| 9676 | char *buf2 = PyUnicode_DATA(str2); |
| 9677 | int srelease = 0, release1 = 0, release2 = 0; |
| 9678 | int skind = PyUnicode_KIND(self); |
| 9679 | int kind1 = PyUnicode_KIND(str1); |
| 9680 | int kind2 = PyUnicode_KIND(str2); |
| 9681 | Py_ssize_t slen = PyUnicode_GET_LENGTH(self); |
| 9682 | Py_ssize_t len1 = PyUnicode_GET_LENGTH(str1); |
| 9683 | Py_ssize_t len2 = PyUnicode_GET_LENGTH(str2); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9684 | |
| 9685 | if (maxcount < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9686 | maxcount = PY_SSIZE_T_MAX; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9687 | else if (maxcount == 0 || slen == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9688 | goto nothing; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9689 | |
Victor Stinner | 59de0ee | 2011-10-07 10:01:28 +0200 | [diff] [blame] | 9690 | if (str1 == str2) |
| 9691 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9692 | if (skind < kind1) |
| 9693 | /* substring too wide to be present */ |
| 9694 | goto nothing; |
| 9695 | |
| 9696 | if (len1 == len2) { |
Antoine Pitrou | cbfdee3 | 2010-01-13 08:58:08 +0000 | [diff] [blame] | 9697 | Py_ssize_t i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9698 | /* same length */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9699 | if (len1 == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9700 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9701 | if (len1 == 1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9702 | /* replace characters */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9703 | Py_UCS4 u1, u2, maxchar; |
| 9704 | int mayshrink, rkind; |
| 9705 | u1 = PyUnicode_READ_CHAR(str1, 0); |
| 9706 | if (!findchar(sbuf, PyUnicode_KIND(self), |
| 9707 | slen, u1, 1)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9708 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9709 | u2 = PyUnicode_READ_CHAR(str2, 0); |
| 9710 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
| 9711 | /* Replacing u1 with u2 may cause a maxchar reduction in the |
| 9712 | result string. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9713 | if (u2 > maxchar) { |
| 9714 | maxchar = u2; |
| 9715 | mayshrink = 0; |
| 9716 | } |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 9717 | else |
| 9718 | mayshrink = maxchar > 127; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9719 | u = PyUnicode_New(slen, maxchar); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9720 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9721 | goto error; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9722 | copy_characters(u, 0, self, 0, slen); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9723 | rkind = PyUnicode_KIND(u); |
| 9724 | for (i = 0; i < PyUnicode_GET_LENGTH(u); i++) |
| 9725 | if (PyUnicode_READ(rkind, PyUnicode_DATA(u), i) == u1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9726 | if (--maxcount < 0) |
| 9727 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9728 | PyUnicode_WRITE(rkind, PyUnicode_DATA(u), i, u2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9729 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9730 | if (mayshrink) { |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 9731 | unicode_adjust_maxchar(&u); |
| 9732 | if (u == NULL) |
| 9733 | goto error; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9734 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9735 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9736 | int rkind = skind; |
| 9737 | char *res; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 9738 | PyObject *rstr; |
| 9739 | Py_UCS4 maxchar; |
| 9740 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9741 | if (kind1 < rkind) { |
| 9742 | /* widen substring */ |
| 9743 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 9744 | if (!buf1) goto error; |
| 9745 | release1 = 1; |
| 9746 | } |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9747 | i = anylib_find(rkind, self, sbuf, slen, str1, buf1, len1, 0); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9748 | if (i < 0) |
| 9749 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9750 | if (rkind > kind2) { |
| 9751 | /* widen replacement */ |
| 9752 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 9753 | if (!buf2) goto error; |
| 9754 | release2 = 1; |
| 9755 | } |
| 9756 | else if (rkind < kind2) { |
| 9757 | /* widen self and buf1 */ |
| 9758 | rkind = kind2; |
| 9759 | if (release1) PyMem_Free(buf1); |
| 9760 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 9761 | if (!sbuf) goto error; |
| 9762 | srelease = 1; |
| 9763 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 9764 | if (!buf1) goto error; |
| 9765 | release1 = 1; |
| 9766 | } |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 9767 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
| 9768 | maxchar = Py_MAX(maxchar, PyUnicode_MAX_CHAR_VALUE(str2)); |
| 9769 | rstr = PyUnicode_New(slen, maxchar); |
| 9770 | if (!rstr) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9771 | goto error; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 9772 | res = PyUnicode_DATA(rstr); |
| 9773 | |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9774 | memcpy(res, sbuf, rkind * slen); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9775 | /* change everything in-place, starting with this one */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9776 | memcpy(res + rkind * i, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9777 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9778 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9779 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9780 | |
| 9781 | while ( --maxcount > 0) { |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9782 | i = anylib_find(rkind, self, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9783 | sbuf+rkind*i, slen-i, |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9784 | str1, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9785 | if (i == -1) |
| 9786 | break; |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9787 | memcpy(res + rkind * i, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9788 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9789 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9790 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9791 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9792 | |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 9793 | u = rstr; |
| 9794 | unicode_adjust_maxchar(&u); |
| 9795 | if (!u) |
| 9796 | goto error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9797 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9798 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9799 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9800 | Py_ssize_t n, i, j, ires; |
| 9801 | Py_ssize_t product, new_size; |
| 9802 | int rkind = skind; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 9803 | PyObject *rstr; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9804 | char *res; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 9805 | Py_UCS4 maxchar; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9806 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9807 | if (kind1 < rkind) { |
| 9808 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 9809 | if (!buf1) goto error; |
| 9810 | release1 = 1; |
| 9811 | } |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9812 | n = anylib_count(rkind, self, sbuf, slen, str1, buf1, len1, maxcount); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9813 | if (n == 0) |
| 9814 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9815 | if (kind2 < rkind) { |
| 9816 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 9817 | if (!buf2) goto error; |
| 9818 | release2 = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9819 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9820 | else if (kind2 > rkind) { |
| 9821 | rkind = kind2; |
| 9822 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 9823 | if (!sbuf) goto error; |
| 9824 | srelease = 1; |
| 9825 | if (release1) PyMem_Free(buf1); |
| 9826 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 9827 | if (!buf1) goto error; |
| 9828 | release1 = 1; |
| 9829 | } |
| 9830 | /* new_size = PyUnicode_GET_LENGTH(self) + n * (PyUnicode_GET_LENGTH(str2) - |
| 9831 | PyUnicode_GET_LENGTH(str1))); */ |
| 9832 | product = n * (len2-len1); |
| 9833 | if ((product / (len2-len1)) != n) { |
| 9834 | PyErr_SetString(PyExc_OverflowError, |
| 9835 | "replace string is too long"); |
| 9836 | goto error; |
| 9837 | } |
| 9838 | new_size = slen + product; |
| 9839 | if (new_size < 0 || new_size > (PY_SSIZE_T_MAX >> (rkind-1))) { |
| 9840 | PyErr_SetString(PyExc_OverflowError, |
| 9841 | "replace string is too long"); |
| 9842 | goto error; |
| 9843 | } |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 9844 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
| 9845 | maxchar = Py_MAX(maxchar, PyUnicode_MAX_CHAR_VALUE(str2)); |
| 9846 | rstr = PyUnicode_New(new_size, maxchar); |
| 9847 | if (!rstr) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9848 | goto error; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 9849 | res = PyUnicode_DATA(rstr); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9850 | ires = i = 0; |
| 9851 | if (len1 > 0) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9852 | while (n-- > 0) { |
| 9853 | /* look for next match */ |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9854 | j = anylib_find(rkind, self, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9855 | sbuf + rkind * i, slen-i, |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9856 | str1, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9857 | if (j == -1) |
| 9858 | break; |
| 9859 | else if (j > i) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9860 | /* copy unchanged part [i:j] */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9861 | memcpy(res + rkind * ires, |
| 9862 | sbuf + rkind * i, |
| 9863 | rkind * (j-i)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9864 | ires += j - i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9865 | } |
| 9866 | /* copy substitution string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9867 | if (len2 > 0) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9868 | memcpy(res + rkind * ires, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9869 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9870 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9871 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9872 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9873 | i = j + len1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9874 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9875 | if (i < slen) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9876 | /* copy tail [i:] */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9877 | memcpy(res + rkind * ires, |
| 9878 | sbuf + rkind * i, |
| 9879 | rkind * (slen-i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9880 | } else { |
| 9881 | /* interleave */ |
| 9882 | while (n > 0) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9883 | memcpy(res + rkind * ires, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9884 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9885 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9886 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9887 | if (--n <= 0) |
| 9888 | break; |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9889 | memcpy(res + rkind * ires, |
| 9890 | sbuf + rkind * i, |
| 9891 | rkind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9892 | ires++; |
| 9893 | i++; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9894 | } |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9895 | memcpy(res + rkind * ires, |
| 9896 | sbuf + rkind * i, |
| 9897 | rkind * (slen-i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9898 | } |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 9899 | u = rstr; |
| 9900 | unicode_adjust_maxchar(&u); |
| 9901 | if (u == NULL) |
| 9902 | goto error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9903 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9904 | if (srelease) |
| 9905 | PyMem_FREE(sbuf); |
| 9906 | if (release1) |
| 9907 | PyMem_FREE(buf1); |
| 9908 | if (release2) |
| 9909 | PyMem_FREE(buf2); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9910 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9911 | return u; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9912 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9913 | nothing: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9914 | /* nothing to replace; return original string (when possible) */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9915 | if (srelease) |
| 9916 | PyMem_FREE(sbuf); |
| 9917 | if (release1) |
| 9918 | PyMem_FREE(buf1); |
| 9919 | if (release2) |
| 9920 | PyMem_FREE(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9921 | if (PyUnicode_CheckExact(self)) { |
| 9922 | Py_INCREF(self); |
| 9923 | return (PyObject *) self; |
| 9924 | } |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 9925 | return PyUnicode_Copy(self); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9926 | error: |
| 9927 | if (srelease && sbuf) |
| 9928 | PyMem_FREE(sbuf); |
| 9929 | if (release1 && buf1) |
| 9930 | PyMem_FREE(buf1); |
| 9931 | if (release2 && buf2) |
| 9932 | PyMem_FREE(buf2); |
| 9933 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9934 | } |
| 9935 | |
| 9936 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 9937 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9938 | PyDoc_STRVAR(title__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9939 | "S.title() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9940 | \n\ |
| 9941 | 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] | 9942 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9943 | |
| 9944 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9945 | unicode_title(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9946 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9947 | return fixup(self, fixtitle); |
| 9948 | } |
| 9949 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9950 | PyDoc_STRVAR(capitalize__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9951 | "S.capitalize() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9952 | \n\ |
| 9953 | 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] | 9954 | have upper case and the rest lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9955 | |
| 9956 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9957 | unicode_capitalize(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9958 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9959 | return fixup(self, fixcapitalize); |
| 9960 | } |
| 9961 | |
| 9962 | #if 0 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9963 | PyDoc_STRVAR(capwords__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9964 | "S.capwords() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9965 | \n\ |
| 9966 | Apply .capitalize() to all words in S and return the result with\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9967 | normalized whitespace (all whitespace strings are replaced by ' ')."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9968 | |
| 9969 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 9970 | unicode_capwords(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9971 | { |
| 9972 | PyObject *list; |
| 9973 | PyObject *item; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9974 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9975 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9976 | /* Split into words */ |
| 9977 | list = split(self, NULL, -1); |
| 9978 | if (!list) |
| 9979 | return NULL; |
| 9980 | |
| 9981 | /* Capitalize each word */ |
| 9982 | for (i = 0; i < PyList_GET_SIZE(list); i++) { |
| 9983 | item = fixup((PyUnicodeObject *)PyList_GET_ITEM(list, i), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9984 | fixcapitalize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9985 | if (item == NULL) |
| 9986 | goto onError; |
| 9987 | Py_DECREF(PyList_GET_ITEM(list, i)); |
| 9988 | PyList_SET_ITEM(list, i, item); |
| 9989 | } |
| 9990 | |
| 9991 | /* Join the words to form a new string */ |
| 9992 | item = PyUnicode_Join(NULL, list); |
| 9993 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9994 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9995 | Py_DECREF(list); |
| 9996 | return (PyObject *)item; |
| 9997 | } |
| 9998 | #endif |
| 9999 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10000 | /* Argument converter. Coerces to a single unicode character */ |
| 10001 | |
| 10002 | static int |
| 10003 | convert_uc(PyObject *obj, void *addr) |
| 10004 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10005 | Py_UCS4 *fillcharloc = (Py_UCS4 *)addr; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10006 | PyObject *uniobj; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10007 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10008 | uniobj = PyUnicode_FromObject(obj); |
| 10009 | if (uniobj == NULL) { |
| 10010 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10011 | "The fill character cannot be converted to Unicode"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10012 | return 0; |
| 10013 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10014 | if (PyUnicode_GET_LENGTH(uniobj) != 1) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10015 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10016 | "The fill character must be exactly one character long"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10017 | Py_DECREF(uniobj); |
| 10018 | return 0; |
| 10019 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10020 | *fillcharloc = PyUnicode_READ_CHAR(uniobj, 0); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10021 | Py_DECREF(uniobj); |
| 10022 | return 1; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10023 | } |
| 10024 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10025 | PyDoc_STRVAR(center__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10026 | "S.center(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10027 | \n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10028 | Return S centered in a string of length width. Padding is\n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10029 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10030 | |
| 10031 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10032 | unicode_center(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10033 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10034 | Py_ssize_t marg, left; |
| 10035 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10036 | Py_UCS4 fillchar = ' '; |
| 10037 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 10038 | 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] | 10039 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10040 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 10041 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10042 | return NULL; |
| 10043 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10044 | if (_PyUnicode_LENGTH(self) >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10045 | Py_INCREF(self); |
| 10046 | return (PyObject*) self; |
| 10047 | } |
| 10048 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10049 | marg = width - _PyUnicode_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10050 | left = marg / 2 + (marg & width & 1); |
| 10051 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10052 | return pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10053 | } |
| 10054 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10055 | /* This function assumes that str1 and str2 are readied by the caller. */ |
| 10056 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10057 | static int |
| 10058 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 10059 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10060 | int kind1, kind2; |
| 10061 | void *data1, *data2; |
| 10062 | Py_ssize_t len1, len2, i; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10063 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10064 | kind1 = PyUnicode_KIND(str1); |
| 10065 | kind2 = PyUnicode_KIND(str2); |
| 10066 | data1 = PyUnicode_DATA(str1); |
| 10067 | data2 = PyUnicode_DATA(str2); |
| 10068 | len1 = PyUnicode_GET_LENGTH(str1); |
| 10069 | len2 = PyUnicode_GET_LENGTH(str2); |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10070 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10071 | for (i = 0; i < len1 && i < len2; ++i) { |
| 10072 | Py_UCS4 c1, c2; |
| 10073 | c1 = PyUnicode_READ(kind1, data1, i); |
| 10074 | c2 = PyUnicode_READ(kind2, data2, i); |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 10075 | |
| 10076 | if (c1 != c2) |
| 10077 | return (c1 < c2) ? -1 : 1; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10078 | } |
| 10079 | |
| 10080 | return (len1 < len2) ? -1 : (len1 != len2); |
| 10081 | } |
| 10082 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10083 | int |
| 10084 | PyUnicode_Compare(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10085 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10086 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 10087 | if (PyUnicode_READY(left) == -1 || |
| 10088 | PyUnicode_READY(right) == -1) |
| 10089 | return -1; |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 10090 | return unicode_compare((PyUnicodeObject *)left, |
| 10091 | (PyUnicodeObject *)right); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10092 | } |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 10093 | PyErr_Format(PyExc_TypeError, |
| 10094 | "Can't compare %.100s and %.100s", |
| 10095 | left->ob_type->tp_name, |
| 10096 | right->ob_type->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10097 | return -1; |
| 10098 | } |
| 10099 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10100 | int |
| 10101 | PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str) |
| 10102 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10103 | Py_ssize_t i; |
| 10104 | int kind; |
| 10105 | void *data; |
| 10106 | Py_UCS4 chr; |
| 10107 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 10108 | assert(_PyUnicode_CHECK(uni)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10109 | if (PyUnicode_READY(uni) == -1) |
| 10110 | return -1; |
| 10111 | kind = PyUnicode_KIND(uni); |
| 10112 | data = PyUnicode_DATA(uni); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10113 | /* Compare Unicode string and source character set string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10114 | for (i = 0; (chr = PyUnicode_READ(kind, data, i)) && str[i]; i++) |
| 10115 | if (chr != str[i]) |
| 10116 | return (chr < (unsigned char)(str[i])) ? -1 : 1; |
Benjamin Peterson | 8667a9b | 2010-01-09 21:45:28 +0000 | [diff] [blame] | 10117 | /* This check keeps Python strings that end in '\0' from comparing equal |
| 10118 | to C strings identical up to that point. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10119 | if (PyUnicode_GET_LENGTH(uni) != i || chr) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10120 | return 1; /* uni is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10121 | if (str[i]) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10122 | return -1; /* str is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10123 | return 0; |
| 10124 | } |
| 10125 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10126 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10127 | #define TEST_COND(cond) \ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10128 | ((cond) ? Py_True : Py_False) |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10129 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10130 | PyObject * |
| 10131 | PyUnicode_RichCompare(PyObject *left, PyObject *right, int op) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10132 | { |
| 10133 | int result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10134 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10135 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 10136 | PyObject *v; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10137 | if (PyUnicode_READY(left) == -1 || |
| 10138 | PyUnicode_READY(right) == -1) |
| 10139 | return NULL; |
| 10140 | if (PyUnicode_GET_LENGTH(left) != PyUnicode_GET_LENGTH(right) || |
| 10141 | PyUnicode_KIND(left) != PyUnicode_KIND(right)) { |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10142 | if (op == Py_EQ) { |
| 10143 | Py_INCREF(Py_False); |
| 10144 | return Py_False; |
| 10145 | } |
| 10146 | if (op == Py_NE) { |
| 10147 | Py_INCREF(Py_True); |
| 10148 | return Py_True; |
| 10149 | } |
| 10150 | } |
| 10151 | if (left == right) |
| 10152 | result = 0; |
| 10153 | else |
| 10154 | result = unicode_compare((PyUnicodeObject *)left, |
| 10155 | (PyUnicodeObject *)right); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10156 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10157 | /* Convert the return value to a Boolean */ |
| 10158 | switch (op) { |
| 10159 | case Py_EQ: |
| 10160 | v = TEST_COND(result == 0); |
| 10161 | break; |
| 10162 | case Py_NE: |
| 10163 | v = TEST_COND(result != 0); |
| 10164 | break; |
| 10165 | case Py_LE: |
| 10166 | v = TEST_COND(result <= 0); |
| 10167 | break; |
| 10168 | case Py_GE: |
| 10169 | v = TEST_COND(result >= 0); |
| 10170 | break; |
| 10171 | case Py_LT: |
| 10172 | v = TEST_COND(result == -1); |
| 10173 | break; |
| 10174 | case Py_GT: |
| 10175 | v = TEST_COND(result == 1); |
| 10176 | break; |
| 10177 | default: |
| 10178 | PyErr_BadArgument(); |
| 10179 | return NULL; |
| 10180 | } |
| 10181 | Py_INCREF(v); |
| 10182 | return v; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10183 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10184 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 10185 | Py_RETURN_NOTIMPLEMENTED; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10186 | } |
| 10187 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10188 | int |
| 10189 | PyUnicode_Contains(PyObject *container, PyObject *element) |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10190 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10191 | PyObject *str, *sub; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10192 | int kind1, kind2, kind; |
| 10193 | void *buf1, *buf2; |
| 10194 | Py_ssize_t len1, len2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10195 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10196 | |
| 10197 | /* Coerce the two arguments */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10198 | sub = PyUnicode_FromObject(element); |
| 10199 | if (!sub) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10200 | PyErr_Format(PyExc_TypeError, |
| 10201 | "'in <string>' requires string as left operand, not %s", |
| 10202 | element->ob_type->tp_name); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10203 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10204 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10205 | if (PyUnicode_READY(sub) == -1) |
| 10206 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10207 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10208 | str = PyUnicode_FromObject(container); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 10209 | if (!str || PyUnicode_READY(str) == -1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10210 | Py_DECREF(sub); |
| 10211 | return -1; |
| 10212 | } |
| 10213 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10214 | kind1 = PyUnicode_KIND(str); |
| 10215 | kind2 = PyUnicode_KIND(sub); |
| 10216 | kind = kind1 > kind2 ? kind1 : kind2; |
| 10217 | buf1 = PyUnicode_DATA(str); |
| 10218 | buf2 = PyUnicode_DATA(sub); |
| 10219 | if (kind1 != kind) |
| 10220 | buf1 = _PyUnicode_AsKind((PyObject*)str, kind); |
| 10221 | if (!buf1) { |
| 10222 | Py_DECREF(sub); |
| 10223 | return -1; |
| 10224 | } |
| 10225 | if (kind2 != kind) |
| 10226 | buf2 = _PyUnicode_AsKind((PyObject*)sub, kind); |
| 10227 | if (!buf2) { |
| 10228 | Py_DECREF(sub); |
| 10229 | if (kind1 != kind) PyMem_Free(buf1); |
| 10230 | return -1; |
| 10231 | } |
| 10232 | len1 = PyUnicode_GET_LENGTH(str); |
| 10233 | len2 = PyUnicode_GET_LENGTH(sub); |
| 10234 | |
| 10235 | switch(kind) { |
| 10236 | case PyUnicode_1BYTE_KIND: |
| 10237 | result = ucs1lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10238 | break; |
| 10239 | case PyUnicode_2BYTE_KIND: |
| 10240 | result = ucs2lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10241 | break; |
| 10242 | case PyUnicode_4BYTE_KIND: |
| 10243 | result = ucs4lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10244 | break; |
| 10245 | default: |
| 10246 | result = -1; |
| 10247 | assert(0); |
| 10248 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10249 | |
| 10250 | Py_DECREF(str); |
| 10251 | Py_DECREF(sub); |
| 10252 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10253 | if (kind1 != kind) |
| 10254 | PyMem_Free(buf1); |
| 10255 | if (kind2 != kind) |
| 10256 | PyMem_Free(buf2); |
| 10257 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10258 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10259 | } |
| 10260 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10261 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 10262 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10263 | PyObject * |
| 10264 | PyUnicode_Concat(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10265 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10266 | PyObject *u = NULL, *v = NULL, *w; |
| 10267 | Py_UCS4 maxchar; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10268 | |
| 10269 | /* Coerce the two arguments */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10270 | u = PyUnicode_FromObject(left); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10271 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10272 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10273 | v = PyUnicode_FromObject(right); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10274 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10275 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10276 | |
| 10277 | /* Shortcuts */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 10278 | if (v == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10279 | Py_DECREF(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10280 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10281 | } |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 10282 | if (u == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10283 | Py_DECREF(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10284 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10285 | } |
| 10286 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10287 | maxchar = PyUnicode_MAX_CHAR_VALUE(u); |
Victor Stinner | ff9e50f | 2011-09-28 22:17:19 +0200 | [diff] [blame] | 10288 | maxchar = Py_MAX(maxchar, PyUnicode_MAX_CHAR_VALUE(v)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10289 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10290 | /* Concat the two Unicode strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10291 | w = PyUnicode_New( |
| 10292 | PyUnicode_GET_LENGTH(u) + PyUnicode_GET_LENGTH(v), |
| 10293 | maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10294 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10295 | goto onError; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 10296 | copy_characters(w, 0, u, 0, PyUnicode_GET_LENGTH(u)); |
| 10297 | copy_characters(w, PyUnicode_GET_LENGTH(u), v, 0, PyUnicode_GET_LENGTH(v)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10298 | Py_DECREF(u); |
| 10299 | Py_DECREF(v); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10300 | assert(_PyUnicode_CheckConsistency(w, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10301 | return w; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10302 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10303 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10304 | Py_XDECREF(u); |
| 10305 | Py_XDECREF(v); |
| 10306 | return NULL; |
| 10307 | } |
| 10308 | |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10309 | static void |
| 10310 | unicode_append_inplace(PyObject **p_left, PyObject *right) |
| 10311 | { |
| 10312 | Py_ssize_t left_len, right_len, new_len; |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10313 | |
| 10314 | assert(PyUnicode_IS_READY(*p_left)); |
| 10315 | assert(PyUnicode_IS_READY(right)); |
| 10316 | |
| 10317 | left_len = PyUnicode_GET_LENGTH(*p_left); |
| 10318 | right_len = PyUnicode_GET_LENGTH(right); |
| 10319 | if (left_len > PY_SSIZE_T_MAX - right_len) { |
| 10320 | PyErr_SetString(PyExc_OverflowError, |
| 10321 | "strings are too large to concat"); |
| 10322 | goto error; |
| 10323 | } |
| 10324 | new_len = left_len + right_len; |
| 10325 | |
| 10326 | /* Now we own the last reference to 'left', so we can resize it |
| 10327 | * in-place. |
| 10328 | */ |
| 10329 | if (unicode_resize(p_left, new_len) != 0) { |
| 10330 | /* XXX if _PyUnicode_Resize() fails, 'left' has been |
| 10331 | * deallocated so it cannot be put back into |
| 10332 | * 'variable'. The MemoryError is raised when there |
| 10333 | * is no value in 'variable', which might (very |
| 10334 | * remotely) be a cause of incompatibilities. |
| 10335 | */ |
| 10336 | goto error; |
| 10337 | } |
| 10338 | /* copy 'right' into the newly allocated area of 'left' */ |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 10339 | copy_characters(*p_left, left_len, right, 0, right_len); |
| 10340 | _PyUnicode_DIRTY(*p_left); |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10341 | return; |
| 10342 | |
| 10343 | error: |
| 10344 | Py_DECREF(*p_left); |
| 10345 | *p_left = NULL; |
| 10346 | } |
| 10347 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10348 | void |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10349 | PyUnicode_Append(PyObject **p_left, PyObject *right) |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10350 | { |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10351 | PyObject *left, *res; |
| 10352 | |
| 10353 | if (p_left == NULL) { |
| 10354 | if (!PyErr_Occurred()) |
| 10355 | PyErr_BadInternalCall(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10356 | return; |
| 10357 | } |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10358 | left = *p_left; |
| 10359 | if (right == NULL || !PyUnicode_Check(left)) { |
| 10360 | if (!PyErr_Occurred()) |
| 10361 | PyErr_BadInternalCall(); |
| 10362 | goto error; |
| 10363 | } |
| 10364 | |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 10365 | if (PyUnicode_READY(left)) |
| 10366 | goto error; |
| 10367 | if (PyUnicode_READY(right)) |
| 10368 | goto error; |
| 10369 | |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10370 | if (PyUnicode_CheckExact(left) && left != unicode_empty |
| 10371 | && PyUnicode_CheckExact(right) && right != unicode_empty |
| 10372 | && unicode_resizable(left) |
| 10373 | && (_PyUnicode_KIND(right) <= _PyUnicode_KIND(left) |
| 10374 | || _PyUnicode_WSTR(left) != NULL)) |
| 10375 | { |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10376 | /* Don't resize for ascii += latin1. Convert ascii to latin1 requires |
| 10377 | to change the structure size, but characters are stored just after |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 10378 | the structure, and so it requires to move all characters which is |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10379 | not so different than duplicating the string. */ |
| 10380 | if (!(PyUnicode_IS_ASCII(left) && !PyUnicode_IS_ASCII(right))) |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10381 | { |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10382 | unicode_append_inplace(p_left, right); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10383 | if (p_left != NULL) |
| 10384 | assert(_PyUnicode_CheckConsistency(*p_left, 1)); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10385 | return; |
| 10386 | } |
| 10387 | } |
| 10388 | |
| 10389 | res = PyUnicode_Concat(left, right); |
| 10390 | if (res == NULL) |
| 10391 | goto error; |
| 10392 | Py_DECREF(left); |
| 10393 | *p_left = res; |
| 10394 | return; |
| 10395 | |
| 10396 | error: |
| 10397 | Py_DECREF(*p_left); |
| 10398 | *p_left = NULL; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10399 | } |
| 10400 | |
| 10401 | void |
| 10402 | PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right) |
| 10403 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10404 | PyUnicode_Append(pleft, right); |
| 10405 | Py_XDECREF(right); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10406 | } |
| 10407 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10408 | PyDoc_STRVAR(count__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10409 | "S.count(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10410 | \n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10411 | Return the number of non-overlapping occurrences of substring sub in\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10412 | 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] | 10413 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10414 | |
| 10415 | static PyObject * |
| 10416 | unicode_count(PyUnicodeObject *self, PyObject *args) |
| 10417 | { |
| 10418 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10419 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10420 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10421 | PyObject *result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10422 | int kind1, kind2, kind; |
| 10423 | void *buf1, *buf2; |
| 10424 | Py_ssize_t len1, len2, iresult; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10425 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10426 | if (!stringlib_parse_args_finds_unicode("count", args, &substring, |
| 10427 | &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10428 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10429 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10430 | kind1 = PyUnicode_KIND(self); |
| 10431 | kind2 = PyUnicode_KIND(substring); |
| 10432 | kind = kind1 > kind2 ? kind1 : kind2; |
| 10433 | buf1 = PyUnicode_DATA(self); |
| 10434 | buf2 = PyUnicode_DATA(substring); |
| 10435 | if (kind1 != kind) |
| 10436 | buf1 = _PyUnicode_AsKind((PyObject*)self, kind); |
| 10437 | if (!buf1) { |
| 10438 | Py_DECREF(substring); |
| 10439 | return NULL; |
| 10440 | } |
| 10441 | if (kind2 != kind) |
| 10442 | buf2 = _PyUnicode_AsKind((PyObject*)substring, kind); |
| 10443 | if (!buf2) { |
| 10444 | Py_DECREF(substring); |
| 10445 | if (kind1 != kind) PyMem_Free(buf1); |
| 10446 | return NULL; |
| 10447 | } |
| 10448 | len1 = PyUnicode_GET_LENGTH(self); |
| 10449 | len2 = PyUnicode_GET_LENGTH(substring); |
| 10450 | |
| 10451 | ADJUST_INDICES(start, end, len1); |
| 10452 | switch(kind) { |
| 10453 | case PyUnicode_1BYTE_KIND: |
| 10454 | iresult = ucs1lib_count( |
| 10455 | ((Py_UCS1*)buf1) + start, end - start, |
| 10456 | buf2, len2, PY_SSIZE_T_MAX |
| 10457 | ); |
| 10458 | break; |
| 10459 | case PyUnicode_2BYTE_KIND: |
| 10460 | iresult = ucs2lib_count( |
| 10461 | ((Py_UCS2*)buf1) + start, end - start, |
| 10462 | buf2, len2, PY_SSIZE_T_MAX |
| 10463 | ); |
| 10464 | break; |
| 10465 | case PyUnicode_4BYTE_KIND: |
| 10466 | iresult = ucs4lib_count( |
| 10467 | ((Py_UCS4*)buf1) + start, end - start, |
| 10468 | buf2, len2, PY_SSIZE_T_MAX |
| 10469 | ); |
| 10470 | break; |
| 10471 | default: |
| 10472 | assert(0); iresult = 0; |
| 10473 | } |
| 10474 | |
| 10475 | result = PyLong_FromSsize_t(iresult); |
| 10476 | |
| 10477 | if (kind1 != kind) |
| 10478 | PyMem_Free(buf1); |
| 10479 | if (kind2 != kind) |
| 10480 | PyMem_Free(buf2); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10481 | |
| 10482 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10483 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10484 | return result; |
| 10485 | } |
| 10486 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10487 | PyDoc_STRVAR(encode__doc__, |
Victor Stinner | c911bbf | 2010-11-07 19:04:46 +0000 | [diff] [blame] | 10488 | "S.encode(encoding='utf-8', errors='strict') -> bytes\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10489 | \n\ |
Victor Stinner | e14e212 | 2010-11-07 18:41:46 +0000 | [diff] [blame] | 10490 | Encode S using the codec registered for encoding. Default encoding\n\ |
| 10491 | 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] | 10492 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 10493 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 10494 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 10495 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10496 | |
| 10497 | static PyObject * |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 10498 | unicode_encode(PyUnicodeObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10499 | { |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 10500 | static char *kwlist[] = {"encoding", "errors", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10501 | char *encoding = NULL; |
| 10502 | char *errors = NULL; |
Guido van Rossum | 35d9428 | 2007-08-27 18:20:11 +0000 | [diff] [blame] | 10503 | |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 10504 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:encode", |
| 10505 | kwlist, &encoding, &errors)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10506 | return NULL; |
Georg Brandl | 3b9406b | 2010-12-03 07:54:09 +0000 | [diff] [blame] | 10507 | return PyUnicode_AsEncodedString((PyObject *)self, encoding, errors); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 10508 | } |
| 10509 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10510 | PyDoc_STRVAR(expandtabs__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10511 | "S.expandtabs([tabsize]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10512 | \n\ |
| 10513 | 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] | 10514 | 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] | 10515 | |
| 10516 | static PyObject* |
| 10517 | unicode_expandtabs(PyUnicodeObject *self, PyObject *args) |
| 10518 | { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10519 | Py_ssize_t i, j, line_pos, src_len, incr; |
| 10520 | Py_UCS4 ch; |
| 10521 | PyObject *u; |
| 10522 | void *src_data, *dest_data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10523 | int tabsize = 8; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10524 | int kind; |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10525 | int found; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10526 | |
| 10527 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10528 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10529 | |
Antoine Pitrou | 2242522 | 2011-10-04 19:10:51 +0200 | [diff] [blame] | 10530 | if (PyUnicode_READY(self) == -1) |
| 10531 | return NULL; |
| 10532 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 10533 | /* First pass: determine size of output string */ |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10534 | src_len = PyUnicode_GET_LENGTH(self); |
| 10535 | i = j = line_pos = 0; |
| 10536 | kind = PyUnicode_KIND(self); |
| 10537 | src_data = PyUnicode_DATA(self); |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10538 | found = 0; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10539 | for (; i < src_len; i++) { |
| 10540 | ch = PyUnicode_READ(kind, src_data, i); |
| 10541 | if (ch == '\t') { |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10542 | found = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10543 | if (tabsize > 0) { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10544 | incr = tabsize - (line_pos % tabsize); /* cannot overflow */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10545 | if (j > PY_SSIZE_T_MAX - incr) |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10546 | goto overflow; |
| 10547 | line_pos += incr; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10548 | j += incr; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10549 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10550 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10551 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10552 | if (j > PY_SSIZE_T_MAX - 1) |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10553 | goto overflow; |
| 10554 | line_pos++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10555 | j++; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10556 | if (ch == '\n' || ch == '\r') |
| 10557 | line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10558 | } |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10559 | } |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10560 | if (!found && PyUnicode_CheckExact(self)) { |
| 10561 | Py_INCREF((PyObject *) self); |
| 10562 | return (PyObject *) self; |
| 10563 | } |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 10564 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10565 | /* Second pass: create output string and fill it */ |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10566 | u = PyUnicode_New(j, PyUnicode_MAX_CHAR_VALUE(self)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10567 | if (!u) |
| 10568 | return NULL; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10569 | dest_data = PyUnicode_DATA(u); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10570 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10571 | i = j = line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10572 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10573 | for (; i < src_len; i++) { |
| 10574 | ch = PyUnicode_READ(kind, src_data, i); |
| 10575 | if (ch == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10576 | if (tabsize > 0) { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10577 | incr = tabsize - (line_pos % tabsize); |
| 10578 | line_pos += incr; |
| 10579 | while (incr--) { |
| 10580 | PyUnicode_WRITE(kind, dest_data, j, ' '); |
| 10581 | j++; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10582 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10583 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10584 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10585 | else { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10586 | line_pos++; |
| 10587 | PyUnicode_WRITE(kind, dest_data, j, ch); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10588 | j++; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10589 | if (ch == '\n' || ch == '\r') |
| 10590 | line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10591 | } |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10592 | } |
| 10593 | assert (j == PyUnicode_GET_LENGTH(u)); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 10594 | #ifndef DONT_MAKE_RESULT_READY |
| 10595 | if (_PyUnicode_READY_REPLACE(&u)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10596 | Py_DECREF(u); |
| 10597 | return NULL; |
| 10598 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 10599 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10600 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10601 | return (PyObject*) u; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10602 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10603 | overflow: |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10604 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 10605 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10606 | } |
| 10607 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10608 | PyDoc_STRVAR(find__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10609 | "S.find(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10610 | \n\ |
| 10611 | Return the lowest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 10612 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10613 | arguments start and end are interpreted as in slice notation.\n\ |
| 10614 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10615 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10616 | |
| 10617 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10618 | unicode_find(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10619 | { |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10620 | PyUnicodeObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 10621 | Py_ssize_t start; |
| 10622 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10623 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10624 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10625 | if (!stringlib_parse_args_finds_unicode("find", args, &substring, |
| 10626 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10627 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10628 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10629 | if (PyUnicode_READY(self) == -1) |
| 10630 | return NULL; |
| 10631 | if (PyUnicode_READY(substring) == -1) |
| 10632 | return NULL; |
| 10633 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 10634 | result = any_find_slice(1, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10635 | self, (PyObject*)substring, start, end |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10636 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10637 | |
| 10638 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10639 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10640 | if (result == -2) |
| 10641 | return NULL; |
| 10642 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 10643 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10644 | } |
| 10645 | |
| 10646 | static PyObject * |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 10647 | unicode_getitem(PyObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10648 | { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 10649 | Py_UCS4 ch = PyUnicode_ReadChar(self, index); |
| 10650 | if (ch == (Py_UCS4)-1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10651 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10652 | return PyUnicode_FromOrdinal(ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10653 | } |
| 10654 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 10655 | /* Believe it or not, this produces the same value for ASCII strings |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 10656 | as bytes_hash(). */ |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 10657 | static Py_hash_t |
Neil Schemenauer | f8c37d1 | 2007-09-07 20:49:04 +0000 | [diff] [blame] | 10658 | unicode_hash(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10659 | { |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 10660 | Py_ssize_t len; |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 10661 | Py_uhash_t x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 10662 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10663 | if (_PyUnicode_HASH(self) != -1) |
| 10664 | return _PyUnicode_HASH(self); |
| 10665 | if (PyUnicode_READY(self) == -1) |
| 10666 | return -1; |
| 10667 | len = PyUnicode_GET_LENGTH(self); |
| 10668 | |
| 10669 | /* The hash function as a macro, gets expanded three times below. */ |
| 10670 | #define HASH(P) \ |
| 10671 | x = (Py_uhash_t)*P << 7; \ |
| 10672 | while (--len >= 0) \ |
| 10673 | x = (1000003*x) ^ (Py_uhash_t)*P++; |
| 10674 | |
| 10675 | switch (PyUnicode_KIND(self)) { |
| 10676 | case PyUnicode_1BYTE_KIND: { |
| 10677 | const unsigned char *c = PyUnicode_1BYTE_DATA(self); |
| 10678 | HASH(c); |
| 10679 | break; |
| 10680 | } |
| 10681 | case PyUnicode_2BYTE_KIND: { |
| 10682 | const Py_UCS2 *s = PyUnicode_2BYTE_DATA(self); |
| 10683 | HASH(s); |
| 10684 | break; |
| 10685 | } |
| 10686 | default: { |
| 10687 | Py_UCS4 *l; |
| 10688 | assert(PyUnicode_KIND(self) == PyUnicode_4BYTE_KIND && |
| 10689 | "Impossible switch case in unicode_hash"); |
| 10690 | l = PyUnicode_4BYTE_DATA(self); |
| 10691 | HASH(l); |
| 10692 | break; |
| 10693 | } |
| 10694 | } |
| 10695 | x ^= (Py_uhash_t)PyUnicode_GET_LENGTH(self); |
| 10696 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 10697 | if (x == -1) |
| 10698 | x = -2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10699 | _PyUnicode_HASH(self) = x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 10700 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10701 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10702 | #undef HASH |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10703 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10704 | PyDoc_STRVAR(index__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10705 | "S.index(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10706 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10707 | 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] | 10708 | |
| 10709 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10710 | unicode_index(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10711 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10712 | Py_ssize_t result; |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10713 | PyUnicodeObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 10714 | Py_ssize_t start; |
| 10715 | Py_ssize_t end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10716 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10717 | if (!stringlib_parse_args_finds_unicode("index", args, &substring, |
| 10718 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10719 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10720 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10721 | if (PyUnicode_READY(self) == -1) |
| 10722 | return NULL; |
| 10723 | if (PyUnicode_READY(substring) == -1) |
| 10724 | return NULL; |
| 10725 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 10726 | result = any_find_slice(1, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10727 | self, (PyObject*)substring, start, end |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10728 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10729 | |
| 10730 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10731 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10732 | if (result == -2) |
| 10733 | return NULL; |
| 10734 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10735 | if (result < 0) { |
| 10736 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 10737 | return NULL; |
| 10738 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10739 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 10740 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10741 | } |
| 10742 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10743 | PyDoc_STRVAR(islower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10744 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10745 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10746 | 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] | 10747 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10748 | |
| 10749 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10750 | unicode_islower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10751 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10752 | Py_ssize_t i, length; |
| 10753 | int kind; |
| 10754 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10755 | int cased; |
| 10756 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10757 | if (PyUnicode_READY(self) == -1) |
| 10758 | return NULL; |
| 10759 | length = PyUnicode_GET_LENGTH(self); |
| 10760 | kind = PyUnicode_KIND(self); |
| 10761 | data = PyUnicode_DATA(self); |
| 10762 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10763 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10764 | if (length == 1) |
| 10765 | return PyBool_FromLong( |
| 10766 | Py_UNICODE_ISLOWER(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10767 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10768 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10769 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10770 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10771 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10772 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10773 | for (i = 0; i < length; i++) { |
| 10774 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10775 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10776 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 10777 | return PyBool_FromLong(0); |
| 10778 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 10779 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10780 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10781 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10782 | } |
| 10783 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10784 | PyDoc_STRVAR(isupper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10785 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10786 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 10787 | 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] | 10788 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10789 | |
| 10790 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10791 | unicode_isupper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10792 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10793 | Py_ssize_t i, length; |
| 10794 | int kind; |
| 10795 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10796 | int cased; |
| 10797 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10798 | if (PyUnicode_READY(self) == -1) |
| 10799 | return NULL; |
| 10800 | length = PyUnicode_GET_LENGTH(self); |
| 10801 | kind = PyUnicode_KIND(self); |
| 10802 | data = PyUnicode_DATA(self); |
| 10803 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10804 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10805 | if (length == 1) |
| 10806 | return PyBool_FromLong( |
| 10807 | Py_UNICODE_ISUPPER(PyUnicode_READ(kind, data, 0)) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10808 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10809 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10810 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10811 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10812 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10813 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10814 | for (i = 0; i < length; i++) { |
| 10815 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10816 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10817 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 10818 | return PyBool_FromLong(0); |
| 10819 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 10820 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10821 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10822 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10823 | } |
| 10824 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10825 | PyDoc_STRVAR(istitle__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10826 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10827 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 10828 | Return True if S is a titlecased string and there is at least one\n\ |
| 10829 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 10830 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 10831 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10832 | |
| 10833 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10834 | unicode_istitle(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10835 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10836 | Py_ssize_t i, length; |
| 10837 | int kind; |
| 10838 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10839 | int cased, previous_is_cased; |
| 10840 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10841 | if (PyUnicode_READY(self) == -1) |
| 10842 | return NULL; |
| 10843 | length = PyUnicode_GET_LENGTH(self); |
| 10844 | kind = PyUnicode_KIND(self); |
| 10845 | data = PyUnicode_DATA(self); |
| 10846 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10847 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10848 | if (length == 1) { |
| 10849 | Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 10850 | return PyBool_FromLong((Py_UNICODE_ISTITLE(ch) != 0) || |
| 10851 | (Py_UNICODE_ISUPPER(ch) != 0)); |
| 10852 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10853 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10854 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10855 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10856 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10857 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10858 | cased = 0; |
| 10859 | previous_is_cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10860 | for (i = 0; i < length; i++) { |
| 10861 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10862 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10863 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 10864 | if (previous_is_cased) |
| 10865 | return PyBool_FromLong(0); |
| 10866 | previous_is_cased = 1; |
| 10867 | cased = 1; |
| 10868 | } |
| 10869 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 10870 | if (!previous_is_cased) |
| 10871 | return PyBool_FromLong(0); |
| 10872 | previous_is_cased = 1; |
| 10873 | cased = 1; |
| 10874 | } |
| 10875 | else |
| 10876 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10877 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10878 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10879 | } |
| 10880 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10881 | PyDoc_STRVAR(isspace__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10882 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10883 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 10884 | Return True if all characters in S are whitespace\n\ |
| 10885 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10886 | |
| 10887 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10888 | unicode_isspace(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10889 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10890 | Py_ssize_t i, length; |
| 10891 | int kind; |
| 10892 | void *data; |
| 10893 | |
| 10894 | if (PyUnicode_READY(self) == -1) |
| 10895 | return NULL; |
| 10896 | length = PyUnicode_GET_LENGTH(self); |
| 10897 | kind = PyUnicode_KIND(self); |
| 10898 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10899 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10900 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10901 | if (length == 1) |
| 10902 | return PyBool_FromLong( |
| 10903 | Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10904 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10905 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10906 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10907 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10908 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10909 | for (i = 0; i < length; i++) { |
| 10910 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 10911 | if (!Py_UNICODE_ISSPACE(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10912 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10913 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10914 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10915 | } |
| 10916 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10917 | PyDoc_STRVAR(isalpha__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10918 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10919 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 10920 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10921 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10922 | |
| 10923 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10924 | unicode_isalpha(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10925 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10926 | Py_ssize_t i, length; |
| 10927 | int kind; |
| 10928 | void *data; |
| 10929 | |
| 10930 | if (PyUnicode_READY(self) == -1) |
| 10931 | return NULL; |
| 10932 | length = PyUnicode_GET_LENGTH(self); |
| 10933 | kind = PyUnicode_KIND(self); |
| 10934 | data = PyUnicode_DATA(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10935 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10936 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10937 | if (length == 1) |
| 10938 | return PyBool_FromLong( |
| 10939 | Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, 0))); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10940 | |
| 10941 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10942 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10943 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10944 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10945 | for (i = 0; i < length; i++) { |
| 10946 | if (!Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10947 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10948 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10949 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10950 | } |
| 10951 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10952 | PyDoc_STRVAR(isalnum__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10953 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10954 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 10955 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10956 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10957 | |
| 10958 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10959 | unicode_isalnum(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10960 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10961 | int kind; |
| 10962 | void *data; |
| 10963 | Py_ssize_t len, i; |
| 10964 | |
| 10965 | if (PyUnicode_READY(self) == -1) |
| 10966 | return NULL; |
| 10967 | |
| 10968 | kind = PyUnicode_KIND(self); |
| 10969 | data = PyUnicode_DATA(self); |
| 10970 | len = PyUnicode_GET_LENGTH(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10971 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10972 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10973 | if (len == 1) { |
| 10974 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 10975 | return PyBool_FromLong(Py_UNICODE_ISALNUM(ch)); |
| 10976 | } |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10977 | |
| 10978 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10979 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10980 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10981 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10982 | for (i = 0; i < len; i++) { |
| 10983 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 10984 | if (!Py_UNICODE_ISALNUM(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10985 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10986 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10987 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10988 | } |
| 10989 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10990 | PyDoc_STRVAR(isdecimal__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10991 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10992 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10993 | 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] | 10994 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10995 | |
| 10996 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10997 | unicode_isdecimal(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10998 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10999 | Py_ssize_t i, length; |
| 11000 | int kind; |
| 11001 | void *data; |
| 11002 | |
| 11003 | if (PyUnicode_READY(self) == -1) |
| 11004 | return NULL; |
| 11005 | length = PyUnicode_GET_LENGTH(self); |
| 11006 | kind = PyUnicode_KIND(self); |
| 11007 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11008 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11009 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11010 | if (length == 1) |
| 11011 | return PyBool_FromLong( |
| 11012 | Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11013 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11014 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11015 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11016 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11017 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11018 | for (i = 0; i < length; i++) { |
| 11019 | if (!Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11020 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11021 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11022 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11023 | } |
| 11024 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11025 | PyDoc_STRVAR(isdigit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11026 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11027 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11028 | Return True if all characters in S are digits\n\ |
| 11029 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11030 | |
| 11031 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11032 | unicode_isdigit(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11033 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11034 | Py_ssize_t i, length; |
| 11035 | int kind; |
| 11036 | void *data; |
| 11037 | |
| 11038 | if (PyUnicode_READY(self) == -1) |
| 11039 | return NULL; |
| 11040 | length = PyUnicode_GET_LENGTH(self); |
| 11041 | kind = PyUnicode_KIND(self); |
| 11042 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11043 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11044 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11045 | if (length == 1) { |
| 11046 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11047 | return PyBool_FromLong(Py_UNICODE_ISDIGIT(ch)); |
| 11048 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11049 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11050 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11051 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11052 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11053 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11054 | for (i = 0; i < length; i++) { |
| 11055 | if (!Py_UNICODE_ISDIGIT(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11056 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11057 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11058 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11059 | } |
| 11060 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11061 | PyDoc_STRVAR(isnumeric__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11062 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11063 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11064 | 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] | 11065 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11066 | |
| 11067 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11068 | unicode_isnumeric(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11069 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11070 | Py_ssize_t i, length; |
| 11071 | int kind; |
| 11072 | void *data; |
| 11073 | |
| 11074 | if (PyUnicode_READY(self) == -1) |
| 11075 | return NULL; |
| 11076 | length = PyUnicode_GET_LENGTH(self); |
| 11077 | kind = PyUnicode_KIND(self); |
| 11078 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11079 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11080 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11081 | if (length == 1) |
| 11082 | return PyBool_FromLong( |
| 11083 | Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11084 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11085 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11086 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11087 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11088 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11089 | for (i = 0; i < length; i++) { |
| 11090 | if (!Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11091 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11092 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11093 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11094 | } |
| 11095 | |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11096 | int |
| 11097 | PyUnicode_IsIdentifier(PyObject *self) |
| 11098 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11099 | int kind; |
| 11100 | void *data; |
| 11101 | Py_ssize_t i; |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11102 | Py_UCS4 first; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11103 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11104 | if (PyUnicode_READY(self) == -1) { |
| 11105 | Py_FatalError("identifier not ready"); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11106 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11107 | } |
| 11108 | |
| 11109 | /* Special case for empty strings */ |
| 11110 | if (PyUnicode_GET_LENGTH(self) == 0) |
| 11111 | return 0; |
| 11112 | kind = PyUnicode_KIND(self); |
| 11113 | data = PyUnicode_DATA(self); |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11114 | |
| 11115 | /* PEP 3131 says that the first character must be in |
| 11116 | XID_Start and subsequent characters in XID_Continue, |
| 11117 | and for the ASCII range, the 2.x rules apply (i.e |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11118 | start with letters and underscore, continue with |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11119 | letters, digits, underscore). However, given the current |
| 11120 | definition of XID_Start and XID_Continue, it is sufficient |
| 11121 | to check just for these, except that _ must be allowed |
| 11122 | as starting an identifier. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11123 | first = PyUnicode_READ(kind, data, 0); |
Benjamin Peterson | f413b80 | 2011-08-12 22:17:18 -0500 | [diff] [blame] | 11124 | if (!_PyUnicode_IsXidStart(first) && first != 0x5F /* LOW LINE */) |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11125 | return 0; |
| 11126 | |
Benjamin Peterson | 9c6e6a0 | 2011-09-28 08:09:05 -0400 | [diff] [blame] | 11127 | for (i = 1; i < PyUnicode_GET_LENGTH(self); i++) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11128 | if (!_PyUnicode_IsXidContinue(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11129 | return 0; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11130 | return 1; |
| 11131 | } |
| 11132 | |
| 11133 | PyDoc_STRVAR(isidentifier__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11134 | "S.isidentifier() -> bool\n\ |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11135 | \n\ |
| 11136 | Return True if S is a valid identifier according\n\ |
| 11137 | to the language definition."); |
| 11138 | |
| 11139 | static PyObject* |
| 11140 | unicode_isidentifier(PyObject *self) |
| 11141 | { |
| 11142 | return PyBool_FromLong(PyUnicode_IsIdentifier(self)); |
| 11143 | } |
| 11144 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11145 | PyDoc_STRVAR(isprintable__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11146 | "S.isprintable() -> bool\n\ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11147 | \n\ |
| 11148 | Return True if all characters in S are considered\n\ |
| 11149 | printable in repr() or S is empty, False otherwise."); |
| 11150 | |
| 11151 | static PyObject* |
| 11152 | unicode_isprintable(PyObject *self) |
| 11153 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11154 | Py_ssize_t i, length; |
| 11155 | int kind; |
| 11156 | void *data; |
| 11157 | |
| 11158 | if (PyUnicode_READY(self) == -1) |
| 11159 | return NULL; |
| 11160 | length = PyUnicode_GET_LENGTH(self); |
| 11161 | kind = PyUnicode_KIND(self); |
| 11162 | data = PyUnicode_DATA(self); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11163 | |
| 11164 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11165 | if (length == 1) |
| 11166 | return PyBool_FromLong( |
| 11167 | Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, 0))); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11168 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11169 | for (i = 0; i < length; i++) { |
| 11170 | if (!Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, i))) { |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11171 | Py_RETURN_FALSE; |
| 11172 | } |
| 11173 | } |
| 11174 | Py_RETURN_TRUE; |
| 11175 | } |
| 11176 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11177 | PyDoc_STRVAR(join__doc__, |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 11178 | "S.join(iterable) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11179 | \n\ |
| 11180 | 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] | 11181 | iterable. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11182 | |
| 11183 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11184 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11185 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11186 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11187 | } |
| 11188 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11189 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11190 | unicode_length(PyUnicodeObject *self) |
| 11191 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11192 | if (PyUnicode_READY(self) == -1) |
| 11193 | return -1; |
| 11194 | return PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11195 | } |
| 11196 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11197 | PyDoc_STRVAR(ljust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11198 | "S.ljust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11199 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 11200 | 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] | 11201 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11202 | |
| 11203 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11204 | unicode_ljust(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11205 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11206 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11207 | Py_UCS4 fillchar = ' '; |
| 11208 | |
| 11209 | if (PyUnicode_READY(self) == -1) |
| 11210 | return NULL; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 11211 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11212 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11213 | return NULL; |
| 11214 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11215 | if (_PyUnicode_LENGTH(self) >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11216 | Py_INCREF(self); |
| 11217 | return (PyObject*) self; |
| 11218 | } |
| 11219 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11220 | return (PyObject*) pad(self, 0, width - _PyUnicode_LENGTH(self), fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11221 | } |
| 11222 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11223 | PyDoc_STRVAR(lower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11224 | "S.lower() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11225 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11226 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11227 | |
| 11228 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11229 | unicode_lower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11230 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11231 | return fixup(self, fixlower); |
| 11232 | } |
| 11233 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11234 | #define LEFTSTRIP 0 |
| 11235 | #define RIGHTSTRIP 1 |
| 11236 | #define BOTHSTRIP 2 |
| 11237 | |
| 11238 | /* Arrays indexed by above */ |
| 11239 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 11240 | |
| 11241 | #define STRIPNAME(i) (stripformat[i]+3) |
| 11242 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11243 | /* externally visible for str.strip(unicode) */ |
| 11244 | PyObject * |
| 11245 | _PyUnicode_XStrip(PyUnicodeObject *self, int striptype, PyObject *sepobj) |
| 11246 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11247 | void *data; |
| 11248 | int kind; |
| 11249 | Py_ssize_t i, j, len; |
| 11250 | BLOOM_MASK sepmask; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11251 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11252 | if (PyUnicode_READY(self) == -1 || PyUnicode_READY(sepobj) == -1) |
| 11253 | return NULL; |
| 11254 | |
| 11255 | kind = PyUnicode_KIND(self); |
| 11256 | data = PyUnicode_DATA(self); |
| 11257 | len = PyUnicode_GET_LENGTH(self); |
| 11258 | sepmask = make_bloom_mask(PyUnicode_KIND(sepobj), |
| 11259 | PyUnicode_DATA(sepobj), |
| 11260 | PyUnicode_GET_LENGTH(sepobj)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11261 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11262 | i = 0; |
| 11263 | if (striptype != RIGHTSTRIP) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11264 | while (i < len && |
| 11265 | BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, i), sepobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11266 | i++; |
| 11267 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11268 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11269 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11270 | j = len; |
| 11271 | if (striptype != LEFTSTRIP) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11272 | do { |
| 11273 | j--; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11274 | } while (j >= i && |
| 11275 | BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, j), sepobj)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11276 | j++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11277 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11278 | |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11279 | return PyUnicode_Substring((PyObject*)self, i, j); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11280 | } |
| 11281 | |
| 11282 | PyObject* |
| 11283 | PyUnicode_Substring(PyObject *self, Py_ssize_t start, Py_ssize_t end) |
| 11284 | { |
| 11285 | unsigned char *data; |
| 11286 | int kind; |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11287 | Py_ssize_t length; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11288 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 11289 | if (PyUnicode_READY(self) == -1) |
| 11290 | return NULL; |
| 11291 | |
| 11292 | end = Py_MIN(end, PyUnicode_GET_LENGTH(self)); |
| 11293 | |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11294 | if (start == 0 && end == PyUnicode_GET_LENGTH(self)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11295 | { |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11296 | if (PyUnicode_CheckExact(self)) { |
| 11297 | Py_INCREF(self); |
| 11298 | return self; |
| 11299 | } |
| 11300 | else |
| 11301 | return PyUnicode_Copy(self); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11302 | } |
| 11303 | |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11304 | length = end - start; |
| 11305 | if (length == 1) |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 11306 | return unicode_getitem(self, start); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11307 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 11308 | if (start < 0 || end < 0) { |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11309 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 11310 | return NULL; |
| 11311 | } |
| 11312 | |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11313 | if (PyUnicode_IS_ASCII(self)) { |
| 11314 | kind = PyUnicode_KIND(self); |
| 11315 | data = PyUnicode_1BYTE_DATA(self); |
| 11316 | return unicode_fromascii(data + start, length); |
| 11317 | } |
| 11318 | else { |
| 11319 | kind = PyUnicode_KIND(self); |
| 11320 | data = PyUnicode_1BYTE_DATA(self); |
| 11321 | return PyUnicode_FromKindAndData(kind, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 11322 | data + kind * start, |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11323 | length); |
| 11324 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11325 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11326 | |
| 11327 | static PyObject * |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11328 | do_strip(PyUnicodeObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11329 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11330 | int kind; |
| 11331 | void *data; |
| 11332 | Py_ssize_t len, i, j; |
| 11333 | |
| 11334 | if (PyUnicode_READY(self) == -1) |
| 11335 | return NULL; |
| 11336 | |
| 11337 | kind = PyUnicode_KIND(self); |
| 11338 | data = PyUnicode_DATA(self); |
| 11339 | len = PyUnicode_GET_LENGTH(self); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11340 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11341 | i = 0; |
| 11342 | if (striptype != RIGHTSTRIP) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11343 | while (i < len && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, i))) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11344 | i++; |
| 11345 | } |
| 11346 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11347 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11348 | j = len; |
| 11349 | if (striptype != LEFTSTRIP) { |
| 11350 | do { |
| 11351 | j--; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11352 | } while (j >= i && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, j))); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11353 | j++; |
| 11354 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11355 | |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11356 | return PyUnicode_Substring((PyObject*)self, i, j); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11357 | } |
| 11358 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11359 | |
| 11360 | static PyObject * |
| 11361 | do_argstrip(PyUnicodeObject *self, int striptype, PyObject *args) |
| 11362 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11363 | PyObject *sep = NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11364 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11365 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
| 11366 | return NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11367 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11368 | if (sep != NULL && sep != Py_None) { |
| 11369 | if (PyUnicode_Check(sep)) |
| 11370 | return _PyUnicode_XStrip(self, striptype, sep); |
| 11371 | else { |
| 11372 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11373 | "%s arg must be None or str", |
| 11374 | STRIPNAME(striptype)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11375 | return NULL; |
| 11376 | } |
| 11377 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11378 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11379 | return do_strip(self, striptype); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11380 | } |
| 11381 | |
| 11382 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11383 | PyDoc_STRVAR(strip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11384 | "S.strip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11385 | \n\ |
| 11386 | Return a copy of the string S with leading and trailing\n\ |
| 11387 | whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11388 | 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] | 11389 | |
| 11390 | static PyObject * |
| 11391 | unicode_strip(PyUnicodeObject *self, PyObject *args) |
| 11392 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11393 | if (PyTuple_GET_SIZE(args) == 0) |
| 11394 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 11395 | else |
| 11396 | return do_argstrip(self, BOTHSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11397 | } |
| 11398 | |
| 11399 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11400 | PyDoc_STRVAR(lstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11401 | "S.lstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11402 | \n\ |
| 11403 | Return a copy of the string S with leading whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11404 | 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] | 11405 | |
| 11406 | static PyObject * |
| 11407 | unicode_lstrip(PyUnicodeObject *self, PyObject *args) |
| 11408 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11409 | if (PyTuple_GET_SIZE(args) == 0) |
| 11410 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 11411 | else |
| 11412 | return do_argstrip(self, LEFTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11413 | } |
| 11414 | |
| 11415 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11416 | PyDoc_STRVAR(rstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11417 | "S.rstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11418 | \n\ |
| 11419 | Return a copy of the string S with trailing whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11420 | 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] | 11421 | |
| 11422 | static PyObject * |
| 11423 | unicode_rstrip(PyUnicodeObject *self, PyObject *args) |
| 11424 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11425 | if (PyTuple_GET_SIZE(args) == 0) |
| 11426 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 11427 | else |
| 11428 | return do_argstrip(self, RIGHTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11429 | } |
| 11430 | |
| 11431 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11432 | static PyObject* |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11433 | unicode_repeat(PyUnicodeObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11434 | { |
| 11435 | PyUnicodeObject *u; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11436 | Py_ssize_t nchars, n; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11437 | |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 11438 | if (len < 1) { |
| 11439 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 11440 | return unicode_empty; |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 11441 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11442 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 11443 | if (len == 1 && PyUnicode_CheckExact(str)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11444 | /* no repeat, return original string */ |
| 11445 | Py_INCREF(str); |
| 11446 | return (PyObject*) str; |
| 11447 | } |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 11448 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11449 | if (PyUnicode_READY(str) == -1) |
| 11450 | return NULL; |
| 11451 | |
Victor Stinner | c759f3e | 2011-10-01 03:09:58 +0200 | [diff] [blame] | 11452 | if (PyUnicode_GET_LENGTH(str) > PY_SSIZE_T_MAX / len) { |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11453 | PyErr_SetString(PyExc_OverflowError, |
| 11454 | "repeated string is too long"); |
| 11455 | return NULL; |
| 11456 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11457 | nchars = len * PyUnicode_GET_LENGTH(str); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11458 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11459 | u = (PyUnicodeObject *)PyUnicode_New(nchars, PyUnicode_MAX_CHAR_VALUE(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11460 | if (!u) |
| 11461 | return NULL; |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11462 | assert(PyUnicode_KIND(u) == PyUnicode_KIND(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11463 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11464 | if (PyUnicode_GET_LENGTH(str) == 1) { |
| 11465 | const int kind = PyUnicode_KIND(str); |
| 11466 | const Py_UCS4 fill_char = PyUnicode_READ(kind, PyUnicode_DATA(str), 0); |
| 11467 | void *to = PyUnicode_DATA(u); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11468 | if (kind == PyUnicode_1BYTE_KIND) |
| 11469 | memset(to, (unsigned char)fill_char, len); |
| 11470 | else { |
| 11471 | for (n = 0; n < len; ++n) |
| 11472 | PyUnicode_WRITE(kind, to, n, fill_char); |
| 11473 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11474 | } |
| 11475 | else { |
| 11476 | /* number of characters copied this far */ |
| 11477 | Py_ssize_t done = PyUnicode_GET_LENGTH(str); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 11478 | const Py_ssize_t char_size = PyUnicode_KIND(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11479 | char *to = (char *) PyUnicode_DATA(u); |
| 11480 | Py_MEMCPY(to, PyUnicode_DATA(str), |
| 11481 | PyUnicode_GET_LENGTH(str) * char_size); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11482 | while (done < nchars) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11483 | n = (done <= nchars-done) ? done : nchars-done; |
| 11484 | Py_MEMCPY(to + (done * char_size), to, n * char_size); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11485 | done += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11486 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11487 | } |
| 11488 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 11489 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11490 | return (PyObject*) u; |
| 11491 | } |
| 11492 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11493 | PyObject * |
| 11494 | PyUnicode_Replace(PyObject *obj, |
| 11495 | PyObject *subobj, |
| 11496 | PyObject *replobj, |
| 11497 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11498 | { |
| 11499 | PyObject *self; |
| 11500 | PyObject *str1; |
| 11501 | PyObject *str2; |
| 11502 | PyObject *result; |
| 11503 | |
| 11504 | self = PyUnicode_FromObject(obj); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11505 | if (self == NULL || PyUnicode_READY(self) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11506 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11507 | str1 = PyUnicode_FromObject(subobj); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11508 | if (str1 == NULL || PyUnicode_READY(str1) == -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11509 | Py_DECREF(self); |
| 11510 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11511 | } |
| 11512 | str2 = PyUnicode_FromObject(replobj); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11513 | if (str2 == NULL || PyUnicode_READY(str2)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11514 | Py_DECREF(self); |
| 11515 | Py_DECREF(str1); |
| 11516 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11517 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11518 | result = replace(self, str1, str2, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11519 | Py_DECREF(self); |
| 11520 | Py_DECREF(str1); |
| 11521 | Py_DECREF(str2); |
| 11522 | return result; |
| 11523 | } |
| 11524 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11525 | PyDoc_STRVAR(replace__doc__, |
Ezio Melotti | c1897e7 | 2010-06-26 18:50:39 +0000 | [diff] [blame] | 11526 | "S.replace(old, new[, count]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11527 | \n\ |
| 11528 | Return a copy of S with all occurrences of substring\n\ |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 11529 | old replaced by new. If the optional argument count is\n\ |
| 11530 | given, only the first count occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11531 | |
| 11532 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11533 | unicode_replace(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11534 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11535 | PyObject *str1; |
| 11536 | PyObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11537 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11538 | PyObject *result; |
| 11539 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11540 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11541 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11542 | if (!PyUnicode_READY(self) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11543 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11544 | str1 = PyUnicode_FromObject(str1); |
| 11545 | if (str1 == NULL || PyUnicode_READY(str1) == -1) |
| 11546 | return NULL; |
| 11547 | str2 = PyUnicode_FromObject(str2); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11548 | if (str2 == NULL || PyUnicode_READY(str2) == -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11549 | Py_DECREF(str1); |
| 11550 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 11551 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11552 | |
| 11553 | result = replace(self, str1, str2, maxcount); |
| 11554 | |
| 11555 | Py_DECREF(str1); |
| 11556 | Py_DECREF(str2); |
| 11557 | return result; |
| 11558 | } |
| 11559 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11560 | static PyObject * |
| 11561 | unicode_repr(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11562 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11563 | PyObject *repr; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11564 | Py_ssize_t isize; |
| 11565 | Py_ssize_t osize, squote, dquote, i, o; |
| 11566 | Py_UCS4 max, quote; |
| 11567 | int ikind, okind; |
| 11568 | void *idata, *odata; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11569 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11570 | if (PyUnicode_READY(unicode) == -1) |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11571 | return NULL; |
| 11572 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11573 | isize = PyUnicode_GET_LENGTH(unicode); |
| 11574 | idata = PyUnicode_DATA(unicode); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11575 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11576 | /* Compute length of output, quote characters, and |
| 11577 | maximum character */ |
| 11578 | osize = 2; /* quotes */ |
| 11579 | max = 127; |
| 11580 | squote = dquote = 0; |
| 11581 | ikind = PyUnicode_KIND(unicode); |
| 11582 | for (i = 0; i < isize; i++) { |
| 11583 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
| 11584 | switch (ch) { |
| 11585 | case '\'': squote++; osize++; break; |
| 11586 | case '"': dquote++; osize++; break; |
| 11587 | case '\\': case '\t': case '\r': case '\n': |
| 11588 | osize += 2; break; |
| 11589 | default: |
| 11590 | /* Fast-path ASCII */ |
| 11591 | if (ch < ' ' || ch == 0x7f) |
| 11592 | osize += 4; /* \xHH */ |
| 11593 | else if (ch < 0x7f) |
| 11594 | osize++; |
| 11595 | else if (Py_UNICODE_ISPRINTABLE(ch)) { |
| 11596 | osize++; |
| 11597 | max = ch > max ? ch : max; |
| 11598 | } |
| 11599 | else if (ch < 0x100) |
| 11600 | osize += 4; /* \xHH */ |
| 11601 | else if (ch < 0x10000) |
| 11602 | osize += 6; /* \uHHHH */ |
| 11603 | else |
| 11604 | osize += 10; /* \uHHHHHHHH */ |
| 11605 | } |
| 11606 | } |
| 11607 | |
| 11608 | quote = '\''; |
| 11609 | if (squote) { |
| 11610 | if (dquote) |
| 11611 | /* Both squote and dquote present. Use squote, |
| 11612 | and escape them */ |
| 11613 | osize += squote; |
| 11614 | else |
| 11615 | quote = '"'; |
| 11616 | } |
| 11617 | |
| 11618 | repr = PyUnicode_New(osize, max); |
| 11619 | if (repr == NULL) |
| 11620 | return NULL; |
| 11621 | okind = PyUnicode_KIND(repr); |
| 11622 | odata = PyUnicode_DATA(repr); |
| 11623 | |
| 11624 | PyUnicode_WRITE(okind, odata, 0, quote); |
| 11625 | PyUnicode_WRITE(okind, odata, osize-1, quote); |
| 11626 | |
| 11627 | for (i = 0, o = 1; i < isize; i++) { |
| 11628 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11629 | |
| 11630 | /* Escape quotes and backslashes */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11631 | if ((ch == quote) || (ch == '\\')) { |
| 11632 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11633 | PyUnicode_WRITE(okind, odata, o++, ch); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11634 | continue; |
| 11635 | } |
| 11636 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11637 | /* Map special whitespace to '\t', \n', '\r' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11638 | if (ch == '\t') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11639 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11640 | PyUnicode_WRITE(okind, odata, o++, 't'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11641 | } |
| 11642 | else if (ch == '\n') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11643 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11644 | PyUnicode_WRITE(okind, odata, o++, 'n'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11645 | } |
| 11646 | else if (ch == '\r') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11647 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11648 | PyUnicode_WRITE(okind, odata, o++, 'r'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11649 | } |
| 11650 | |
| 11651 | /* Map non-printable US ASCII to '\xhh' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11652 | else if (ch < ' ' || ch == 0x7F) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11653 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11654 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
| 11655 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 4) & 0x000F]); |
| 11656 | PyUnicode_WRITE(okind, odata, o++, hexdigits[ch & 0x000F]); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11657 | } |
| 11658 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11659 | /* Copy ASCII characters as-is */ |
| 11660 | else if (ch < 0x7F) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11661 | PyUnicode_WRITE(okind, odata, o++, ch); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11662 | } |
| 11663 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11664 | /* Non-ASCII characters */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11665 | else { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11666 | /* Map Unicode whitespace and control characters |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11667 | (categories Z* and C* except ASCII space) |
| 11668 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11669 | if (!Py_UNICODE_ISPRINTABLE(ch)) { |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11670 | /* Map 8-bit characters to '\xhh' */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11671 | if (ch <= 0xff) { |
| 11672 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11673 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
| 11674 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 4) & 0x000F]); |
| 11675 | PyUnicode_WRITE(okind, odata, o++, hexdigits[ch & 0x000F]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11676 | } |
| 11677 | /* Map 21-bit characters to '\U00xxxxxx' */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11678 | else if (ch >= 0x10000) { |
| 11679 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11680 | PyUnicode_WRITE(okind, odata, o++, 'U'); |
| 11681 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 28) & 0xF]); |
| 11682 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 24) & 0xF]); |
| 11683 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 20) & 0xF]); |
| 11684 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 16) & 0xF]); |
| 11685 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 12) & 0xF]); |
| 11686 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 8) & 0xF]); |
| 11687 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 4) & 0xF]); |
| 11688 | PyUnicode_WRITE(okind, odata, o++, hexdigits[ch & 0xF]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11689 | } |
| 11690 | /* Map 16-bit characters to '\uxxxx' */ |
| 11691 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11692 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11693 | PyUnicode_WRITE(okind, odata, o++, 'u'); |
| 11694 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 12) & 0xF]); |
| 11695 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 8) & 0xF]); |
| 11696 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 4) & 0xF]); |
| 11697 | PyUnicode_WRITE(okind, odata, o++, hexdigits[ch & 0xF]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11698 | } |
| 11699 | } |
| 11700 | /* Copy characters as-is */ |
| 11701 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11702 | PyUnicode_WRITE(okind, odata, o++, ch); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11703 | } |
| 11704 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11705 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11706 | /* Closing quote already added at the beginning */ |
Victor Stinner | 05d1189 | 2011-10-06 01:13:58 +0200 | [diff] [blame] | 11707 | assert(_PyUnicode_CheckConsistency(repr, 1)); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11708 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11709 | } |
| 11710 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11711 | PyDoc_STRVAR(rfind__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11712 | "S.rfind(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11713 | \n\ |
| 11714 | Return the highest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 11715 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11716 | arguments start and end are interpreted as in slice notation.\n\ |
| 11717 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11718 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11719 | |
| 11720 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11721 | unicode_rfind(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11722 | { |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11723 | PyUnicodeObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 11724 | Py_ssize_t start; |
| 11725 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11726 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11727 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11728 | if (!stringlib_parse_args_finds_unicode("rfind", args, &substring, |
| 11729 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11730 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11731 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11732 | if (PyUnicode_READY(self) == -1) |
| 11733 | return NULL; |
| 11734 | if (PyUnicode_READY(substring) == -1) |
| 11735 | return NULL; |
| 11736 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 11737 | result = any_find_slice(-1, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11738 | self, (PyObject*)substring, start, end |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11739 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11740 | |
| 11741 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11742 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11743 | if (result == -2) |
| 11744 | return NULL; |
| 11745 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11746 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11747 | } |
| 11748 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11749 | PyDoc_STRVAR(rindex__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11750 | "S.rindex(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11751 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11752 | 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] | 11753 | |
| 11754 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11755 | unicode_rindex(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11756 | { |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11757 | PyUnicodeObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 11758 | Py_ssize_t start; |
| 11759 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11760 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11761 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11762 | if (!stringlib_parse_args_finds_unicode("rindex", args, &substring, |
| 11763 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11764 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11765 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11766 | if (PyUnicode_READY(self) == -1) |
| 11767 | return NULL; |
| 11768 | if (PyUnicode_READY(substring) == -1) |
| 11769 | return NULL; |
| 11770 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 11771 | result = any_find_slice(-1, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11772 | self, (PyObject*)substring, start, end |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11773 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11774 | |
| 11775 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11776 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11777 | if (result == -2) |
| 11778 | return NULL; |
| 11779 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11780 | if (result < 0) { |
| 11781 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 11782 | return NULL; |
| 11783 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11784 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11785 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11786 | } |
| 11787 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11788 | PyDoc_STRVAR(rjust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11789 | "S.rjust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11790 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 11791 | 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] | 11792 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11793 | |
| 11794 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11795 | unicode_rjust(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11796 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11797 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11798 | Py_UCS4 fillchar = ' '; |
| 11799 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11800 | 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] | 11801 | return NULL; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 11802 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11803 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11804 | return NULL; |
| 11805 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11806 | if (_PyUnicode_LENGTH(self) >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11807 | Py_INCREF(self); |
| 11808 | return (PyObject*) self; |
| 11809 | } |
| 11810 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11811 | return (PyObject*) pad(self, width - _PyUnicode_LENGTH(self), 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11812 | } |
| 11813 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11814 | PyObject * |
| 11815 | PyUnicode_Split(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11816 | { |
| 11817 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11818 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11819 | s = PyUnicode_FromObject(s); |
| 11820 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11821 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11822 | if (sep != NULL) { |
| 11823 | sep = PyUnicode_FromObject(sep); |
| 11824 | if (sep == NULL) { |
| 11825 | Py_DECREF(s); |
| 11826 | return NULL; |
| 11827 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11828 | } |
| 11829 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11830 | result = split(s, sep, maxsplit); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11831 | |
| 11832 | Py_DECREF(s); |
| 11833 | Py_XDECREF(sep); |
| 11834 | return result; |
| 11835 | } |
| 11836 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11837 | PyDoc_STRVAR(split__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11838 | "S.split([sep[, maxsplit]]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11839 | \n\ |
| 11840 | Return a list of the words in S, using sep as the\n\ |
| 11841 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 11842 | 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] | 11843 | whitespace string is a separator and empty strings are\n\ |
| 11844 | removed from the result."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11845 | |
| 11846 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11847 | unicode_split(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11848 | { |
| 11849 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11850 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11851 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11852 | if (!PyArg_ParseTuple(args, "|On:split", &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11853 | return NULL; |
| 11854 | |
| 11855 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11856 | return split(self, NULL, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11857 | else if (PyUnicode_Check(substring)) |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11858 | return split(self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11859 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11860 | return PyUnicode_Split((PyObject *)self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11861 | } |
| 11862 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11863 | PyObject * |
| 11864 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 11865 | { |
| 11866 | PyObject* str_obj; |
| 11867 | PyObject* sep_obj; |
| 11868 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11869 | int kind1, kind2, kind; |
| 11870 | void *buf1 = NULL, *buf2 = NULL; |
| 11871 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11872 | |
| 11873 | str_obj = PyUnicode_FromObject(str_in); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11874 | if (!str_obj || PyUnicode_READY(str_obj) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11875 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11876 | sep_obj = PyUnicode_FromObject(sep_in); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11877 | if (!sep_obj || PyUnicode_READY(sep_obj) == -1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11878 | Py_DECREF(str_obj); |
| 11879 | return NULL; |
| 11880 | } |
| 11881 | |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 11882 | kind1 = PyUnicode_KIND(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11883 | kind2 = PyUnicode_KIND(sep_obj); |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 11884 | kind = Py_MAX(kind1, kind2); |
| 11885 | buf1 = PyUnicode_DATA(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11886 | if (kind1 != kind) |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 11887 | buf1 = _PyUnicode_AsKind(str_obj, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11888 | if (!buf1) |
| 11889 | goto onError; |
| 11890 | buf2 = PyUnicode_DATA(sep_obj); |
| 11891 | if (kind2 != kind) |
| 11892 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 11893 | if (!buf2) |
| 11894 | goto onError; |
| 11895 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 11896 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 11897 | |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 11898 | switch(PyUnicode_KIND(str_obj)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11899 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 11900 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) |
| 11901 | out = asciilib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 11902 | else |
| 11903 | 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] | 11904 | break; |
| 11905 | case PyUnicode_2BYTE_KIND: |
| 11906 | out = ucs2lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 11907 | break; |
| 11908 | case PyUnicode_4BYTE_KIND: |
| 11909 | out = ucs4lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 11910 | break; |
| 11911 | default: |
| 11912 | assert(0); |
| 11913 | out = 0; |
| 11914 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11915 | |
| 11916 | Py_DECREF(sep_obj); |
| 11917 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11918 | if (kind1 != kind) |
| 11919 | PyMem_Free(buf1); |
| 11920 | if (kind2 != kind) |
| 11921 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11922 | |
| 11923 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11924 | onError: |
| 11925 | Py_DECREF(sep_obj); |
| 11926 | Py_DECREF(str_obj); |
| 11927 | if (kind1 != kind && buf1) |
| 11928 | PyMem_Free(buf1); |
| 11929 | if (kind2 != kind && buf2) |
| 11930 | PyMem_Free(buf2); |
| 11931 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11932 | } |
| 11933 | |
| 11934 | |
| 11935 | PyObject * |
| 11936 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 11937 | { |
| 11938 | PyObject* str_obj; |
| 11939 | PyObject* sep_obj; |
| 11940 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11941 | int kind1, kind2, kind; |
| 11942 | void *buf1 = NULL, *buf2 = NULL; |
| 11943 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11944 | |
| 11945 | str_obj = PyUnicode_FromObject(str_in); |
| 11946 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11947 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11948 | sep_obj = PyUnicode_FromObject(sep_in); |
| 11949 | if (!sep_obj) { |
| 11950 | Py_DECREF(str_obj); |
| 11951 | return NULL; |
| 11952 | } |
| 11953 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11954 | kind1 = PyUnicode_KIND(str_in); |
| 11955 | kind2 = PyUnicode_KIND(sep_obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 11956 | kind = Py_MAX(kind1, kind2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11957 | buf1 = PyUnicode_DATA(str_in); |
| 11958 | if (kind1 != kind) |
| 11959 | buf1 = _PyUnicode_AsKind(str_in, kind); |
| 11960 | if (!buf1) |
| 11961 | goto onError; |
| 11962 | buf2 = PyUnicode_DATA(sep_obj); |
| 11963 | if (kind2 != kind) |
| 11964 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 11965 | if (!buf2) |
| 11966 | goto onError; |
| 11967 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 11968 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 11969 | |
| 11970 | switch(PyUnicode_KIND(str_in)) { |
| 11971 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 11972 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) |
| 11973 | out = asciilib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 11974 | else |
| 11975 | 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] | 11976 | break; |
| 11977 | case PyUnicode_2BYTE_KIND: |
| 11978 | out = ucs2lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 11979 | break; |
| 11980 | case PyUnicode_4BYTE_KIND: |
| 11981 | out = ucs4lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 11982 | break; |
| 11983 | default: |
| 11984 | assert(0); |
| 11985 | out = 0; |
| 11986 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11987 | |
| 11988 | Py_DECREF(sep_obj); |
| 11989 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11990 | if (kind1 != kind) |
| 11991 | PyMem_Free(buf1); |
| 11992 | if (kind2 != kind) |
| 11993 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11994 | |
| 11995 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11996 | onError: |
| 11997 | Py_DECREF(sep_obj); |
| 11998 | Py_DECREF(str_obj); |
| 11999 | if (kind1 != kind && buf1) |
| 12000 | PyMem_Free(buf1); |
| 12001 | if (kind2 != kind && buf2) |
| 12002 | PyMem_Free(buf2); |
| 12003 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12004 | } |
| 12005 | |
| 12006 | PyDoc_STRVAR(partition__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12007 | "S.partition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12008 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 12009 | 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] | 12010 | 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] | 12011 | found, return S and two empty strings."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12012 | |
| 12013 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12014 | unicode_partition(PyObject *self, PyObject *separator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12015 | { |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12016 | return PyUnicode_Partition(self, separator); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12017 | } |
| 12018 | |
| 12019 | PyDoc_STRVAR(rpartition__doc__, |
Ezio Melotti | 5b2b242 | 2010-01-25 11:58:28 +0000 | [diff] [blame] | 12020 | "S.rpartition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12021 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 12022 | 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] | 12023 | 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] | 12024 | separator is not found, return two empty strings and S."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12025 | |
| 12026 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12027 | unicode_rpartition(PyObject *self, PyObject *separator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12028 | { |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12029 | return PyUnicode_RPartition(self, separator); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12030 | } |
| 12031 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12032 | PyObject * |
| 12033 | PyUnicode_RSplit(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12034 | { |
| 12035 | PyObject *result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12036 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12037 | s = PyUnicode_FromObject(s); |
| 12038 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12039 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12040 | if (sep != NULL) { |
| 12041 | sep = PyUnicode_FromObject(sep); |
| 12042 | if (sep == NULL) { |
| 12043 | Py_DECREF(s); |
| 12044 | return NULL; |
| 12045 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12046 | } |
| 12047 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12048 | result = rsplit(s, sep, maxsplit); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12049 | |
| 12050 | Py_DECREF(s); |
| 12051 | Py_XDECREF(sep); |
| 12052 | return result; |
| 12053 | } |
| 12054 | |
| 12055 | PyDoc_STRVAR(rsplit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12056 | "S.rsplit([sep[, maxsplit]]) -> list of strings\n\ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12057 | \n\ |
| 12058 | Return a list of the words in S, using sep as the\n\ |
| 12059 | delimiter string, starting at the end of the string and\n\ |
| 12060 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 12061 | splits are done. If sep is not specified, any whitespace string\n\ |
| 12062 | is a separator."); |
| 12063 | |
| 12064 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12065 | unicode_rsplit(PyObject *self, PyObject *args) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12066 | { |
| 12067 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12068 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12069 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12070 | if (!PyArg_ParseTuple(args, "|On:rsplit", &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12071 | return NULL; |
| 12072 | |
| 12073 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12074 | return rsplit(self, NULL, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12075 | else if (PyUnicode_Check(substring)) |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12076 | return rsplit(self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12077 | else |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12078 | return PyUnicode_RSplit(self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12079 | } |
| 12080 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12081 | PyDoc_STRVAR(splitlines__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12082 | "S.splitlines([keepends]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12083 | \n\ |
| 12084 | 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] | 12085 | 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] | 12086 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12087 | |
| 12088 | static PyObject* |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12089 | unicode_splitlines(PyUnicodeObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12090 | { |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12091 | static char *kwlist[] = {"keepends", 0}; |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 12092 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12093 | |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12094 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:splitlines", |
| 12095 | kwlist, &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12096 | return NULL; |
| 12097 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 12098 | return PyUnicode_Splitlines((PyObject *)self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12099 | } |
| 12100 | |
| 12101 | static |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 12102 | PyObject *unicode_str(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12103 | { |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 12104 | if (PyUnicode_CheckExact(self)) { |
| 12105 | Py_INCREF(self); |
| 12106 | return self; |
| 12107 | } else |
| 12108 | /* Subtype -- return genuine unicode string with the same value. */ |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 12109 | return PyUnicode_Copy(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12110 | } |
| 12111 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12112 | PyDoc_STRVAR(swapcase__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12113 | "S.swapcase() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12114 | \n\ |
| 12115 | 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] | 12116 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12117 | |
| 12118 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12119 | unicode_swapcase(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12120 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12121 | return fixup(self, fixswapcase); |
| 12122 | } |
| 12123 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12124 | PyDoc_STRVAR(maketrans__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12125 | "str.maketrans(x[, y[, z]]) -> dict (static method)\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12126 | \n\ |
| 12127 | Return a translation table usable for str.translate().\n\ |
| 12128 | If there is only one argument, it must be a dictionary mapping Unicode\n\ |
| 12129 | ordinals (integers) or characters to Unicode ordinals, strings or None.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12130 | Character keys will be then converted to ordinals.\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12131 | If there are two arguments, they must be strings of equal length, and\n\ |
| 12132 | in the resulting dictionary, each character in x will be mapped to the\n\ |
| 12133 | character at the same position in y. If there is a third argument, it\n\ |
| 12134 | must be a string, whose characters will be mapped to None in the result."); |
| 12135 | |
| 12136 | static PyObject* |
| 12137 | unicode_maketrans(PyUnicodeObject *null, PyObject *args) |
| 12138 | { |
| 12139 | PyObject *x, *y = NULL, *z = NULL; |
| 12140 | PyObject *new = NULL, *key, *value; |
| 12141 | Py_ssize_t i = 0; |
| 12142 | int res; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12143 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12144 | if (!PyArg_ParseTuple(args, "O|UU:maketrans", &x, &y, &z)) |
| 12145 | return NULL; |
| 12146 | new = PyDict_New(); |
| 12147 | if (!new) |
| 12148 | return NULL; |
| 12149 | if (y != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12150 | int x_kind, y_kind, z_kind; |
| 12151 | void *x_data, *y_data, *z_data; |
| 12152 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12153 | /* x must be a string too, of equal length */ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12154 | if (!PyUnicode_Check(x)) { |
| 12155 | PyErr_SetString(PyExc_TypeError, "first maketrans argument must " |
| 12156 | "be a string if there is a second argument"); |
| 12157 | goto err; |
| 12158 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12159 | if (PyUnicode_GET_LENGTH(x) != PyUnicode_GET_LENGTH(y)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12160 | PyErr_SetString(PyExc_ValueError, "the first two maketrans " |
| 12161 | "arguments must have equal length"); |
| 12162 | goto err; |
| 12163 | } |
| 12164 | /* 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] | 12165 | x_kind = PyUnicode_KIND(x); |
| 12166 | y_kind = PyUnicode_KIND(y); |
| 12167 | x_data = PyUnicode_DATA(x); |
| 12168 | y_data = PyUnicode_DATA(y); |
| 12169 | for (i = 0; i < PyUnicode_GET_LENGTH(x); i++) { |
| 12170 | key = PyLong_FromLong(PyUnicode_READ(x_kind, x_data, i)); |
| 12171 | value = PyLong_FromLong(PyUnicode_READ(y_kind, y_data, i)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12172 | if (!key || !value) |
| 12173 | goto err; |
| 12174 | res = PyDict_SetItem(new, key, value); |
| 12175 | Py_DECREF(key); |
| 12176 | Py_DECREF(value); |
| 12177 | if (res < 0) |
| 12178 | goto err; |
| 12179 | } |
| 12180 | /* create entries for deleting chars in z */ |
| 12181 | if (z != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12182 | z_kind = PyUnicode_KIND(z); |
| 12183 | z_data = PyUnicode_DATA(z); |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 12184 | for (i = 0; i < PyUnicode_GET_LENGTH(z); i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12185 | key = PyLong_FromLong(PyUnicode_READ(z_kind, z_data, i)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12186 | if (!key) |
| 12187 | goto err; |
| 12188 | res = PyDict_SetItem(new, key, Py_None); |
| 12189 | Py_DECREF(key); |
| 12190 | if (res < 0) |
| 12191 | goto err; |
| 12192 | } |
| 12193 | } |
| 12194 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12195 | int kind; |
| 12196 | void *data; |
| 12197 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12198 | /* x must be a dict */ |
Raymond Hettinger | 3ad0576 | 2009-05-29 22:11:22 +0000 | [diff] [blame] | 12199 | if (!PyDict_CheckExact(x)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12200 | PyErr_SetString(PyExc_TypeError, "if you give only one argument " |
| 12201 | "to maketrans it must be a dict"); |
| 12202 | goto err; |
| 12203 | } |
| 12204 | /* copy entries into the new dict, converting string keys to int keys */ |
| 12205 | while (PyDict_Next(x, &i, &key, &value)) { |
| 12206 | if (PyUnicode_Check(key)) { |
| 12207 | /* convert string keys to integer keys */ |
| 12208 | PyObject *newkey; |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 12209 | if (PyUnicode_GET_LENGTH(key) != 1) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12210 | PyErr_SetString(PyExc_ValueError, "string keys in translate " |
| 12211 | "table must be of length 1"); |
| 12212 | goto err; |
| 12213 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12214 | kind = PyUnicode_KIND(key); |
| 12215 | data = PyUnicode_DATA(key); |
| 12216 | newkey = PyLong_FromLong(PyUnicode_READ(kind, data, 0)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12217 | if (!newkey) |
| 12218 | goto err; |
| 12219 | res = PyDict_SetItem(new, newkey, value); |
| 12220 | Py_DECREF(newkey); |
| 12221 | if (res < 0) |
| 12222 | goto err; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12223 | } else if (PyLong_Check(key)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12224 | /* just keep integer keys */ |
| 12225 | if (PyDict_SetItem(new, key, value) < 0) |
| 12226 | goto err; |
| 12227 | } else { |
| 12228 | PyErr_SetString(PyExc_TypeError, "keys in translate table must " |
| 12229 | "be strings or integers"); |
| 12230 | goto err; |
| 12231 | } |
| 12232 | } |
| 12233 | } |
| 12234 | return new; |
| 12235 | err: |
| 12236 | Py_DECREF(new); |
| 12237 | return NULL; |
| 12238 | } |
| 12239 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12240 | PyDoc_STRVAR(translate__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12241 | "S.translate(table) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12242 | \n\ |
| 12243 | Return a copy of the string S, where all characters have been mapped\n\ |
| 12244 | through the given translation table, which must be a mapping of\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12245 | Unicode ordinals to Unicode ordinals, strings, or None.\n\ |
Walter Dörwald | 5c1ee17 | 2002-09-04 20:31:32 +0000 | [diff] [blame] | 12246 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 12247 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12248 | |
| 12249 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12250 | unicode_translate(PyObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12251 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12252 | return _PyUnicode_TranslateCharmap(self, table, "ignore"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12253 | } |
| 12254 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12255 | PyDoc_STRVAR(upper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12256 | "S.upper() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12257 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12258 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12259 | |
| 12260 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12261 | unicode_upper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12262 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12263 | return fixup(self, fixupper); |
| 12264 | } |
| 12265 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12266 | PyDoc_STRVAR(zfill__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12267 | "S.zfill(width) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12268 | \n\ |
Benjamin Peterson | 9aa4299 | 2008-09-10 21:57:34 +0000 | [diff] [blame] | 12269 | Pad a numeric string S with zeros on the left, to fill a field\n\ |
| 12270 | of the specified width. The string S is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12271 | |
| 12272 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12273 | unicode_zfill(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12274 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12275 | Py_ssize_t fill; |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12276 | PyObject *u; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12277 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12278 | int kind; |
| 12279 | void *data; |
| 12280 | Py_UCS4 chr; |
| 12281 | |
| 12282 | if (PyUnicode_READY(self) == -1) |
| 12283 | return NULL; |
| 12284 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12285 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12286 | return NULL; |
| 12287 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12288 | if (PyUnicode_GET_LENGTH(self) >= width) { |
Walter Dörwald | 0fe940c | 2002-04-15 18:42:15 +0000 | [diff] [blame] | 12289 | if (PyUnicode_CheckExact(self)) { |
| 12290 | Py_INCREF(self); |
| 12291 | return (PyObject*) self; |
| 12292 | } |
| 12293 | else |
Victor Stinner | 2219e0a | 2011-10-01 01:16:59 +0200 | [diff] [blame] | 12294 | return PyUnicode_Copy((PyObject*)self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12295 | } |
| 12296 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12297 | fill = width - _PyUnicode_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12298 | |
| 12299 | u = pad(self, fill, 0, '0'); |
| 12300 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 12301 | if (u == NULL) |
| 12302 | return NULL; |
| 12303 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12304 | kind = PyUnicode_KIND(u); |
| 12305 | data = PyUnicode_DATA(u); |
| 12306 | chr = PyUnicode_READ(kind, data, fill); |
| 12307 | |
| 12308 | if (chr == '+' || chr == '-') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12309 | /* move sign to beginning of string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12310 | PyUnicode_WRITE(kind, data, 0, chr); |
| 12311 | PyUnicode_WRITE(kind, data, fill, '0'); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12312 | } |
| 12313 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12314 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12315 | return (PyObject*) u; |
| 12316 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12317 | |
| 12318 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12319 | static PyObject * |
| 12320 | unicode__decimal2ascii(PyObject *self) |
| 12321 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12322 | return PyUnicode_TransformDecimalAndSpaceToASCII(self); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12323 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12324 | #endif |
| 12325 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12326 | PyDoc_STRVAR(startswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12327 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12328 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 12329 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 12330 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12331 | With optional end, stop comparing S at that position.\n\ |
| 12332 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12333 | |
| 12334 | static PyObject * |
| 12335 | unicode_startswith(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12336 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12337 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12338 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12339 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12340 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12341 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12342 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12343 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12344 | if (!stringlib_parse_args_finds("startswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12345 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12346 | if (PyTuple_Check(subobj)) { |
| 12347 | Py_ssize_t i; |
| 12348 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 12349 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12350 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12351 | if (substring == NULL) |
| 12352 | return NULL; |
| 12353 | result = tailmatch(self, substring, start, end, -1); |
| 12354 | Py_DECREF(substring); |
| 12355 | if (result) { |
| 12356 | Py_RETURN_TRUE; |
| 12357 | } |
| 12358 | } |
| 12359 | /* nothing matched */ |
| 12360 | Py_RETURN_FALSE; |
| 12361 | } |
| 12362 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12363 | if (substring == NULL) { |
| 12364 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 12365 | PyErr_Format(PyExc_TypeError, "startswith first arg must be str or " |
| 12366 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12367 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12368 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12369 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12370 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12371 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12372 | } |
| 12373 | |
| 12374 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12375 | PyDoc_STRVAR(endswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12376 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12377 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 12378 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 12379 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12380 | With optional end, stop comparing S at that position.\n\ |
| 12381 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12382 | |
| 12383 | static PyObject * |
| 12384 | unicode_endswith(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12385 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12386 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12387 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12388 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12389 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12390 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12391 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12392 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12393 | if (!stringlib_parse_args_finds("endswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12394 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12395 | if (PyTuple_Check(subobj)) { |
| 12396 | Py_ssize_t i; |
| 12397 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 12398 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12399 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12400 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12401 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12402 | result = tailmatch(self, substring, start, end, +1); |
| 12403 | Py_DECREF(substring); |
| 12404 | if (result) { |
| 12405 | Py_RETURN_TRUE; |
| 12406 | } |
| 12407 | } |
| 12408 | Py_RETURN_FALSE; |
| 12409 | } |
| 12410 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12411 | if (substring == NULL) { |
| 12412 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 12413 | PyErr_Format(PyExc_TypeError, "endswith first arg must be str or " |
| 12414 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12415 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12416 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12417 | result = tailmatch(self, substring, start, end, +1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12418 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12419 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12420 | } |
| 12421 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12422 | #include "stringlib/unicode_format.h" |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12423 | |
| 12424 | PyDoc_STRVAR(format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12425 | "S.format(*args, **kwargs) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12426 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 12427 | Return a formatted version of S, using substitutions from args and kwargs.\n\ |
| 12428 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12429 | |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 12430 | PyDoc_STRVAR(format_map__doc__, |
| 12431 | "S.format_map(mapping) -> str\n\ |
| 12432 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 12433 | Return a formatted version of S, using substitutions from mapping.\n\ |
| 12434 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 12435 | |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12436 | static PyObject * |
| 12437 | unicode__format__(PyObject* self, PyObject* args) |
| 12438 | { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12439 | PyObject *format_spec, *out; |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12440 | |
| 12441 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
| 12442 | return NULL; |
| 12443 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12444 | out = _PyUnicode_FormatAdvanced(self, format_spec, 0, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12445 | PyUnicode_GET_LENGTH(format_spec)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12446 | return out; |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12447 | } |
| 12448 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12449 | PyDoc_STRVAR(p_format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12450 | "S.__format__(format_spec) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12451 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 12452 | Return a formatted version of S as described by format_spec."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12453 | |
| 12454 | static PyObject * |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12455 | unicode__sizeof__(PyUnicodeObject *v) |
| 12456 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12457 | Py_ssize_t size; |
| 12458 | |
| 12459 | /* If it's a compact object, account for base structure + |
| 12460 | character data. */ |
| 12461 | if (PyUnicode_IS_COMPACT_ASCII(v)) |
| 12462 | size = sizeof(PyASCIIObject) + PyUnicode_GET_LENGTH(v) + 1; |
| 12463 | else if (PyUnicode_IS_COMPACT(v)) |
| 12464 | size = sizeof(PyCompactUnicodeObject) + |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 12465 | (PyUnicode_GET_LENGTH(v) + 1) * PyUnicode_KIND(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12466 | else { |
| 12467 | /* If it is a two-block object, account for base object, and |
| 12468 | for character block if present. */ |
| 12469 | size = sizeof(PyUnicodeObject); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 12470 | if (_PyUnicode_DATA_ANY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12471 | size += (PyUnicode_GET_LENGTH(v) + 1) * |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 12472 | PyUnicode_KIND(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12473 | } |
| 12474 | /* 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] | 12475 | with the data pointer. Check if the data is not shared. */ |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 12476 | if (_PyUnicode_HAS_WSTR_MEMORY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12477 | size += (PyUnicode_WSTR_LENGTH(v) + 1) * sizeof(wchar_t); |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 12478 | if (_PyUnicode_HAS_UTF8_MEMORY(v)) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 12479 | size += PyUnicode_UTF8_LENGTH(v) + 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12480 | |
| 12481 | return PyLong_FromSsize_t(size); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12482 | } |
| 12483 | |
| 12484 | PyDoc_STRVAR(sizeof__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12485 | "S.__sizeof__() -> size of S in memory, in bytes"); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12486 | |
| 12487 | static PyObject * |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 12488 | unicode_getnewargs(PyObject *v) |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 12489 | { |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 12490 | PyObject *copy = PyUnicode_Copy(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12491 | if (!copy) |
| 12492 | return NULL; |
| 12493 | return Py_BuildValue("(N)", copy); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 12494 | } |
| 12495 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12496 | static PyMethodDef unicode_methods[] = { |
| 12497 | |
| 12498 | /* Order is according to common usage: often used methods should |
| 12499 | appear first, since lookup is done sequentially. */ |
| 12500 | |
Benjamin Peterson | 28a4dce | 2010-12-12 01:33:04 +0000 | [diff] [blame] | 12501 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS | METH_KEYWORDS, encode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12502 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
| 12503 | {"split", (PyCFunction) unicode_split, METH_VARARGS, split__doc__}, |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12504 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12505 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 12506 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
| 12507 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 12508 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 12509 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 12510 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 12511 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12512 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12513 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 12514 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 12515 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12516 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12517 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 12518 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 12519 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12520 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12521 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12522 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS | METH_KEYWORDS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12523 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12524 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 12525 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 12526 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 12527 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 12528 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 12529 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 12530 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 12531 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 12532 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 12533 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 12534 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 12535 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 12536 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 12537 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 12538 | {"isidentifier", (PyCFunction) unicode_isidentifier, METH_NOARGS, isidentifier__doc__}, |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12539 | {"isprintable", (PyCFunction) unicode_isprintable, METH_NOARGS, isprintable__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12540 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Eric Smith | 9cd1e09 | 2007-08-31 18:39:38 +0000 | [diff] [blame] | 12541 | {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 12542 | {"format_map", (PyCFunction) do_string_format_map, METH_O, format_map__doc__}, |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12543 | {"__format__", (PyCFunction) unicode__format__, METH_VARARGS, p_format__doc__}, |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12544 | {"maketrans", (PyCFunction) unicode_maketrans, |
| 12545 | METH_VARARGS | METH_STATIC, maketrans__doc__}, |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12546 | {"__sizeof__", (PyCFunction) unicode__sizeof__, METH_NOARGS, sizeof__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 12547 | #if 0 |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12548 | {"capwords", (PyCFunction) unicode_capwords, METH_NOARGS, capwords__doc__}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12549 | #endif |
| 12550 | |
| 12551 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12552 | /* These methods are just used for debugging the implementation. */ |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12553 | {"_decimal2ascii", (PyCFunction) unicode__decimal2ascii, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12554 | #endif |
| 12555 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12556 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12557 | {NULL, NULL} |
| 12558 | }; |
| 12559 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 12560 | static PyObject * |
| 12561 | unicode_mod(PyObject *v, PyObject *w) |
| 12562 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 12563 | if (!PyUnicode_Check(v)) |
| 12564 | Py_RETURN_NOTIMPLEMENTED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12565 | return PyUnicode_Format(v, w); |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 12566 | } |
| 12567 | |
| 12568 | static PyNumberMethods unicode_as_number = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12569 | 0, /*nb_add*/ |
| 12570 | 0, /*nb_subtract*/ |
| 12571 | 0, /*nb_multiply*/ |
| 12572 | unicode_mod, /*nb_remainder*/ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 12573 | }; |
| 12574 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12575 | static PySequenceMethods unicode_as_sequence = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12576 | (lenfunc) unicode_length, /* sq_length */ |
| 12577 | PyUnicode_Concat, /* sq_concat */ |
| 12578 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 12579 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
| 12580 | 0, /* sq_slice */ |
| 12581 | 0, /* sq_ass_item */ |
| 12582 | 0, /* sq_ass_slice */ |
| 12583 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12584 | }; |
| 12585 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12586 | static PyObject* |
| 12587 | unicode_subscript(PyUnicodeObject* self, PyObject* item) |
| 12588 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12589 | if (PyUnicode_READY(self) == -1) |
| 12590 | return NULL; |
| 12591 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 12592 | if (PyIndex_Check(item)) { |
| 12593 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12594 | if (i == -1 && PyErr_Occurred()) |
| 12595 | return NULL; |
| 12596 | if (i < 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12597 | i += PyUnicode_GET_LENGTH(self); |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 12598 | return unicode_getitem((PyObject*)self, i); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12599 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12600 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 12601 | PyObject *result; |
| 12602 | void *src_data, *dest_data; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 12603 | int src_kind, dest_kind; |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 12604 | Py_UCS4 ch, max_char, kind_limit; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12605 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12606 | if (PySlice_GetIndicesEx(item, PyUnicode_GET_LENGTH(self), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12607 | &start, &stop, &step, &slicelength) < 0) { |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12608 | return NULL; |
| 12609 | } |
| 12610 | |
| 12611 | if (slicelength <= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12612 | return PyUnicode_New(0, 0); |
| 12613 | } else if (start == 0 && step == 1 && |
| 12614 | slicelength == PyUnicode_GET_LENGTH(self) && |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 12615 | PyUnicode_CheckExact(self)) { |
| 12616 | Py_INCREF(self); |
| 12617 | return (PyObject *)self; |
| 12618 | } else if (step == 1) { |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 12619 | return PyUnicode_Substring((PyObject*)self, |
| 12620 | start, start + slicelength); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12621 | } |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 12622 | /* General case */ |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 12623 | max_char = 0; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 12624 | src_kind = PyUnicode_KIND(self); |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 12625 | kind_limit = kind_maxchar_limit(src_kind); |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 12626 | src_data = PyUnicode_DATA(self); |
| 12627 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 12628 | ch = PyUnicode_READ(src_kind, src_data, cur); |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 12629 | if (ch > max_char) { |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 12630 | max_char = ch; |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 12631 | if (max_char >= kind_limit) |
| 12632 | break; |
| 12633 | } |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 12634 | } |
| 12635 | result = PyUnicode_New(slicelength, max_char); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 12636 | if (result == NULL) |
| 12637 | return NULL; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 12638 | dest_kind = PyUnicode_KIND(result); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 12639 | dest_data = PyUnicode_DATA(result); |
| 12640 | |
| 12641 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 12642 | Py_UCS4 ch = PyUnicode_READ(src_kind, src_data, cur); |
| 12643 | PyUnicode_WRITE(dest_kind, dest_data, i, ch); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 12644 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12645 | assert(_PyUnicode_CheckConsistency(result, 1)); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 12646 | return result; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12647 | } else { |
| 12648 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 12649 | return NULL; |
| 12650 | } |
| 12651 | } |
| 12652 | |
| 12653 | static PyMappingMethods unicode_as_mapping = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12654 | (lenfunc)unicode_length, /* mp_length */ |
| 12655 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 12656 | (objobjargproc)0, /* mp_ass_subscript */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12657 | }; |
| 12658 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12659 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12660 | /* Helpers for PyUnicode_Format() */ |
| 12661 | |
| 12662 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12663 | getnextarg(PyObject *args, Py_ssize_t arglen, Py_ssize_t *p_argidx) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12664 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12665 | Py_ssize_t argidx = *p_argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12666 | if (argidx < arglen) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12667 | (*p_argidx)++; |
| 12668 | if (arglen < 0) |
| 12669 | return args; |
| 12670 | else |
| 12671 | return PyTuple_GetItem(args, argidx); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12672 | } |
| 12673 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12674 | "not enough arguments for format string"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12675 | return NULL; |
| 12676 | } |
| 12677 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 12678 | /* 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] | 12679 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 12680 | static PyObject * |
| 12681 | formatfloat(PyObject *v, int flags, int prec, int type) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12682 | { |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 12683 | char *p; |
| 12684 | PyObject *result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12685 | double x; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 12686 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12687 | x = PyFloat_AsDouble(v); |
| 12688 | if (x == -1.0 && PyErr_Occurred()) |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 12689 | return NULL; |
| 12690 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12691 | if (prec < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12692 | prec = 6; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 12693 | |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 12694 | p = PyOS_double_to_string(x, type, prec, |
| 12695 | (flags & F_ALT) ? Py_DTSF_ALT : 0, NULL); |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 12696 | if (p == NULL) |
| 12697 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12698 | result = PyUnicode_DecodeASCII(p, strlen(p), NULL); |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 12699 | PyMem_Free(p); |
| 12700 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12701 | } |
| 12702 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 12703 | static PyObject* |
| 12704 | formatlong(PyObject *val, int flags, int prec, int type) |
| 12705 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12706 | char *buf; |
| 12707 | int len; |
| 12708 | PyObject *str; /* temporary string object. */ |
| 12709 | PyObject *result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 12710 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12711 | str = _PyBytes_FormatLong(val, flags, prec, type, &buf, &len); |
| 12712 | if (!str) |
| 12713 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12714 | result = PyUnicode_DecodeASCII(buf, len, NULL); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12715 | Py_DECREF(str); |
| 12716 | return result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 12717 | } |
| 12718 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 12719 | static Py_UCS4 |
| 12720 | formatchar(PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12721 | { |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 12722 | /* presume that the buffer is at least 3 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 12723 | if (PyUnicode_Check(v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12724 | if (PyUnicode_GET_LENGTH(v) == 1) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 12725 | return PyUnicode_READ_CHAR(v, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12726 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12727 | goto onError; |
| 12728 | } |
| 12729 | else { |
| 12730 | /* Integer input truncated to a character */ |
| 12731 | long x; |
| 12732 | x = PyLong_AsLong(v); |
| 12733 | if (x == -1 && PyErr_Occurred()) |
| 12734 | goto onError; |
| 12735 | |
| 12736 | if (x < 0 || x > 0x10ffff) { |
| 12737 | PyErr_SetString(PyExc_OverflowError, |
| 12738 | "%c arg not in range(0x110000)"); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 12739 | return (Py_UCS4) -1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12740 | } |
| 12741 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 12742 | return (Py_UCS4) x; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12743 | } |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 12744 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12745 | onError: |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 12746 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12747 | "%c requires int or char"); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 12748 | return (Py_UCS4) -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12749 | } |
| 12750 | |
Antoine Pitrou | 978b9d2 | 2011-10-07 12:35:48 +0200 | [diff] [blame] | 12751 | static int |
| 12752 | repeat_accumulate(_PyAccu *acc, PyObject *obj, Py_ssize_t count) |
| 12753 | { |
| 12754 | int r; |
| 12755 | assert(count > 0); |
| 12756 | assert(PyUnicode_Check(obj)); |
| 12757 | if (count > 5) { |
| 12758 | PyObject *repeated = unicode_repeat((PyUnicodeObject *) obj, count); |
| 12759 | if (repeated == NULL) |
| 12760 | return -1; |
| 12761 | r = _PyAccu_Accumulate(acc, repeated); |
| 12762 | Py_DECREF(repeated); |
| 12763 | return r; |
| 12764 | } |
| 12765 | else { |
| 12766 | do { |
| 12767 | if (_PyAccu_Accumulate(acc, obj)) |
| 12768 | return -1; |
| 12769 | } while (--count); |
| 12770 | return 0; |
| 12771 | } |
| 12772 | } |
| 12773 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12774 | PyObject * |
| 12775 | PyUnicode_Format(PyObject *format, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12776 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12777 | void *fmt; |
| 12778 | int fmtkind; |
| 12779 | PyObject *result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12780 | int kind; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 12781 | int r; |
| 12782 | Py_ssize_t fmtcnt, fmtpos, arglen, argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12783 | int args_owned = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12784 | PyObject *dict = NULL; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 12785 | PyObject *temp = NULL; |
| 12786 | PyObject *second = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12787 | PyUnicodeObject *uformat; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 12788 | _PyAccu acc; |
| 12789 | static PyObject *plus, *minus, *blank, *zero, *percent; |
| 12790 | |
| 12791 | if (!plus && !(plus = get_latin1_char('+'))) |
| 12792 | return NULL; |
| 12793 | if (!minus && !(minus = get_latin1_char('-'))) |
| 12794 | return NULL; |
| 12795 | if (!blank && !(blank = get_latin1_char(' '))) |
| 12796 | return NULL; |
| 12797 | if (!zero && !(zero = get_latin1_char('0'))) |
| 12798 | return NULL; |
| 12799 | if (!percent && !(percent = get_latin1_char('%'))) |
| 12800 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 12801 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12802 | if (format == NULL || args == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12803 | PyErr_BadInternalCall(); |
| 12804 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12805 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12806 | uformat = (PyUnicodeObject*)PyUnicode_FromObject(format); |
| 12807 | if (uformat == NULL || PyUnicode_READY(uformat) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12808 | return NULL; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 12809 | if (_PyAccu_Init(&acc)) |
| 12810 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12811 | fmt = PyUnicode_DATA(uformat); |
| 12812 | fmtkind = PyUnicode_KIND(uformat); |
| 12813 | fmtcnt = PyUnicode_GET_LENGTH(uformat); |
| 12814 | fmtpos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12815 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12816 | if (PyTuple_Check(args)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12817 | arglen = PyTuple_Size(args); |
| 12818 | argidx = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12819 | } |
| 12820 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12821 | arglen = -1; |
| 12822 | argidx = -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12823 | } |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 12824 | if (Py_TYPE(args)->tp_as_mapping && !PyTuple_Check(args) && |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 12825 | !PyUnicode_Check(args)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12826 | dict = args; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12827 | |
| 12828 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12829 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) != '%') { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 12830 | PyObject *nonfmt; |
| 12831 | Py_ssize_t nonfmtpos; |
| 12832 | nonfmtpos = fmtpos++; |
| 12833 | while (fmtcnt >= 0 && |
| 12834 | PyUnicode_READ(fmtkind, fmt, fmtpos) != '%') { |
| 12835 | fmtpos++; |
| 12836 | fmtcnt--; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12837 | } |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 12838 | nonfmt = PyUnicode_Substring((PyObject *) uformat, nonfmtpos, fmtpos); |
| 12839 | if (nonfmt == NULL) |
| 12840 | goto onError; |
| 12841 | r = _PyAccu_Accumulate(&acc, nonfmt); |
| 12842 | Py_DECREF(nonfmt); |
| 12843 | if (r) |
| 12844 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12845 | } |
| 12846 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12847 | /* Got a format specifier */ |
| 12848 | int flags = 0; |
| 12849 | Py_ssize_t width = -1; |
| 12850 | int prec = -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12851 | Py_UCS4 c = '\0'; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 12852 | Py_UCS4 fill, sign; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12853 | int isnumok; |
| 12854 | PyObject *v = NULL; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 12855 | void *pbuf = NULL; |
| 12856 | Py_ssize_t pindex, len; |
| 12857 | PyObject *signobj = NULL, *fillobj = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12858 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12859 | fmtpos++; |
| 12860 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) == '(') { |
| 12861 | Py_ssize_t keystart; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12862 | Py_ssize_t keylen; |
| 12863 | PyObject *key; |
| 12864 | int pcount = 1; |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 12865 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12866 | if (dict == NULL) { |
| 12867 | PyErr_SetString(PyExc_TypeError, |
| 12868 | "format requires a mapping"); |
| 12869 | goto onError; |
| 12870 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12871 | ++fmtpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12872 | --fmtcnt; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12873 | keystart = fmtpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12874 | /* Skip over balanced parentheses */ |
| 12875 | while (pcount > 0 && --fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12876 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) == ')') |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12877 | --pcount; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12878 | else if (PyUnicode_READ(fmtkind, fmt, fmtpos) == '(') |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12879 | ++pcount; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12880 | fmtpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12881 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12882 | keylen = fmtpos - keystart - 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12883 | if (fmtcnt < 0 || pcount > 0) { |
| 12884 | PyErr_SetString(PyExc_ValueError, |
| 12885 | "incomplete format key"); |
| 12886 | goto onError; |
| 12887 | } |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 12888 | key = PyUnicode_Substring((PyObject*)uformat, |
| 12889 | keystart, keystart + keylen); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12890 | if (key == NULL) |
| 12891 | goto onError; |
| 12892 | if (args_owned) { |
| 12893 | Py_DECREF(args); |
| 12894 | args_owned = 0; |
| 12895 | } |
| 12896 | args = PyObject_GetItem(dict, key); |
| 12897 | Py_DECREF(key); |
| 12898 | if (args == NULL) { |
| 12899 | goto onError; |
| 12900 | } |
| 12901 | args_owned = 1; |
| 12902 | arglen = -1; |
| 12903 | argidx = -2; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12904 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12905 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12906 | switch (c = PyUnicode_READ(fmtkind, fmt, fmtpos++)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12907 | case '-': flags |= F_LJUST; continue; |
| 12908 | case '+': flags |= F_SIGN; continue; |
| 12909 | case ' ': flags |= F_BLANK; continue; |
| 12910 | case '#': flags |= F_ALT; continue; |
| 12911 | case '0': flags |= F_ZERO; continue; |
| 12912 | } |
| 12913 | break; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12914 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12915 | if (c == '*') { |
| 12916 | v = getnextarg(args, arglen, &argidx); |
| 12917 | if (v == NULL) |
| 12918 | goto onError; |
| 12919 | if (!PyLong_Check(v)) { |
| 12920 | PyErr_SetString(PyExc_TypeError, |
| 12921 | "* wants int"); |
| 12922 | goto onError; |
| 12923 | } |
| 12924 | width = PyLong_AsLong(v); |
| 12925 | if (width == -1 && PyErr_Occurred()) |
| 12926 | goto onError; |
| 12927 | if (width < 0) { |
| 12928 | flags |= F_LJUST; |
| 12929 | width = -width; |
| 12930 | } |
| 12931 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12932 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12933 | } |
| 12934 | else if (c >= '0' && c <= '9') { |
| 12935 | width = c - '0'; |
| 12936 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12937 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12938 | if (c < '0' || c > '9') |
| 12939 | break; |
| 12940 | if ((width*10) / 10 != width) { |
| 12941 | PyErr_SetString(PyExc_ValueError, |
| 12942 | "width too big"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12943 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12944 | } |
| 12945 | width = width*10 + (c - '0'); |
| 12946 | } |
| 12947 | } |
| 12948 | if (c == '.') { |
| 12949 | prec = 0; |
| 12950 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12951 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12952 | if (c == '*') { |
| 12953 | v = getnextarg(args, arglen, &argidx); |
| 12954 | if (v == NULL) |
| 12955 | goto onError; |
| 12956 | if (!PyLong_Check(v)) { |
| 12957 | PyErr_SetString(PyExc_TypeError, |
| 12958 | "* wants int"); |
| 12959 | goto onError; |
| 12960 | } |
| 12961 | prec = PyLong_AsLong(v); |
| 12962 | if (prec == -1 && PyErr_Occurred()) |
| 12963 | goto onError; |
| 12964 | if (prec < 0) |
| 12965 | prec = 0; |
| 12966 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12967 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12968 | } |
| 12969 | else if (c >= '0' && c <= '9') { |
| 12970 | prec = c - '0'; |
| 12971 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12972 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12973 | if (c < '0' || c > '9') |
| 12974 | break; |
| 12975 | if ((prec*10) / 10 != prec) { |
| 12976 | PyErr_SetString(PyExc_ValueError, |
| 12977 | "prec too big"); |
| 12978 | goto onError; |
| 12979 | } |
| 12980 | prec = prec*10 + (c - '0'); |
| 12981 | } |
| 12982 | } |
| 12983 | } /* prec */ |
| 12984 | if (fmtcnt >= 0) { |
| 12985 | if (c == 'h' || c == 'l' || c == 'L') { |
| 12986 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12987 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12988 | } |
| 12989 | } |
| 12990 | if (fmtcnt < 0) { |
| 12991 | PyErr_SetString(PyExc_ValueError, |
| 12992 | "incomplete format"); |
| 12993 | goto onError; |
| 12994 | } |
| 12995 | if (c != '%') { |
| 12996 | v = getnextarg(args, arglen, &argidx); |
| 12997 | if (v == NULL) |
| 12998 | goto onError; |
| 12999 | } |
| 13000 | sign = 0; |
| 13001 | fill = ' '; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13002 | fillobj = blank; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13003 | switch (c) { |
| 13004 | |
| 13005 | case '%': |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13006 | _PyAccu_Accumulate(&acc, percent); |
| 13007 | continue; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13008 | |
| 13009 | case 's': |
| 13010 | case 'r': |
| 13011 | case 'a': |
Victor Stinner | 808fc0a | 2010-03-22 12:50:40 +0000 | [diff] [blame] | 13012 | if (PyUnicode_CheckExact(v) && c == 's') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13013 | temp = v; |
| 13014 | Py_INCREF(temp); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13015 | } |
| 13016 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13017 | if (c == 's') |
| 13018 | temp = PyObject_Str(v); |
| 13019 | else if (c == 'r') |
| 13020 | temp = PyObject_Repr(v); |
| 13021 | else |
| 13022 | temp = PyObject_ASCII(v); |
| 13023 | if (temp == NULL) |
| 13024 | goto onError; |
| 13025 | if (PyUnicode_Check(temp)) |
| 13026 | /* nothing to do */; |
| 13027 | else { |
| 13028 | Py_DECREF(temp); |
| 13029 | PyErr_SetString(PyExc_TypeError, |
| 13030 | "%s argument has non-string str()"); |
| 13031 | goto onError; |
| 13032 | } |
| 13033 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13034 | if (PyUnicode_READY(temp) == -1) { |
| 13035 | Py_CLEAR(temp); |
| 13036 | goto onError; |
| 13037 | } |
| 13038 | pbuf = PyUnicode_DATA(temp); |
| 13039 | kind = PyUnicode_KIND(temp); |
| 13040 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13041 | if (prec >= 0 && len > prec) |
| 13042 | len = prec; |
| 13043 | break; |
| 13044 | |
| 13045 | case 'i': |
| 13046 | case 'd': |
| 13047 | case 'u': |
| 13048 | case 'o': |
| 13049 | case 'x': |
| 13050 | case 'X': |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13051 | isnumok = 0; |
| 13052 | if (PyNumber_Check(v)) { |
| 13053 | PyObject *iobj=NULL; |
| 13054 | |
| 13055 | if (PyLong_Check(v)) { |
| 13056 | iobj = v; |
| 13057 | Py_INCREF(iobj); |
| 13058 | } |
| 13059 | else { |
| 13060 | iobj = PyNumber_Long(v); |
| 13061 | } |
| 13062 | if (iobj!=NULL) { |
| 13063 | if (PyLong_Check(iobj)) { |
| 13064 | isnumok = 1; |
Senthil Kumaran | 9ebe08d | 2011-07-03 21:03:16 -0700 | [diff] [blame] | 13065 | temp = formatlong(iobj, flags, prec, (c == 'i'? 'd': c)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13066 | Py_DECREF(iobj); |
| 13067 | if (!temp) |
| 13068 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13069 | if (PyUnicode_READY(temp) == -1) { |
| 13070 | Py_CLEAR(temp); |
| 13071 | goto onError; |
| 13072 | } |
| 13073 | pbuf = PyUnicode_DATA(temp); |
| 13074 | kind = PyUnicode_KIND(temp); |
| 13075 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13076 | sign = 1; |
| 13077 | } |
| 13078 | else { |
| 13079 | Py_DECREF(iobj); |
| 13080 | } |
| 13081 | } |
| 13082 | } |
| 13083 | if (!isnumok) { |
| 13084 | PyErr_Format(PyExc_TypeError, |
| 13085 | "%%%c format: a number is required, " |
| 13086 | "not %.200s", (char)c, Py_TYPE(v)->tp_name); |
| 13087 | goto onError; |
| 13088 | } |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13089 | if (flags & F_ZERO) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13090 | fill = '0'; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13091 | fillobj = zero; |
| 13092 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13093 | break; |
| 13094 | |
| 13095 | case 'e': |
| 13096 | case 'E': |
| 13097 | case 'f': |
| 13098 | case 'F': |
| 13099 | case 'g': |
| 13100 | case 'G': |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13101 | temp = formatfloat(v, flags, prec, c); |
| 13102 | if (!temp) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13103 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13104 | if (PyUnicode_READY(temp) == -1) { |
| 13105 | Py_CLEAR(temp); |
| 13106 | goto onError; |
| 13107 | } |
| 13108 | pbuf = PyUnicode_DATA(temp); |
| 13109 | kind = PyUnicode_KIND(temp); |
| 13110 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13111 | sign = 1; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13112 | if (flags & F_ZERO) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13113 | fill = '0'; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13114 | fillobj = zero; |
| 13115 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13116 | break; |
| 13117 | |
| 13118 | case 'c': |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13119 | { |
| 13120 | Py_UCS4 ch = formatchar(v); |
| 13121 | if (ch == (Py_UCS4) -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13122 | goto onError; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13123 | temp = _PyUnicode_FromUCS4(&ch, 1); |
| 13124 | if (temp == NULL) |
| 13125 | goto onError; |
| 13126 | pbuf = PyUnicode_DATA(temp); |
| 13127 | kind = PyUnicode_KIND(temp); |
| 13128 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13129 | break; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13130 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13131 | |
| 13132 | default: |
| 13133 | PyErr_Format(PyExc_ValueError, |
| 13134 | "unsupported format character '%c' (0x%x) " |
| 13135 | "at index %zd", |
| 13136 | (31<=c && c<=126) ? (char)c : '?', |
| 13137 | (int)c, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13138 | fmtpos - 1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13139 | goto onError; |
| 13140 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13141 | /* pbuf is initialized here. */ |
| 13142 | pindex = 0; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13143 | if (sign) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13144 | if (PyUnicode_READ(kind, pbuf, pindex) == '-') { |
| 13145 | signobj = minus; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13146 | len--; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13147 | pindex++; |
| 13148 | } |
| 13149 | else if (PyUnicode_READ(kind, pbuf, pindex) == '+') { |
| 13150 | signobj = plus; |
| 13151 | len--; |
| 13152 | pindex++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13153 | } |
| 13154 | else if (flags & F_SIGN) |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13155 | signobj = plus; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13156 | else if (flags & F_BLANK) |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13157 | signobj = blank; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13158 | else |
| 13159 | sign = 0; |
| 13160 | } |
| 13161 | if (width < len) |
| 13162 | width = len; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13163 | if (sign) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13164 | if (fill != ' ') { |
| 13165 | assert(signobj != NULL); |
| 13166 | if (_PyAccu_Accumulate(&acc, signobj)) |
| 13167 | goto onError; |
| 13168 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13169 | if (width > len) |
| 13170 | width--; |
| 13171 | } |
| 13172 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13173 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13174 | assert(PyUnicode_READ(kind, pbuf, pindex + 1) == c); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13175 | if (fill != ' ') { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13176 | second = get_latin1_char( |
| 13177 | PyUnicode_READ(kind, pbuf, pindex + 1)); |
| 13178 | pindex += 2; |
| 13179 | if (second == NULL || |
| 13180 | _PyAccu_Accumulate(&acc, zero) || |
| 13181 | _PyAccu_Accumulate(&acc, second)) |
| 13182 | goto onError; |
| 13183 | Py_CLEAR(second); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13184 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13185 | width -= 2; |
| 13186 | if (width < 0) |
| 13187 | width = 0; |
| 13188 | len -= 2; |
| 13189 | } |
| 13190 | if (width > len && !(flags & F_LJUST)) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13191 | assert(fillobj != NULL); |
Antoine Pitrou | 978b9d2 | 2011-10-07 12:35:48 +0200 | [diff] [blame] | 13192 | if (repeat_accumulate(&acc, fillobj, width - len)) |
| 13193 | goto onError; |
| 13194 | width = len; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13195 | } |
| 13196 | if (fill == ' ') { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13197 | if (sign) { |
| 13198 | assert(signobj != NULL); |
| 13199 | if (_PyAccu_Accumulate(&acc, signobj)) |
| 13200 | goto onError; |
| 13201 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13202 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13203 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
| 13204 | assert(PyUnicode_READ(kind, pbuf, pindex+1) == c); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13205 | second = get_latin1_char( |
| 13206 | PyUnicode_READ(kind, pbuf, pindex + 1)); |
| 13207 | pindex += 2; |
| 13208 | if (second == NULL || |
| 13209 | _PyAccu_Accumulate(&acc, zero) || |
| 13210 | _PyAccu_Accumulate(&acc, second)) |
| 13211 | goto onError; |
| 13212 | Py_CLEAR(second); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13213 | } |
| 13214 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13215 | /* Copy all characters, preserving len */ |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13216 | if (temp != NULL) { |
| 13217 | assert(pbuf == PyUnicode_DATA(temp)); |
| 13218 | v = PyUnicode_Substring(temp, pindex, pindex + len); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13219 | } |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13220 | else { |
| 13221 | const char *p = (const char *) pbuf; |
| 13222 | assert(pbuf != NULL); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 13223 | p += kind * pindex; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13224 | v = PyUnicode_FromKindAndData(kind, p, len); |
| 13225 | } |
| 13226 | if (v == NULL) |
| 13227 | goto onError; |
| 13228 | r = _PyAccu_Accumulate(&acc, v); |
| 13229 | Py_DECREF(v); |
| 13230 | if (r) |
| 13231 | goto onError; |
Antoine Pitrou | 978b9d2 | 2011-10-07 12:35:48 +0200 | [diff] [blame] | 13232 | if (width > len && repeat_accumulate(&acc, blank, width - len)) |
| 13233 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13234 | if (dict && (argidx < arglen) && c != '%') { |
| 13235 | PyErr_SetString(PyExc_TypeError, |
| 13236 | "not all arguments converted during string formatting"); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13237 | goto onError; |
| 13238 | } |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13239 | Py_CLEAR(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13240 | } /* '%' */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13241 | } /* until end */ |
| 13242 | if (argidx < arglen && !dict) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13243 | PyErr_SetString(PyExc_TypeError, |
| 13244 | "not all arguments converted during string formatting"); |
| 13245 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13246 | } |
| 13247 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13248 | result = _PyAccu_Finish(&acc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13249 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13250 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13251 | } |
| 13252 | Py_DECREF(uformat); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13253 | Py_XDECREF(temp); |
| 13254 | Py_XDECREF(second); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13255 | return (PyObject *)result; |
| 13256 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13257 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13258 | Py_DECREF(uformat); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13259 | Py_XDECREF(temp); |
| 13260 | Py_XDECREF(second); |
| 13261 | _PyAccu_Destroy(&acc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13262 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13263 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13264 | } |
| 13265 | return NULL; |
| 13266 | } |
| 13267 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 13268 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 13269 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 13270 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13271 | static PyObject * |
| 13272 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 13273 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13274 | PyObject *x = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13275 | static char *kwlist[] = {"object", "encoding", "errors", 0}; |
| 13276 | char *encoding = NULL; |
| 13277 | char *errors = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13278 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13279 | if (type != &PyUnicode_Type) |
| 13280 | return unicode_subtype_new(type, args, kwds); |
| 13281 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:str", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13282 | kwlist, &x, &encoding, &errors)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13283 | return NULL; |
| 13284 | if (x == NULL) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13285 | return (PyObject *)PyUnicode_New(0, 0); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13286 | if (encoding == NULL && errors == NULL) |
| 13287 | return PyObject_Str(x); |
| 13288 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13289 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13290 | } |
| 13291 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 13292 | static PyObject * |
| 13293 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 13294 | { |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13295 | PyUnicodeObject *unicode, *self; |
| 13296 | Py_ssize_t length, char_size; |
| 13297 | int share_wstr, share_utf8; |
| 13298 | unsigned int kind; |
| 13299 | void *data; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 13300 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13301 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13302 | |
| 13303 | unicode = (PyUnicodeObject *)unicode_new(&PyUnicode_Type, args, kwds); |
| 13304 | if (unicode == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13305 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 13306 | assert(_PyUnicode_CHECK(unicode)); |
Victor Stinner | e06e145 | 2011-10-04 20:52:31 +0200 | [diff] [blame] | 13307 | if (PyUnicode_READY(unicode)) |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13308 | return NULL; |
| 13309 | |
| 13310 | self = (PyUnicodeObject *) type->tp_alloc(type, 0); |
| 13311 | if (self == NULL) { |
| 13312 | Py_DECREF(unicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13313 | return NULL; |
| 13314 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13315 | kind = PyUnicode_KIND(unicode); |
| 13316 | length = PyUnicode_GET_LENGTH(unicode); |
| 13317 | |
| 13318 | _PyUnicode_LENGTH(self) = length; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 13319 | #ifdef Py_DEBUG |
| 13320 | _PyUnicode_HASH(self) = -1; |
| 13321 | #else |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13322 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 13323 | #endif |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13324 | _PyUnicode_STATE(self).interned = 0; |
| 13325 | _PyUnicode_STATE(self).kind = kind; |
| 13326 | _PyUnicode_STATE(self).compact = 0; |
Victor Stinner | 3cf4637 | 2011-10-03 14:42:15 +0200 | [diff] [blame] | 13327 | _PyUnicode_STATE(self).ascii = _PyUnicode_STATE(unicode).ascii; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13328 | _PyUnicode_STATE(self).ready = 1; |
| 13329 | _PyUnicode_WSTR(self) = NULL; |
| 13330 | _PyUnicode_UTF8_LENGTH(self) = 0; |
| 13331 | _PyUnicode_UTF8(self) = NULL; |
| 13332 | _PyUnicode_WSTR_LENGTH(self) = 0; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 13333 | _PyUnicode_DATA_ANY(self) = NULL; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13334 | |
| 13335 | share_utf8 = 0; |
| 13336 | share_wstr = 0; |
| 13337 | if (kind == PyUnicode_1BYTE_KIND) { |
| 13338 | char_size = 1; |
| 13339 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) |
| 13340 | share_utf8 = 1; |
| 13341 | } |
| 13342 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 13343 | char_size = 2; |
| 13344 | if (sizeof(wchar_t) == 2) |
| 13345 | share_wstr = 1; |
| 13346 | } |
| 13347 | else { |
| 13348 | assert(kind == PyUnicode_4BYTE_KIND); |
| 13349 | char_size = 4; |
| 13350 | if (sizeof(wchar_t) == 4) |
| 13351 | share_wstr = 1; |
| 13352 | } |
| 13353 | |
| 13354 | /* Ensure we won't overflow the length. */ |
| 13355 | if (length > (PY_SSIZE_T_MAX / char_size - 1)) { |
| 13356 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13357 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13358 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13359 | data = PyObject_MALLOC((length + 1) * char_size); |
| 13360 | if (data == NULL) { |
| 13361 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13362 | goto onError; |
| 13363 | } |
| 13364 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 13365 | _PyUnicode_DATA_ANY(self) = data; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13366 | if (share_utf8) { |
| 13367 | _PyUnicode_UTF8_LENGTH(self) = length; |
| 13368 | _PyUnicode_UTF8(self) = data; |
| 13369 | } |
| 13370 | if (share_wstr) { |
| 13371 | _PyUnicode_WSTR_LENGTH(self) = length; |
| 13372 | _PyUnicode_WSTR(self) = (wchar_t *)data; |
| 13373 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13374 | |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13375 | Py_MEMCPY(data, PyUnicode_DATA(unicode), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 13376 | kind * (length + 1)); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13377 | Py_DECREF(unicode); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13378 | assert(_PyUnicode_CheckConsistency(self, 1)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 13379 | #ifdef Py_DEBUG |
| 13380 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
| 13381 | #endif |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13382 | return (PyObject *)self; |
| 13383 | |
| 13384 | onError: |
| 13385 | Py_DECREF(unicode); |
| 13386 | Py_DECREF(self); |
| 13387 | return NULL; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 13388 | } |
| 13389 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13390 | PyDoc_STRVAR(unicode_doc, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13391 | "str(string[, encoding[, errors]]) -> str\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13392 | \n\ |
Collin Winter | d474ce8 | 2007-08-07 19:42:11 +0000 | [diff] [blame] | 13393 | Create a new string object from the given encoded string.\n\ |
Skip Montanaro | 35b37a5 | 2002-07-26 16:22:46 +0000 | [diff] [blame] | 13394 | encoding defaults to the current default string encoding.\n\ |
| 13395 | errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13396 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13397 | static PyObject *unicode_iter(PyObject *seq); |
| 13398 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13399 | PyTypeObject PyUnicode_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 13400 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13401 | "str", /* tp_name */ |
| 13402 | sizeof(PyUnicodeObject), /* tp_size */ |
| 13403 | 0, /* tp_itemsize */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13404 | /* Slots */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13405 | (destructor)unicode_dealloc, /* tp_dealloc */ |
| 13406 | 0, /* tp_print */ |
| 13407 | 0, /* tp_getattr */ |
| 13408 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 13409 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13410 | unicode_repr, /* tp_repr */ |
| 13411 | &unicode_as_number, /* tp_as_number */ |
| 13412 | &unicode_as_sequence, /* tp_as_sequence */ |
| 13413 | &unicode_as_mapping, /* tp_as_mapping */ |
| 13414 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 13415 | 0, /* tp_call*/ |
| 13416 | (reprfunc) unicode_str, /* tp_str */ |
| 13417 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 13418 | 0, /* tp_setattro */ |
| 13419 | 0, /* tp_as_buffer */ |
| 13420 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13421 | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13422 | unicode_doc, /* tp_doc */ |
| 13423 | 0, /* tp_traverse */ |
| 13424 | 0, /* tp_clear */ |
| 13425 | PyUnicode_RichCompare, /* tp_richcompare */ |
| 13426 | 0, /* tp_weaklistoffset */ |
| 13427 | unicode_iter, /* tp_iter */ |
| 13428 | 0, /* tp_iternext */ |
| 13429 | unicode_methods, /* tp_methods */ |
| 13430 | 0, /* tp_members */ |
| 13431 | 0, /* tp_getset */ |
| 13432 | &PyBaseObject_Type, /* tp_base */ |
| 13433 | 0, /* tp_dict */ |
| 13434 | 0, /* tp_descr_get */ |
| 13435 | 0, /* tp_descr_set */ |
| 13436 | 0, /* tp_dictoffset */ |
| 13437 | 0, /* tp_init */ |
| 13438 | 0, /* tp_alloc */ |
| 13439 | unicode_new, /* tp_new */ |
| 13440 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13441 | }; |
| 13442 | |
| 13443 | /* Initialize the Unicode implementation */ |
| 13444 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 13445 | void _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13446 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13447 | int i; |
| 13448 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13449 | /* XXX - move this array to unicodectype.c ? */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13450 | Py_UCS2 linebreak[] = { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13451 | 0x000A, /* LINE FEED */ |
| 13452 | 0x000D, /* CARRIAGE RETURN */ |
| 13453 | 0x001C, /* FILE SEPARATOR */ |
| 13454 | 0x001D, /* GROUP SEPARATOR */ |
| 13455 | 0x001E, /* RECORD SEPARATOR */ |
| 13456 | 0x0085, /* NEXT LINE */ |
| 13457 | 0x2028, /* LINE SEPARATOR */ |
| 13458 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 13459 | }; |
| 13460 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 13461 | /* Init the implementation */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 13462 | unicode_empty = PyUnicode_New(0, 0); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13463 | assert(_PyUnicode_CheckConsistency(unicode_empty, 1)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13464 | if (!unicode_empty) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13465 | Py_FatalError("Can't create empty string"); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13466 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13467 | for (i = 0; i < 256; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13468 | unicode_latin1[i] = NULL; |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 13469 | if (PyType_Ready(&PyUnicode_Type) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13470 | Py_FatalError("Can't initialize 'unicode'"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13471 | |
| 13472 | /* initialize the linebreak bloom filter */ |
| 13473 | bloom_linebreak = make_bloom_mask( |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13474 | PyUnicode_2BYTE_KIND, linebreak, |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 13475 | Py_ARRAY_LENGTH(linebreak)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13476 | |
| 13477 | PyType_Ready(&EncodingMapType); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13478 | } |
| 13479 | |
| 13480 | /* Finalize the Unicode implementation */ |
| 13481 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 13482 | int |
| 13483 | PyUnicode_ClearFreeList(void) |
| 13484 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13485 | return 0; |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 13486 | } |
| 13487 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13488 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 13489 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13490 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13491 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13492 | |
Guido van Rossum | 4ae8ef8 | 2000-10-03 18:09:04 +0000 | [diff] [blame] | 13493 | Py_XDECREF(unicode_empty); |
| 13494 | unicode_empty = NULL; |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 13495 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13496 | for (i = 0; i < 256; i++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13497 | if (unicode_latin1[i]) { |
| 13498 | Py_DECREF(unicode_latin1[i]); |
| 13499 | unicode_latin1[i] = NULL; |
| 13500 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13501 | } |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 13502 | _PyUnicode_ClearStaticStrings(); |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 13503 | (void)PyUnicode_ClearFreeList(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13504 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 13505 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13506 | void |
| 13507 | PyUnicode_InternInPlace(PyObject **p) |
| 13508 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13509 | register PyUnicodeObject *s = (PyUnicodeObject *)(*p); |
| 13510 | PyObject *t; |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 13511 | #ifdef Py_DEBUG |
| 13512 | assert(s != NULL); |
| 13513 | assert(_PyUnicode_CHECK(s)); |
| 13514 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13515 | if (s == NULL || !PyUnicode_Check(s)) |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 13516 | return; |
| 13517 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13518 | /* If it's a subclass, we don't really know what putting |
| 13519 | it in the interned dict might do. */ |
| 13520 | if (!PyUnicode_CheckExact(s)) |
| 13521 | return; |
| 13522 | if (PyUnicode_CHECK_INTERNED(s)) |
| 13523 | return; |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 13524 | if (_PyUnicode_READY_REPLACE(p)) { |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 13525 | assert(0 && "_PyUnicode_READY_REPLACE fail in PyUnicode_InternInPlace"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13526 | return; |
| 13527 | } |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 13528 | s = (PyUnicodeObject *)(*p); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13529 | if (interned == NULL) { |
| 13530 | interned = PyDict_New(); |
| 13531 | if (interned == NULL) { |
| 13532 | PyErr_Clear(); /* Don't leave an exception */ |
| 13533 | return; |
| 13534 | } |
| 13535 | } |
| 13536 | /* It might be that the GetItem call fails even |
| 13537 | though the key is present in the dictionary, |
| 13538 | namely when this happens during a stack overflow. */ |
| 13539 | Py_ALLOW_RECURSION |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13540 | t = PyDict_GetItem(interned, (PyObject *)s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13541 | Py_END_ALLOW_RECURSION |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 13542 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13543 | if (t) { |
| 13544 | Py_INCREF(t); |
| 13545 | Py_DECREF(*p); |
| 13546 | *p = t; |
| 13547 | return; |
| 13548 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13549 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13550 | PyThreadState_GET()->recursion_critical = 1; |
| 13551 | if (PyDict_SetItem(interned, (PyObject *)s, (PyObject *)s) < 0) { |
| 13552 | PyErr_Clear(); |
| 13553 | PyThreadState_GET()->recursion_critical = 0; |
| 13554 | return; |
| 13555 | } |
| 13556 | PyThreadState_GET()->recursion_critical = 0; |
| 13557 | /* The two references in interned are not counted by refcnt. |
| 13558 | The deallocator will take care of this */ |
| 13559 | Py_REFCNT(s) -= 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13560 | _PyUnicode_STATE(s).interned = SSTATE_INTERNED_MORTAL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13561 | } |
| 13562 | |
| 13563 | void |
| 13564 | PyUnicode_InternImmortal(PyObject **p) |
| 13565 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13566 | PyUnicodeObject *u = (PyUnicodeObject *)*p; |
| 13567 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13568 | PyUnicode_InternInPlace(p); |
| 13569 | if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13570 | _PyUnicode_STATE(u).interned = SSTATE_INTERNED_IMMORTAL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13571 | Py_INCREF(*p); |
| 13572 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13573 | } |
| 13574 | |
| 13575 | PyObject * |
| 13576 | PyUnicode_InternFromString(const char *cp) |
| 13577 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13578 | PyObject *s = PyUnicode_FromString(cp); |
| 13579 | if (s == NULL) |
| 13580 | return NULL; |
| 13581 | PyUnicode_InternInPlace(&s); |
| 13582 | return s; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13583 | } |
| 13584 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 13585 | void |
| 13586 | _Py_ReleaseInternedUnicodeStrings(void) |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13587 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13588 | PyObject *keys; |
| 13589 | PyUnicodeObject *s; |
| 13590 | Py_ssize_t i, n; |
| 13591 | Py_ssize_t immortal_size = 0, mortal_size = 0; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13592 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13593 | if (interned == NULL || !PyDict_Check(interned)) |
| 13594 | return; |
| 13595 | keys = PyDict_Keys(interned); |
| 13596 | if (keys == NULL || !PyList_Check(keys)) { |
| 13597 | PyErr_Clear(); |
| 13598 | return; |
| 13599 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13600 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13601 | /* Since _Py_ReleaseInternedUnicodeStrings() is intended to help a leak |
| 13602 | detector, interned unicode strings are not forcibly deallocated; |
| 13603 | rather, we give them their stolen references back, and then clear |
| 13604 | and DECREF the interned dict. */ |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13605 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13606 | n = PyList_GET_SIZE(keys); |
| 13607 | fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13608 | n); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13609 | for (i = 0; i < n; i++) { |
| 13610 | s = (PyUnicodeObject *) PyList_GET_ITEM(keys, i); |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 13611 | if (PyUnicode_READY(s) == -1) { |
| 13612 | assert(0 && "could not ready string"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13613 | fprintf(stderr, "could not ready string\n"); |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 13614 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13615 | switch (PyUnicode_CHECK_INTERNED(s)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13616 | case SSTATE_NOT_INTERNED: |
| 13617 | /* XXX Shouldn't happen */ |
| 13618 | break; |
| 13619 | case SSTATE_INTERNED_IMMORTAL: |
| 13620 | Py_REFCNT(s) += 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13621 | immortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13622 | break; |
| 13623 | case SSTATE_INTERNED_MORTAL: |
| 13624 | Py_REFCNT(s) += 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13625 | mortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13626 | break; |
| 13627 | default: |
| 13628 | Py_FatalError("Inconsistent interned string state."); |
| 13629 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13630 | _PyUnicode_STATE(s).interned = SSTATE_NOT_INTERNED; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13631 | } |
| 13632 | fprintf(stderr, "total size of all interned strings: " |
| 13633 | "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " |
| 13634 | "mortal/immortal\n", mortal_size, immortal_size); |
| 13635 | Py_DECREF(keys); |
| 13636 | PyDict_Clear(interned); |
| 13637 | Py_DECREF(interned); |
| 13638 | interned = NULL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13639 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13640 | |
| 13641 | |
| 13642 | /********************* Unicode Iterator **************************/ |
| 13643 | |
| 13644 | typedef struct { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13645 | PyObject_HEAD |
| 13646 | Py_ssize_t it_index; |
| 13647 | PyUnicodeObject *it_seq; /* Set to NULL when iterator is exhausted */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13648 | } unicodeiterobject; |
| 13649 | |
| 13650 | static void |
| 13651 | unicodeiter_dealloc(unicodeiterobject *it) |
| 13652 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13653 | _PyObject_GC_UNTRACK(it); |
| 13654 | Py_XDECREF(it->it_seq); |
| 13655 | PyObject_GC_Del(it); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13656 | } |
| 13657 | |
| 13658 | static int |
| 13659 | unicodeiter_traverse(unicodeiterobject *it, visitproc visit, void *arg) |
| 13660 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13661 | Py_VISIT(it->it_seq); |
| 13662 | return 0; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13663 | } |
| 13664 | |
| 13665 | static PyObject * |
| 13666 | unicodeiter_next(unicodeiterobject *it) |
| 13667 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13668 | PyUnicodeObject *seq; |
| 13669 | PyObject *item; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13670 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13671 | assert(it != NULL); |
| 13672 | seq = it->it_seq; |
| 13673 | if (seq == NULL) |
| 13674 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 13675 | assert(_PyUnicode_CHECK(seq)); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13676 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13677 | if (it->it_index < PyUnicode_GET_LENGTH(seq)) { |
| 13678 | int kind = PyUnicode_KIND(seq); |
| 13679 | void *data = PyUnicode_DATA(seq); |
| 13680 | Py_UCS4 chr = PyUnicode_READ(kind, data, it->it_index); |
| 13681 | item = PyUnicode_FromOrdinal(chr); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13682 | if (item != NULL) |
| 13683 | ++it->it_index; |
| 13684 | return item; |
| 13685 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13686 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13687 | Py_DECREF(seq); |
| 13688 | it->it_seq = NULL; |
| 13689 | return NULL; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13690 | } |
| 13691 | |
| 13692 | static PyObject * |
| 13693 | unicodeiter_len(unicodeiterobject *it) |
| 13694 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13695 | Py_ssize_t len = 0; |
| 13696 | if (it->it_seq) |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 13697 | len = PyUnicode_GET_LENGTH(it->it_seq) - it->it_index; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13698 | return PyLong_FromSsize_t(len); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13699 | } |
| 13700 | |
| 13701 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
| 13702 | |
| 13703 | static PyMethodDef unicodeiter_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13704 | {"__length_hint__", (PyCFunction)unicodeiter_len, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13705 | length_hint_doc}, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13706 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13707 | }; |
| 13708 | |
| 13709 | PyTypeObject PyUnicodeIter_Type = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13710 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 13711 | "str_iterator", /* tp_name */ |
| 13712 | sizeof(unicodeiterobject), /* tp_basicsize */ |
| 13713 | 0, /* tp_itemsize */ |
| 13714 | /* methods */ |
| 13715 | (destructor)unicodeiter_dealloc, /* tp_dealloc */ |
| 13716 | 0, /* tp_print */ |
| 13717 | 0, /* tp_getattr */ |
| 13718 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 13719 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13720 | 0, /* tp_repr */ |
| 13721 | 0, /* tp_as_number */ |
| 13722 | 0, /* tp_as_sequence */ |
| 13723 | 0, /* tp_as_mapping */ |
| 13724 | 0, /* tp_hash */ |
| 13725 | 0, /* tp_call */ |
| 13726 | 0, /* tp_str */ |
| 13727 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 13728 | 0, /* tp_setattro */ |
| 13729 | 0, /* tp_as_buffer */ |
| 13730 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 13731 | 0, /* tp_doc */ |
| 13732 | (traverseproc)unicodeiter_traverse, /* tp_traverse */ |
| 13733 | 0, /* tp_clear */ |
| 13734 | 0, /* tp_richcompare */ |
| 13735 | 0, /* tp_weaklistoffset */ |
| 13736 | PyObject_SelfIter, /* tp_iter */ |
| 13737 | (iternextfunc)unicodeiter_next, /* tp_iternext */ |
| 13738 | unicodeiter_methods, /* tp_methods */ |
| 13739 | 0, |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13740 | }; |
| 13741 | |
| 13742 | static PyObject * |
| 13743 | unicode_iter(PyObject *seq) |
| 13744 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13745 | unicodeiterobject *it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13746 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13747 | if (!PyUnicode_Check(seq)) { |
| 13748 | PyErr_BadInternalCall(); |
| 13749 | return NULL; |
| 13750 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13751 | if (PyUnicode_READY(seq) == -1) |
| 13752 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13753 | it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type); |
| 13754 | if (it == NULL) |
| 13755 | return NULL; |
| 13756 | it->it_index = 0; |
| 13757 | Py_INCREF(seq); |
| 13758 | it->it_seq = (PyUnicodeObject *)seq; |
| 13759 | _PyObject_GC_TRACK(it); |
| 13760 | return (PyObject *)it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13761 | } |
| 13762 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13763 | #define UNIOP(x) Py_UNICODE_##x |
| 13764 | #define UNIOP_t Py_UNICODE |
| 13765 | #include "uniops.h" |
| 13766 | #undef UNIOP |
| 13767 | #undef UNIOP_t |
| 13768 | #define UNIOP(x) Py_UCS4_##x |
| 13769 | #define UNIOP_t Py_UCS4 |
| 13770 | #include "uniops.h" |
| 13771 | #undef UNIOP |
| 13772 | #undef UNIOP_t |
Victor Stinner | 331ea92 | 2010-08-10 16:37:20 +0000 | [diff] [blame] | 13773 | |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 13774 | Py_UNICODE* |
Victor Stinner | 4640860 | 2010-09-03 16:18:00 +0000 | [diff] [blame] | 13775 | PyUnicode_AsUnicodeCopy(PyObject *object) |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 13776 | { |
| 13777 | PyUnicodeObject *unicode = (PyUnicodeObject *)object; |
| 13778 | Py_UNICODE *copy; |
| 13779 | Py_ssize_t size; |
| 13780 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13781 | if (!PyUnicode_Check(unicode)) { |
| 13782 | PyErr_BadArgument(); |
| 13783 | return NULL; |
| 13784 | } |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 13785 | /* Ensure we won't overflow the size. */ |
| 13786 | if (PyUnicode_GET_SIZE(unicode) > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
| 13787 | PyErr_NoMemory(); |
| 13788 | return NULL; |
| 13789 | } |
| 13790 | size = PyUnicode_GET_SIZE(unicode) + 1; /* copy the nul character */ |
| 13791 | size *= sizeof(Py_UNICODE); |
| 13792 | copy = PyMem_Malloc(size); |
| 13793 | if (copy == NULL) { |
| 13794 | PyErr_NoMemory(); |
| 13795 | return NULL; |
| 13796 | } |
| 13797 | memcpy(copy, PyUnicode_AS_UNICODE(unicode), size); |
| 13798 | return copy; |
| 13799 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 13800 | |
Georg Brandl | 66c221e | 2010-10-14 07:04:07 +0000 | [diff] [blame] | 13801 | /* A _string module, to export formatter_parser and formatter_field_name_split |
| 13802 | to the string.Formatter class implemented in Python. */ |
| 13803 | |
| 13804 | static PyMethodDef _string_methods[] = { |
| 13805 | {"formatter_field_name_split", (PyCFunction) formatter_field_name_split, |
| 13806 | METH_O, PyDoc_STR("split the argument as a field name")}, |
| 13807 | {"formatter_parser", (PyCFunction) formatter_parser, |
| 13808 | METH_O, PyDoc_STR("parse the argument as a format string")}, |
| 13809 | {NULL, NULL} |
| 13810 | }; |
| 13811 | |
| 13812 | static struct PyModuleDef _string_module = { |
| 13813 | PyModuleDef_HEAD_INIT, |
| 13814 | "_string", |
| 13815 | PyDoc_STR("string helper module"), |
| 13816 | 0, |
| 13817 | _string_methods, |
| 13818 | NULL, |
| 13819 | NULL, |
| 13820 | NULL, |
| 13821 | NULL |
| 13822 | }; |
| 13823 | |
| 13824 | PyMODINIT_FUNC |
| 13825 | PyInit__string(void) |
| 13826 | { |
| 13827 | return PyModule_Create(&_string_module); |
| 13828 | } |
| 13829 | |
| 13830 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 13831 | #ifdef __cplusplus |
| 13832 | } |
| 13833 | #endif |