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 * |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 263 | unicode_fromascii(const unsigned char *s, Py_ssize_t size); |
| 264 | static PyObject * |
| 265 | _PyUnicode_FromUCS1(const unsigned char *s, Py_ssize_t size); |
| 266 | static PyObject * |
| 267 | _PyUnicode_FromUCS2(const Py_UCS2 *s, Py_ssize_t size); |
| 268 | static PyObject * |
| 269 | _PyUnicode_FromUCS4(const Py_UCS4 *s, Py_ssize_t size); |
| 270 | |
| 271 | static PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 272 | unicode_encode_call_errorhandler(const char *errors, |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 273 | PyObject **errorHandler,const char *encoding, const char *reason, |
| 274 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 275 | Py_ssize_t startpos, Py_ssize_t endpos, Py_ssize_t *newpos); |
| 276 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 277 | static void |
| 278 | raise_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 279 | const char *encoding, |
| 280 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 281 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 282 | const char *reason); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 283 | |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 284 | /* Same for linebreaks */ |
| 285 | static unsigned char ascii_linebreak[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 286 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 287 | /* 0x000A, * LINE FEED */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 288 | /* 0x000B, * LINE TABULATION */ |
| 289 | /* 0x000C, * FORM FEED */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 290 | /* 0x000D, * CARRIAGE RETURN */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 291 | 0, 0, 1, 1, 1, 1, 0, 0, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 292 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 293 | /* 0x001C, * FILE SEPARATOR */ |
| 294 | /* 0x001D, * GROUP SEPARATOR */ |
| 295 | /* 0x001E, * RECORD SEPARATOR */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 296 | 0, 0, 0, 0, 1, 1, 1, 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 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 302 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 303 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 304 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 305 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 306 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 307 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 308 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 309 | 0, 0, 0, 0, 0, 0, 0, 0 |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 310 | }; |
| 311 | |
Ezio Melotti | 48a2f8f | 2011-09-29 00:18:19 +0300 | [diff] [blame] | 312 | /* The max unicode value is always 0x10FFFF while using the PEP-393 API. |
| 313 | 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] | 314 | Py_UNICODE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 315 | PyUnicode_GetMax(void) |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 316 | { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 317 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 318 | return 0x10FFFF; |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 319 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 320 | /* This is actually an illegal character, so it should |
| 321 | not be passed to unichr. */ |
| 322 | return 0xFFFF; |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 323 | #endif |
| 324 | } |
| 325 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 326 | #ifdef Py_DEBUG |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 327 | int |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 328 | /* FIXME: use PyObject* type for op */ |
| 329 | _PyUnicode_CheckConsistency(void *op, int check_content) |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 330 | { |
| 331 | PyASCIIObject *ascii; |
| 332 | unsigned int kind; |
| 333 | |
| 334 | assert(PyUnicode_Check(op)); |
| 335 | |
| 336 | ascii = (PyASCIIObject *)op; |
| 337 | kind = ascii->state.kind; |
| 338 | |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 339 | if (ascii->state.ascii == 1 && ascii->state.compact == 1) { |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 340 | assert(kind == PyUnicode_1BYTE_KIND); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 341 | assert(ascii->state.ready == 1); |
| 342 | } |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 343 | else { |
Victor Stinner | 85041a5 | 2011-10-03 14:42:39 +0200 | [diff] [blame] | 344 | PyCompactUnicodeObject *compact = (PyCompactUnicodeObject *)op; |
Victor Stinner | 7f11ad4 | 2011-10-04 00:00:20 +0200 | [diff] [blame] | 345 | void *data; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 346 | |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 347 | if (ascii->state.compact == 1) { |
| 348 | data = compact + 1; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 349 | assert(kind == PyUnicode_1BYTE_KIND |
| 350 | || kind == PyUnicode_2BYTE_KIND |
| 351 | || kind == PyUnicode_4BYTE_KIND); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 352 | assert(ascii->state.ascii == 0); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 353 | assert(ascii->state.ready == 1); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 354 | assert (compact->utf8 != data); |
| 355 | } else { |
| 356 | PyUnicodeObject *unicode = (PyUnicodeObject *)op; |
| 357 | |
| 358 | data = unicode->data.any; |
| 359 | if (kind == PyUnicode_WCHAR_KIND) { |
| 360 | assert(ascii->state.compact == 0); |
| 361 | assert(ascii->state.ascii == 0); |
| 362 | assert(ascii->state.ready == 0); |
| 363 | assert(ascii->wstr != NULL); |
| 364 | assert(data == NULL); |
| 365 | assert(compact->utf8 == NULL); |
| 366 | assert(ascii->state.interned == SSTATE_NOT_INTERNED); |
| 367 | } |
| 368 | else { |
| 369 | assert(kind == PyUnicode_1BYTE_KIND |
| 370 | || kind == PyUnicode_2BYTE_KIND |
| 371 | || kind == PyUnicode_4BYTE_KIND); |
| 372 | assert(ascii->state.compact == 0); |
| 373 | assert(ascii->state.ready == 1); |
| 374 | assert(data != NULL); |
| 375 | if (ascii->state.ascii) { |
| 376 | assert (compact->utf8 == data); |
| 377 | assert (compact->utf8_length == ascii->length); |
| 378 | } |
| 379 | else |
| 380 | assert (compact->utf8 != data); |
| 381 | } |
| 382 | } |
| 383 | if (kind != PyUnicode_WCHAR_KIND) { |
Victor Stinner | 7f11ad4 | 2011-10-04 00:00:20 +0200 | [diff] [blame] | 384 | if ( |
| 385 | #if SIZEOF_WCHAR_T == 2 |
| 386 | kind == PyUnicode_2BYTE_KIND |
| 387 | #else |
| 388 | kind == PyUnicode_4BYTE_KIND |
| 389 | #endif |
| 390 | ) |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 391 | { |
| 392 | assert(ascii->wstr == data); |
| 393 | assert(compact->wstr_length == ascii->length); |
| 394 | } else |
| 395 | assert(ascii->wstr != data); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 396 | } |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 397 | |
| 398 | if (compact->utf8 == NULL) |
| 399 | assert(compact->utf8_length == 0); |
| 400 | if (ascii->wstr == NULL) |
| 401 | assert(compact->wstr_length == 0); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 402 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 403 | /* check that the best kind is used */ |
| 404 | if (check_content && kind != PyUnicode_WCHAR_KIND) |
| 405 | { |
| 406 | Py_ssize_t i; |
| 407 | Py_UCS4 maxchar = 0; |
| 408 | void *data = PyUnicode_DATA(ascii); |
| 409 | for (i=0; i < ascii->length; i++) |
| 410 | { |
| 411 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 412 | if (ch > maxchar) |
| 413 | maxchar = ch; |
| 414 | } |
| 415 | if (kind == PyUnicode_1BYTE_KIND) { |
| 416 | if (ascii->state.ascii == 0) |
| 417 | assert(maxchar >= 128); |
| 418 | else |
| 419 | assert(maxchar < 128); |
| 420 | } |
| 421 | else if (kind == PyUnicode_2BYTE_KIND) |
| 422 | assert(maxchar >= 0x100); |
| 423 | else |
| 424 | assert(maxchar >= 0x10000); |
| 425 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 426 | if (check_content && !unicode_is_singleton((PyObject*)ascii)) |
| 427 | assert(ascii->hash == -1); |
Benjamin Peterson | ccc51c1 | 2011-10-03 19:34:12 -0400 | [diff] [blame] | 428 | return 1; |
| 429 | } |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 430 | #endif |
| 431 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 432 | /* --- Bloom Filters ----------------------------------------------------- */ |
| 433 | |
| 434 | /* stuff to implement simple "bloom filters" for Unicode characters. |
| 435 | to keep things simple, we use a single bitmask, using the least 5 |
| 436 | bits from each unicode characters as the bit index. */ |
| 437 | |
| 438 | /* the linebreak mask is set up by Unicode_Init below */ |
| 439 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 440 | #if LONG_BIT >= 128 |
| 441 | #define BLOOM_WIDTH 128 |
| 442 | #elif LONG_BIT >= 64 |
| 443 | #define BLOOM_WIDTH 64 |
| 444 | #elif LONG_BIT >= 32 |
| 445 | #define BLOOM_WIDTH 32 |
| 446 | #else |
| 447 | #error "LONG_BIT is smaller than 32" |
| 448 | #endif |
| 449 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 450 | #define BLOOM_MASK unsigned long |
| 451 | |
| 452 | static BLOOM_MASK bloom_linebreak; |
| 453 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 454 | #define BLOOM_ADD(mask, ch) ((mask |= (1UL << ((ch) & (BLOOM_WIDTH - 1))))) |
| 455 | #define BLOOM(mask, ch) ((mask & (1UL << ((ch) & (BLOOM_WIDTH - 1))))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 456 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 457 | #define BLOOM_LINEBREAK(ch) \ |
| 458 | ((ch) < 128U ? ascii_linebreak[(ch)] : \ |
| 459 | (BLOOM(bloom_linebreak, (ch)) && Py_UNICODE_ISLINEBREAK(ch))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 460 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 461 | Py_LOCAL_INLINE(BLOOM_MASK) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 462 | make_bloom_mask(int kind, void* ptr, Py_ssize_t len) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 463 | { |
| 464 | /* calculate simple bloom-style bitmask for a given unicode string */ |
| 465 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 466 | BLOOM_MASK mask; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 467 | Py_ssize_t i; |
| 468 | |
| 469 | mask = 0; |
| 470 | for (i = 0; i < len; i++) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 471 | BLOOM_ADD(mask, PyUnicode_READ(kind, ptr, i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 472 | |
| 473 | return mask; |
| 474 | } |
| 475 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 476 | #define BLOOM_MEMBER(mask, chr, str) \ |
| 477 | (BLOOM(mask, chr) \ |
| 478 | && (PyUnicode_FindChar(str, chr, 0, PyUnicode_GET_LENGTH(str), 1) >= 0)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 479 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 480 | /* Compilation of templated routines */ |
| 481 | |
| 482 | #include "stringlib/asciilib.h" |
| 483 | #include "stringlib/fastsearch.h" |
| 484 | #include "stringlib/partition.h" |
| 485 | #include "stringlib/split.h" |
| 486 | #include "stringlib/count.h" |
| 487 | #include "stringlib/find.h" |
| 488 | #include "stringlib/find_max_char.h" |
| 489 | #include "stringlib/localeutil.h" |
| 490 | #include "stringlib/undef.h" |
| 491 | |
| 492 | #include "stringlib/ucs1lib.h" |
| 493 | #include "stringlib/fastsearch.h" |
| 494 | #include "stringlib/partition.h" |
| 495 | #include "stringlib/split.h" |
| 496 | #include "stringlib/count.h" |
| 497 | #include "stringlib/find.h" |
| 498 | #include "stringlib/find_max_char.h" |
| 499 | #include "stringlib/localeutil.h" |
| 500 | #include "stringlib/undef.h" |
| 501 | |
| 502 | #include "stringlib/ucs2lib.h" |
| 503 | #include "stringlib/fastsearch.h" |
| 504 | #include "stringlib/partition.h" |
| 505 | #include "stringlib/split.h" |
| 506 | #include "stringlib/count.h" |
| 507 | #include "stringlib/find.h" |
| 508 | #include "stringlib/find_max_char.h" |
| 509 | #include "stringlib/localeutil.h" |
| 510 | #include "stringlib/undef.h" |
| 511 | |
| 512 | #include "stringlib/ucs4lib.h" |
| 513 | #include "stringlib/fastsearch.h" |
| 514 | #include "stringlib/partition.h" |
| 515 | #include "stringlib/split.h" |
| 516 | #include "stringlib/count.h" |
| 517 | #include "stringlib/find.h" |
| 518 | #include "stringlib/find_max_char.h" |
| 519 | #include "stringlib/localeutil.h" |
| 520 | #include "stringlib/undef.h" |
| 521 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 522 | /* --- Unicode Object ----------------------------------------------------- */ |
| 523 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 524 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 525 | fixup(PyObject *self, Py_UCS4 (*fixfct)(PyObject *s)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 526 | |
| 527 | Py_LOCAL_INLINE(char *) findchar(void *s, int kind, |
| 528 | Py_ssize_t size, Py_UCS4 ch, |
| 529 | int direction) |
| 530 | { |
| 531 | /* like wcschr, but doesn't stop at NULL characters */ |
| 532 | Py_ssize_t i; |
Victor Stinner | 9e7a1bc | 2011-10-13 00:18:12 +0200 | [diff] [blame] | 533 | if (kind == 1) { |
| 534 | if (direction == 1) |
| 535 | return memchr(s, ch, size); |
| 536 | #ifdef HAVE_MEMRCHR |
| 537 | else |
| 538 | return memrchr(s, ch, size); |
| 539 | #endif |
| 540 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 541 | if (direction == 1) { |
| 542 | for(i = 0; i < size; i++) |
| 543 | if (PyUnicode_READ(kind, s, i) == ch) |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 544 | return (char*)s + kind * i; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 545 | } |
| 546 | else { |
| 547 | for(i = size-1; i >= 0; i--) |
| 548 | if (PyUnicode_READ(kind, s, i) == ch) |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 549 | return (char*)s + kind * i; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 550 | } |
| 551 | return NULL; |
| 552 | } |
| 553 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 554 | static PyObject* |
| 555 | resize_compact(PyObject *unicode, Py_ssize_t length) |
| 556 | { |
| 557 | Py_ssize_t char_size; |
| 558 | Py_ssize_t struct_size; |
| 559 | Py_ssize_t new_size; |
| 560 | int share_wstr; |
| 561 | |
| 562 | assert(PyUnicode_IS_READY(unicode)); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 563 | char_size = PyUnicode_KIND(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 564 | if (PyUnicode_IS_COMPACT_ASCII(unicode)) |
| 565 | struct_size = sizeof(PyASCIIObject); |
| 566 | else |
| 567 | struct_size = sizeof(PyCompactUnicodeObject); |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 568 | share_wstr = _PyUnicode_SHARE_WSTR(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 569 | |
| 570 | _Py_DEC_REFTOTAL; |
| 571 | _Py_ForgetReference(unicode); |
| 572 | |
| 573 | if (length > ((PY_SSIZE_T_MAX - struct_size) / char_size - 1)) { |
| 574 | PyErr_NoMemory(); |
| 575 | return NULL; |
| 576 | } |
| 577 | new_size = (struct_size + (length + 1) * char_size); |
| 578 | |
| 579 | unicode = (PyObject *)PyObject_REALLOC((char *)unicode, new_size); |
| 580 | if (unicode == NULL) { |
| 581 | PyObject_Del(unicode); |
| 582 | PyErr_NoMemory(); |
| 583 | return NULL; |
| 584 | } |
| 585 | _Py_NewReference(unicode); |
| 586 | _PyUnicode_LENGTH(unicode) = length; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 587 | if (share_wstr) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 588 | _PyUnicode_WSTR(unicode) = PyUnicode_DATA(unicode); |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 589 | if (!PyUnicode_IS_COMPACT_ASCII(unicode)) |
| 590 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 591 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 592 | PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), |
| 593 | length, 0); |
| 594 | return unicode; |
| 595 | } |
| 596 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 597 | static int |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 598 | resize_inplace(PyUnicodeObject *unicode, Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 599 | { |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 600 | wchar_t *wstr; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 601 | assert(!PyUnicode_IS_COMPACT(unicode)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 602 | assert(Py_REFCNT(unicode) == 1); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 603 | |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 604 | _PyUnicode_DIRTY(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 605 | |
| 606 | if (PyUnicode_IS_READY(unicode)) { |
| 607 | Py_ssize_t char_size; |
| 608 | Py_ssize_t new_size; |
Victor Stinner | 1c8d0c7 | 2011-10-03 12:11:00 +0200 | [diff] [blame] | 609 | int share_wstr, share_utf8; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 610 | void *data; |
| 611 | |
| 612 | data = _PyUnicode_DATA_ANY(unicode); |
| 613 | assert(data != NULL); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 614 | char_size = PyUnicode_KIND(unicode); |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 615 | share_wstr = _PyUnicode_SHARE_WSTR(unicode); |
| 616 | share_utf8 = _PyUnicode_SHARE_UTF8(unicode); |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 617 | if (!share_utf8 && _PyUnicode_HAS_UTF8_MEMORY(unicode)) |
| 618 | { |
| 619 | PyObject_DEL(_PyUnicode_UTF8(unicode)); |
| 620 | _PyUnicode_UTF8(unicode) = NULL; |
| 621 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
| 622 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 623 | |
| 624 | if (length > (PY_SSIZE_T_MAX / char_size - 1)) { |
| 625 | PyErr_NoMemory(); |
| 626 | return -1; |
| 627 | } |
| 628 | new_size = (length + 1) * char_size; |
| 629 | |
| 630 | data = (PyObject *)PyObject_REALLOC(data, new_size); |
| 631 | if (data == NULL) { |
| 632 | PyErr_NoMemory(); |
| 633 | return -1; |
| 634 | } |
| 635 | _PyUnicode_DATA_ANY(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 636 | if (share_wstr) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 637 | _PyUnicode_WSTR(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 638 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 639 | } |
| 640 | if (share_utf8) { |
Victor Stinner | 1c8d0c7 | 2011-10-03 12:11:00 +0200 | [diff] [blame] | 641 | _PyUnicode_UTF8(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 642 | _PyUnicode_UTF8_LENGTH(unicode) = length; |
| 643 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 644 | _PyUnicode_LENGTH(unicode) = length; |
| 645 | PyUnicode_WRITE(PyUnicode_KIND(unicode), data, length, 0); |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 646 | if (share_wstr || _PyUnicode_WSTR(unicode) == NULL) { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 647 | assert(_PyUnicode_CheckConsistency(unicode, 0)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 648 | return 0; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 649 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 650 | } |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 651 | assert(_PyUnicode_WSTR(unicode) != NULL); |
| 652 | |
| 653 | /* check for integer overflow */ |
| 654 | if (length > PY_SSIZE_T_MAX / sizeof(wchar_t) - 1) { |
| 655 | PyErr_NoMemory(); |
| 656 | return -1; |
| 657 | } |
| 658 | wstr = _PyUnicode_WSTR(unicode); |
| 659 | wstr = PyObject_REALLOC(wstr, sizeof(wchar_t) * (length + 1)); |
| 660 | if (!wstr) { |
| 661 | PyErr_NoMemory(); |
| 662 | return -1; |
| 663 | } |
| 664 | _PyUnicode_WSTR(unicode) = wstr; |
| 665 | _PyUnicode_WSTR(unicode)[length] = 0; |
| 666 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 667 | assert(_PyUnicode_CheckConsistency(unicode, 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 668 | return 0; |
| 669 | } |
| 670 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 671 | static PyObject* |
| 672 | resize_copy(PyObject *unicode, Py_ssize_t length) |
| 673 | { |
| 674 | Py_ssize_t copy_length; |
| 675 | if (PyUnicode_IS_COMPACT(unicode)) { |
| 676 | PyObject *copy; |
| 677 | assert(PyUnicode_IS_READY(unicode)); |
| 678 | |
| 679 | copy = PyUnicode_New(length, PyUnicode_MAX_CHAR_VALUE(unicode)); |
| 680 | if (copy == NULL) |
| 681 | return NULL; |
| 682 | |
| 683 | copy_length = Py_MIN(length, PyUnicode_GET_LENGTH(unicode)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 684 | copy_characters(copy, 0, unicode, 0, copy_length); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 685 | return copy; |
Victor Stinner | 8cfcbed | 2011-10-03 23:19:21 +0200 | [diff] [blame] | 686 | } |
| 687 | else { |
Victor Stinner | 2fd8227 | 2011-10-03 04:06:05 +0200 | [diff] [blame] | 688 | PyUnicodeObject *w; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 689 | assert(_PyUnicode_WSTR(unicode) != NULL); |
| 690 | assert(_PyUnicode_DATA_ANY(unicode) == NULL); |
Victor Stinner | 2fd8227 | 2011-10-03 04:06:05 +0200 | [diff] [blame] | 691 | w = _PyUnicode_New(length); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 692 | if (w == NULL) |
| 693 | return NULL; |
| 694 | copy_length = _PyUnicode_WSTR_LENGTH(unicode); |
| 695 | copy_length = Py_MIN(copy_length, length); |
| 696 | Py_UNICODE_COPY(_PyUnicode_WSTR(w), _PyUnicode_WSTR(unicode), |
| 697 | copy_length); |
| 698 | return (PyObject*)w; |
| 699 | } |
| 700 | } |
| 701 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 702 | /* 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] | 703 | Ux0000 terminated; some code (e.g. new_identifier) |
| 704 | relies on that. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 705 | |
| 706 | XXX This allocator could further be enhanced by assuring that the |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 707 | free list never reduces its size below 1. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 708 | |
| 709 | */ |
| 710 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 711 | #ifdef Py_DEBUG |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 712 | static int unicode_old_new_calls = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 713 | #endif |
| 714 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 715 | static PyUnicodeObject * |
| 716 | _PyUnicode_New(Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 717 | { |
| 718 | register PyUnicodeObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 719 | size_t new_size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 720 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 721 | /* Optimization for empty strings */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 722 | if (length == 0 && unicode_empty != NULL) { |
| 723 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 724 | return (PyUnicodeObject*)unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 725 | } |
| 726 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 727 | /* Ensure we won't overflow the size. */ |
| 728 | if (length > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
| 729 | return (PyUnicodeObject *)PyErr_NoMemory(); |
| 730 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 731 | if (length < 0) { |
| 732 | PyErr_SetString(PyExc_SystemError, |
| 733 | "Negative size passed to _PyUnicode_New"); |
| 734 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 735 | } |
| 736 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 737 | #ifdef Py_DEBUG |
| 738 | ++unicode_old_new_calls; |
| 739 | #endif |
| 740 | |
| 741 | unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type); |
| 742 | if (unicode == NULL) |
| 743 | return NULL; |
| 744 | new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
| 745 | _PyUnicode_WSTR(unicode) = (Py_UNICODE*) PyObject_MALLOC(new_size); |
| 746 | if (!_PyUnicode_WSTR(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 747 | PyErr_NoMemory(); |
| 748 | goto onError; |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 749 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 750 | |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 751 | /* Initialize the first element to guard against cases where |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 752 | * the caller fails before initializing str -- unicode_resize() |
| 753 | * reads str[0], and the Keep-Alive optimization can keep memory |
| 754 | * allocated for str alive across a call to unicode_dealloc(unicode). |
| 755 | * We don't want unicode_resize to read uninitialized memory in |
| 756 | * that case. |
| 757 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 758 | _PyUnicode_WSTR(unicode)[0] = 0; |
| 759 | _PyUnicode_WSTR(unicode)[length] = 0; |
| 760 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 761 | _PyUnicode_HASH(unicode) = -1; |
| 762 | _PyUnicode_STATE(unicode).interned = 0; |
| 763 | _PyUnicode_STATE(unicode).kind = 0; |
| 764 | _PyUnicode_STATE(unicode).compact = 0; |
| 765 | _PyUnicode_STATE(unicode).ready = 0; |
| 766 | _PyUnicode_STATE(unicode).ascii = 0; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 767 | _PyUnicode_DATA_ANY(unicode) = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 768 | _PyUnicode_LENGTH(unicode) = 0; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 769 | _PyUnicode_UTF8(unicode) = NULL; |
| 770 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 771 | return unicode; |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 772 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 773 | onError: |
Amaury Forgeot d'Arc | 7888d08 | 2008-08-01 01:06:32 +0000 | [diff] [blame] | 774 | /* XXX UNREF/NEWREF interface should be more symmetrical */ |
| 775 | _Py_DEC_REFTOTAL; |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 776 | _Py_ForgetReference((PyObject *)unicode); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 777 | PyObject_Del(unicode); |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 778 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 779 | } |
| 780 | |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 781 | static const char* |
| 782 | unicode_kind_name(PyObject *unicode) |
| 783 | { |
Victor Stinner | 42dfd71 | 2011-10-03 14:41:45 +0200 | [diff] [blame] | 784 | /* don't check consistency: unicode_kind_name() is called from |
| 785 | _PyUnicode_Dump() */ |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 786 | if (!PyUnicode_IS_COMPACT(unicode)) |
| 787 | { |
| 788 | if (!PyUnicode_IS_READY(unicode)) |
| 789 | return "wstr"; |
| 790 | switch(PyUnicode_KIND(unicode)) |
| 791 | { |
| 792 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 793 | if (PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 794 | return "legacy ascii"; |
| 795 | else |
| 796 | return "legacy latin1"; |
| 797 | case PyUnicode_2BYTE_KIND: |
| 798 | return "legacy UCS2"; |
| 799 | case PyUnicode_4BYTE_KIND: |
| 800 | return "legacy UCS4"; |
| 801 | default: |
| 802 | return "<legacy invalid kind>"; |
| 803 | } |
| 804 | } |
| 805 | assert(PyUnicode_IS_READY(unicode)); |
| 806 | switch(PyUnicode_KIND(unicode)) |
| 807 | { |
| 808 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 809 | if (PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 810 | return "ascii"; |
| 811 | else |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 812 | return "latin1"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 813 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 814 | return "UCS2"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 815 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 816 | return "UCS4"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 817 | default: |
| 818 | return "<invalid compact kind>"; |
| 819 | } |
| 820 | } |
| 821 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 822 | #ifdef Py_DEBUG |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 823 | static int unicode_new_new_calls = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 824 | |
| 825 | /* Functions wrapping macros for use in debugger */ |
| 826 | char *_PyUnicode_utf8(void *unicode){ |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 827 | return PyUnicode_UTF8(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 828 | } |
| 829 | |
| 830 | void *_PyUnicode_compact_data(void *unicode) { |
| 831 | return _PyUnicode_COMPACT_DATA(unicode); |
| 832 | } |
| 833 | void *_PyUnicode_data(void *unicode){ |
| 834 | printf("obj %p\n", unicode); |
| 835 | printf("compact %d\n", PyUnicode_IS_COMPACT(unicode)); |
| 836 | printf("compact ascii %d\n", PyUnicode_IS_COMPACT_ASCII(unicode)); |
| 837 | printf("ascii op %p\n", ((void*)((PyASCIIObject*)(unicode) + 1))); |
| 838 | printf("compact op %p\n", ((void*)((PyCompactUnicodeObject*)(unicode) + 1))); |
| 839 | printf("compact data %p\n", _PyUnicode_COMPACT_DATA(unicode)); |
| 840 | return PyUnicode_DATA(unicode); |
| 841 | } |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 842 | |
| 843 | void |
| 844 | _PyUnicode_Dump(PyObject *op) |
| 845 | { |
| 846 | PyASCIIObject *ascii = (PyASCIIObject *)op; |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 847 | PyCompactUnicodeObject *compact = (PyCompactUnicodeObject *)op; |
| 848 | PyUnicodeObject *unicode = (PyUnicodeObject *)op; |
| 849 | void *data; |
| 850 | printf("%s: len=%zu, ",unicode_kind_name(op), ascii->length); |
| 851 | if (ascii->state.compact) |
| 852 | data = (compact + 1); |
| 853 | else |
| 854 | data = unicode->data.any; |
| 855 | if (ascii->wstr == data) |
| 856 | printf("shared "); |
| 857 | printf("wstr=%p", ascii->wstr); |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 858 | if (!(ascii->state.ascii == 1 && ascii->state.compact == 1)) { |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 859 | printf(" (%zu), ", compact->wstr_length); |
| 860 | if (!ascii->state.compact && compact->utf8 == unicode->data.any) |
| 861 | printf("shared "); |
| 862 | printf("utf8=%p (%zu)", compact->utf8, compact->utf8_length); |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 863 | } |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 864 | printf(", data=%p\n", data); |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 865 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 866 | #endif |
| 867 | |
| 868 | PyObject * |
| 869 | PyUnicode_New(Py_ssize_t size, Py_UCS4 maxchar) |
| 870 | { |
| 871 | PyObject *obj; |
| 872 | PyCompactUnicodeObject *unicode; |
| 873 | void *data; |
| 874 | int kind_state; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 875 | int is_sharing, is_ascii; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 876 | Py_ssize_t char_size; |
| 877 | Py_ssize_t struct_size; |
| 878 | |
| 879 | /* Optimization for empty strings */ |
| 880 | if (size == 0 && unicode_empty != NULL) { |
| 881 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 882 | return unicode_empty; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 883 | } |
| 884 | |
| 885 | #ifdef Py_DEBUG |
| 886 | ++unicode_new_new_calls; |
| 887 | #endif |
| 888 | |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 889 | is_ascii = 0; |
| 890 | is_sharing = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 891 | struct_size = sizeof(PyCompactUnicodeObject); |
| 892 | if (maxchar < 128) { |
| 893 | kind_state = PyUnicode_1BYTE_KIND; |
| 894 | char_size = 1; |
| 895 | is_ascii = 1; |
| 896 | struct_size = sizeof(PyASCIIObject); |
| 897 | } |
| 898 | else if (maxchar < 256) { |
| 899 | kind_state = PyUnicode_1BYTE_KIND; |
| 900 | char_size = 1; |
| 901 | } |
| 902 | else if (maxchar < 65536) { |
| 903 | kind_state = PyUnicode_2BYTE_KIND; |
| 904 | char_size = 2; |
| 905 | if (sizeof(wchar_t) == 2) |
| 906 | is_sharing = 1; |
| 907 | } |
| 908 | else { |
| 909 | kind_state = PyUnicode_4BYTE_KIND; |
| 910 | char_size = 4; |
| 911 | if (sizeof(wchar_t) == 4) |
| 912 | is_sharing = 1; |
| 913 | } |
| 914 | |
| 915 | /* Ensure we won't overflow the size. */ |
| 916 | if (size < 0) { |
| 917 | PyErr_SetString(PyExc_SystemError, |
| 918 | "Negative size passed to PyUnicode_New"); |
| 919 | return NULL; |
| 920 | } |
| 921 | if (size > ((PY_SSIZE_T_MAX - struct_size) / char_size - 1)) |
| 922 | return PyErr_NoMemory(); |
| 923 | |
| 924 | /* Duplicated allocation code from _PyObject_New() instead of a call to |
| 925 | * PyObject_New() so we are able to allocate space for the object and |
| 926 | * it's data buffer. |
| 927 | */ |
| 928 | obj = (PyObject *) PyObject_MALLOC(struct_size + (size + 1) * char_size); |
| 929 | if (obj == NULL) |
| 930 | return PyErr_NoMemory(); |
| 931 | obj = PyObject_INIT(obj, &PyUnicode_Type); |
| 932 | if (obj == NULL) |
| 933 | return NULL; |
| 934 | |
| 935 | unicode = (PyCompactUnicodeObject *)obj; |
| 936 | if (is_ascii) |
| 937 | data = ((PyASCIIObject*)obj) + 1; |
| 938 | else |
| 939 | data = unicode + 1; |
| 940 | _PyUnicode_LENGTH(unicode) = size; |
| 941 | _PyUnicode_HASH(unicode) = -1; |
| 942 | _PyUnicode_STATE(unicode).interned = 0; |
| 943 | _PyUnicode_STATE(unicode).kind = kind_state; |
| 944 | _PyUnicode_STATE(unicode).compact = 1; |
| 945 | _PyUnicode_STATE(unicode).ready = 1; |
| 946 | _PyUnicode_STATE(unicode).ascii = is_ascii; |
| 947 | if (is_ascii) { |
| 948 | ((char*)data)[size] = 0; |
| 949 | _PyUnicode_WSTR(unicode) = NULL; |
| 950 | } |
| 951 | else if (kind_state == PyUnicode_1BYTE_KIND) { |
| 952 | ((char*)data)[size] = 0; |
| 953 | _PyUnicode_WSTR(unicode) = NULL; |
| 954 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 955 | unicode->utf8 = NULL; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 956 | unicode->utf8_length = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 957 | } |
| 958 | else { |
| 959 | unicode->utf8 = NULL; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 960 | unicode->utf8_length = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 961 | if (kind_state == PyUnicode_2BYTE_KIND) |
| 962 | ((Py_UCS2*)data)[size] = 0; |
| 963 | else /* kind_state == PyUnicode_4BYTE_KIND */ |
| 964 | ((Py_UCS4*)data)[size] = 0; |
| 965 | if (is_sharing) { |
| 966 | _PyUnicode_WSTR_LENGTH(unicode) = size; |
| 967 | _PyUnicode_WSTR(unicode) = (wchar_t *)data; |
| 968 | } |
| 969 | else { |
| 970 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 971 | _PyUnicode_WSTR(unicode) = NULL; |
| 972 | } |
| 973 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 974 | assert(_PyUnicode_CheckConsistency(unicode, 0)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 975 | return obj; |
| 976 | } |
| 977 | |
| 978 | #if SIZEOF_WCHAR_T == 2 |
| 979 | /* Helper function to convert a 16-bits wchar_t representation to UCS4, this |
| 980 | will decode surrogate pairs, the other conversions are implemented as macros |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 981 | for efficiency. |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 982 | |
| 983 | This function assumes that unicode can hold one more code point than wstr |
| 984 | characters for a terminating null character. */ |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 985 | static void |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 986 | unicode_convert_wchar_to_ucs4(const wchar_t *begin, const wchar_t *end, |
| 987 | PyUnicodeObject *unicode) |
| 988 | { |
| 989 | const wchar_t *iter; |
| 990 | Py_UCS4 *ucs4_out; |
| 991 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 992 | assert(unicode != NULL); |
| 993 | assert(_PyUnicode_CHECK(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 994 | assert(_PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND); |
| 995 | ucs4_out = PyUnicode_4BYTE_DATA(unicode); |
| 996 | |
| 997 | for (iter = begin; iter < end; ) { |
| 998 | assert(ucs4_out < (PyUnicode_4BYTE_DATA(unicode) + |
| 999 | _PyUnicode_GET_LENGTH(unicode))); |
| 1000 | if (*iter >= 0xD800 && *iter <= 0xDBFF |
| 1001 | && (iter+1) < end && iter[1] >= 0xDC00 && iter[1] <= 0xDFFF) |
| 1002 | { |
| 1003 | *ucs4_out++ = (((iter[0] & 0x3FF)<<10) | (iter[1] & 0x3FF)) + 0x10000; |
| 1004 | iter += 2; |
| 1005 | } |
| 1006 | else { |
| 1007 | *ucs4_out++ = *iter; |
| 1008 | iter++; |
| 1009 | } |
| 1010 | } |
| 1011 | assert(ucs4_out == (PyUnicode_4BYTE_DATA(unicode) + |
| 1012 | _PyUnicode_GET_LENGTH(unicode))); |
| 1013 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1014 | } |
| 1015 | #endif |
| 1016 | |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1017 | static int |
| 1018 | _PyUnicode_Dirty(PyObject *unicode) |
| 1019 | { |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1020 | assert(_PyUnicode_CHECK(unicode)); |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1021 | if (Py_REFCNT(unicode) != 1) { |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 1022 | PyErr_SetString(PyExc_SystemError, |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1023 | "Cannot modify a string having more than 1 reference"); |
| 1024 | return -1; |
| 1025 | } |
| 1026 | _PyUnicode_DIRTY(unicode); |
| 1027 | return 0; |
| 1028 | } |
| 1029 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1030 | static int |
| 1031 | _copy_characters(PyObject *to, Py_ssize_t to_start, |
| 1032 | PyObject *from, Py_ssize_t from_start, |
| 1033 | Py_ssize_t how_many, int check_maxchar) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1034 | { |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1035 | unsigned int from_kind, to_kind; |
| 1036 | void *from_data, *to_data; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1037 | int fast; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1038 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1039 | assert(PyUnicode_Check(from)); |
| 1040 | assert(PyUnicode_Check(to)); |
| 1041 | assert(PyUnicode_IS_READY(from)); |
| 1042 | assert(PyUnicode_IS_READY(to)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1043 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1044 | assert(PyUnicode_GET_LENGTH(from) >= how_many); |
| 1045 | assert(to_start + how_many <= PyUnicode_GET_LENGTH(to)); |
| 1046 | assert(0 <= how_many); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1047 | |
Victor Stinner | f5ca1a2 | 2011-09-28 23:54:59 +0200 | [diff] [blame] | 1048 | if (how_many == 0) |
| 1049 | return 0; |
| 1050 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1051 | from_kind = PyUnicode_KIND(from); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1052 | from_data = PyUnicode_DATA(from); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1053 | to_kind = PyUnicode_KIND(to); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1054 | to_data = PyUnicode_DATA(to); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1055 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1056 | #ifdef Py_DEBUG |
| 1057 | if (!check_maxchar |
| 1058 | && (from_kind > to_kind |
| 1059 | || (!PyUnicode_IS_ASCII(from) && PyUnicode_IS_ASCII(to)))) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1060 | { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1061 | const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to); |
| 1062 | Py_UCS4 ch; |
| 1063 | Py_ssize_t i; |
| 1064 | for (i=0; i < how_many; i++) { |
| 1065 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
| 1066 | assert(ch <= to_maxchar); |
| 1067 | } |
| 1068 | } |
| 1069 | #endif |
| 1070 | fast = (from_kind == to_kind); |
| 1071 | if (check_maxchar |
| 1072 | && (!PyUnicode_IS_ASCII(from) && PyUnicode_IS_ASCII(to))) |
| 1073 | { |
| 1074 | /* deny latin1 => ascii */ |
| 1075 | fast = 0; |
| 1076 | } |
| 1077 | |
| 1078 | if (fast) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 1079 | Py_MEMCPY((char*)to_data + to_kind * to_start, |
| 1080 | (char*)from_data + from_kind * from_start, |
| 1081 | to_kind * how_many); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1082 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1083 | else if (from_kind == PyUnicode_1BYTE_KIND |
| 1084 | && to_kind == PyUnicode_2BYTE_KIND) |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1085 | { |
| 1086 | _PyUnicode_CONVERT_BYTES( |
| 1087 | Py_UCS1, Py_UCS2, |
| 1088 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 1089 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 1090 | PyUnicode_2BYTE_DATA(to) + to_start |
| 1091 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1092 | } |
Victor Stinner | 157f83f | 2011-09-28 21:41:31 +0200 | [diff] [blame] | 1093 | else if (from_kind == PyUnicode_1BYTE_KIND |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1094 | && to_kind == PyUnicode_4BYTE_KIND) |
| 1095 | { |
| 1096 | _PyUnicode_CONVERT_BYTES( |
| 1097 | Py_UCS1, Py_UCS4, |
| 1098 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 1099 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 1100 | PyUnicode_4BYTE_DATA(to) + to_start |
| 1101 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1102 | } |
| 1103 | else if (from_kind == PyUnicode_2BYTE_KIND |
| 1104 | && to_kind == PyUnicode_4BYTE_KIND) |
| 1105 | { |
| 1106 | _PyUnicode_CONVERT_BYTES( |
| 1107 | Py_UCS2, Py_UCS4, |
| 1108 | PyUnicode_2BYTE_DATA(from) + from_start, |
| 1109 | PyUnicode_2BYTE_DATA(from) + from_start + how_many, |
| 1110 | PyUnicode_4BYTE_DATA(to) + to_start |
| 1111 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1112 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1113 | else { |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1114 | /* check if max_char(from substring) <= max_char(to) */ |
| 1115 | if (from_kind > to_kind |
| 1116 | /* latin1 => ascii */ |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1117 | || (!PyUnicode_IS_ASCII(from) && PyUnicode_IS_ASCII(to))) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1118 | { |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1119 | /* slow path to check for character overflow */ |
| 1120 | const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1121 | Py_UCS4 ch; |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1122 | Py_ssize_t i; |
| 1123 | |
Victor Stinner | 56c161a | 2011-10-06 02:47:11 +0200 | [diff] [blame] | 1124 | #ifdef Py_DEBUG |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1125 | for (i=0; i < how_many; i++) { |
| 1126 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
Victor Stinner | 56c161a | 2011-10-06 02:47:11 +0200 | [diff] [blame] | 1127 | assert(ch <= to_maxchar); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1128 | PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); |
| 1129 | } |
Victor Stinner | 56c161a | 2011-10-06 02:47:11 +0200 | [diff] [blame] | 1130 | #else |
| 1131 | if (!check_maxchar) { |
| 1132 | for (i=0; i < how_many; i++) { |
| 1133 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
| 1134 | PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); |
| 1135 | } |
| 1136 | } |
| 1137 | else { |
| 1138 | for (i=0; i < how_many; i++) { |
| 1139 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
| 1140 | if (ch > to_maxchar) |
| 1141 | return 1; |
| 1142 | PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); |
| 1143 | } |
| 1144 | } |
| 1145 | #endif |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1146 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1147 | else { |
Victor Stinner | 56c161a | 2011-10-06 02:47:11 +0200 | [diff] [blame] | 1148 | assert(0 && "inconsistent state"); |
| 1149 | return 1; |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1150 | } |
| 1151 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1152 | return 0; |
| 1153 | } |
| 1154 | |
| 1155 | static void |
| 1156 | copy_characters(PyObject *to, Py_ssize_t to_start, |
| 1157 | PyObject *from, Py_ssize_t from_start, |
| 1158 | Py_ssize_t how_many) |
| 1159 | { |
| 1160 | (void)_copy_characters(to, to_start, from, from_start, how_many, 0); |
| 1161 | } |
| 1162 | |
| 1163 | Py_ssize_t |
| 1164 | PyUnicode_CopyCharacters(PyObject *to, Py_ssize_t to_start, |
| 1165 | PyObject *from, Py_ssize_t from_start, |
| 1166 | Py_ssize_t how_many) |
| 1167 | { |
| 1168 | int err; |
| 1169 | |
| 1170 | if (!PyUnicode_Check(from) || !PyUnicode_Check(to)) { |
| 1171 | PyErr_BadInternalCall(); |
| 1172 | return -1; |
| 1173 | } |
| 1174 | |
| 1175 | if (PyUnicode_READY(from)) |
| 1176 | return -1; |
| 1177 | if (PyUnicode_READY(to)) |
| 1178 | return -1; |
| 1179 | |
| 1180 | how_many = Py_MIN(PyUnicode_GET_LENGTH(from), how_many); |
| 1181 | if (to_start + how_many > PyUnicode_GET_LENGTH(to)) { |
| 1182 | PyErr_Format(PyExc_SystemError, |
| 1183 | "Cannot write %zi characters at %zi " |
| 1184 | "in a string of %zi characters", |
| 1185 | how_many, to_start, PyUnicode_GET_LENGTH(to)); |
| 1186 | return -1; |
| 1187 | } |
| 1188 | |
| 1189 | if (how_many == 0) |
| 1190 | return 0; |
| 1191 | |
| 1192 | if (_PyUnicode_Dirty(to)) |
| 1193 | return -1; |
| 1194 | |
| 1195 | err = _copy_characters(to, to_start, from, from_start, how_many, 1); |
| 1196 | if (err) { |
| 1197 | PyErr_Format(PyExc_SystemError, |
| 1198 | "Cannot copy %s characters " |
| 1199 | "into a string of %s characters", |
| 1200 | unicode_kind_name(from), |
| 1201 | unicode_kind_name(to)); |
| 1202 | return -1; |
| 1203 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1204 | return how_many; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1205 | } |
| 1206 | |
Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 1207 | /* Find the maximum code point and count the number of surrogate pairs so a |
| 1208 | correct string length can be computed before converting a string to UCS4. |
| 1209 | This function counts single surrogates as a character and not as a pair. |
| 1210 | |
| 1211 | Return 0 on success, or -1 on error. */ |
| 1212 | static int |
| 1213 | find_maxchar_surrogates(const wchar_t *begin, const wchar_t *end, |
| 1214 | Py_UCS4 *maxchar, Py_ssize_t *num_surrogates) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1215 | { |
| 1216 | const wchar_t *iter; |
| 1217 | |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1218 | assert(num_surrogates != NULL && maxchar != NULL); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1219 | *num_surrogates = 0; |
| 1220 | *maxchar = 0; |
| 1221 | |
| 1222 | for (iter = begin; iter < end; ) { |
Victor Stinner | ae86485 | 2011-10-05 14:02:44 +0200 | [diff] [blame] | 1223 | if (*iter > *maxchar) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1224 | *maxchar = *iter; |
Victor Stinner | ae86485 | 2011-10-05 14:02:44 +0200 | [diff] [blame] | 1225 | #if SIZEOF_WCHAR_T != 2 |
| 1226 | if (*maxchar >= 0x10000) |
| 1227 | return 0; |
| 1228 | #endif |
| 1229 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1230 | #if SIZEOF_WCHAR_T == 2 |
| 1231 | if (*iter >= 0xD800 && *iter <= 0xDBFF |
| 1232 | && (iter+1) < end && iter[1] >= 0xDC00 && iter[1] <= 0xDFFF) |
| 1233 | { |
| 1234 | Py_UCS4 surrogate_val; |
| 1235 | surrogate_val = (((iter[0] & 0x3FF)<<10) |
| 1236 | | (iter[1] & 0x3FF)) + 0x10000; |
| 1237 | ++(*num_surrogates); |
| 1238 | if (surrogate_val > *maxchar) |
| 1239 | *maxchar = surrogate_val; |
| 1240 | iter += 2; |
| 1241 | } |
| 1242 | else |
| 1243 | iter++; |
| 1244 | #else |
| 1245 | iter++; |
| 1246 | #endif |
| 1247 | } |
| 1248 | return 0; |
| 1249 | } |
| 1250 | |
| 1251 | #ifdef Py_DEBUG |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 1252 | static int unicode_ready_calls = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1253 | #endif |
| 1254 | |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 1255 | static int |
| 1256 | unicode_ready(PyObject **p_obj, int replace) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1257 | { |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 1258 | PyUnicodeObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1259 | wchar_t *end; |
| 1260 | Py_UCS4 maxchar = 0; |
| 1261 | Py_ssize_t num_surrogates; |
| 1262 | #if SIZEOF_WCHAR_T == 2 |
| 1263 | Py_ssize_t length_wo_surrogates; |
| 1264 | #endif |
| 1265 | |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 1266 | assert(p_obj != NULL); |
| 1267 | unicode = (PyUnicodeObject *)*p_obj; |
| 1268 | |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 1269 | /* _PyUnicode_Ready() is only intended for old-style API usage where |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1270 | strings were created using _PyObject_New() and where no canonical |
| 1271 | representation (the str field) has been set yet aka strings |
| 1272 | which are not yet ready. */ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1273 | assert(_PyUnicode_CHECK(unicode)); |
| 1274 | assert(_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1275 | assert(_PyUnicode_WSTR(unicode) != NULL); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1276 | assert(_PyUnicode_DATA_ANY(unicode) == NULL); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1277 | assert(_PyUnicode_UTF8(unicode) == NULL); |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1278 | /* Actually, it should neither be interned nor be anything else: */ |
| 1279 | assert(_PyUnicode_STATE(unicode).interned == SSTATE_NOT_INTERNED); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1280 | |
| 1281 | #ifdef Py_DEBUG |
| 1282 | ++unicode_ready_calls; |
| 1283 | #endif |
| 1284 | |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 1285 | #ifdef Py_DEBUG |
| 1286 | assert(!replace || Py_REFCNT(unicode) == 1); |
| 1287 | #else |
| 1288 | if (replace && Py_REFCNT(unicode) != 1) |
| 1289 | replace = 0; |
| 1290 | #endif |
| 1291 | if (replace) { |
| 1292 | Py_ssize_t len = _PyUnicode_WSTR_LENGTH(unicode); |
| 1293 | wchar_t *wstr = _PyUnicode_WSTR(unicode); |
| 1294 | /* Optimization for empty strings */ |
| 1295 | if (len == 0) { |
| 1296 | Py_INCREF(unicode_empty); |
| 1297 | Py_DECREF(*p_obj); |
| 1298 | *p_obj = unicode_empty; |
| 1299 | return 0; |
| 1300 | } |
| 1301 | if (len == 1 && wstr[0] < 256) { |
| 1302 | PyObject *latin1_char = get_latin1_char((unsigned char)wstr[0]); |
| 1303 | if (latin1_char == NULL) |
| 1304 | return -1; |
| 1305 | Py_DECREF(*p_obj); |
| 1306 | *p_obj = latin1_char; |
| 1307 | return 0; |
| 1308 | } |
| 1309 | } |
| 1310 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1311 | end = _PyUnicode_WSTR(unicode) + _PyUnicode_WSTR_LENGTH(unicode); |
Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 1312 | if (find_maxchar_surrogates(_PyUnicode_WSTR(unicode), end, |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1313 | &maxchar, &num_surrogates) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1314 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1315 | |
| 1316 | if (maxchar < 256) { |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1317 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(_PyUnicode_WSTR_LENGTH(unicode) + 1); |
| 1318 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1319 | PyErr_NoMemory(); |
| 1320 | return -1; |
| 1321 | } |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1322 | _PyUnicode_CONVERT_BYTES(wchar_t, unsigned char, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1323 | _PyUnicode_WSTR(unicode), end, |
| 1324 | PyUnicode_1BYTE_DATA(unicode)); |
| 1325 | PyUnicode_1BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1326 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1327 | _PyUnicode_STATE(unicode).kind = PyUnicode_1BYTE_KIND; |
| 1328 | if (maxchar < 128) { |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1329 | _PyUnicode_STATE(unicode).ascii = 1; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1330 | _PyUnicode_UTF8(unicode) = _PyUnicode_DATA_ANY(unicode); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1331 | _PyUnicode_UTF8_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1332 | } |
| 1333 | else { |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1334 | _PyUnicode_STATE(unicode).ascii = 0; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1335 | _PyUnicode_UTF8(unicode) = NULL; |
| 1336 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1337 | } |
| 1338 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1339 | _PyUnicode_WSTR(unicode) = NULL; |
| 1340 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1341 | } |
| 1342 | /* In this case we might have to convert down from 4-byte native |
| 1343 | wchar_t to 2-byte unicode. */ |
| 1344 | else if (maxchar < 65536) { |
| 1345 | assert(num_surrogates == 0 && |
| 1346 | "FindMaxCharAndNumSurrogatePairs() messed up"); |
| 1347 | |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1348 | #if SIZEOF_WCHAR_T == 2 |
| 1349 | /* We can share representations and are done. */ |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1350 | _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1351 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1352 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1353 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1354 | _PyUnicode_UTF8(unicode) = NULL; |
| 1355 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1356 | #else |
| 1357 | /* sizeof(wchar_t) == 4 */ |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1358 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC( |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1359 | 2 * (_PyUnicode_WSTR_LENGTH(unicode) + 1)); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1360 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1361 | PyErr_NoMemory(); |
| 1362 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1363 | } |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1364 | _PyUnicode_CONVERT_BYTES(wchar_t, Py_UCS2, |
| 1365 | _PyUnicode_WSTR(unicode), end, |
| 1366 | PyUnicode_2BYTE_DATA(unicode)); |
| 1367 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1368 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1369 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1370 | _PyUnicode_UTF8(unicode) = NULL; |
| 1371 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1372 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1373 | _PyUnicode_WSTR(unicode) = NULL; |
| 1374 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1375 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1376 | } |
| 1377 | /* maxchar exeeds 16 bit, wee need 4 bytes for unicode characters */ |
| 1378 | else { |
| 1379 | #if SIZEOF_WCHAR_T == 2 |
| 1380 | /* in case the native representation is 2-bytes, we need to allocate a |
| 1381 | new normalized 4-byte version. */ |
| 1382 | length_wo_surrogates = _PyUnicode_WSTR_LENGTH(unicode) - num_surrogates; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1383 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(4 * (length_wo_surrogates + 1)); |
| 1384 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1385 | PyErr_NoMemory(); |
| 1386 | return -1; |
| 1387 | } |
| 1388 | _PyUnicode_LENGTH(unicode) = length_wo_surrogates; |
| 1389 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1390 | _PyUnicode_UTF8(unicode) = NULL; |
| 1391 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 126c559 | 2011-10-03 04:17:10 +0200 | [diff] [blame] | 1392 | /* unicode_convert_wchar_to_ucs4() requires a ready string */ |
| 1393 | _PyUnicode_STATE(unicode).ready = 1; |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1394 | unicode_convert_wchar_to_ucs4(_PyUnicode_WSTR(unicode), end, unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1395 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1396 | _PyUnicode_WSTR(unicode) = NULL; |
| 1397 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1398 | #else |
| 1399 | assert(num_surrogates == 0); |
| 1400 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1401 | _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1402 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1403 | _PyUnicode_UTF8(unicode) = NULL; |
| 1404 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1405 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; |
| 1406 | #endif |
| 1407 | PyUnicode_4BYTE_DATA(unicode)[_PyUnicode_LENGTH(unicode)] = '\0'; |
| 1408 | } |
| 1409 | _PyUnicode_STATE(unicode).ready = 1; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1410 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1411 | return 0; |
| 1412 | } |
| 1413 | |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 1414 | int |
| 1415 | _PyUnicode_ReadyReplace(PyObject **op) |
| 1416 | { |
| 1417 | return unicode_ready(op, 1); |
| 1418 | } |
| 1419 | |
| 1420 | int |
| 1421 | _PyUnicode_Ready(PyObject *op) |
| 1422 | { |
| 1423 | return unicode_ready(&op, 0); |
| 1424 | } |
| 1425 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1426 | static void |
| 1427 | unicode_dealloc(register PyUnicodeObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1428 | { |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1429 | switch (PyUnicode_CHECK_INTERNED(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1430 | case SSTATE_NOT_INTERNED: |
| 1431 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1432 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1433 | case SSTATE_INTERNED_MORTAL: |
| 1434 | /* revive dead object temporarily for DelItem */ |
| 1435 | Py_REFCNT(unicode) = 3; |
| 1436 | if (PyDict_DelItem(interned, (PyObject *)unicode) != 0) |
| 1437 | Py_FatalError( |
| 1438 | "deletion of interned string failed"); |
| 1439 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1440 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1441 | case SSTATE_INTERNED_IMMORTAL: |
| 1442 | Py_FatalError("Immortal interned string died."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1443 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1444 | default: |
| 1445 | Py_FatalError("Inconsistent interned string state."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1446 | } |
| 1447 | |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 1448 | if (_PyUnicode_HAS_WSTR_MEMORY(unicode)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1449 | PyObject_DEL(_PyUnicode_WSTR(unicode)); |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 1450 | if (_PyUnicode_HAS_UTF8_MEMORY(unicode)) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1451 | PyObject_DEL(_PyUnicode_UTF8(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1452 | |
| 1453 | if (PyUnicode_IS_COMPACT(unicode)) { |
| 1454 | Py_TYPE(unicode)->tp_free((PyObject *)unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1455 | } |
| 1456 | else { |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1457 | if (_PyUnicode_DATA_ANY(unicode)) |
| 1458 | PyObject_DEL(_PyUnicode_DATA_ANY(unicode)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1459 | Py_TYPE(unicode)->tp_free((PyObject *)unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1460 | } |
| 1461 | } |
| 1462 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1463 | #ifdef Py_DEBUG |
| 1464 | static int |
| 1465 | unicode_is_singleton(PyObject *unicode) |
| 1466 | { |
| 1467 | PyASCIIObject *ascii = (PyASCIIObject *)unicode; |
| 1468 | if (unicode == unicode_empty) |
| 1469 | return 1; |
| 1470 | if (ascii->state.kind != PyUnicode_WCHAR_KIND && ascii->length == 1) |
| 1471 | { |
| 1472 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, 0); |
| 1473 | if (ch < 256 && unicode_latin1[ch] == unicode) |
| 1474 | return 1; |
| 1475 | } |
| 1476 | return 0; |
| 1477 | } |
| 1478 | #endif |
| 1479 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1480 | static int |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1481 | unicode_resizable(PyObject *unicode) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1482 | { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1483 | if (Py_REFCNT(unicode) != 1) |
| 1484 | return 0; |
| 1485 | if (PyUnicode_CHECK_INTERNED(unicode)) |
| 1486 | return 0; |
Victor Stinner | 77bb47b | 2011-10-03 20:06:05 +0200 | [diff] [blame] | 1487 | #ifdef Py_DEBUG |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1488 | /* singleton refcount is greater than 1 */ |
| 1489 | assert(!unicode_is_singleton(unicode)); |
Victor Stinner | 77bb47b | 2011-10-03 20:06:05 +0200 | [diff] [blame] | 1490 | #endif |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1491 | return 1; |
| 1492 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1493 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1494 | static int |
| 1495 | unicode_resize(PyObject **p_unicode, Py_ssize_t length) |
| 1496 | { |
| 1497 | PyObject *unicode; |
| 1498 | Py_ssize_t old_length; |
| 1499 | |
| 1500 | assert(p_unicode != NULL); |
| 1501 | unicode = *p_unicode; |
| 1502 | |
| 1503 | assert(unicode != NULL); |
| 1504 | assert(PyUnicode_Check(unicode)); |
| 1505 | assert(0 <= length); |
| 1506 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1507 | if (_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1508 | old_length = PyUnicode_WSTR_LENGTH(unicode); |
| 1509 | else |
| 1510 | old_length = PyUnicode_GET_LENGTH(unicode); |
| 1511 | if (old_length == length) |
| 1512 | return 0; |
| 1513 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1514 | if (!unicode_resizable(unicode)) { |
| 1515 | PyObject *copy = resize_copy(unicode, length); |
| 1516 | if (copy == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1517 | return -1; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1518 | Py_DECREF(*p_unicode); |
| 1519 | *p_unicode = copy; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1520 | return 0; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1521 | } |
| 1522 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1523 | if (PyUnicode_IS_COMPACT(unicode)) { |
| 1524 | *p_unicode = resize_compact(unicode, length); |
| 1525 | if (*p_unicode == NULL) |
| 1526 | return -1; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1527 | assert(_PyUnicode_CheckConsistency(*p_unicode, 0)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1528 | return 0; |
Benjamin Peterson | 4bfce8f | 2011-10-03 19:35:07 -0400 | [diff] [blame] | 1529 | } |
| 1530 | return resize_inplace((PyUnicodeObject*)unicode, length); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1531 | } |
| 1532 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1533 | int |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1534 | PyUnicode_Resize(PyObject **p_unicode, Py_ssize_t length) |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1535 | { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1536 | PyObject *unicode; |
| 1537 | if (p_unicode == NULL) { |
| 1538 | PyErr_BadInternalCall(); |
| 1539 | return -1; |
| 1540 | } |
| 1541 | unicode = *p_unicode; |
| 1542 | if (unicode == NULL || !PyUnicode_Check(unicode) || length < 0 |
| 1543 | || _PyUnicode_KIND(unicode) != PyUnicode_WCHAR_KIND) |
| 1544 | { |
| 1545 | PyErr_BadInternalCall(); |
| 1546 | return -1; |
| 1547 | } |
| 1548 | return unicode_resize(p_unicode, length); |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1549 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1550 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1551 | static PyObject* |
| 1552 | get_latin1_char(unsigned char ch) |
| 1553 | { |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1554 | PyObject *unicode = unicode_latin1[ch]; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1555 | if (!unicode) { |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1556 | unicode = PyUnicode_New(1, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1557 | if (!unicode) |
| 1558 | return NULL; |
| 1559 | PyUnicode_1BYTE_DATA(unicode)[0] = ch; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1560 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1561 | unicode_latin1[ch] = unicode; |
| 1562 | } |
| 1563 | Py_INCREF(unicode); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1564 | return unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1565 | } |
| 1566 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1567 | PyObject * |
| 1568 | PyUnicode_FromUnicode(const Py_UNICODE *u, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1569 | { |
| 1570 | PyUnicodeObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1571 | Py_UCS4 maxchar = 0; |
| 1572 | Py_ssize_t num_surrogates; |
| 1573 | |
| 1574 | if (u == NULL) |
| 1575 | return (PyObject*)_PyUnicode_New(size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1576 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1577 | /* If the Unicode data is known at construction time, we can apply |
| 1578 | some optimizations which share commonly used objects. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1579 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1580 | /* Optimization for empty strings */ |
| 1581 | if (size == 0 && unicode_empty != NULL) { |
| 1582 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1583 | return unicode_empty; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1584 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1585 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1586 | /* Single character Unicode objects in the Latin-1 range are |
| 1587 | shared when using this constructor */ |
| 1588 | if (size == 1 && *u < 256) |
| 1589 | return get_latin1_char((unsigned char)*u); |
| 1590 | |
| 1591 | /* If not empty and not single character, copy the Unicode data |
| 1592 | into the new object */ |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1593 | if (find_maxchar_surrogates(u, u + size, |
| 1594 | &maxchar, &num_surrogates) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1595 | return NULL; |
| 1596 | |
| 1597 | unicode = (PyUnicodeObject *) PyUnicode_New(size - num_surrogates, |
| 1598 | maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1599 | if (!unicode) |
| 1600 | return NULL; |
| 1601 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1602 | switch (PyUnicode_KIND(unicode)) { |
| 1603 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1604 | _PyUnicode_CONVERT_BYTES(Py_UNICODE, unsigned char, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1605 | u, u + size, PyUnicode_1BYTE_DATA(unicode)); |
| 1606 | break; |
| 1607 | case PyUnicode_2BYTE_KIND: |
| 1608 | #if Py_UNICODE_SIZE == 2 |
| 1609 | Py_MEMCPY(PyUnicode_2BYTE_DATA(unicode), u, size * 2); |
| 1610 | #else |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1611 | _PyUnicode_CONVERT_BYTES(Py_UNICODE, Py_UCS2, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1612 | u, u + size, PyUnicode_2BYTE_DATA(unicode)); |
| 1613 | #endif |
| 1614 | break; |
| 1615 | case PyUnicode_4BYTE_KIND: |
| 1616 | #if SIZEOF_WCHAR_T == 2 |
| 1617 | /* This is the only case which has to process surrogates, thus |
| 1618 | a simple copy loop is not enough and we need a function. */ |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1619 | unicode_convert_wchar_to_ucs4(u, u + size, unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1620 | #else |
| 1621 | assert(num_surrogates == 0); |
| 1622 | Py_MEMCPY(PyUnicode_4BYTE_DATA(unicode), u, size * 4); |
| 1623 | #endif |
| 1624 | break; |
| 1625 | default: |
| 1626 | assert(0 && "Impossible state"); |
| 1627 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1628 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1629 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1630 | return (PyObject *)unicode; |
| 1631 | } |
| 1632 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1633 | PyObject * |
| 1634 | PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size) |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1635 | { |
| 1636 | PyUnicodeObject *unicode; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1637 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1638 | if (size < 0) { |
| 1639 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1640 | "Negative size passed to PyUnicode_FromStringAndSize"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1641 | return NULL; |
| 1642 | } |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1643 | |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1644 | /* 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] | 1645 | some optimizations which share commonly used objects. |
| 1646 | Also, this means the input must be UTF-8, so fall back to the |
| 1647 | UTF-8 decoder at the end. */ |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1648 | if (u != NULL) { |
| 1649 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1650 | /* Optimization for empty strings */ |
| 1651 | if (size == 0 && unicode_empty != NULL) { |
| 1652 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1653 | return unicode_empty; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1654 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1655 | |
| 1656 | /* Single characters are shared when using this constructor. |
| 1657 | Restrict to ASCII, since the input must be UTF-8. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1658 | if (size == 1 && Py_CHARMASK(*u) < 128) |
| 1659 | return get_latin1_char(Py_CHARMASK(*u)); |
Martin v. Löwis | 9c12106 | 2007-08-05 20:26:11 +0000 | [diff] [blame] | 1660 | |
| 1661 | return PyUnicode_DecodeUTF8(u, size, NULL); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1662 | } |
| 1663 | |
Walter Dörwald | 5550731 | 2007-05-18 13:12:10 +0000 | [diff] [blame] | 1664 | unicode = _PyUnicode_New(size); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1665 | if (!unicode) |
| 1666 | return NULL; |
| 1667 | |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1668 | return (PyObject *)unicode; |
| 1669 | } |
| 1670 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1671 | PyObject * |
| 1672 | PyUnicode_FromString(const char *u) |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1673 | { |
| 1674 | size_t size = strlen(u); |
| 1675 | if (size > PY_SSIZE_T_MAX) { |
| 1676 | PyErr_SetString(PyExc_OverflowError, "input too long"); |
| 1677 | return NULL; |
| 1678 | } |
| 1679 | |
| 1680 | return PyUnicode_FromStringAndSize(u, size); |
| 1681 | } |
| 1682 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1683 | PyObject * |
| 1684 | _PyUnicode_FromId(_Py_Identifier *id) |
| 1685 | { |
| 1686 | if (!id->object) { |
| 1687 | id->object = PyUnicode_FromString(id->string); |
| 1688 | if (!id->object) |
| 1689 | return NULL; |
| 1690 | PyUnicode_InternInPlace(&id->object); |
| 1691 | assert(!id->next); |
| 1692 | id->next = static_strings; |
| 1693 | static_strings = id; |
| 1694 | } |
| 1695 | Py_INCREF(id->object); |
| 1696 | return id->object; |
| 1697 | } |
| 1698 | |
| 1699 | void |
| 1700 | _PyUnicode_ClearStaticStrings() |
| 1701 | { |
| 1702 | _Py_Identifier *i; |
| 1703 | for (i = static_strings; i; i = i->next) { |
| 1704 | Py_DECREF(i->object); |
| 1705 | i->object = NULL; |
| 1706 | i->next = NULL; |
| 1707 | } |
| 1708 | } |
| 1709 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1710 | static PyObject* |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1711 | unicode_fromascii(const unsigned char* s, Py_ssize_t size) |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1712 | { |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1713 | PyObject *res; |
| 1714 | #ifdef Py_DEBUG |
| 1715 | const unsigned char *p; |
| 1716 | const unsigned char *end = s + size; |
| 1717 | for (p=s; p < end; p++) { |
| 1718 | assert(*p < 128); |
| 1719 | } |
| 1720 | #endif |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1721 | if (size == 1) |
| 1722 | return get_latin1_char(s[0]); |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1723 | res = PyUnicode_New(size, 127); |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1724 | if (!res) |
| 1725 | return NULL; |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1726 | memcpy(PyUnicode_1BYTE_DATA(res), s, size); |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1727 | return res; |
| 1728 | } |
| 1729 | |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 1730 | static Py_UCS4 |
| 1731 | kind_maxchar_limit(unsigned int kind) |
| 1732 | { |
| 1733 | switch(kind) { |
| 1734 | case PyUnicode_1BYTE_KIND: |
| 1735 | return 0x80; |
| 1736 | case PyUnicode_2BYTE_KIND: |
| 1737 | return 0x100; |
| 1738 | case PyUnicode_4BYTE_KIND: |
| 1739 | return 0x10000; |
| 1740 | default: |
| 1741 | assert(0 && "invalid kind"); |
| 1742 | return 0x10ffff; |
| 1743 | } |
| 1744 | } |
| 1745 | |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1746 | static PyObject* |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1747 | _PyUnicode_FromUCS1(const unsigned char* u, Py_ssize_t size) |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1748 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1749 | PyObject *res; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1750 | unsigned char max_char = 127; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1751 | |
| 1752 | assert(size >= 0); |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1753 | if (size == 1) |
| 1754 | return get_latin1_char(u[0]); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1755 | max_char = ucs1lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1756 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1757 | if (!res) |
| 1758 | return NULL; |
| 1759 | memcpy(PyUnicode_1BYTE_DATA(res), u, size); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1760 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1761 | return res; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1762 | } |
| 1763 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1764 | static PyObject* |
| 1765 | _PyUnicode_FromUCS2(const Py_UCS2 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1766 | { |
| 1767 | PyObject *res; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1768 | Py_UCS2 max_char = 0; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1769 | |
| 1770 | assert(size >= 0); |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1771 | if (size == 1 && u[0] < 256) |
Victor Stinner | 4e10100 | 2011-10-11 23:27:52 +0200 | [diff] [blame] | 1772 | return get_latin1_char((unsigned char)u[0]); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1773 | max_char = ucs2lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1774 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1775 | if (!res) |
| 1776 | return NULL; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1777 | if (max_char >= 256) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1778 | memcpy(PyUnicode_2BYTE_DATA(res), u, sizeof(Py_UCS2)*size); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1779 | else { |
| 1780 | _PyUnicode_CONVERT_BYTES( |
| 1781 | Py_UCS2, Py_UCS1, u, u + size, PyUnicode_1BYTE_DATA(res)); |
| 1782 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1783 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1784 | return res; |
| 1785 | } |
| 1786 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1787 | static PyObject* |
| 1788 | _PyUnicode_FromUCS4(const Py_UCS4 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1789 | { |
| 1790 | PyObject *res; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1791 | Py_UCS4 max_char = 0; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1792 | |
| 1793 | assert(size >= 0); |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1794 | if (size == 1 && u[0] < 256) |
| 1795 | return get_latin1_char(u[0]); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1796 | max_char = ucs4lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1797 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1798 | if (!res) |
| 1799 | return NULL; |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 1800 | if (max_char < 256) |
| 1801 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS1, u, u + size, |
| 1802 | PyUnicode_1BYTE_DATA(res)); |
| 1803 | else if (max_char < 0x10000) |
| 1804 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS2, u, u + size, |
| 1805 | PyUnicode_2BYTE_DATA(res)); |
| 1806 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1807 | memcpy(PyUnicode_4BYTE_DATA(res), u, sizeof(Py_UCS4)*size); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1808 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1809 | return res; |
| 1810 | } |
| 1811 | |
| 1812 | PyObject* |
| 1813 | PyUnicode_FromKindAndData(int kind, const void *buffer, Py_ssize_t size) |
| 1814 | { |
| 1815 | switch(kind) { |
| 1816 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1817 | return _PyUnicode_FromUCS1(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1818 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1819 | return _PyUnicode_FromUCS2(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1820 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1821 | return _PyUnicode_FromUCS4(buffer, size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1822 | default: |
| 1823 | assert(0 && "invalid kind"); |
| 1824 | PyErr_SetString(PyExc_SystemError, "invalid kind"); |
| 1825 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1826 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1827 | } |
| 1828 | |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1829 | /* Ensure that a string uses the most efficient storage, if it is not the |
| 1830 | case: create a new string with of the right kind. Write NULL into *p_unicode |
| 1831 | on error. */ |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 1832 | static void |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1833 | unicode_adjust_maxchar(PyObject **p_unicode) |
| 1834 | { |
| 1835 | PyObject *unicode, *copy; |
| 1836 | Py_UCS4 max_char; |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1837 | Py_ssize_t len; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1838 | unsigned int kind; |
| 1839 | |
| 1840 | assert(p_unicode != NULL); |
| 1841 | unicode = *p_unicode; |
| 1842 | assert(PyUnicode_IS_READY(unicode)); |
| 1843 | if (PyUnicode_IS_ASCII(unicode)) |
| 1844 | return; |
| 1845 | |
| 1846 | len = PyUnicode_GET_LENGTH(unicode); |
| 1847 | kind = PyUnicode_KIND(unicode); |
| 1848 | if (kind == PyUnicode_1BYTE_KIND) { |
| 1849 | const Py_UCS1 *u = PyUnicode_1BYTE_DATA(unicode); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1850 | max_char = ucs1lib_find_max_char(u, u + len); |
| 1851 | if (max_char >= 128) |
| 1852 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1853 | } |
| 1854 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 1855 | const Py_UCS2 *u = PyUnicode_2BYTE_DATA(unicode); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1856 | max_char = ucs2lib_find_max_char(u, u + len); |
| 1857 | if (max_char >= 256) |
| 1858 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1859 | } |
| 1860 | else { |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1861 | const Py_UCS4 *u = PyUnicode_4BYTE_DATA(unicode); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1862 | assert(kind == PyUnicode_4BYTE_KIND); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1863 | max_char = ucs4lib_find_max_char(u, u + len); |
| 1864 | if (max_char >= 0x10000) |
| 1865 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1866 | } |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 1867 | copy = PyUnicode_New(len, max_char); |
| 1868 | copy_characters(copy, 0, unicode, 0, len); |
| 1869 | Py_DECREF(unicode); |
| 1870 | *p_unicode = copy; |
| 1871 | } |
| 1872 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 1873 | PyObject* |
| 1874 | PyUnicode_Copy(PyObject *unicode) |
| 1875 | { |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 1876 | Py_ssize_t size; |
| 1877 | PyObject *copy; |
| 1878 | void *data; |
| 1879 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 1880 | if (!PyUnicode_Check(unicode)) { |
| 1881 | PyErr_BadInternalCall(); |
| 1882 | return NULL; |
| 1883 | } |
| 1884 | if (PyUnicode_READY(unicode)) |
| 1885 | return NULL; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 1886 | |
| 1887 | size = PyUnicode_GET_LENGTH(unicode); |
| 1888 | copy = PyUnicode_New(size, PyUnicode_MAX_CHAR_VALUE(unicode)); |
| 1889 | if (!copy) |
| 1890 | return NULL; |
| 1891 | assert(PyUnicode_KIND(copy) == PyUnicode_KIND(unicode)); |
| 1892 | |
| 1893 | data = PyUnicode_DATA(unicode); |
| 1894 | switch (PyUnicode_KIND(unicode)) |
| 1895 | { |
| 1896 | case PyUnicode_1BYTE_KIND: |
| 1897 | memcpy(PyUnicode_1BYTE_DATA(copy), data, size); |
| 1898 | break; |
| 1899 | case PyUnicode_2BYTE_KIND: |
| 1900 | memcpy(PyUnicode_2BYTE_DATA(copy), data, sizeof(Py_UCS2) * size); |
| 1901 | break; |
| 1902 | case PyUnicode_4BYTE_KIND: |
| 1903 | memcpy(PyUnicode_4BYTE_DATA(copy), data, sizeof(Py_UCS4) * size); |
| 1904 | break; |
| 1905 | default: |
| 1906 | assert(0); |
| 1907 | break; |
| 1908 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1909 | assert(_PyUnicode_CheckConsistency(copy, 1)); |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 1910 | return copy; |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 1911 | } |
| 1912 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1913 | |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1914 | /* Widen Unicode objects to larger buffers. Don't write terminating null |
| 1915 | character. Return NULL on error. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1916 | |
| 1917 | void* |
| 1918 | _PyUnicode_AsKind(PyObject *s, unsigned int kind) |
| 1919 | { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1920 | Py_ssize_t len; |
| 1921 | void *result; |
| 1922 | unsigned int skind; |
| 1923 | |
| 1924 | if (PyUnicode_READY(s)) |
| 1925 | return NULL; |
| 1926 | |
| 1927 | len = PyUnicode_GET_LENGTH(s); |
| 1928 | skind = PyUnicode_KIND(s); |
| 1929 | if (skind >= kind) { |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 1930 | PyErr_SetString(PyExc_SystemError, "invalid widening attempt"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1931 | return NULL; |
| 1932 | } |
| 1933 | switch(kind) { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1934 | case PyUnicode_2BYTE_KIND: |
| 1935 | result = PyMem_Malloc(len * sizeof(Py_UCS2)); |
| 1936 | if (!result) |
| 1937 | return PyErr_NoMemory(); |
| 1938 | assert(skind == PyUnicode_1BYTE_KIND); |
| 1939 | _PyUnicode_CONVERT_BYTES( |
| 1940 | Py_UCS1, Py_UCS2, |
| 1941 | PyUnicode_1BYTE_DATA(s), |
| 1942 | PyUnicode_1BYTE_DATA(s) + len, |
| 1943 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1944 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1945 | case PyUnicode_4BYTE_KIND: |
| 1946 | result = PyMem_Malloc(len * sizeof(Py_UCS4)); |
| 1947 | if (!result) |
| 1948 | return PyErr_NoMemory(); |
| 1949 | if (skind == PyUnicode_2BYTE_KIND) { |
| 1950 | _PyUnicode_CONVERT_BYTES( |
| 1951 | Py_UCS2, Py_UCS4, |
| 1952 | PyUnicode_2BYTE_DATA(s), |
| 1953 | PyUnicode_2BYTE_DATA(s) + len, |
| 1954 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1955 | } |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1956 | else { |
| 1957 | assert(skind == PyUnicode_1BYTE_KIND); |
| 1958 | _PyUnicode_CONVERT_BYTES( |
| 1959 | Py_UCS1, Py_UCS4, |
| 1960 | PyUnicode_1BYTE_DATA(s), |
| 1961 | PyUnicode_1BYTE_DATA(s) + len, |
| 1962 | result); |
| 1963 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1964 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1965 | default: |
| 1966 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1967 | } |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 1968 | PyErr_SetString(PyExc_SystemError, "invalid kind"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1969 | return NULL; |
| 1970 | } |
| 1971 | |
| 1972 | static Py_UCS4* |
| 1973 | as_ucs4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 1974 | int copy_null) |
| 1975 | { |
| 1976 | int kind; |
| 1977 | void *data; |
| 1978 | Py_ssize_t len, targetlen; |
| 1979 | if (PyUnicode_READY(string) == -1) |
| 1980 | return NULL; |
| 1981 | kind = PyUnicode_KIND(string); |
| 1982 | data = PyUnicode_DATA(string); |
| 1983 | len = PyUnicode_GET_LENGTH(string); |
| 1984 | targetlen = len; |
| 1985 | if (copy_null) |
| 1986 | targetlen++; |
| 1987 | if (!target) { |
| 1988 | if (PY_SSIZE_T_MAX / sizeof(Py_UCS4) < targetlen) { |
| 1989 | PyErr_NoMemory(); |
| 1990 | return NULL; |
| 1991 | } |
| 1992 | target = PyMem_Malloc(targetlen * sizeof(Py_UCS4)); |
| 1993 | if (!target) { |
| 1994 | PyErr_NoMemory(); |
| 1995 | return NULL; |
| 1996 | } |
| 1997 | } |
| 1998 | else { |
| 1999 | if (targetsize < targetlen) { |
| 2000 | PyErr_Format(PyExc_SystemError, |
| 2001 | "string is longer than the buffer"); |
| 2002 | if (copy_null && 0 < targetsize) |
| 2003 | target[0] = 0; |
| 2004 | return NULL; |
| 2005 | } |
| 2006 | } |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2007 | if (kind == PyUnicode_1BYTE_KIND) { |
| 2008 | Py_UCS1 *start = (Py_UCS1 *) data; |
| 2009 | _PyUnicode_CONVERT_BYTES(Py_UCS1, Py_UCS4, start, start + len, target); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2010 | } |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2011 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 2012 | Py_UCS2 *start = (Py_UCS2 *) data; |
| 2013 | _PyUnicode_CONVERT_BYTES(Py_UCS2, Py_UCS4, start, start + len, target); |
| 2014 | } |
| 2015 | else { |
| 2016 | assert(kind == PyUnicode_4BYTE_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2017 | Py_MEMCPY(target, data, len * sizeof(Py_UCS4)); |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2018 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2019 | if (copy_null) |
| 2020 | target[len] = 0; |
| 2021 | return target; |
| 2022 | } |
| 2023 | |
| 2024 | Py_UCS4* |
| 2025 | PyUnicode_AsUCS4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 2026 | int copy_null) |
| 2027 | { |
| 2028 | if (target == NULL || targetsize < 1) { |
| 2029 | PyErr_BadInternalCall(); |
| 2030 | return NULL; |
| 2031 | } |
| 2032 | return as_ucs4(string, target, targetsize, copy_null); |
| 2033 | } |
| 2034 | |
| 2035 | Py_UCS4* |
| 2036 | PyUnicode_AsUCS4Copy(PyObject *string) |
| 2037 | { |
| 2038 | return as_ucs4(string, NULL, 0, 1); |
| 2039 | } |
| 2040 | |
| 2041 | #ifdef HAVE_WCHAR_H |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 2042 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2043 | PyObject * |
| 2044 | PyUnicode_FromWideChar(register const wchar_t *w, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2045 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2046 | if (w == NULL) { |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 2047 | if (size == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2048 | return PyUnicode_New(0, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2049 | PyErr_BadInternalCall(); |
| 2050 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2051 | } |
| 2052 | |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 2053 | if (size == -1) { |
| 2054 | size = wcslen(w); |
| 2055 | } |
| 2056 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2057 | return PyUnicode_FromUnicode(w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2058 | } |
| 2059 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2060 | #endif /* HAVE_WCHAR_H */ |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 2061 | |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2062 | static void |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2063 | makefmt(char *fmt, int longflag, int longlongflag, int size_tflag, |
| 2064 | int zeropad, int width, int precision, char c) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2065 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2066 | *fmt++ = '%'; |
| 2067 | if (width) { |
| 2068 | if (zeropad) |
| 2069 | *fmt++ = '0'; |
| 2070 | fmt += sprintf(fmt, "%d", width); |
| 2071 | } |
| 2072 | if (precision) |
| 2073 | fmt += sprintf(fmt, ".%d", precision); |
| 2074 | if (longflag) |
| 2075 | *fmt++ = 'l'; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2076 | else if (longlongflag) { |
| 2077 | /* longlongflag should only ever be nonzero on machines with |
| 2078 | HAVE_LONG_LONG defined */ |
| 2079 | #ifdef HAVE_LONG_LONG |
| 2080 | char *f = PY_FORMAT_LONG_LONG; |
| 2081 | while (*f) |
| 2082 | *fmt++ = *f++; |
| 2083 | #else |
| 2084 | /* we shouldn't ever get here */ |
| 2085 | assert(0); |
| 2086 | *fmt++ = 'l'; |
| 2087 | #endif |
| 2088 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2089 | else if (size_tflag) { |
| 2090 | char *f = PY_FORMAT_SIZE_T; |
| 2091 | while (*f) |
| 2092 | *fmt++ = *f++; |
| 2093 | } |
| 2094 | *fmt++ = c; |
| 2095 | *fmt = '\0'; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2096 | } |
| 2097 | |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2098 | /* helper for PyUnicode_FromFormatV() */ |
| 2099 | |
| 2100 | static const char* |
| 2101 | parse_format_flags(const char *f, |
| 2102 | int *p_width, int *p_precision, |
| 2103 | int *p_longflag, int *p_longlongflag, int *p_size_tflag) |
| 2104 | { |
| 2105 | int width, precision, longflag, longlongflag, size_tflag; |
| 2106 | |
| 2107 | /* parse the width.precision part, e.g. "%2.5s" => width=2, precision=5 */ |
| 2108 | f++; |
| 2109 | width = 0; |
| 2110 | while (Py_ISDIGIT((unsigned)*f)) |
| 2111 | width = (width*10) + *f++ - '0'; |
| 2112 | precision = 0; |
| 2113 | if (*f == '.') { |
| 2114 | f++; |
| 2115 | while (Py_ISDIGIT((unsigned)*f)) |
| 2116 | precision = (precision*10) + *f++ - '0'; |
| 2117 | if (*f == '%') { |
| 2118 | /* "%.3%s" => f points to "3" */ |
| 2119 | f--; |
| 2120 | } |
| 2121 | } |
| 2122 | if (*f == '\0') { |
| 2123 | /* bogus format "%.1" => go backward, f points to "1" */ |
| 2124 | f--; |
| 2125 | } |
| 2126 | if (p_width != NULL) |
| 2127 | *p_width = width; |
| 2128 | if (p_precision != NULL) |
| 2129 | *p_precision = precision; |
| 2130 | |
| 2131 | /* Handle %ld, %lu, %lld and %llu. */ |
| 2132 | longflag = 0; |
| 2133 | longlongflag = 0; |
Victor Stinner | e7faec1 | 2011-03-02 00:01:53 +0000 | [diff] [blame] | 2134 | size_tflag = 0; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2135 | |
| 2136 | if (*f == 'l') { |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2137 | if (f[1] == 'd' || f[1] == 'u' || f[1] == 'i') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2138 | longflag = 1; |
| 2139 | ++f; |
| 2140 | } |
| 2141 | #ifdef HAVE_LONG_LONG |
| 2142 | else if (f[1] == 'l' && |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2143 | (f[2] == 'd' || f[2] == 'u' || f[2] == 'i')) { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2144 | longlongflag = 1; |
| 2145 | f += 2; |
| 2146 | } |
| 2147 | #endif |
| 2148 | } |
| 2149 | /* handle the size_t flag. */ |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2150 | 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] | 2151 | size_tflag = 1; |
| 2152 | ++f; |
| 2153 | } |
| 2154 | if (p_longflag != NULL) |
| 2155 | *p_longflag = longflag; |
| 2156 | if (p_longlongflag != NULL) |
| 2157 | *p_longlongflag = longlongflag; |
| 2158 | if (p_size_tflag != NULL) |
| 2159 | *p_size_tflag = size_tflag; |
| 2160 | return f; |
| 2161 | } |
| 2162 | |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2163 | /* maximum number of characters required for output of %ld. 21 characters |
| 2164 | allows for 64-bit integers (in decimal) and an optional sign. */ |
| 2165 | #define MAX_LONG_CHARS 21 |
| 2166 | /* maximum number of characters required for output of %lld. |
| 2167 | We need at most ceil(log10(256)*SIZEOF_LONG_LONG) digits, |
| 2168 | plus 1 for the sign. 53/22 is an upper bound for log10(256). */ |
| 2169 | #define MAX_LONG_LONG_CHARS (2 + (SIZEOF_LONG_LONG*53-1) / 22) |
| 2170 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2171 | PyObject * |
| 2172 | PyUnicode_FromFormatV(const char *format, va_list vargs) |
| 2173 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2174 | va_list count; |
| 2175 | Py_ssize_t callcount = 0; |
| 2176 | PyObject **callresults = NULL; |
| 2177 | PyObject **callresult = NULL; |
| 2178 | Py_ssize_t n = 0; |
| 2179 | int width = 0; |
| 2180 | int precision = 0; |
| 2181 | int zeropad; |
| 2182 | const char* f; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2183 | PyObject *string; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2184 | /* used by sprintf */ |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2185 | char fmt[61]; /* should be enough for %0width.precisionlld */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2186 | Py_UCS4 maxchar = 127; /* result is ASCII by default */ |
| 2187 | Py_UCS4 argmaxchar; |
| 2188 | Py_ssize_t numbersize = 0; |
| 2189 | char *numberresults = NULL; |
| 2190 | char *numberresult = NULL; |
| 2191 | Py_ssize_t i; |
| 2192 | int kind; |
| 2193 | void *data; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2194 | |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 2195 | Py_VA_COPY(count, vargs); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2196 | /* step 1: count the number of %S/%R/%A/%s format specifications |
| 2197 | * (we call PyObject_Str()/PyObject_Repr()/PyObject_ASCII()/ |
| 2198 | * 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] | 2199 | * result in an array) |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 2200 | * also estimate a upper bound for all the number formats in the string, |
| 2201 | * 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] | 2202 | * buffer before putting everything together. */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2203 | for (f = format; *f; f++) { |
| 2204 | if (*f == '%') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2205 | int longlongflag; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2206 | /* skip width or width.precision (eg. "1.2" of "%1.2f") */ |
| 2207 | f = parse_format_flags(f, &width, NULL, NULL, &longlongflag, NULL); |
| 2208 | if (*f == 's' || *f=='S' || *f=='R' || *f=='A' || *f=='V') |
| 2209 | ++callcount; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2210 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2211 | else if (*f == 'd' || *f=='u' || *f=='i' || *f=='x' || *f=='p') { |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2212 | #ifdef HAVE_LONG_LONG |
| 2213 | if (longlongflag) { |
| 2214 | if (width < MAX_LONG_LONG_CHARS) |
| 2215 | width = MAX_LONG_LONG_CHARS; |
| 2216 | } |
| 2217 | else |
| 2218 | #endif |
| 2219 | /* MAX_LONG_CHARS is enough to hold a 64-bit integer, |
| 2220 | including sign. Decimal takes the most space. This |
| 2221 | isn't enough for octal. If a width is specified we |
| 2222 | need more (which we allocate later). */ |
| 2223 | if (width < MAX_LONG_CHARS) |
| 2224 | width = MAX_LONG_CHARS; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2225 | |
| 2226 | /* account for the size + '\0' to separate numbers |
| 2227 | inside of the numberresults buffer */ |
| 2228 | numbersize += (width + 1); |
| 2229 | } |
| 2230 | } |
| 2231 | else if ((unsigned char)*f > 127) { |
| 2232 | PyErr_Format(PyExc_ValueError, |
| 2233 | "PyUnicode_FromFormatV() expects an ASCII-encoded format " |
| 2234 | "string, got a non-ASCII byte: 0x%02x", |
| 2235 | (unsigned char)*f); |
| 2236 | return NULL; |
| 2237 | } |
| 2238 | } |
| 2239 | /* step 2: allocate memory for the results of |
| 2240 | * PyObject_Str()/PyObject_Repr()/PyUnicode_DecodeUTF8() calls */ |
| 2241 | if (callcount) { |
| 2242 | callresults = PyObject_Malloc(sizeof(PyObject *) * callcount); |
| 2243 | if (!callresults) { |
| 2244 | PyErr_NoMemory(); |
| 2245 | return NULL; |
| 2246 | } |
| 2247 | callresult = callresults; |
| 2248 | } |
| 2249 | /* step 2.5: allocate memory for the results of formating numbers */ |
| 2250 | if (numbersize) { |
| 2251 | numberresults = PyObject_Malloc(numbersize); |
| 2252 | if (!numberresults) { |
| 2253 | PyErr_NoMemory(); |
| 2254 | goto fail; |
| 2255 | } |
| 2256 | numberresult = numberresults; |
| 2257 | } |
| 2258 | |
| 2259 | /* step 3: format numbers and figure out how large a buffer we need */ |
| 2260 | for (f = format; *f; f++) { |
| 2261 | if (*f == '%') { |
| 2262 | const char* p; |
| 2263 | int longflag; |
| 2264 | int longlongflag; |
| 2265 | int size_tflag; |
| 2266 | int numprinted; |
| 2267 | |
| 2268 | p = f; |
| 2269 | zeropad = (f[1] == '0'); |
| 2270 | f = parse_format_flags(f, &width, &precision, |
| 2271 | &longflag, &longlongflag, &size_tflag); |
| 2272 | switch (*f) { |
| 2273 | case 'c': |
| 2274 | { |
| 2275 | Py_UCS4 ordinal = va_arg(count, int); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2276 | maxchar = Py_MAX(maxchar, ordinal); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2277 | n++; |
| 2278 | break; |
| 2279 | } |
| 2280 | case '%': |
| 2281 | n++; |
| 2282 | break; |
| 2283 | case 'i': |
| 2284 | case 'd': |
| 2285 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, |
| 2286 | width, precision, *f); |
| 2287 | if (longflag) |
| 2288 | numprinted = sprintf(numberresult, fmt, |
| 2289 | va_arg(count, long)); |
| 2290 | #ifdef HAVE_LONG_LONG |
| 2291 | else if (longlongflag) |
| 2292 | numprinted = sprintf(numberresult, fmt, |
| 2293 | va_arg(count, PY_LONG_LONG)); |
| 2294 | #endif |
| 2295 | else if (size_tflag) |
| 2296 | numprinted = sprintf(numberresult, fmt, |
| 2297 | va_arg(count, Py_ssize_t)); |
| 2298 | else |
| 2299 | numprinted = sprintf(numberresult, fmt, |
| 2300 | va_arg(count, int)); |
| 2301 | n += numprinted; |
| 2302 | /* advance by +1 to skip over the '\0' */ |
| 2303 | numberresult += (numprinted + 1); |
| 2304 | assert(*(numberresult - 1) == '\0'); |
| 2305 | assert(*(numberresult - 2) != '\0'); |
| 2306 | assert(numprinted >= 0); |
| 2307 | assert(numberresult <= numberresults + numbersize); |
| 2308 | break; |
| 2309 | case 'u': |
| 2310 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, |
| 2311 | width, precision, 'u'); |
| 2312 | if (longflag) |
| 2313 | numprinted = sprintf(numberresult, fmt, |
| 2314 | va_arg(count, unsigned long)); |
| 2315 | #ifdef HAVE_LONG_LONG |
| 2316 | else if (longlongflag) |
| 2317 | numprinted = sprintf(numberresult, fmt, |
| 2318 | va_arg(count, unsigned PY_LONG_LONG)); |
| 2319 | #endif |
| 2320 | else if (size_tflag) |
| 2321 | numprinted = sprintf(numberresult, fmt, |
| 2322 | va_arg(count, size_t)); |
| 2323 | else |
| 2324 | numprinted = sprintf(numberresult, fmt, |
| 2325 | va_arg(count, unsigned int)); |
| 2326 | n += numprinted; |
| 2327 | numberresult += (numprinted + 1); |
| 2328 | assert(*(numberresult - 1) == '\0'); |
| 2329 | assert(*(numberresult - 2) != '\0'); |
| 2330 | assert(numprinted >= 0); |
| 2331 | assert(numberresult <= numberresults + numbersize); |
| 2332 | break; |
| 2333 | case 'x': |
| 2334 | makefmt(fmt, 0, 0, 0, zeropad, width, precision, 'x'); |
| 2335 | numprinted = sprintf(numberresult, fmt, va_arg(count, int)); |
| 2336 | n += numprinted; |
| 2337 | numberresult += (numprinted + 1); |
| 2338 | assert(*(numberresult - 1) == '\0'); |
| 2339 | assert(*(numberresult - 2) != '\0'); |
| 2340 | assert(numprinted >= 0); |
| 2341 | assert(numberresult <= numberresults + numbersize); |
| 2342 | break; |
| 2343 | case 'p': |
| 2344 | numprinted = sprintf(numberresult, "%p", va_arg(count, void*)); |
| 2345 | /* %p is ill-defined: ensure leading 0x. */ |
| 2346 | if (numberresult[1] == 'X') |
| 2347 | numberresult[1] = 'x'; |
| 2348 | else if (numberresult[1] != 'x') { |
| 2349 | memmove(numberresult + 2, numberresult, |
| 2350 | strlen(numberresult) + 1); |
| 2351 | numberresult[0] = '0'; |
| 2352 | numberresult[1] = 'x'; |
| 2353 | numprinted += 2; |
| 2354 | } |
| 2355 | n += numprinted; |
| 2356 | numberresult += (numprinted + 1); |
| 2357 | assert(*(numberresult - 1) == '\0'); |
| 2358 | assert(*(numberresult - 2) != '\0'); |
| 2359 | assert(numprinted >= 0); |
| 2360 | assert(numberresult <= numberresults + numbersize); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2361 | break; |
| 2362 | case 's': |
| 2363 | { |
| 2364 | /* UTF-8 */ |
Georg Brandl | 780b2a6 | 2009-05-05 09:19:59 +0000 | [diff] [blame] | 2365 | const char *s = va_arg(count, const char*); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2366 | PyObject *str = PyUnicode_DecodeUTF8(s, strlen(s), "replace"); |
| 2367 | if (!str) |
| 2368 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2369 | /* since PyUnicode_DecodeUTF8 returns already flexible |
| 2370 | unicode objects, there is no need to call ready on them */ |
| 2371 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2372 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2373 | n += PyUnicode_GET_LENGTH(str); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2374 | /* Remember the str and switch to the next slot */ |
| 2375 | *callresult++ = str; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2376 | break; |
| 2377 | } |
| 2378 | case 'U': |
| 2379 | { |
| 2380 | PyObject *obj = va_arg(count, PyObject *); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 2381 | assert(obj && _PyUnicode_CHECK(obj)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2382 | if (PyUnicode_READY(obj) == -1) |
| 2383 | goto fail; |
| 2384 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2385 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2386 | n += PyUnicode_GET_LENGTH(obj); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2387 | break; |
| 2388 | } |
| 2389 | case 'V': |
| 2390 | { |
| 2391 | PyObject *obj = va_arg(count, PyObject *); |
| 2392 | const char *str = va_arg(count, const char *); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2393 | PyObject *str_obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2394 | assert(obj || str); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 2395 | assert(!obj || _PyUnicode_CHECK(obj)); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2396 | if (obj) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2397 | if (PyUnicode_READY(obj) == -1) |
| 2398 | goto fail; |
| 2399 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2400 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2401 | n += PyUnicode_GET_LENGTH(obj); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2402 | *callresult++ = NULL; |
| 2403 | } |
| 2404 | else { |
| 2405 | str_obj = PyUnicode_DecodeUTF8(str, strlen(str), "replace"); |
| 2406 | if (!str_obj) |
| 2407 | goto fail; |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 2408 | if (PyUnicode_READY(str_obj)) { |
| 2409 | Py_DECREF(str_obj); |
| 2410 | goto fail; |
| 2411 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2412 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str_obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2413 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2414 | n += PyUnicode_GET_LENGTH(str_obj); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2415 | *callresult++ = str_obj; |
| 2416 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2417 | break; |
| 2418 | } |
| 2419 | case 'S': |
| 2420 | { |
| 2421 | PyObject *obj = va_arg(count, PyObject *); |
| 2422 | PyObject *str; |
| 2423 | assert(obj); |
| 2424 | str = PyObject_Str(obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2425 | if (!str || PyUnicode_READY(str) == -1) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2426 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2427 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2428 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2429 | n += PyUnicode_GET_LENGTH(str); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2430 | /* Remember the str and switch to the next slot */ |
| 2431 | *callresult++ = str; |
| 2432 | break; |
| 2433 | } |
| 2434 | case 'R': |
| 2435 | { |
| 2436 | PyObject *obj = va_arg(count, PyObject *); |
| 2437 | PyObject *repr; |
| 2438 | assert(obj); |
| 2439 | repr = PyObject_Repr(obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2440 | if (!repr || PyUnicode_READY(repr) == -1) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2441 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2442 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(repr); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2443 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2444 | n += PyUnicode_GET_LENGTH(repr); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2445 | /* Remember the repr and switch to the next slot */ |
| 2446 | *callresult++ = repr; |
| 2447 | break; |
| 2448 | } |
| 2449 | case 'A': |
| 2450 | { |
| 2451 | PyObject *obj = va_arg(count, PyObject *); |
| 2452 | PyObject *ascii; |
| 2453 | assert(obj); |
| 2454 | ascii = PyObject_ASCII(obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2455 | if (!ascii || PyUnicode_READY(ascii) == -1) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2456 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2457 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(ascii); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2458 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2459 | n += PyUnicode_GET_LENGTH(ascii); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2460 | /* Remember the repr and switch to the next slot */ |
| 2461 | *callresult++ = ascii; |
| 2462 | break; |
| 2463 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2464 | default: |
| 2465 | /* if we stumble upon an unknown |
| 2466 | formatting code, copy the rest of |
| 2467 | the format string to the output |
| 2468 | string. (we cannot just skip the |
| 2469 | code, since there's no way to know |
| 2470 | what's in the argument list) */ |
| 2471 | n += strlen(p); |
| 2472 | goto expand; |
| 2473 | } |
| 2474 | } else |
| 2475 | n++; |
| 2476 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2477 | expand: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2478 | /* step 4: fill the buffer */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2479 | /* Since we've analyzed how much space we need, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2480 | we don't have to resize the string. |
| 2481 | There can be no errors beyond this point. */ |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2482 | string = PyUnicode_New(n, maxchar); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2483 | if (!string) |
| 2484 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2485 | kind = PyUnicode_KIND(string); |
| 2486 | data = PyUnicode_DATA(string); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2487 | callresult = callresults; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2488 | numberresult = numberresults; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2489 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2490 | for (i = 0, f = format; *f; f++) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2491 | if (*f == '%') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2492 | const char* p; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2493 | |
| 2494 | p = f; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2495 | f = parse_format_flags(f, NULL, NULL, NULL, NULL, NULL); |
| 2496 | /* checking for == because the last argument could be a empty |
| 2497 | string, which causes i to point to end, the assert at the end of |
| 2498 | the loop */ |
| 2499 | assert(i <= PyUnicode_GET_LENGTH(string)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2500 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2501 | switch (*f) { |
| 2502 | case 'c': |
Victor Stinner | 5ed8b2c | 2011-02-21 21:13:44 +0000 | [diff] [blame] | 2503 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2504 | const int ordinal = va_arg(vargs, int); |
| 2505 | PyUnicode_WRITE(kind, data, i++, ordinal); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2506 | break; |
Victor Stinner | 5ed8b2c | 2011-02-21 21:13:44 +0000 | [diff] [blame] | 2507 | } |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2508 | case 'i': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2509 | case 'd': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2510 | case 'u': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2511 | case 'x': |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2512 | case 'p': |
| 2513 | /* unused, since we already have the result */ |
| 2514 | if (*f == 'p') |
| 2515 | (void) va_arg(vargs, void *); |
| 2516 | else |
| 2517 | (void) va_arg(vargs, int); |
| 2518 | /* extract the result from numberresults and append. */ |
| 2519 | for (; *numberresult; ++i, ++numberresult) |
| 2520 | PyUnicode_WRITE(kind, data, i, *numberresult); |
| 2521 | /* skip over the separating '\0' */ |
| 2522 | assert(*numberresult == '\0'); |
| 2523 | numberresult++; |
| 2524 | assert(numberresult <= numberresults + numbersize); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2525 | break; |
| 2526 | case 's': |
| 2527 | { |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2528 | /* unused, since we already have the result */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2529 | Py_ssize_t size; |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2530 | (void) va_arg(vargs, char *); |
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) <= PyUnicode_KIND(string)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2533 | copy_characters(string, i, *callresult, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2534 | i += size; |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2535 | /* We're done with the unicode()/repr() => forget it */ |
| 2536 | Py_DECREF(*callresult); |
| 2537 | /* switch to next unicode()/repr() result */ |
| 2538 | ++callresult; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2539 | break; |
| 2540 | } |
| 2541 | case 'U': |
| 2542 | { |
| 2543 | PyObject *obj = va_arg(vargs, PyObject *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2544 | Py_ssize_t size; |
| 2545 | assert(PyUnicode_KIND(obj) <= PyUnicode_KIND(string)); |
| 2546 | size = PyUnicode_GET_LENGTH(obj); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2547 | copy_characters(string, i, obj, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2548 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2549 | break; |
| 2550 | } |
| 2551 | case 'V': |
| 2552 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2553 | Py_ssize_t size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2554 | PyObject *obj = va_arg(vargs, PyObject *); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2555 | va_arg(vargs, const char *); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2556 | if (obj) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2557 | size = PyUnicode_GET_LENGTH(obj); |
| 2558 | assert(PyUnicode_KIND(obj) <= PyUnicode_KIND(string)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2559 | copy_characters(string, i, obj, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2560 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2561 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2562 | size = PyUnicode_GET_LENGTH(*callresult); |
| 2563 | assert(PyUnicode_KIND(*callresult) <= |
| 2564 | PyUnicode_KIND(string)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2565 | copy_characters(string, i, *callresult, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2566 | i += size; |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2567 | Py_DECREF(*callresult); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2568 | } |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2569 | ++callresult; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2570 | break; |
| 2571 | } |
| 2572 | case 'S': |
| 2573 | case 'R': |
Victor Stinner | 9a90900 | 2010-10-18 20:59:24 +0000 | [diff] [blame] | 2574 | case 'A': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2575 | { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2576 | Py_ssize_t size = PyUnicode_GET_LENGTH(*callresult); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2577 | /* unused, since we already have the result */ |
| 2578 | (void) va_arg(vargs, PyObject *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2579 | assert(PyUnicode_KIND(*callresult) <= PyUnicode_KIND(string)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2580 | copy_characters(string, i, *callresult, 0, size); |
| 2581 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2582 | /* We're done with the unicode()/repr() => forget it */ |
| 2583 | Py_DECREF(*callresult); |
| 2584 | /* switch to next unicode()/repr() result */ |
| 2585 | ++callresult; |
| 2586 | break; |
| 2587 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2588 | case '%': |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2589 | PyUnicode_WRITE(kind, data, i++, '%'); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2590 | break; |
| 2591 | default: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2592 | for (; *p; ++p, ++i) |
| 2593 | PyUnicode_WRITE(kind, data, i, *p); |
| 2594 | assert(i == PyUnicode_GET_LENGTH(string)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2595 | goto end; |
| 2596 | } |
Victor Stinner | 1205f27 | 2010-09-11 00:54:47 +0000 | [diff] [blame] | 2597 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2598 | else { |
| 2599 | assert(i < PyUnicode_GET_LENGTH(string)); |
| 2600 | PyUnicode_WRITE(kind, data, i++, *f); |
| 2601 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2602 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2603 | assert(i == PyUnicode_GET_LENGTH(string)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2604 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2605 | end: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2606 | if (callresults) |
| 2607 | PyObject_Free(callresults); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2608 | if (numberresults) |
| 2609 | PyObject_Free(numberresults); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2610 | assert(_PyUnicode_CheckConsistency(string, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2611 | return (PyObject *)string; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2612 | fail: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2613 | if (callresults) { |
| 2614 | PyObject **callresult2 = callresults; |
| 2615 | while (callresult2 < callresult) { |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2616 | Py_XDECREF(*callresult2); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2617 | ++callresult2; |
| 2618 | } |
| 2619 | PyObject_Free(callresults); |
| 2620 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2621 | if (numberresults) |
| 2622 | PyObject_Free(numberresults); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2623 | return NULL; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2624 | } |
| 2625 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2626 | PyObject * |
| 2627 | PyUnicode_FromFormat(const char *format, ...) |
| 2628 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2629 | PyObject* ret; |
| 2630 | va_list vargs; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2631 | |
| 2632 | #ifdef HAVE_STDARG_PROTOTYPES |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2633 | va_start(vargs, format); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2634 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2635 | va_start(vargs); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2636 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2637 | ret = PyUnicode_FromFormatV(format, vargs); |
| 2638 | va_end(vargs); |
| 2639 | return ret; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2640 | } |
| 2641 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2642 | #ifdef HAVE_WCHAR_H |
| 2643 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2644 | /* Helper function for PyUnicode_AsWideChar() and PyUnicode_AsWideCharString(): |
| 2645 | convert a Unicode object to a wide character string. |
| 2646 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2647 | - 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] | 2648 | character) required to convert the unicode object. Ignore size argument. |
| 2649 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2650 | - Otherwise: return the number of wide characters (excluding the null |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2651 | character) written into w. Write at most size wide characters (including |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2652 | the null character). */ |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2653 | static Py_ssize_t |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2654 | unicode_aswidechar(PyUnicodeObject *unicode, |
| 2655 | wchar_t *w, |
| 2656 | Py_ssize_t size) |
| 2657 | { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2658 | Py_ssize_t res; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2659 | const wchar_t *wstr; |
| 2660 | |
| 2661 | wstr = PyUnicode_AsUnicodeAndSize((PyObject *)unicode, &res); |
| 2662 | if (wstr == NULL) |
| 2663 | return -1; |
| 2664 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2665 | if (w != NULL) { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2666 | if (size > res) |
| 2667 | size = res + 1; |
| 2668 | else |
| 2669 | res = size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2670 | Py_MEMCPY(w, wstr, size * sizeof(wchar_t)); |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2671 | return res; |
| 2672 | } |
| 2673 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2674 | return res + 1; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2675 | } |
| 2676 | |
| 2677 | Py_ssize_t |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2678 | PyUnicode_AsWideChar(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2679 | wchar_t *w, |
| 2680 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2681 | { |
| 2682 | if (unicode == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2683 | PyErr_BadInternalCall(); |
| 2684 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2685 | } |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2686 | return unicode_aswidechar((PyUnicodeObject*)unicode, w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2687 | } |
| 2688 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2689 | wchar_t* |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 2690 | PyUnicode_AsWideCharString(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2691 | Py_ssize_t *size) |
| 2692 | { |
| 2693 | wchar_t* buffer; |
| 2694 | Py_ssize_t buflen; |
| 2695 | |
| 2696 | if (unicode == NULL) { |
| 2697 | PyErr_BadInternalCall(); |
| 2698 | return NULL; |
| 2699 | } |
| 2700 | |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 2701 | buflen = unicode_aswidechar((PyUnicodeObject *)unicode, NULL, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2702 | if (buflen == -1) |
| 2703 | return NULL; |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2704 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < buflen) { |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2705 | PyErr_NoMemory(); |
| 2706 | return NULL; |
| 2707 | } |
| 2708 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2709 | buffer = PyMem_MALLOC(buflen * sizeof(wchar_t)); |
| 2710 | if (buffer == NULL) { |
| 2711 | PyErr_NoMemory(); |
| 2712 | return NULL; |
| 2713 | } |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 2714 | buflen = unicode_aswidechar((PyUnicodeObject *)unicode, buffer, buflen); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2715 | if (buflen == -1) |
| 2716 | return NULL; |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2717 | if (size != NULL) |
| 2718 | *size = buflen; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2719 | return buffer; |
| 2720 | } |
| 2721 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2722 | #endif /* HAVE_WCHAR_H */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2723 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2724 | PyObject * |
| 2725 | PyUnicode_FromOrdinal(int ordinal) |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2726 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2727 | PyObject *v; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2728 | if (ordinal < 0 || ordinal > 0x10ffff) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2729 | PyErr_SetString(PyExc_ValueError, |
| 2730 | "chr() arg not in range(0x110000)"); |
| 2731 | return NULL; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2732 | } |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 2733 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2734 | if (ordinal < 256) |
| 2735 | return get_latin1_char(ordinal); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2736 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2737 | v = PyUnicode_New(1, ordinal); |
| 2738 | if (v == NULL) |
| 2739 | return NULL; |
| 2740 | PyUnicode_WRITE(PyUnicode_KIND(v), PyUnicode_DATA(v), 0, ordinal); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2741 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2742 | return v; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2743 | } |
| 2744 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2745 | PyObject * |
| 2746 | PyUnicode_FromObject(register PyObject *obj) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2747 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2748 | /* XXX Perhaps we should make this API an alias of |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2749 | PyObject_Str() instead ?! */ |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2750 | if (PyUnicode_CheckExact(obj)) { |
Victor Stinner | d3a83d5 | 2011-10-01 03:09:33 +0200 | [diff] [blame] | 2751 | if (PyUnicode_READY(obj)) |
| 2752 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2753 | Py_INCREF(obj); |
| 2754 | return obj; |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2755 | } |
| 2756 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2757 | /* For a Unicode subtype that's not a Unicode object, |
| 2758 | return a true Unicode object with the same data. */ |
Victor Stinner | 2219e0a | 2011-10-01 01:16:59 +0200 | [diff] [blame] | 2759 | return PyUnicode_Copy(obj); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2760 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2761 | PyErr_Format(PyExc_TypeError, |
| 2762 | "Can't convert '%.100s' object to str implicitly", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2763 | Py_TYPE(obj)->tp_name); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2764 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2765 | } |
| 2766 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2767 | PyObject * |
| 2768 | PyUnicode_FromEncodedObject(register PyObject *obj, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2769 | const char *encoding, |
| 2770 | const char *errors) |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2771 | { |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2772 | Py_buffer buffer; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2773 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2774 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2775 | if (obj == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2776 | PyErr_BadInternalCall(); |
| 2777 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2778 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2779 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2780 | /* Decoding bytes objects is the most common case and should be fast */ |
| 2781 | if (PyBytes_Check(obj)) { |
| 2782 | if (PyBytes_GET_SIZE(obj) == 0) { |
| 2783 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 2784 | v = unicode_empty; |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2785 | } |
| 2786 | else { |
| 2787 | v = PyUnicode_Decode( |
| 2788 | PyBytes_AS_STRING(obj), PyBytes_GET_SIZE(obj), |
| 2789 | encoding, errors); |
| 2790 | } |
| 2791 | return v; |
| 2792 | } |
| 2793 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2794 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2795 | PyErr_SetString(PyExc_TypeError, |
| 2796 | "decoding str is not supported"); |
| 2797 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2798 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2799 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2800 | /* Retrieve a bytes buffer view through the PEP 3118 buffer interface */ |
| 2801 | if (PyObject_GetBuffer(obj, &buffer, PyBUF_SIMPLE) < 0) { |
| 2802 | PyErr_Format(PyExc_TypeError, |
| 2803 | "coercing to str: need bytes, bytearray " |
| 2804 | "or buffer-like object, %.80s found", |
| 2805 | Py_TYPE(obj)->tp_name); |
| 2806 | return NULL; |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 2807 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2808 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2809 | if (buffer.len == 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2810 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 2811 | v = unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2812 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2813 | else |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2814 | v = PyUnicode_Decode((char*) buffer.buf, buffer.len, encoding, errors); |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 2815 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2816 | PyBuffer_Release(&buffer); |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2817 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2818 | } |
| 2819 | |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2820 | /* Convert encoding to lower case and replace '_' with '-' in order to |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2821 | catch e.g. UTF_8. Return 0 on error (encoding is longer than lower_len-1), |
| 2822 | 1 on success. */ |
| 2823 | static int |
| 2824 | normalize_encoding(const char *encoding, |
| 2825 | char *lower, |
| 2826 | size_t lower_len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2827 | { |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2828 | const char *e; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2829 | char *l; |
| 2830 | char *l_end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2831 | |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2832 | e = encoding; |
| 2833 | l = lower; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2834 | l_end = &lower[lower_len - 1]; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2835 | while (*e) { |
| 2836 | if (l == l_end) |
| 2837 | return 0; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 2838 | if (Py_ISUPPER(*e)) { |
| 2839 | *l++ = Py_TOLOWER(*e++); |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2840 | } |
| 2841 | else if (*e == '_') { |
| 2842 | *l++ = '-'; |
| 2843 | e++; |
| 2844 | } |
| 2845 | else { |
| 2846 | *l++ = *e++; |
| 2847 | } |
| 2848 | } |
| 2849 | *l = '\0'; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2850 | return 1; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2851 | } |
| 2852 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2853 | PyObject * |
| 2854 | PyUnicode_Decode(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2855 | Py_ssize_t size, |
| 2856 | const char *encoding, |
| 2857 | const char *errors) |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2858 | { |
| 2859 | PyObject *buffer = NULL, *unicode; |
| 2860 | Py_buffer info; |
| 2861 | char lower[11]; /* Enough for any encoding shortcut */ |
| 2862 | |
| 2863 | if (encoding == NULL) |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2864 | return PyUnicode_DecodeUTF8(s, size, errors); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 2865 | |
| 2866 | /* Shortcuts for common default encodings */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2867 | if (normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2868 | if ((strcmp(lower, "utf-8") == 0) || |
| 2869 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2870 | return PyUnicode_DecodeUTF8(s, size, errors); |
| 2871 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2872 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2873 | (strcmp(lower, "iso-8859-1") == 0)) |
| 2874 | return PyUnicode_DecodeLatin1(s, size, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 2875 | #ifdef HAVE_MBCS |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2876 | else if (strcmp(lower, "mbcs") == 0) |
| 2877 | return PyUnicode_DecodeMBCS(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 2878 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2879 | else if (strcmp(lower, "ascii") == 0) |
| 2880 | return PyUnicode_DecodeASCII(s, size, errors); |
| 2881 | else if (strcmp(lower, "utf-16") == 0) |
| 2882 | return PyUnicode_DecodeUTF16(s, size, errors, 0); |
| 2883 | else if (strcmp(lower, "utf-32") == 0) |
| 2884 | return PyUnicode_DecodeUTF32(s, size, errors, 0); |
| 2885 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2886 | |
| 2887 | /* Decode via the codec registry */ |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 2888 | buffer = NULL; |
Antoine Pitrou | c3b3924 | 2009-01-03 16:59:18 +0000 | [diff] [blame] | 2889 | 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] | 2890 | goto onError; |
Antoine Pitrou | ee58fa4 | 2008-08-19 18:22:14 +0000 | [diff] [blame] | 2891 | buffer = PyMemoryView_FromBuffer(&info); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2892 | if (buffer == NULL) |
| 2893 | goto onError; |
| 2894 | unicode = PyCodec_Decode(buffer, encoding, errors); |
| 2895 | if (unicode == NULL) |
| 2896 | goto onError; |
| 2897 | if (!PyUnicode_Check(unicode)) { |
| 2898 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 2899 | "decoder did not return a str object (type=%.400s)", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2900 | Py_TYPE(unicode)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2901 | Py_DECREF(unicode); |
| 2902 | goto onError; |
| 2903 | } |
| 2904 | Py_DECREF(buffer); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 2905 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 2906 | if (_PyUnicode_READY_REPLACE(&unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2907 | Py_DECREF(unicode); |
| 2908 | return NULL; |
| 2909 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 2910 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2911 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2912 | return unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2913 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2914 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2915 | Py_XDECREF(buffer); |
| 2916 | return NULL; |
| 2917 | } |
| 2918 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2919 | PyObject * |
| 2920 | PyUnicode_AsDecodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2921 | const char *encoding, |
| 2922 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2923 | { |
| 2924 | PyObject *v; |
| 2925 | |
| 2926 | if (!PyUnicode_Check(unicode)) { |
| 2927 | PyErr_BadArgument(); |
| 2928 | goto onError; |
| 2929 | } |
| 2930 | |
| 2931 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2932 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2933 | |
| 2934 | /* Decode via the codec registry */ |
| 2935 | v = PyCodec_Decode(unicode, encoding, errors); |
| 2936 | if (v == NULL) |
| 2937 | goto onError; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2938 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2939 | return v; |
| 2940 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2941 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2942 | return NULL; |
| 2943 | } |
| 2944 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2945 | PyObject * |
| 2946 | PyUnicode_AsDecodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2947 | const char *encoding, |
| 2948 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2949 | { |
| 2950 | PyObject *v; |
| 2951 | |
| 2952 | if (!PyUnicode_Check(unicode)) { |
| 2953 | PyErr_BadArgument(); |
| 2954 | goto onError; |
| 2955 | } |
| 2956 | |
| 2957 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2958 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2959 | |
| 2960 | /* Decode via the codec registry */ |
| 2961 | v = PyCodec_Decode(unicode, encoding, errors); |
| 2962 | if (v == NULL) |
| 2963 | goto onError; |
| 2964 | if (!PyUnicode_Check(v)) { |
| 2965 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 2966 | "decoder did not return a str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2967 | Py_TYPE(v)->tp_name); |
| 2968 | Py_DECREF(v); |
| 2969 | goto onError; |
| 2970 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2971 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2972 | return v; |
| 2973 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2974 | onError: |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2975 | return NULL; |
| 2976 | } |
| 2977 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2978 | PyObject * |
| 2979 | PyUnicode_Encode(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2980 | Py_ssize_t size, |
| 2981 | const char *encoding, |
| 2982 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2983 | { |
| 2984 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2985 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2986 | unicode = PyUnicode_FromUnicode(s, size); |
| 2987 | if (unicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2988 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2989 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 2990 | Py_DECREF(unicode); |
| 2991 | return v; |
| 2992 | } |
| 2993 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2994 | PyObject * |
| 2995 | PyUnicode_AsEncodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2996 | const char *encoding, |
| 2997 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2998 | { |
| 2999 | PyObject *v; |
| 3000 | |
| 3001 | if (!PyUnicode_Check(unicode)) { |
| 3002 | PyErr_BadArgument(); |
| 3003 | goto onError; |
| 3004 | } |
| 3005 | |
| 3006 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3007 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3008 | |
| 3009 | /* Encode via the codec registry */ |
| 3010 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3011 | if (v == NULL) |
| 3012 | goto onError; |
| 3013 | return v; |
| 3014 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3015 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3016 | return NULL; |
| 3017 | } |
| 3018 | |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3019 | PyObject * |
| 3020 | PyUnicode_EncodeFSDefault(PyObject *unicode) |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3021 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3022 | #ifdef HAVE_MBCS |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3023 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
| 3024 | PyUnicode_GET_SIZE(unicode), |
| 3025 | NULL); |
| 3026 | #elif defined(__APPLE__) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3027 | return _PyUnicode_AsUTF8String(unicode, "surrogateescape"); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3028 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 3029 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 3030 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 3031 | cannot use it to encode and decode filenames before it is loaded. Load |
| 3032 | the Python codec requires to encode at least its own filename. Use the C |
| 3033 | version of the locale codec until the codec registry is initialized and |
| 3034 | the Python codec is loaded. |
| 3035 | |
| 3036 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 3037 | cannot only rely on it: check also interp->fscodec_initialized for |
| 3038 | subinterpreters. */ |
| 3039 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3040 | return PyUnicode_AsEncodedString(unicode, |
| 3041 | Py_FileSystemDefaultEncoding, |
| 3042 | "surrogateescape"); |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 3043 | } |
| 3044 | else { |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3045 | /* locale encoding with surrogateescape */ |
| 3046 | wchar_t *wchar; |
| 3047 | char *bytes; |
| 3048 | PyObject *bytes_obj; |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 3049 | size_t error_pos; |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3050 | |
| 3051 | wchar = PyUnicode_AsWideCharString(unicode, NULL); |
| 3052 | if (wchar == NULL) |
| 3053 | return NULL; |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 3054 | bytes = _Py_wchar2char(wchar, &error_pos); |
| 3055 | if (bytes == NULL) { |
| 3056 | if (error_pos != (size_t)-1) { |
| 3057 | char *errmsg = strerror(errno); |
| 3058 | PyObject *exc = NULL; |
| 3059 | if (errmsg == NULL) |
| 3060 | errmsg = "Py_wchar2char() failed"; |
| 3061 | raise_encode_exception(&exc, |
| 3062 | "filesystemencoding", |
| 3063 | PyUnicode_AS_UNICODE(unicode), PyUnicode_GET_SIZE(unicode), |
| 3064 | error_pos, error_pos+1, |
| 3065 | errmsg); |
| 3066 | Py_XDECREF(exc); |
| 3067 | } |
| 3068 | else |
| 3069 | PyErr_NoMemory(); |
| 3070 | PyMem_Free(wchar); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3071 | return NULL; |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 3072 | } |
| 3073 | PyMem_Free(wchar); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3074 | |
| 3075 | bytes_obj = PyBytes_FromString(bytes); |
| 3076 | PyMem_Free(bytes); |
| 3077 | return bytes_obj; |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 3078 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3079 | #endif |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3080 | } |
| 3081 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3082 | PyObject * |
| 3083 | PyUnicode_AsEncodedString(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3084 | const char *encoding, |
| 3085 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3086 | { |
| 3087 | PyObject *v; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3088 | char lower[11]; /* Enough for any encoding shortcut */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3089 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3090 | if (!PyUnicode_Check(unicode)) { |
| 3091 | PyErr_BadArgument(); |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3092 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3093 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3094 | |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3095 | if (encoding == NULL) { |
| 3096 | if (errors == NULL || strcmp(errors, "strict") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3097 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3098 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3099 | return _PyUnicode_AsUTF8String(unicode, errors); |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3100 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3101 | |
| 3102 | /* Shortcuts for common default encodings */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3103 | if (normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3104 | if ((strcmp(lower, "utf-8") == 0) || |
| 3105 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3106 | { |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3107 | if (errors == NULL || strcmp(errors, "strict") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3108 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3109 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3110 | return _PyUnicode_AsUTF8String(unicode, errors); |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3111 | } |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3112 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3113 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3114 | (strcmp(lower, "iso-8859-1") == 0)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3115 | return _PyUnicode_AsLatin1String(unicode, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3116 | #ifdef HAVE_MBCS |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3117 | else if (strcmp(lower, "mbcs") == 0) |
| 3118 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
| 3119 | PyUnicode_GET_SIZE(unicode), |
| 3120 | errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 3121 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3122 | else if (strcmp(lower, "ascii") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3123 | return _PyUnicode_AsASCIIString(unicode, errors); |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3124 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3125 | |
| 3126 | /* Encode via the codec registry */ |
| 3127 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3128 | if (v == NULL) |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3129 | return NULL; |
| 3130 | |
| 3131 | /* The normal path */ |
| 3132 | if (PyBytes_Check(v)) |
| 3133 | return v; |
| 3134 | |
| 3135 | /* 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] | 3136 | if (PyByteArray_Check(v)) { |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3137 | int error; |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3138 | PyObject *b; |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3139 | |
| 3140 | error = PyErr_WarnFormat(PyExc_RuntimeWarning, 1, |
| 3141 | "encoder %s returned bytearray instead of bytes", |
| 3142 | encoding); |
| 3143 | if (error) { |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3144 | Py_DECREF(v); |
| 3145 | return NULL; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3146 | } |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3147 | |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3148 | b = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(v), Py_SIZE(v)); |
| 3149 | Py_DECREF(v); |
| 3150 | return b; |
| 3151 | } |
| 3152 | |
| 3153 | PyErr_Format(PyExc_TypeError, |
| 3154 | "encoder did not return a bytes object (type=%.400s)", |
| 3155 | Py_TYPE(v)->tp_name); |
| 3156 | Py_DECREF(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3157 | return NULL; |
| 3158 | } |
| 3159 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3160 | PyObject * |
| 3161 | PyUnicode_AsEncodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3162 | const char *encoding, |
| 3163 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3164 | { |
| 3165 | PyObject *v; |
| 3166 | |
| 3167 | if (!PyUnicode_Check(unicode)) { |
| 3168 | PyErr_BadArgument(); |
| 3169 | goto onError; |
| 3170 | } |
| 3171 | |
| 3172 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3173 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3174 | |
| 3175 | /* Encode via the codec registry */ |
| 3176 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3177 | if (v == NULL) |
| 3178 | goto onError; |
| 3179 | if (!PyUnicode_Check(v)) { |
| 3180 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3181 | "encoder did not return an str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3182 | Py_TYPE(v)->tp_name); |
| 3183 | Py_DECREF(v); |
| 3184 | goto onError; |
| 3185 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3186 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3187 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3188 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3189 | return NULL; |
| 3190 | } |
| 3191 | |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3192 | PyObject* |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3193 | PyUnicode_DecodeFSDefault(const char *s) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3194 | Py_ssize_t size = (Py_ssize_t)strlen(s); |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3195 | return PyUnicode_DecodeFSDefaultAndSize(s, size); |
| 3196 | } |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3197 | |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3198 | PyObject* |
| 3199 | PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size) |
| 3200 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3201 | #ifdef HAVE_MBCS |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3202 | return PyUnicode_DecodeMBCS(s, size, NULL); |
| 3203 | #elif defined(__APPLE__) |
| 3204 | return PyUnicode_DecodeUTF8(s, size, "surrogateescape"); |
| 3205 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 3206 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 3207 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 3208 | cannot use it to encode and decode filenames before it is loaded. Load |
| 3209 | the Python codec requires to encode at least its own filename. Use the C |
| 3210 | version of the locale codec until the codec registry is initialized and |
| 3211 | the Python codec is loaded. |
| 3212 | |
| 3213 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 3214 | cannot only rely on it: check also interp->fscodec_initialized for |
| 3215 | subinterpreters. */ |
| 3216 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3217 | return PyUnicode_Decode(s, size, |
| 3218 | Py_FileSystemDefaultEncoding, |
Victor Stinner | b9a20ad | 2010-04-30 16:37:52 +0000 | [diff] [blame] | 3219 | "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3220 | } |
| 3221 | else { |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3222 | /* locale encoding with surrogateescape */ |
| 3223 | wchar_t *wchar; |
| 3224 | PyObject *unicode; |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 3225 | size_t len; |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3226 | |
| 3227 | if (s[size] != '\0' || size != strlen(s)) { |
| 3228 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 3229 | return NULL; |
| 3230 | } |
| 3231 | |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 3232 | wchar = _Py_char2wchar(s, &len); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3233 | if (wchar == NULL) |
Victor Stinner | d5af0a5 | 2010-11-08 23:34:29 +0000 | [diff] [blame] | 3234 | return PyErr_NoMemory(); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3235 | |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 3236 | unicode = PyUnicode_FromWideChar(wchar, len); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 3237 | PyMem_Free(wchar); |
| 3238 | return unicode; |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3239 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3240 | #endif |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3241 | } |
| 3242 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3243 | |
| 3244 | int |
| 3245 | PyUnicode_FSConverter(PyObject* arg, void* addr) |
| 3246 | { |
| 3247 | PyObject *output = NULL; |
| 3248 | Py_ssize_t size; |
| 3249 | void *data; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3250 | if (arg == NULL) { |
| 3251 | Py_DECREF(*(PyObject**)addr); |
| 3252 | return 1; |
| 3253 | } |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 3254 | if (PyBytes_Check(arg)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3255 | output = arg; |
| 3256 | Py_INCREF(output); |
| 3257 | } |
| 3258 | else { |
| 3259 | arg = PyUnicode_FromObject(arg); |
| 3260 | if (!arg) |
| 3261 | return 0; |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3262 | output = PyUnicode_EncodeFSDefault(arg); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3263 | Py_DECREF(arg); |
| 3264 | if (!output) |
| 3265 | return 0; |
| 3266 | if (!PyBytes_Check(output)) { |
| 3267 | Py_DECREF(output); |
| 3268 | PyErr_SetString(PyExc_TypeError, "encoder failed to return bytes"); |
| 3269 | return 0; |
| 3270 | } |
| 3271 | } |
Victor Stinner | 0ea2a46 | 2010-04-30 00:22:08 +0000 | [diff] [blame] | 3272 | size = PyBytes_GET_SIZE(output); |
| 3273 | data = PyBytes_AS_STRING(output); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3274 | if (size != strlen(data)) { |
Benjamin Peterson | 7a6b44a | 2011-08-18 13:51:47 -0500 | [diff] [blame] | 3275 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3276 | Py_DECREF(output); |
| 3277 | return 0; |
| 3278 | } |
| 3279 | *(PyObject**)addr = output; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3280 | return Py_CLEANUP_SUPPORTED; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3281 | } |
| 3282 | |
| 3283 | |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3284 | int |
| 3285 | PyUnicode_FSDecoder(PyObject* arg, void* addr) |
| 3286 | { |
| 3287 | PyObject *output = NULL; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3288 | if (arg == NULL) { |
| 3289 | Py_DECREF(*(PyObject**)addr); |
| 3290 | return 1; |
| 3291 | } |
| 3292 | if (PyUnicode_Check(arg)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3293 | if (PyUnicode_READY(arg)) |
| 3294 | return 0; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3295 | output = arg; |
| 3296 | Py_INCREF(output); |
| 3297 | } |
| 3298 | else { |
| 3299 | arg = PyBytes_FromObject(arg); |
| 3300 | if (!arg) |
| 3301 | return 0; |
| 3302 | output = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AS_STRING(arg), |
| 3303 | PyBytes_GET_SIZE(arg)); |
| 3304 | Py_DECREF(arg); |
| 3305 | if (!output) |
| 3306 | return 0; |
| 3307 | if (!PyUnicode_Check(output)) { |
| 3308 | Py_DECREF(output); |
| 3309 | PyErr_SetString(PyExc_TypeError, "decoder failed to return unicode"); |
| 3310 | return 0; |
| 3311 | } |
| 3312 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3313 | if (findchar(PyUnicode_DATA(output), PyUnicode_KIND(output), |
| 3314 | PyUnicode_GET_LENGTH(output), 0, 1)) { |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3315 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 3316 | Py_DECREF(output); |
| 3317 | return 0; |
| 3318 | } |
| 3319 | *(PyObject**)addr = output; |
| 3320 | return Py_CLEANUP_SUPPORTED; |
| 3321 | } |
| 3322 | |
| 3323 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3324 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3325 | PyUnicode_AsUTF8AndSize(PyObject *unicode, Py_ssize_t *psize) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3326 | { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 3327 | PyObject *bytes; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3328 | PyUnicodeObject *u = (PyUnicodeObject *)unicode; |
| 3329 | |
Neal Norwitz | e0a0a6e | 2007-08-25 01:04:21 +0000 | [diff] [blame] | 3330 | if (!PyUnicode_Check(unicode)) { |
| 3331 | PyErr_BadArgument(); |
| 3332 | return NULL; |
| 3333 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3334 | if (PyUnicode_READY(u) == -1) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3335 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3336 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3337 | if (PyUnicode_UTF8(unicode) == NULL) { |
| 3338 | assert(!PyUnicode_IS_COMPACT_ASCII(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3339 | bytes = _PyUnicode_AsUTF8String(unicode, "strict"); |
| 3340 | if (bytes == NULL) |
| 3341 | return NULL; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3342 | _PyUnicode_UTF8(u) = PyObject_MALLOC(PyBytes_GET_SIZE(bytes) + 1); |
| 3343 | if (_PyUnicode_UTF8(u) == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3344 | Py_DECREF(bytes); |
| 3345 | return NULL; |
| 3346 | } |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3347 | _PyUnicode_UTF8_LENGTH(u) = PyBytes_GET_SIZE(bytes); |
| 3348 | 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] | 3349 | Py_DECREF(bytes); |
| 3350 | } |
| 3351 | |
| 3352 | if (psize) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3353 | *psize = PyUnicode_UTF8_LENGTH(unicode); |
| 3354 | return PyUnicode_UTF8(unicode); |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3355 | } |
| 3356 | |
| 3357 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3358 | PyUnicode_AsUTF8(PyObject *unicode) |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3359 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3360 | return PyUnicode_AsUTF8AndSize(unicode, NULL); |
| 3361 | } |
| 3362 | |
| 3363 | #ifdef Py_DEBUG |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 3364 | static int unicode_as_unicode_calls = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3365 | #endif |
| 3366 | |
| 3367 | |
| 3368 | Py_UNICODE * |
| 3369 | PyUnicode_AsUnicodeAndSize(PyObject *unicode, Py_ssize_t *size) |
| 3370 | { |
| 3371 | PyUnicodeObject *u; |
| 3372 | const unsigned char *one_byte; |
| 3373 | #if SIZEOF_WCHAR_T == 4 |
| 3374 | const Py_UCS2 *two_bytes; |
| 3375 | #else |
| 3376 | const Py_UCS4 *four_bytes; |
| 3377 | const Py_UCS4 *ucs4_end; |
| 3378 | Py_ssize_t num_surrogates; |
| 3379 | #endif |
| 3380 | wchar_t *w; |
| 3381 | wchar_t *wchar_end; |
| 3382 | |
| 3383 | if (!PyUnicode_Check(unicode)) { |
| 3384 | PyErr_BadArgument(); |
| 3385 | return NULL; |
| 3386 | } |
| 3387 | u = (PyUnicodeObject*)unicode; |
| 3388 | if (_PyUnicode_WSTR(u) == NULL) { |
| 3389 | /* Non-ASCII compact unicode object */ |
| 3390 | assert(_PyUnicode_KIND(u) != 0); |
| 3391 | assert(PyUnicode_IS_READY(u)); |
| 3392 | |
| 3393 | #ifdef Py_DEBUG |
| 3394 | ++unicode_as_unicode_calls; |
| 3395 | #endif |
| 3396 | |
| 3397 | if (PyUnicode_KIND(u) == PyUnicode_4BYTE_KIND) { |
| 3398 | #if SIZEOF_WCHAR_T == 2 |
| 3399 | four_bytes = PyUnicode_4BYTE_DATA(u); |
| 3400 | ucs4_end = four_bytes + _PyUnicode_LENGTH(u); |
| 3401 | num_surrogates = 0; |
| 3402 | |
| 3403 | for (; four_bytes < ucs4_end; ++four_bytes) { |
| 3404 | if (*four_bytes > 0xFFFF) |
| 3405 | ++num_surrogates; |
| 3406 | } |
| 3407 | |
| 3408 | _PyUnicode_WSTR(u) = (wchar_t *) PyObject_MALLOC( |
| 3409 | sizeof(wchar_t) * (_PyUnicode_LENGTH(u) + 1 + num_surrogates)); |
| 3410 | if (!_PyUnicode_WSTR(u)) { |
| 3411 | PyErr_NoMemory(); |
| 3412 | return NULL; |
| 3413 | } |
| 3414 | _PyUnicode_WSTR_LENGTH(u) = _PyUnicode_LENGTH(u) + num_surrogates; |
| 3415 | |
| 3416 | w = _PyUnicode_WSTR(u); |
| 3417 | wchar_end = w + _PyUnicode_WSTR_LENGTH(u); |
| 3418 | four_bytes = PyUnicode_4BYTE_DATA(u); |
| 3419 | for (; four_bytes < ucs4_end; ++four_bytes, ++w) { |
| 3420 | if (*four_bytes > 0xFFFF) { |
| 3421 | /* encode surrogate pair in this case */ |
| 3422 | *w++ = 0xD800 | ((*four_bytes - 0x10000) >> 10); |
| 3423 | *w = 0xDC00 | ((*four_bytes - 0x10000) & 0x3FF); |
| 3424 | } |
| 3425 | else |
| 3426 | *w = *four_bytes; |
| 3427 | |
| 3428 | if (w > wchar_end) { |
| 3429 | assert(0 && "Miscalculated string end"); |
| 3430 | } |
| 3431 | } |
| 3432 | *w = 0; |
| 3433 | #else |
| 3434 | /* sizeof(wchar_t) == 4 */ |
| 3435 | Py_FatalError("Impossible unicode object state, wstr and str " |
| 3436 | "should share memory already."); |
| 3437 | return NULL; |
| 3438 | #endif |
| 3439 | } |
| 3440 | else { |
| 3441 | _PyUnicode_WSTR(u) = (wchar_t *) PyObject_MALLOC(sizeof(wchar_t) * |
| 3442 | (_PyUnicode_LENGTH(u) + 1)); |
| 3443 | if (!_PyUnicode_WSTR(u)) { |
| 3444 | PyErr_NoMemory(); |
| 3445 | return NULL; |
| 3446 | } |
| 3447 | if (!PyUnicode_IS_COMPACT_ASCII(u)) |
| 3448 | _PyUnicode_WSTR_LENGTH(u) = _PyUnicode_LENGTH(u); |
| 3449 | w = _PyUnicode_WSTR(u); |
| 3450 | wchar_end = w + _PyUnicode_LENGTH(u); |
| 3451 | |
| 3452 | if (PyUnicode_KIND(u) == PyUnicode_1BYTE_KIND) { |
| 3453 | one_byte = PyUnicode_1BYTE_DATA(u); |
| 3454 | for (; w < wchar_end; ++one_byte, ++w) |
| 3455 | *w = *one_byte; |
| 3456 | /* null-terminate the wstr */ |
| 3457 | *w = 0; |
| 3458 | } |
| 3459 | else if (PyUnicode_KIND(u) == PyUnicode_2BYTE_KIND) { |
| 3460 | #if SIZEOF_WCHAR_T == 4 |
| 3461 | two_bytes = PyUnicode_2BYTE_DATA(u); |
| 3462 | for (; w < wchar_end; ++two_bytes, ++w) |
| 3463 | *w = *two_bytes; |
| 3464 | /* null-terminate the wstr */ |
| 3465 | *w = 0; |
| 3466 | #else |
| 3467 | /* sizeof(wchar_t) == 2 */ |
| 3468 | PyObject_FREE(_PyUnicode_WSTR(u)); |
| 3469 | _PyUnicode_WSTR(u) = NULL; |
| 3470 | Py_FatalError("Impossible unicode object state, wstr " |
| 3471 | "and str should share memory already."); |
| 3472 | return NULL; |
| 3473 | #endif |
| 3474 | } |
| 3475 | else { |
| 3476 | assert(0 && "This should never happen."); |
| 3477 | } |
| 3478 | } |
| 3479 | } |
| 3480 | if (size != NULL) |
| 3481 | *size = PyUnicode_WSTR_LENGTH(u); |
| 3482 | return _PyUnicode_WSTR(u); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3483 | } |
| 3484 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3485 | Py_UNICODE * |
| 3486 | PyUnicode_AsUnicode(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3487 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3488 | return PyUnicode_AsUnicodeAndSize(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3489 | } |
| 3490 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3491 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3492 | Py_ssize_t |
| 3493 | PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3494 | { |
| 3495 | if (!PyUnicode_Check(unicode)) { |
| 3496 | PyErr_BadArgument(); |
| 3497 | goto onError; |
| 3498 | } |
| 3499 | return PyUnicode_GET_SIZE(unicode); |
| 3500 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3501 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3502 | return -1; |
| 3503 | } |
| 3504 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3505 | Py_ssize_t |
| 3506 | PyUnicode_GetLength(PyObject *unicode) |
| 3507 | { |
Victor Stinner | 5a706cf | 2011-10-02 00:36:53 +0200 | [diff] [blame] | 3508 | if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3509 | PyErr_BadArgument(); |
| 3510 | return -1; |
| 3511 | } |
| 3512 | |
| 3513 | return PyUnicode_GET_LENGTH(unicode); |
| 3514 | } |
| 3515 | |
| 3516 | Py_UCS4 |
| 3517 | PyUnicode_ReadChar(PyObject *unicode, Py_ssize_t index) |
| 3518 | { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 3519 | if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) { |
| 3520 | PyErr_BadArgument(); |
| 3521 | return (Py_UCS4)-1; |
| 3522 | } |
| 3523 | if (index < 0 || index >= _PyUnicode_LENGTH(unicode)) { |
| 3524 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3525 | return (Py_UCS4)-1; |
| 3526 | } |
| 3527 | return PyUnicode_READ_CHAR(unicode, index); |
| 3528 | } |
| 3529 | |
| 3530 | int |
| 3531 | PyUnicode_WriteChar(PyObject *unicode, Py_ssize_t index, Py_UCS4 ch) |
| 3532 | { |
| 3533 | if (!PyUnicode_Check(unicode) || !PyUnicode_IS_COMPACT(unicode)) { |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3534 | PyErr_BadArgument(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3535 | return -1; |
| 3536 | } |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3537 | if (index < 0 || index >= _PyUnicode_LENGTH(unicode)) { |
| 3538 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 3539 | return -1; |
| 3540 | } |
| 3541 | if (_PyUnicode_Dirty(unicode)) |
| 3542 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3543 | PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), |
| 3544 | index, ch); |
| 3545 | return 0; |
| 3546 | } |
| 3547 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3548 | const char * |
| 3549 | PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3550 | { |
Victor Stinner | 42cb462 | 2010-09-01 19:39:01 +0000 | [diff] [blame] | 3551 | return "utf-8"; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3552 | } |
| 3553 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 3554 | /* create or adjust a UnicodeDecodeError */ |
| 3555 | static void |
| 3556 | make_decode_exception(PyObject **exceptionObject, |
| 3557 | const char *encoding, |
| 3558 | const char *input, Py_ssize_t length, |
| 3559 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 3560 | const char *reason) |
| 3561 | { |
| 3562 | if (*exceptionObject == NULL) { |
| 3563 | *exceptionObject = PyUnicodeDecodeError_Create( |
| 3564 | encoding, input, length, startpos, endpos, reason); |
| 3565 | } |
| 3566 | else { |
| 3567 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, startpos)) |
| 3568 | goto onError; |
| 3569 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, endpos)) |
| 3570 | goto onError; |
| 3571 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 3572 | goto onError; |
| 3573 | } |
| 3574 | return; |
| 3575 | |
| 3576 | onError: |
| 3577 | Py_DECREF(*exceptionObject); |
| 3578 | *exceptionObject = NULL; |
| 3579 | } |
| 3580 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3581 | /* error handling callback helper: |
| 3582 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 3583 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3584 | and adjust various state variables. |
| 3585 | return 0 on success, -1 on error |
| 3586 | */ |
| 3587 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3588 | static int |
| 3589 | unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3590 | const char *encoding, const char *reason, |
| 3591 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 3592 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
| 3593 | PyUnicodeObject **output, Py_ssize_t *outpos, Py_UNICODE **outptr) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3594 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3595 | 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] | 3596 | |
| 3597 | PyObject *restuple = NULL; |
| 3598 | PyObject *repunicode = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3599 | Py_ssize_t outsize = PyUnicode_GET_SIZE(*output); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3600 | Py_ssize_t insize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3601 | Py_ssize_t requiredsize; |
| 3602 | Py_ssize_t newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3603 | const Py_UNICODE *repptr; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3604 | PyObject *inputobj = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3605 | Py_ssize_t repsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3606 | int res = -1; |
| 3607 | |
| 3608 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3609 | *errorHandler = PyCodec_LookupError(errors); |
| 3610 | if (*errorHandler == NULL) |
| 3611 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3612 | } |
| 3613 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 3614 | make_decode_exception(exceptionObject, |
| 3615 | encoding, |
| 3616 | *input, *inend - *input, |
| 3617 | *startinpos, *endinpos, |
| 3618 | reason); |
| 3619 | if (*exceptionObject == NULL) |
| 3620 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3621 | |
| 3622 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 3623 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3624 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3625 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 3626 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3627 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3628 | } |
| 3629 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3630 | goto onError; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3631 | |
| 3632 | /* Copy back the bytes variables, which might have been modified by the |
| 3633 | callback */ |
| 3634 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 3635 | if (!inputobj) |
| 3636 | goto onError; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3637 | if (!PyBytes_Check(inputobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3638 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3639 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3640 | *input = PyBytes_AS_STRING(inputobj); |
| 3641 | insize = PyBytes_GET_SIZE(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3642 | *inend = *input + insize; |
Walter Dörwald | 36f938f | 2007-08-10 10:11:43 +0000 | [diff] [blame] | 3643 | /* we can DECREF safely, as the exception has another reference, |
| 3644 | so the object won't go away. */ |
| 3645 | Py_DECREF(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3646 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3647 | if (newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3648 | newpos = insize+newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 3649 | if (newpos<0 || newpos>insize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3650 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos); |
| 3651 | goto onError; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 3652 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3653 | |
| 3654 | /* need more space? (at least enough for what we |
| 3655 | have+the replacement+the rest of the string (starting |
| 3656 | at the new input position), so we won't have to check space |
| 3657 | when there are no errors in the rest of the string) */ |
| 3658 | repptr = PyUnicode_AS_UNICODE(repunicode); |
| 3659 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 3660 | requiredsize = *outpos + repsize + insize-newpos; |
| 3661 | if (requiredsize > outsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3662 | if (requiredsize<2*outsize) |
| 3663 | requiredsize = 2*outsize; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 3664 | if (PyUnicode_Resize((PyObject**)output, requiredsize) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3665 | goto onError; |
| 3666 | *outptr = PyUnicode_AS_UNICODE(*output) + *outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3667 | } |
| 3668 | *endinpos = newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3669 | *inptr = *input + newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3670 | Py_UNICODE_COPY(*outptr, repptr, repsize); |
| 3671 | *outptr += repsize; |
| 3672 | *outpos += repsize; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3673 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3674 | /* we made it! */ |
| 3675 | res = 0; |
| 3676 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3677 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3678 | Py_XDECREF(restuple); |
| 3679 | return res; |
| 3680 | } |
| 3681 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3682 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 3683 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3684 | /* See RFC2152 for details. We encode conservatively and decode liberally. */ |
| 3685 | |
| 3686 | /* Three simple macros defining base-64. */ |
| 3687 | |
| 3688 | /* Is c a base-64 character? */ |
| 3689 | |
| 3690 | #define IS_BASE64(c) \ |
| 3691 | (((c) >= 'A' && (c) <= 'Z') || \ |
| 3692 | ((c) >= 'a' && (c) <= 'z') || \ |
| 3693 | ((c) >= '0' && (c) <= '9') || \ |
| 3694 | (c) == '+' || (c) == '/') |
| 3695 | |
| 3696 | /* given that c is a base-64 character, what is its base-64 value? */ |
| 3697 | |
| 3698 | #define FROM_BASE64(c) \ |
| 3699 | (((c) >= 'A' && (c) <= 'Z') ? (c) - 'A' : \ |
| 3700 | ((c) >= 'a' && (c) <= 'z') ? (c) - 'a' + 26 : \ |
| 3701 | ((c) >= '0' && (c) <= '9') ? (c) - '0' + 52 : \ |
| 3702 | (c) == '+' ? 62 : 63) |
| 3703 | |
| 3704 | /* What is the base-64 character of the bottom 6 bits of n? */ |
| 3705 | |
| 3706 | #define TO_BASE64(n) \ |
| 3707 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 3708 | |
| 3709 | /* DECODE_DIRECT: this byte encountered in a UTF-7 string should be |
| 3710 | * decoded as itself. We are permissive on decoding; the only ASCII |
| 3711 | * byte not decoding to itself is the + which begins a base64 |
| 3712 | * string. */ |
| 3713 | |
| 3714 | #define DECODE_DIRECT(c) \ |
| 3715 | ((c) <= 127 && (c) != '+') |
| 3716 | |
| 3717 | /* The UTF-7 encoder treats ASCII characters differently according to |
| 3718 | * whether they are Set D, Set O, Whitespace, or special (i.e. none of |
| 3719 | * the above). See RFC2152. This array identifies these different |
| 3720 | * sets: |
| 3721 | * 0 : "Set D" |
| 3722 | * alphanumeric and '(),-./:? |
| 3723 | * 1 : "Set O" |
| 3724 | * !"#$%&*;<=>@[]^_`{|} |
| 3725 | * 2 : "whitespace" |
| 3726 | * ht nl cr sp |
| 3727 | * 3 : special (must be base64 encoded) |
| 3728 | * everything else (i.e. +\~ and non-printing codes 0-8 11-12 14-31 127) |
| 3729 | */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3730 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3731 | static |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3732 | char utf7_category[128] = { |
| 3733 | /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */ |
| 3734 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 3, 3, 2, 3, 3, |
| 3735 | /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */ |
| 3736 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 3737 | /* sp ! " # $ % & ' ( ) * + , - . / */ |
| 3738 | 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 3, 0, 0, 0, 0, |
| 3739 | /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */ |
| 3740 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, |
| 3741 | /* @ A B C D E F G H I J K L M N O */ |
| 3742 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 3743 | /* P Q R S T U V W X Y Z [ \ ] ^ _ */ |
| 3744 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 1, 1, 1, |
| 3745 | /* ` a b c d e f g h i j k l m n o */ |
| 3746 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 3747 | /* p q r s t u v w x y z { | } ~ del */ |
| 3748 | 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] | 3749 | }; |
| 3750 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3751 | /* ENCODE_DIRECT: this character should be encoded as itself. The |
| 3752 | * answer depends on whether we are encoding set O as itself, and also |
| 3753 | * on whether we are encoding whitespace as itself. RFC2152 makes it |
| 3754 | * clear that the answers to these questions vary between |
| 3755 | * applications, so this code needs to be flexible. */ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 3756 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3757 | #define ENCODE_DIRECT(c, directO, directWS) \ |
| 3758 | ((c) < 128 && (c) > 0 && \ |
| 3759 | ((utf7_category[(c)] == 0) || \ |
| 3760 | (directWS && (utf7_category[(c)] == 2)) || \ |
| 3761 | (directO && (utf7_category[(c)] == 1)))) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3762 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3763 | PyObject * |
| 3764 | PyUnicode_DecodeUTF7(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3765 | Py_ssize_t size, |
| 3766 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3767 | { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3768 | return PyUnicode_DecodeUTF7Stateful(s, size, errors, NULL); |
| 3769 | } |
| 3770 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3771 | /* The decoder. The only state we preserve is our read position, |
| 3772 | * i.e. how many characters we have consumed. So if we end in the |
| 3773 | * middle of a shift sequence we have to back off the read position |
| 3774 | * and the output to the beginning of the sequence, otherwise we lose |
| 3775 | * all the shift state (seen bits, number of bits seen, high |
| 3776 | * surrogate). */ |
| 3777 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3778 | PyObject * |
| 3779 | PyUnicode_DecodeUTF7Stateful(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3780 | Py_ssize_t size, |
| 3781 | const char *errors, |
| 3782 | Py_ssize_t *consumed) |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3783 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3784 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3785 | Py_ssize_t startinpos; |
| 3786 | Py_ssize_t endinpos; |
| 3787 | Py_ssize_t outpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3788 | const char *e; |
| 3789 | PyUnicodeObject *unicode; |
| 3790 | Py_UNICODE *p; |
| 3791 | const char *errmsg = ""; |
| 3792 | int inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3793 | Py_UNICODE *shiftOutStart; |
| 3794 | unsigned int base64bits = 0; |
| 3795 | unsigned long base64buffer = 0; |
| 3796 | Py_UNICODE surrogate = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3797 | PyObject *errorHandler = NULL; |
| 3798 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3799 | |
| 3800 | unicode = _PyUnicode_New(size); |
| 3801 | if (!unicode) |
| 3802 | return NULL; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3803 | if (size == 0) { |
| 3804 | if (consumed) |
| 3805 | *consumed = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3806 | return (PyObject *)unicode; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3807 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3808 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3809 | p = PyUnicode_AS_UNICODE(unicode); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3810 | shiftOutStart = p; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3811 | e = s + size; |
| 3812 | |
| 3813 | while (s < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3814 | Py_UNICODE ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3815 | restart: |
Antoine Pitrou | 5ffd9e9 | 2008-07-25 18:05:24 +0000 | [diff] [blame] | 3816 | ch = (unsigned char) *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3817 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3818 | if (inShift) { /* in a base-64 section */ |
| 3819 | if (IS_BASE64(ch)) { /* consume a base-64 character */ |
| 3820 | base64buffer = (base64buffer << 6) | FROM_BASE64(ch); |
| 3821 | base64bits += 6; |
| 3822 | s++; |
| 3823 | if (base64bits >= 16) { |
| 3824 | /* we have enough bits for a UTF-16 value */ |
| 3825 | Py_UNICODE outCh = (Py_UNICODE) |
| 3826 | (base64buffer >> (base64bits-16)); |
| 3827 | base64bits -= 16; |
| 3828 | base64buffer &= (1 << base64bits) - 1; /* clear high bits */ |
| 3829 | if (surrogate) { |
| 3830 | /* expecting a second surrogate */ |
| 3831 | if (outCh >= 0xDC00 && outCh <= 0xDFFF) { |
| 3832 | #ifdef Py_UNICODE_WIDE |
| 3833 | *p++ = (((surrogate & 0x3FF)<<10) |
| 3834 | | (outCh & 0x3FF)) + 0x10000; |
| 3835 | #else |
| 3836 | *p++ = surrogate; |
| 3837 | *p++ = outCh; |
| 3838 | #endif |
| 3839 | surrogate = 0; |
| 3840 | } |
| 3841 | else { |
| 3842 | surrogate = 0; |
| 3843 | errmsg = "second surrogate missing"; |
| 3844 | goto utf7Error; |
| 3845 | } |
| 3846 | } |
| 3847 | else if (outCh >= 0xD800 && outCh <= 0xDBFF) { |
| 3848 | /* first surrogate */ |
| 3849 | surrogate = outCh; |
| 3850 | } |
| 3851 | else if (outCh >= 0xDC00 && outCh <= 0xDFFF) { |
| 3852 | errmsg = "unexpected second surrogate"; |
| 3853 | goto utf7Error; |
| 3854 | } |
| 3855 | else { |
| 3856 | *p++ = outCh; |
| 3857 | } |
| 3858 | } |
| 3859 | } |
| 3860 | else { /* now leaving a base-64 section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3861 | inShift = 0; |
| 3862 | s++; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3863 | if (surrogate) { |
| 3864 | errmsg = "second surrogate missing at end of shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3865 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3866 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3867 | if (base64bits > 0) { /* left-over bits */ |
| 3868 | if (base64bits >= 6) { |
| 3869 | /* We've seen at least one base-64 character */ |
| 3870 | errmsg = "partial character in shift sequence"; |
| 3871 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3872 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3873 | else { |
| 3874 | /* Some bits remain; they should be zero */ |
| 3875 | if (base64buffer != 0) { |
| 3876 | errmsg = "non-zero padding bits in shift sequence"; |
| 3877 | goto utf7Error; |
| 3878 | } |
| 3879 | } |
| 3880 | } |
| 3881 | if (ch != '-') { |
| 3882 | /* '-' is absorbed; other terminating |
| 3883 | characters are preserved */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3884 | *p++ = ch; |
| 3885 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3886 | } |
| 3887 | } |
| 3888 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3889 | startinpos = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3890 | s++; /* consume '+' */ |
| 3891 | if (s < e && *s == '-') { /* '+-' encodes '+' */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3892 | s++; |
| 3893 | *p++ = '+'; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3894 | } |
| 3895 | else { /* begin base64-encoded section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3896 | inShift = 1; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3897 | shiftOutStart = p; |
| 3898 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3899 | } |
| 3900 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3901 | else if (DECODE_DIRECT(ch)) { /* character decodes as itself */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3902 | *p++ = ch; |
| 3903 | s++; |
| 3904 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3905 | else { |
| 3906 | startinpos = s-starts; |
| 3907 | s++; |
| 3908 | errmsg = "unexpected special character"; |
| 3909 | goto utf7Error; |
| 3910 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3911 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3912 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3913 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 3914 | endinpos = s-starts; |
| 3915 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3916 | errors, &errorHandler, |
| 3917 | "utf7", errmsg, |
| 3918 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 3919 | &unicode, &outpos, &p)) |
| 3920 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3921 | } |
| 3922 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3923 | /* end of string */ |
| 3924 | |
| 3925 | if (inShift && !consumed) { /* in shift sequence, no more to follow */ |
| 3926 | /* if we're in an inconsistent state, that's an error */ |
| 3927 | if (surrogate || |
| 3928 | (base64bits >= 6) || |
| 3929 | (base64bits > 0 && base64buffer != 0)) { |
| 3930 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 3931 | endinpos = size; |
| 3932 | if (unicode_decode_call_errorhandler( |
| 3933 | errors, &errorHandler, |
| 3934 | "utf7", "unterminated shift sequence", |
| 3935 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 3936 | &unicode, &outpos, &p)) |
| 3937 | goto onError; |
| 3938 | if (s < e) |
| 3939 | goto restart; |
| 3940 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3941 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3942 | |
| 3943 | /* return state */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3944 | if (consumed) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3945 | if (inShift) { |
| 3946 | p = shiftOutStart; /* back off output */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3947 | *consumed = startinpos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3948 | } |
| 3949 | else { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3950 | *consumed = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3951 | } |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3952 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3953 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 3954 | if (PyUnicode_Resize((PyObject**)&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3955 | goto onError; |
| 3956 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3957 | Py_XDECREF(errorHandler); |
| 3958 | Py_XDECREF(exc); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 3959 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 3960 | if (_PyUnicode_READY_REPLACE(&unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3961 | Py_DECREF(unicode); |
| 3962 | return NULL; |
| 3963 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 3964 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 3965 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3966 | return (PyObject *)unicode; |
| 3967 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3968 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3969 | Py_XDECREF(errorHandler); |
| 3970 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3971 | Py_DECREF(unicode); |
| 3972 | return NULL; |
| 3973 | } |
| 3974 | |
| 3975 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3976 | PyObject * |
| 3977 | PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3978 | Py_ssize_t size, |
| 3979 | int base64SetO, |
| 3980 | int base64WhiteSpace, |
| 3981 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3982 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3983 | PyObject *v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3984 | /* It might be possible to tighten this worst case */ |
Alexandre Vassalotti | e85bd98 | 2009-07-21 00:39:03 +0000 | [diff] [blame] | 3985 | Py_ssize_t allocated = 8 * size; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3986 | int inShift = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3987 | Py_ssize_t i = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3988 | unsigned int base64bits = 0; |
| 3989 | unsigned long base64buffer = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3990 | char * out; |
| 3991 | char * start; |
| 3992 | |
| 3993 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3994 | return PyBytes_FromStringAndSize(NULL, 0); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3995 | |
Alexandre Vassalotti | e85bd98 | 2009-07-21 00:39:03 +0000 | [diff] [blame] | 3996 | if (allocated / 8 != size) |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3997 | return PyErr_NoMemory(); |
| 3998 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3999 | v = PyBytes_FromStringAndSize(NULL, allocated); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4000 | if (v == NULL) |
| 4001 | return NULL; |
| 4002 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4003 | start = out = PyBytes_AS_STRING(v); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4004 | for (;i < size; ++i) { |
| 4005 | Py_UNICODE ch = s[i]; |
| 4006 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4007 | if (inShift) { |
| 4008 | if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 4009 | /* shifting out */ |
| 4010 | if (base64bits) { /* output remaining bits */ |
| 4011 | *out++ = TO_BASE64(base64buffer << (6-base64bits)); |
| 4012 | base64buffer = 0; |
| 4013 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4014 | } |
| 4015 | inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4016 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 4017 | so no '-' is required, except if the character is itself a '-' */ |
| 4018 | if (IS_BASE64(ch) || ch == '-') { |
| 4019 | *out++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4020 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4021 | *out++ = (char) ch; |
| 4022 | } |
| 4023 | else { |
| 4024 | goto encode_char; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4025 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4026 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4027 | else { /* not in a shift sequence */ |
| 4028 | if (ch == '+') { |
| 4029 | *out++ = '+'; |
| 4030 | *out++ = '-'; |
| 4031 | } |
| 4032 | else if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 4033 | *out++ = (char) ch; |
| 4034 | } |
| 4035 | else { |
| 4036 | *out++ = '+'; |
| 4037 | inShift = 1; |
| 4038 | goto encode_char; |
| 4039 | } |
| 4040 | } |
| 4041 | continue; |
| 4042 | encode_char: |
| 4043 | #ifdef Py_UNICODE_WIDE |
| 4044 | if (ch >= 0x10000) { |
| 4045 | /* code first surrogate */ |
| 4046 | base64bits += 16; |
| 4047 | base64buffer = (base64buffer << 16) | 0xd800 | ((ch-0x10000) >> 10); |
| 4048 | while (base64bits >= 6) { |
| 4049 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 4050 | base64bits -= 6; |
| 4051 | } |
| 4052 | /* prepare second surrogate */ |
| 4053 | ch = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 4054 | } |
| 4055 | #endif |
| 4056 | base64bits += 16; |
| 4057 | base64buffer = (base64buffer << 16) | ch; |
| 4058 | while (base64bits >= 6) { |
| 4059 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 4060 | base64bits -= 6; |
| 4061 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 4062 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4063 | if (base64bits) |
| 4064 | *out++= TO_BASE64(base64buffer << (6-base64bits) ); |
| 4065 | if (inShift) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4066 | *out++ = '-'; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4067 | if (_PyBytes_Resize(&v, out - start) < 0) |
| 4068 | return NULL; |
| 4069 | return v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4070 | } |
| 4071 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4072 | #undef IS_BASE64 |
| 4073 | #undef FROM_BASE64 |
| 4074 | #undef TO_BASE64 |
| 4075 | #undef DECODE_DIRECT |
| 4076 | #undef ENCODE_DIRECT |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4077 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4078 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 4079 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4080 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4081 | char utf8_code_length[256] = { |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4082 | /* Map UTF-8 encoded prefix byte to sequence length. Zero means |
| 4083 | illegal prefix. See RFC 3629 for details */ |
| 4084 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 00-0F */ |
| 4085 | 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] | 4086 | 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] | 4087 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 4088 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 4089 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 4090 | 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] | 4091 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 70-7F */ |
| 4092 | 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] | 4093 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4094 | 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] | 4095 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* B0-BF */ |
| 4096 | 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* C0-C1 + C2-CF */ |
| 4097 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* D0-DF */ |
| 4098 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* E0-EF */ |
| 4099 | 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] | 4100 | }; |
| 4101 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4102 | PyObject * |
| 4103 | PyUnicode_DecodeUTF8(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4104 | Py_ssize_t size, |
| 4105 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4106 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4107 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 4108 | } |
| 4109 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4110 | /* Mask to check or force alignment of a pointer to C 'long' boundaries */ |
| 4111 | #define LONG_PTR_MASK (size_t) (SIZEOF_LONG - 1) |
| 4112 | |
| 4113 | /* Mask to quickly check whether a C 'long' contains a |
| 4114 | non-ASCII, UTF8-encoded char. */ |
| 4115 | #if (SIZEOF_LONG == 8) |
| 4116 | # define ASCII_CHAR_MASK 0x8080808080808080L |
| 4117 | #elif (SIZEOF_LONG == 4) |
| 4118 | # define ASCII_CHAR_MASK 0x80808080L |
| 4119 | #else |
| 4120 | # error C 'long' size should be either 4 or 8! |
| 4121 | #endif |
| 4122 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4123 | /* Scans a UTF-8 string and returns the maximum character to be expected, |
| 4124 | the size of the decoded unicode string and if any major errors were |
| 4125 | encountered. |
| 4126 | |
| 4127 | This function does check basic UTF-8 sanity, it does however NOT CHECK |
| 4128 | if the string contains surrogates, and if all continuation bytes are |
| 4129 | within the correct ranges, these checks are performed in |
| 4130 | PyUnicode_DecodeUTF8Stateful. |
| 4131 | |
| 4132 | If it sets has_errors to 1, it means the value of unicode_size and max_char |
| 4133 | will be bogus and you should not rely on useful information in them. |
| 4134 | */ |
| 4135 | static Py_UCS4 |
| 4136 | utf8_max_char_size_and_has_errors(const char *s, Py_ssize_t string_size, |
| 4137 | Py_ssize_t *unicode_size, Py_ssize_t* consumed, |
| 4138 | int *has_errors) |
| 4139 | { |
| 4140 | Py_ssize_t n; |
| 4141 | Py_ssize_t char_count = 0; |
| 4142 | Py_UCS4 max_char = 127, new_max; |
| 4143 | Py_UCS4 upper_bound; |
| 4144 | const unsigned char *p = (const unsigned char *)s; |
| 4145 | const unsigned char *end = p + string_size; |
| 4146 | const unsigned char *aligned_end = (const unsigned char *) ((size_t) end & ~LONG_PTR_MASK); |
| 4147 | int err = 0; |
| 4148 | |
| 4149 | for (; p < end && !err; ++p, ++char_count) { |
| 4150 | /* Only check value if it's not a ASCII char... */ |
| 4151 | if (*p < 0x80) { |
| 4152 | /* Fast path, see below in PyUnicode_DecodeUTF8Stateful for |
| 4153 | an explanation. */ |
| 4154 | if (!((size_t) p & LONG_PTR_MASK)) { |
| 4155 | /* Help register allocation */ |
| 4156 | register const unsigned char *_p = p; |
| 4157 | while (_p < aligned_end) { |
| 4158 | unsigned long value = *(unsigned long *) _p; |
| 4159 | if (value & ASCII_CHAR_MASK) |
| 4160 | break; |
| 4161 | _p += SIZEOF_LONG; |
| 4162 | char_count += SIZEOF_LONG; |
| 4163 | } |
| 4164 | p = _p; |
| 4165 | if (p == end) |
| 4166 | break; |
| 4167 | } |
| 4168 | } |
| 4169 | if (*p >= 0x80) { |
| 4170 | n = utf8_code_length[*p]; |
| 4171 | new_max = max_char; |
| 4172 | switch (n) { |
| 4173 | /* invalid start byte */ |
| 4174 | case 0: |
| 4175 | err = 1; |
| 4176 | break; |
| 4177 | case 2: |
| 4178 | /* Code points between 0x00FF and 0x07FF inclusive. |
| 4179 | Approximate the upper bound of the code point, |
| 4180 | if this flips over 255 we can be sure it will be more |
| 4181 | than 255 and the string will need 2 bytes per code coint, |
| 4182 | if it stays under or equal to 255, we can be sure 1 byte |
| 4183 | is enough. |
| 4184 | ((*p & 0b00011111) << 6) | 0b00111111 */ |
| 4185 | upper_bound = ((*p & 0x1F) << 6) | 0x3F; |
| 4186 | if (max_char < upper_bound) |
| 4187 | new_max = upper_bound; |
| 4188 | /* Ensure we track at least that we left ASCII space. */ |
| 4189 | if (new_max < 128) |
| 4190 | new_max = 128; |
| 4191 | break; |
| 4192 | case 3: |
| 4193 | /* Between 0x0FFF and 0xFFFF inclusive, so values are |
| 4194 | always > 255 and <= 65535 and will always need 2 bytes. */ |
| 4195 | if (max_char < 65535) |
| 4196 | new_max = 65535; |
| 4197 | break; |
| 4198 | case 4: |
| 4199 | /* Code point will be above 0xFFFF for sure in this case. */ |
| 4200 | new_max = 65537; |
| 4201 | break; |
| 4202 | /* Internal error, this should be caught by the first if */ |
| 4203 | case 1: |
| 4204 | default: |
| 4205 | assert(0 && "Impossible case in utf8_max_char_and_size"); |
| 4206 | err = 1; |
| 4207 | } |
| 4208 | /* Instead of number of overall bytes for this code point, |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 4209 | n contains the number of following bytes: */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4210 | --n; |
| 4211 | /* Check if the follow up chars are all valid continuation bytes */ |
| 4212 | if (n >= 1) { |
| 4213 | const unsigned char *cont; |
| 4214 | if ((p + n) >= end) { |
| 4215 | if (consumed == 0) |
| 4216 | /* incomplete data, non-incremental decoding */ |
| 4217 | err = 1; |
| 4218 | break; |
| 4219 | } |
| 4220 | for (cont = p + 1; cont < (p + n); ++cont) { |
| 4221 | if ((*cont & 0xc0) != 0x80) { |
| 4222 | err = 1; |
| 4223 | break; |
| 4224 | } |
| 4225 | } |
| 4226 | p += n; |
| 4227 | } |
| 4228 | else |
| 4229 | err = 1; |
| 4230 | max_char = new_max; |
| 4231 | } |
| 4232 | } |
| 4233 | |
| 4234 | if (unicode_size) |
| 4235 | *unicode_size = char_count; |
| 4236 | if (has_errors) |
| 4237 | *has_errors = err; |
| 4238 | return max_char; |
| 4239 | } |
| 4240 | |
| 4241 | /* Similar to PyUnicode_WRITE but can also write into wstr field |
| 4242 | of the legacy unicode representation */ |
| 4243 | #define WRITE_FLEXIBLE_OR_WSTR(kind, buf, index, value) \ |
| 4244 | do { \ |
| 4245 | const int k_ = (kind); \ |
| 4246 | if (k_ == PyUnicode_WCHAR_KIND) \ |
| 4247 | ((Py_UNICODE *)(buf))[(index)] = (Py_UNICODE)(value); \ |
| 4248 | else if (k_ == PyUnicode_1BYTE_KIND) \ |
| 4249 | ((unsigned char *)(buf))[(index)] = (unsigned char)(value); \ |
| 4250 | else if (k_ == PyUnicode_2BYTE_KIND) \ |
| 4251 | ((Py_UCS2 *)(buf))[(index)] = (Py_UCS2)(value); \ |
| 4252 | else \ |
| 4253 | ((Py_UCS4 *)(buf))[(index)] = (Py_UCS4)(value); \ |
| 4254 | } while (0) |
| 4255 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4256 | PyObject * |
| 4257 | PyUnicode_DecodeUTF8Stateful(const char *s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4258 | Py_ssize_t size, |
| 4259 | const char *errors, |
| 4260 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4261 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4262 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4263 | int n; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4264 | int k; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4265 | Py_ssize_t startinpos; |
| 4266 | Py_ssize_t endinpos; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4267 | const char *e, *aligned_end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4268 | PyUnicodeObject *unicode; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4269 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4270 | PyObject *errorHandler = NULL; |
| 4271 | PyObject *exc = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4272 | Py_UCS4 maxchar = 0; |
| 4273 | Py_ssize_t unicode_size; |
| 4274 | Py_ssize_t i; |
| 4275 | int kind; |
| 4276 | void *data; |
| 4277 | int has_errors; |
| 4278 | Py_UNICODE *error_outptr; |
| 4279 | #if SIZEOF_WCHAR_T == 2 |
| 4280 | Py_ssize_t wchar_offset = 0; |
| 4281 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4282 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4283 | if (size == 0) { |
| 4284 | if (consumed) |
| 4285 | *consumed = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4286 | return (PyObject *)PyUnicode_New(0, 0); |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4287 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4288 | maxchar = utf8_max_char_size_and_has_errors(s, size, &unicode_size, |
| 4289 | consumed, &has_errors); |
| 4290 | if (has_errors) { |
| 4291 | unicode = _PyUnicode_New(size); |
| 4292 | if (!unicode) |
| 4293 | return NULL; |
| 4294 | kind = PyUnicode_WCHAR_KIND; |
| 4295 | data = PyUnicode_AS_UNICODE(unicode); |
| 4296 | assert(data != NULL); |
| 4297 | } |
| 4298 | else { |
| 4299 | unicode = (PyUnicodeObject *)PyUnicode_New(unicode_size, maxchar); |
| 4300 | if (!unicode) |
| 4301 | return NULL; |
| 4302 | /* When the string is ASCII only, just use memcpy and return. |
| 4303 | unicode_size may be != size if there is an incomplete UTF-8 |
| 4304 | sequence at the end of the ASCII block. */ |
| 4305 | if (maxchar < 128 && size == unicode_size) { |
| 4306 | Py_MEMCPY(PyUnicode_1BYTE_DATA(unicode), s, unicode_size); |
| 4307 | return (PyObject *)unicode; |
| 4308 | } |
| 4309 | kind = PyUnicode_KIND(unicode); |
| 4310 | data = PyUnicode_DATA(unicode); |
| 4311 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4312 | /* Unpack UTF-8 encoded data */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4313 | i = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4314 | e = s + size; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4315 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4316 | |
| 4317 | while (s < e) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4318 | Py_UCS4 ch = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4319 | |
| 4320 | if (ch < 0x80) { |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4321 | /* Fast path for runs of ASCII characters. Given that common UTF-8 |
| 4322 | input will consist of an overwhelming majority of ASCII |
| 4323 | characters, we try to optimize for this case by checking |
| 4324 | as many characters as a C 'long' can contain. |
| 4325 | First, check if we can do an aligned read, as most CPUs have |
| 4326 | a penalty for unaligned reads. |
| 4327 | */ |
| 4328 | if (!((size_t) s & LONG_PTR_MASK)) { |
| 4329 | /* Help register allocation */ |
| 4330 | register const char *_s = s; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4331 | register Py_ssize_t _i = i; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4332 | while (_s < aligned_end) { |
| 4333 | /* Read a whole long at a time (either 4 or 8 bytes), |
| 4334 | and do a fast unrolled copy if it only contains ASCII |
| 4335 | characters. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4336 | unsigned long value = *(unsigned long *) _s; |
| 4337 | if (value & ASCII_CHAR_MASK) |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4338 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4339 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+0, _s[0]); |
| 4340 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+1, _s[1]); |
| 4341 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+2, _s[2]); |
| 4342 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+3, _s[3]); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4343 | #if (SIZEOF_LONG == 8) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4344 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+4, _s[4]); |
| 4345 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+5, _s[5]); |
| 4346 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+6, _s[6]); |
| 4347 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+7, _s[7]); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4348 | #endif |
| 4349 | _s += SIZEOF_LONG; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4350 | _i += SIZEOF_LONG; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4351 | } |
| 4352 | s = _s; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4353 | i = _i; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4354 | if (s == e) |
| 4355 | break; |
| 4356 | ch = (unsigned char)*s; |
| 4357 | } |
| 4358 | } |
| 4359 | |
| 4360 | if (ch < 0x80) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4361 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4362 | s++; |
| 4363 | continue; |
| 4364 | } |
| 4365 | |
| 4366 | n = utf8_code_length[ch]; |
| 4367 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4368 | if (s + n > e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4369 | if (consumed) |
| 4370 | break; |
| 4371 | else { |
| 4372 | errmsg = "unexpected end of data"; |
| 4373 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4374 | endinpos = startinpos+1; |
| 4375 | for (k=1; (k < size-startinpos) && ((s[k]&0xC0) == 0x80); k++) |
| 4376 | endinpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4377 | goto utf8Error; |
| 4378 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4379 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4380 | |
| 4381 | switch (n) { |
| 4382 | |
| 4383 | case 0: |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4384 | errmsg = "invalid start byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4385 | startinpos = s-starts; |
| 4386 | endinpos = startinpos+1; |
| 4387 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4388 | |
| 4389 | case 1: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4390 | errmsg = "internal error"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4391 | startinpos = s-starts; |
| 4392 | endinpos = startinpos+1; |
| 4393 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4394 | |
| 4395 | case 2: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4396 | if ((s[1] & 0xc0) != 0x80) { |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4397 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4398 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4399 | endinpos = startinpos + 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4400 | goto utf8Error; |
| 4401 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4402 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4403 | assert ((ch > 0x007F) && (ch <= 0x07FF)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4404 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4405 | break; |
| 4406 | |
| 4407 | case 3: |
Ezio Melotti | 9bf2b3a | 2010-07-03 04:52:19 +0000 | [diff] [blame] | 4408 | /* Decoding UTF-8 sequences in range \xed\xa0\x80-\xed\xbf\xbf |
| 4409 | will result in surrogates in range d800-dfff. Surrogates are |
| 4410 | not valid UTF-8 so they are rejected. |
| 4411 | See http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf |
| 4412 | (table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4413 | if ((s[1] & 0xc0) != 0x80 || |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4414 | (s[2] & 0xc0) != 0x80 || |
| 4415 | ((unsigned char)s[0] == 0xE0 && |
| 4416 | (unsigned char)s[1] < 0xA0) || |
| 4417 | ((unsigned char)s[0] == 0xED && |
| 4418 | (unsigned char)s[1] > 0x9F)) { |
| 4419 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4420 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4421 | endinpos = startinpos + 1; |
| 4422 | |
| 4423 | /* if s[1] first two bits are 1 and 0, then the invalid |
| 4424 | continuation byte is s[2], so increment endinpos by 1, |
| 4425 | if not, s[1] is invalid and endinpos doesn't need to |
| 4426 | be incremented. */ |
| 4427 | if ((s[1] & 0xC0) == 0x80) |
| 4428 | endinpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4429 | goto utf8Error; |
| 4430 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4431 | ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4432 | assert ((ch > 0x07FF) && (ch <= 0xFFFF)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4433 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, ch); |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4434 | break; |
| 4435 | |
| 4436 | case 4: |
| 4437 | if ((s[1] & 0xc0) != 0x80 || |
| 4438 | (s[2] & 0xc0) != 0x80 || |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4439 | (s[3] & 0xc0) != 0x80 || |
| 4440 | ((unsigned char)s[0] == 0xF0 && |
| 4441 | (unsigned char)s[1] < 0x90) || |
| 4442 | ((unsigned char)s[0] == 0xF4 && |
| 4443 | (unsigned char)s[1] > 0x8F)) { |
| 4444 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4445 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4446 | endinpos = startinpos + 1; |
| 4447 | if ((s[1] & 0xC0) == 0x80) { |
| 4448 | endinpos++; |
| 4449 | if ((s[2] & 0xC0) == 0x80) |
| 4450 | endinpos++; |
| 4451 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4452 | goto utf8Error; |
| 4453 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4454 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4455 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
| 4456 | assert ((ch > 0xFFFF) && (ch <= 0x10ffff)); |
| 4457 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4458 | /* If the string is flexible or we have native UCS-4, write |
| 4459 | directly.. */ |
| 4460 | if (sizeof(Py_UNICODE) > 2 || kind != PyUnicode_WCHAR_KIND) |
| 4461 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, ch); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4462 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4463 | else { |
| 4464 | /* compute and append the two surrogates: */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4465 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4466 | /* translate from 10000..10FFFF to 0..FFFF */ |
| 4467 | ch -= 0x10000; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4468 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4469 | /* high surrogate = top 10 bits added to D800 */ |
| 4470 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, |
| 4471 | (Py_UNICODE)(0xD800 + (ch >> 10))); |
| 4472 | |
| 4473 | /* low surrogate = bottom 10 bits added to DC00 */ |
| 4474 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, |
| 4475 | (Py_UNICODE)(0xDC00 + (ch & 0x03FF))); |
| 4476 | } |
| 4477 | #if SIZEOF_WCHAR_T == 2 |
| 4478 | wchar_offset++; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 4479 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4480 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4481 | } |
| 4482 | s += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4483 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4484 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4485 | utf8Error: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4486 | /* If this is not yet a resizable string, make it one.. */ |
| 4487 | if (kind != PyUnicode_WCHAR_KIND) { |
| 4488 | const Py_UNICODE *u; |
| 4489 | PyUnicodeObject *new_unicode = _PyUnicode_New(size); |
| 4490 | if (!new_unicode) |
| 4491 | goto onError; |
| 4492 | u = PyUnicode_AsUnicode((PyObject *)unicode); |
| 4493 | if (!u) |
| 4494 | goto onError; |
| 4495 | #if SIZEOF_WCHAR_T == 2 |
| 4496 | i += wchar_offset; |
| 4497 | #endif |
| 4498 | Py_UNICODE_COPY(PyUnicode_AS_UNICODE(new_unicode), u, i); |
| 4499 | Py_DECREF(unicode); |
| 4500 | unicode = new_unicode; |
| 4501 | kind = 0; |
| 4502 | data = PyUnicode_AS_UNICODE(new_unicode); |
| 4503 | assert(data != NULL); |
| 4504 | } |
| 4505 | error_outptr = PyUnicode_AS_UNICODE(unicode) + i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4506 | if (unicode_decode_call_errorhandler( |
| 4507 | errors, &errorHandler, |
| 4508 | "utf8", errmsg, |
| 4509 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4510 | &unicode, &i, &error_outptr)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4511 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4512 | /* Update data because unicode_decode_call_errorhandler might have |
| 4513 | re-created or resized the unicode object. */ |
| 4514 | data = PyUnicode_AS_UNICODE(unicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4515 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4516 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4517 | /* Ensure the unicode_size calculation above was correct: */ |
| 4518 | assert(kind == PyUnicode_WCHAR_KIND || i == unicode_size); |
| 4519 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4520 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4521 | *consumed = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4522 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4523 | /* Adjust length and ready string when it contained errors and |
| 4524 | is of the old resizable kind. */ |
| 4525 | if (kind == PyUnicode_WCHAR_KIND) { |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 4526 | if (PyUnicode_Resize((PyObject**)&unicode, i) < 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4527 | goto onError; |
| 4528 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4529 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4530 | Py_XDECREF(errorHandler); |
| 4531 | Py_XDECREF(exc); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 4532 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 4533 | if (_PyUnicode_READY_REPLACE(&unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4534 | Py_DECREF(unicode); |
| 4535 | return NULL; |
| 4536 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 4537 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 4538 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4539 | return (PyObject *)unicode; |
| 4540 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4541 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4542 | Py_XDECREF(errorHandler); |
| 4543 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4544 | Py_DECREF(unicode); |
| 4545 | return NULL; |
| 4546 | } |
| 4547 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4548 | #undef WRITE_FLEXIBLE_OR_WSTR |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4549 | |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4550 | #ifdef __APPLE__ |
| 4551 | |
| 4552 | /* Simplified UTF-8 decoder using surrogateescape error handler, |
| 4553 | used to decode the command line arguments on Mac OS X. */ |
| 4554 | |
| 4555 | wchar_t* |
| 4556 | _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size) |
| 4557 | { |
| 4558 | int n; |
| 4559 | const char *e; |
| 4560 | wchar_t *unicode, *p; |
| 4561 | |
| 4562 | /* Note: size will always be longer than the resulting Unicode |
| 4563 | character count */ |
| 4564 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < (size + 1)) { |
| 4565 | PyErr_NoMemory(); |
| 4566 | return NULL; |
| 4567 | } |
| 4568 | unicode = PyMem_Malloc((size + 1) * sizeof(wchar_t)); |
| 4569 | if (!unicode) |
| 4570 | return NULL; |
| 4571 | |
| 4572 | /* Unpack UTF-8 encoded data */ |
| 4573 | p = unicode; |
| 4574 | e = s + size; |
| 4575 | while (s < e) { |
| 4576 | Py_UCS4 ch = (unsigned char)*s; |
| 4577 | |
| 4578 | if (ch < 0x80) { |
| 4579 | *p++ = (wchar_t)ch; |
| 4580 | s++; |
| 4581 | continue; |
| 4582 | } |
| 4583 | |
| 4584 | n = utf8_code_length[ch]; |
| 4585 | if (s + n > e) { |
| 4586 | goto surrogateescape; |
| 4587 | } |
| 4588 | |
| 4589 | switch (n) { |
| 4590 | case 0: |
| 4591 | case 1: |
| 4592 | goto surrogateescape; |
| 4593 | |
| 4594 | case 2: |
| 4595 | if ((s[1] & 0xc0) != 0x80) |
| 4596 | goto surrogateescape; |
| 4597 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
| 4598 | assert ((ch > 0x007F) && (ch <= 0x07FF)); |
| 4599 | *p++ = (wchar_t)ch; |
| 4600 | break; |
| 4601 | |
| 4602 | case 3: |
| 4603 | /* Decoding UTF-8 sequences in range \xed\xa0\x80-\xed\xbf\xbf |
| 4604 | will result in surrogates in range d800-dfff. Surrogates are |
| 4605 | not valid UTF-8 so they are rejected. |
| 4606 | See http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf |
| 4607 | (table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt */ |
| 4608 | if ((s[1] & 0xc0) != 0x80 || |
| 4609 | (s[2] & 0xc0) != 0x80 || |
| 4610 | ((unsigned char)s[0] == 0xE0 && |
| 4611 | (unsigned char)s[1] < 0xA0) || |
| 4612 | ((unsigned char)s[0] == 0xED && |
| 4613 | (unsigned char)s[1] > 0x9F)) { |
| 4614 | |
| 4615 | goto surrogateescape; |
| 4616 | } |
| 4617 | ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); |
| 4618 | assert ((ch > 0x07FF) && (ch <= 0xFFFF)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4619 | *p++ = (wchar_t)ch; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4620 | break; |
| 4621 | |
| 4622 | case 4: |
| 4623 | if ((s[1] & 0xc0) != 0x80 || |
| 4624 | (s[2] & 0xc0) != 0x80 || |
| 4625 | (s[3] & 0xc0) != 0x80 || |
| 4626 | ((unsigned char)s[0] == 0xF0 && |
| 4627 | (unsigned char)s[1] < 0x90) || |
| 4628 | ((unsigned char)s[0] == 0xF4 && |
| 4629 | (unsigned char)s[1] > 0x8F)) { |
| 4630 | goto surrogateescape; |
| 4631 | } |
| 4632 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
| 4633 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
| 4634 | assert ((ch > 0xFFFF) && (ch <= 0x10ffff)); |
| 4635 | |
| 4636 | #if SIZEOF_WCHAR_T == 4 |
| 4637 | *p++ = (wchar_t)ch; |
| 4638 | #else |
| 4639 | /* compute and append the two surrogates: */ |
| 4640 | |
| 4641 | /* translate from 10000..10FFFF to 0..FFFF */ |
| 4642 | ch -= 0x10000; |
| 4643 | |
| 4644 | /* high surrogate = top 10 bits added to D800 */ |
| 4645 | *p++ = (wchar_t)(0xD800 + (ch >> 10)); |
| 4646 | |
| 4647 | /* low surrogate = bottom 10 bits added to DC00 */ |
| 4648 | *p++ = (wchar_t)(0xDC00 + (ch & 0x03FF)); |
| 4649 | #endif |
| 4650 | break; |
| 4651 | } |
| 4652 | s += n; |
| 4653 | continue; |
| 4654 | |
| 4655 | surrogateescape: |
| 4656 | *p++ = 0xDC00 + ch; |
| 4657 | s++; |
| 4658 | } |
| 4659 | *p = L'\0'; |
| 4660 | return unicode; |
| 4661 | } |
| 4662 | |
| 4663 | #endif /* __APPLE__ */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4664 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4665 | /* Primary internal function which creates utf8 encoded bytes objects. |
| 4666 | |
| 4667 | Allocation strategy: if the string is short, convert into a stack buffer |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4668 | and allocate exactly as much space needed at the end. Else allocate the |
| 4669 | maximum possible needed (4 result bytes per Unicode character), and return |
| 4670 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4671 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 4672 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4673 | _PyUnicode_AsUTF8String(PyObject *obj, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4674 | { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4675 | #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] | 4676 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4677 | Py_ssize_t i; /* index into s of next input byte */ |
| 4678 | PyObject *result; /* result string object */ |
| 4679 | char *p; /* next free byte in output buffer */ |
| 4680 | Py_ssize_t nallocated; /* number of result bytes allocated */ |
| 4681 | Py_ssize_t nneeded; /* number of result bytes needed */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4682 | char stackbuf[MAX_SHORT_UNICHARS * 4]; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4683 | PyObject *errorHandler = NULL; |
| 4684 | PyObject *exc = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4685 | int kind; |
| 4686 | void *data; |
| 4687 | Py_ssize_t size; |
| 4688 | PyUnicodeObject *unicode = (PyUnicodeObject *)obj; |
| 4689 | #if SIZEOF_WCHAR_T == 2 |
| 4690 | Py_ssize_t wchar_offset = 0; |
| 4691 | #endif |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 4692 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4693 | if (!PyUnicode_Check(unicode)) { |
| 4694 | PyErr_BadArgument(); |
| 4695 | return NULL; |
| 4696 | } |
| 4697 | |
| 4698 | if (PyUnicode_READY(unicode) == -1) |
| 4699 | return NULL; |
| 4700 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 4701 | if (PyUnicode_UTF8(unicode)) |
| 4702 | return PyBytes_FromStringAndSize(PyUnicode_UTF8(unicode), |
| 4703 | PyUnicode_UTF8_LENGTH(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4704 | |
| 4705 | kind = PyUnicode_KIND(unicode); |
| 4706 | data = PyUnicode_DATA(unicode); |
| 4707 | size = PyUnicode_GET_LENGTH(unicode); |
| 4708 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4709 | assert(size >= 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4710 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4711 | if (size <= MAX_SHORT_UNICHARS) { |
| 4712 | /* Write into the stack buffer; nallocated can't overflow. |
| 4713 | * At the end, we'll allocate exactly as much heap space as it |
| 4714 | * turns out we need. |
| 4715 | */ |
| 4716 | nallocated = Py_SAFE_DOWNCAST(sizeof(stackbuf), size_t, int); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4717 | result = NULL; /* will allocate after we're done */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4718 | p = stackbuf; |
| 4719 | } |
| 4720 | else { |
| 4721 | /* Overallocate on the heap, and give the excess back at the end. */ |
| 4722 | nallocated = size * 4; |
| 4723 | if (nallocated / 4 != size) /* overflow! */ |
| 4724 | return PyErr_NoMemory(); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4725 | result = PyBytes_FromStringAndSize(NULL, nallocated); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4726 | if (result == NULL) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4727 | return NULL; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4728 | p = PyBytes_AS_STRING(result); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4729 | } |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4730 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4731 | for (i = 0; i < size;) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4732 | Py_UCS4 ch = PyUnicode_READ(kind, data, i++); |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 4733 | |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4734 | if (ch < 0x80) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4735 | /* Encode ASCII */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4736 | *p++ = (char) ch; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 4737 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4738 | else if (ch < 0x0800) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4739 | /* Encode Latin-1 */ |
Marc-André Lemburg | dc724d6 | 2002-02-06 18:20:19 +0000 | [diff] [blame] | 4740 | *p++ = (char)(0xc0 | (ch >> 6)); |
| 4741 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4742 | } else if (0xD800 <= ch && ch <= 0xDFFF) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4743 | Py_ssize_t newpos; |
| 4744 | PyObject *rep; |
| 4745 | Py_ssize_t repsize, k, startpos; |
| 4746 | startpos = i-1; |
| 4747 | #if SIZEOF_WCHAR_T == 2 |
| 4748 | startpos += wchar_offset; |
Victor Stinner | 445a623 | 2010-04-22 20:01:57 +0000 | [diff] [blame] | 4749 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4750 | rep = unicode_encode_call_errorhandler( |
| 4751 | errors, &errorHandler, "utf-8", "surrogates not allowed", |
| 4752 | PyUnicode_AS_UNICODE(unicode), PyUnicode_GET_SIZE(unicode), |
| 4753 | &exc, startpos, startpos+1, &newpos); |
| 4754 | if (!rep) |
| 4755 | goto error; |
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 | repsize = PyBytes_GET_SIZE(rep); |
| 4759 | else |
| 4760 | repsize = PyUnicode_GET_SIZE(rep); |
| 4761 | |
| 4762 | if (repsize > 4) { |
| 4763 | Py_ssize_t offset; |
| 4764 | |
| 4765 | if (result == NULL) |
| 4766 | offset = p - stackbuf; |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4767 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4768 | offset = p - PyBytes_AS_STRING(result); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4769 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4770 | if (nallocated > PY_SSIZE_T_MAX - repsize + 4) { |
| 4771 | /* integer overflow */ |
| 4772 | PyErr_NoMemory(); |
| 4773 | goto error; |
| 4774 | } |
| 4775 | nallocated += repsize - 4; |
| 4776 | if (result != NULL) { |
| 4777 | if (_PyBytes_Resize(&result, nallocated) < 0) |
| 4778 | goto error; |
| 4779 | } else { |
| 4780 | result = PyBytes_FromStringAndSize(NULL, nallocated); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4781 | if (result == NULL) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4782 | goto error; |
| 4783 | Py_MEMCPY(PyBytes_AS_STRING(result), stackbuf, offset); |
| 4784 | } |
| 4785 | p = PyBytes_AS_STRING(result) + offset; |
| 4786 | } |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4787 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4788 | if (PyBytes_Check(rep)) { |
| 4789 | char *prep = PyBytes_AS_STRING(rep); |
| 4790 | for(k = repsize; k > 0; k--) |
| 4791 | *p++ = *prep++; |
| 4792 | } else /* rep is unicode */ { |
| 4793 | const Py_UNICODE *prep = PyUnicode_AS_UNICODE(rep); |
| 4794 | Py_UNICODE c; |
| 4795 | |
| 4796 | for(k=0; k<repsize; k++) { |
| 4797 | c = prep[k]; |
| 4798 | if (0x80 <= c) { |
| 4799 | raise_encode_exception(&exc, "utf-8", |
| 4800 | PyUnicode_AS_UNICODE(unicode), |
| 4801 | size, i-1, i, |
| 4802 | "surrogates not allowed"); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4803 | goto error; |
| 4804 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4805 | *p++ = (char)prep[k]; |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4806 | } |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4807 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4808 | Py_DECREF(rep); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4809 | } else if (ch < 0x10000) { |
| 4810 | *p++ = (char)(0xe0 | (ch >> 12)); |
| 4811 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 4812 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 4813 | } else /* ch >= 0x10000 */ { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4814 | /* Encode UCS4 Unicode ordinals */ |
| 4815 | *p++ = (char)(0xf0 | (ch >> 18)); |
| 4816 | *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); |
| 4817 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 4818 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4819 | #if SIZEOF_WCHAR_T == 2 |
| 4820 | wchar_offset++; |
| 4821 | #endif |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4822 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4823 | } |
Tim Peters | 0eca65c | 2002-04-21 17:28:06 +0000 | [diff] [blame] | 4824 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4825 | if (result == NULL) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4826 | /* This was stack allocated. */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4827 | nneeded = p - stackbuf; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4828 | assert(nneeded <= nallocated); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4829 | result = PyBytes_FromStringAndSize(stackbuf, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4830 | } |
| 4831 | else { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 4832 | /* Cut back to size actually needed. */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4833 | nneeded = p - PyBytes_AS_STRING(result); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4834 | assert(nneeded <= nallocated); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4835 | _PyBytes_Resize(&result, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4836 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4837 | |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4838 | Py_XDECREF(errorHandler); |
| 4839 | Py_XDECREF(exc); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4840 | return result; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4841 | error: |
| 4842 | Py_XDECREF(errorHandler); |
| 4843 | Py_XDECREF(exc); |
| 4844 | Py_XDECREF(result); |
| 4845 | return NULL; |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4846 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4847 | #undef MAX_SHORT_UNICHARS |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4848 | } |
| 4849 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4850 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4851 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
| 4852 | Py_ssize_t size, |
| 4853 | const char *errors) |
| 4854 | { |
| 4855 | PyObject *v, *unicode; |
| 4856 | |
| 4857 | unicode = PyUnicode_FromUnicode(s, size); |
| 4858 | if (unicode == NULL) |
| 4859 | return NULL; |
| 4860 | v = _PyUnicode_AsUTF8String(unicode, errors); |
| 4861 | Py_DECREF(unicode); |
| 4862 | return v; |
| 4863 | } |
| 4864 | |
| 4865 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4866 | PyUnicode_AsUTF8String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4867 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4868 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4869 | } |
| 4870 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4871 | /* --- UTF-32 Codec ------------------------------------------------------- */ |
| 4872 | |
| 4873 | PyObject * |
| 4874 | PyUnicode_DecodeUTF32(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4875 | Py_ssize_t size, |
| 4876 | const char *errors, |
| 4877 | int *byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4878 | { |
| 4879 | return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); |
| 4880 | } |
| 4881 | |
| 4882 | PyObject * |
| 4883 | PyUnicode_DecodeUTF32Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4884 | Py_ssize_t size, |
| 4885 | const char *errors, |
| 4886 | int *byteorder, |
| 4887 | Py_ssize_t *consumed) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4888 | { |
| 4889 | const char *starts = s; |
| 4890 | Py_ssize_t startinpos; |
| 4891 | Py_ssize_t endinpos; |
| 4892 | Py_ssize_t outpos; |
| 4893 | PyUnicodeObject *unicode; |
| 4894 | Py_UNICODE *p; |
| 4895 | #ifndef Py_UNICODE_WIDE |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 4896 | int pairs = 0; |
Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 4897 | const unsigned char *qq; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4898 | #else |
| 4899 | const int pairs = 0; |
| 4900 | #endif |
Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 4901 | const unsigned char *q, *e; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4902 | int bo = 0; /* assume native ordering by default */ |
| 4903 | const char *errmsg = ""; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4904 | /* Offsets from q for retrieving bytes in the right order. */ |
| 4905 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 4906 | int iorder[] = {0, 1, 2, 3}; |
| 4907 | #else |
| 4908 | int iorder[] = {3, 2, 1, 0}; |
| 4909 | #endif |
| 4910 | PyObject *errorHandler = NULL; |
| 4911 | PyObject *exc = NULL; |
Victor Stinner | 313a120 | 2010-06-11 23:56:51 +0000 | [diff] [blame] | 4912 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4913 | q = (unsigned char *)s; |
| 4914 | e = q + size; |
| 4915 | |
| 4916 | if (byteorder) |
| 4917 | bo = *byteorder; |
| 4918 | |
| 4919 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 4920 | byte order setting accordingly. In native mode, the leading BOM |
| 4921 | mark is skipped, in all other modes, it is copied to the output |
| 4922 | stream as-is (giving a ZWNBSP character). */ |
| 4923 | if (bo == 0) { |
| 4924 | if (size >= 4) { |
| 4925 | const Py_UCS4 bom = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4926 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4927 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4928 | if (bom == 0x0000FEFF) { |
| 4929 | q += 4; |
| 4930 | bo = -1; |
| 4931 | } |
| 4932 | else if (bom == 0xFFFE0000) { |
| 4933 | q += 4; |
| 4934 | bo = 1; |
| 4935 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4936 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4937 | if (bom == 0x0000FEFF) { |
| 4938 | q += 4; |
| 4939 | bo = 1; |
| 4940 | } |
| 4941 | else if (bom == 0xFFFE0000) { |
| 4942 | q += 4; |
| 4943 | bo = -1; |
| 4944 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4945 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4946 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4947 | } |
| 4948 | |
| 4949 | if (bo == -1) { |
| 4950 | /* force LE */ |
| 4951 | iorder[0] = 0; |
| 4952 | iorder[1] = 1; |
| 4953 | iorder[2] = 2; |
| 4954 | iorder[3] = 3; |
| 4955 | } |
| 4956 | else if (bo == 1) { |
| 4957 | /* force BE */ |
| 4958 | iorder[0] = 3; |
| 4959 | iorder[1] = 2; |
| 4960 | iorder[2] = 1; |
| 4961 | iorder[3] = 0; |
| 4962 | } |
| 4963 | |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 4964 | /* On narrow builds we split characters outside the BMP into two |
| 4965 | codepoints => count how much extra space we need. */ |
| 4966 | #ifndef Py_UNICODE_WIDE |
| 4967 | for (qq = q; qq < e; qq += 4) |
| 4968 | if (qq[iorder[2]] != 0 || qq[iorder[3]] != 0) |
| 4969 | pairs++; |
| 4970 | #endif |
| 4971 | |
| 4972 | /* This might be one to much, because of a BOM */ |
| 4973 | unicode = _PyUnicode_New((size+3)/4+pairs); |
| 4974 | if (!unicode) |
| 4975 | return NULL; |
| 4976 | if (size == 0) |
| 4977 | return (PyObject *)unicode; |
| 4978 | |
| 4979 | /* Unpack UTF-32 encoded data */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4980 | p = PyUnicode_AS_UNICODE(unicode); |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 4981 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4982 | while (q < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4983 | Py_UCS4 ch; |
| 4984 | /* remaining bytes at the end? (size should be divisible by 4) */ |
| 4985 | if (e-q<4) { |
| 4986 | if (consumed) |
| 4987 | break; |
| 4988 | errmsg = "truncated data"; |
| 4989 | startinpos = ((const char *)q)-starts; |
| 4990 | endinpos = ((const char *)e)-starts; |
| 4991 | goto utf32Error; |
| 4992 | /* The remaining input chars are ignored if the callback |
| 4993 | chooses to skip the input */ |
| 4994 | } |
| 4995 | ch = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
| 4996 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4997 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4998 | if (ch >= 0x110000) |
| 4999 | { |
| 5000 | errmsg = "codepoint not in range(0x110000)"; |
| 5001 | startinpos = ((const char *)q)-starts; |
| 5002 | endinpos = startinpos+4; |
| 5003 | goto utf32Error; |
| 5004 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5005 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5006 | if (ch >= 0x10000) |
| 5007 | { |
| 5008 | *p++ = 0xD800 | ((ch-0x10000) >> 10); |
| 5009 | *p++ = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 5010 | } |
| 5011 | else |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5012 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5013 | *p++ = ch; |
| 5014 | q += 4; |
| 5015 | continue; |
| 5016 | utf32Error: |
| 5017 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 5018 | if (unicode_decode_call_errorhandler( |
| 5019 | errors, &errorHandler, |
| 5020 | "utf32", errmsg, |
| 5021 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, |
| 5022 | &unicode, &outpos, &p)) |
| 5023 | goto onError; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5024 | } |
| 5025 | |
| 5026 | if (byteorder) |
| 5027 | *byteorder = bo; |
| 5028 | |
| 5029 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5030 | *consumed = (const char *)q-starts; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5031 | |
| 5032 | /* Adjust length */ |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 5033 | if (PyUnicode_Resize((PyObject**)&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5034 | goto onError; |
| 5035 | |
| 5036 | Py_XDECREF(errorHandler); |
| 5037 | Py_XDECREF(exc); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 5038 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 5039 | if (_PyUnicode_READY_REPLACE(&unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5040 | Py_DECREF(unicode); |
| 5041 | return NULL; |
| 5042 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 5043 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 5044 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5045 | return (PyObject *)unicode; |
| 5046 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5047 | onError: |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5048 | Py_DECREF(unicode); |
| 5049 | Py_XDECREF(errorHandler); |
| 5050 | Py_XDECREF(exc); |
| 5051 | return NULL; |
| 5052 | } |
| 5053 | |
| 5054 | PyObject * |
| 5055 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5056 | Py_ssize_t size, |
| 5057 | const char *errors, |
| 5058 | int byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5059 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5060 | PyObject *v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5061 | unsigned char *p; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5062 | Py_ssize_t nsize, bytesize; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5063 | #ifndef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5064 | Py_ssize_t i, pairs; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5065 | #else |
| 5066 | const int pairs = 0; |
| 5067 | #endif |
| 5068 | /* Offsets from p for storing byte pairs in the right order. */ |
| 5069 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5070 | int iorder[] = {0, 1, 2, 3}; |
| 5071 | #else |
| 5072 | int iorder[] = {3, 2, 1, 0}; |
| 5073 | #endif |
| 5074 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5075 | #define STORECHAR(CH) \ |
| 5076 | do { \ |
| 5077 | p[iorder[3]] = ((CH) >> 24) & 0xff; \ |
| 5078 | p[iorder[2]] = ((CH) >> 16) & 0xff; \ |
| 5079 | p[iorder[1]] = ((CH) >> 8) & 0xff; \ |
| 5080 | p[iorder[0]] = (CH) & 0xff; \ |
| 5081 | p += 4; \ |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5082 | } while(0) |
| 5083 | |
| 5084 | /* In narrow builds we can output surrogate pairs as one codepoint, |
| 5085 | so we need less space. */ |
| 5086 | #ifndef Py_UNICODE_WIDE |
| 5087 | for (i = pairs = 0; i < size-1; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5088 | if (0xD800 <= s[i] && s[i] <= 0xDBFF && |
| 5089 | 0xDC00 <= s[i+1] && s[i+1] <= 0xDFFF) |
| 5090 | pairs++; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5091 | #endif |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5092 | nsize = (size - pairs + (byteorder == 0)); |
| 5093 | bytesize = nsize * 4; |
| 5094 | if (bytesize / 4 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5095 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5096 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5097 | if (v == NULL) |
| 5098 | return NULL; |
| 5099 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5100 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5101 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5102 | STORECHAR(0xFEFF); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5103 | if (size == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5104 | goto done; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5105 | |
| 5106 | if (byteorder == -1) { |
| 5107 | /* force LE */ |
| 5108 | iorder[0] = 0; |
| 5109 | iorder[1] = 1; |
| 5110 | iorder[2] = 2; |
| 5111 | iorder[3] = 3; |
| 5112 | } |
| 5113 | else if (byteorder == 1) { |
| 5114 | /* force BE */ |
| 5115 | iorder[0] = 3; |
| 5116 | iorder[1] = 2; |
| 5117 | iorder[2] = 1; |
| 5118 | iorder[3] = 0; |
| 5119 | } |
| 5120 | |
| 5121 | while (size-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5122 | Py_UCS4 ch = *s++; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5123 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5124 | if (0xD800 <= ch && ch <= 0xDBFF && size > 0) { |
| 5125 | Py_UCS4 ch2 = *s; |
| 5126 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
| 5127 | ch = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
| 5128 | s++; |
| 5129 | size--; |
| 5130 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5131 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5132 | #endif |
| 5133 | STORECHAR(ch); |
| 5134 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5135 | |
| 5136 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5137 | return v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5138 | #undef STORECHAR |
| 5139 | } |
| 5140 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5141 | PyObject * |
| 5142 | PyUnicode_AsUTF32String(PyObject *unicode) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5143 | { |
| 5144 | if (!PyUnicode_Check(unicode)) { |
| 5145 | PyErr_BadArgument(); |
| 5146 | return NULL; |
| 5147 | } |
| 5148 | return PyUnicode_EncodeUTF32(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5149 | PyUnicode_GET_SIZE(unicode), |
| 5150 | NULL, |
| 5151 | 0); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5152 | } |
| 5153 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5154 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 5155 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5156 | PyObject * |
| 5157 | PyUnicode_DecodeUTF16(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5158 | Py_ssize_t size, |
| 5159 | const char *errors, |
| 5160 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5161 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5162 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 5163 | } |
| 5164 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5165 | /* Two masks for fast checking of whether a C 'long' may contain |
| 5166 | UTF16-encoded surrogate characters. This is an efficient heuristic, |
| 5167 | assuming that non-surrogate characters with a code point >= 0x8000 are |
| 5168 | rare in most input. |
| 5169 | FAST_CHAR_MASK is used when the input is in native byte ordering, |
| 5170 | SWAPPED_FAST_CHAR_MASK when the input is in byteswapped ordering. |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5171 | */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5172 | #if (SIZEOF_LONG == 8) |
| 5173 | # define FAST_CHAR_MASK 0x8000800080008000L |
| 5174 | # define SWAPPED_FAST_CHAR_MASK 0x0080008000800080L |
| 5175 | #elif (SIZEOF_LONG == 4) |
| 5176 | # define FAST_CHAR_MASK 0x80008000L |
| 5177 | # define SWAPPED_FAST_CHAR_MASK 0x00800080L |
| 5178 | #else |
| 5179 | # error C 'long' size should be either 4 or 8! |
| 5180 | #endif |
| 5181 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5182 | PyObject * |
| 5183 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5184 | Py_ssize_t size, |
| 5185 | const char *errors, |
| 5186 | int *byteorder, |
| 5187 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5188 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5189 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5190 | Py_ssize_t startinpos; |
| 5191 | Py_ssize_t endinpos; |
| 5192 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5193 | PyUnicodeObject *unicode; |
| 5194 | Py_UNICODE *p; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5195 | const unsigned char *q, *e, *aligned_end; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5196 | int bo = 0; /* assume native ordering by default */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5197 | int native_ordering = 0; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 5198 | const char *errmsg = ""; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5199 | /* Offsets from q for retrieving byte pairs in the right order. */ |
| 5200 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5201 | int ihi = 1, ilo = 0; |
| 5202 | #else |
| 5203 | int ihi = 0, ilo = 1; |
| 5204 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5205 | PyObject *errorHandler = NULL; |
| 5206 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5207 | |
| 5208 | /* Note: size will always be longer than the resulting Unicode |
| 5209 | character count */ |
| 5210 | unicode = _PyUnicode_New(size); |
| 5211 | if (!unicode) |
| 5212 | return NULL; |
| 5213 | if (size == 0) |
| 5214 | return (PyObject *)unicode; |
| 5215 | |
| 5216 | /* Unpack UTF-16 encoded data */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5217 | p = PyUnicode_AS_UNICODE(unicode); |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5218 | q = (unsigned char *)s; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5219 | e = q + size - 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5220 | |
| 5221 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5222 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5223 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5224 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 5225 | byte order setting accordingly. In native mode, the leading BOM |
| 5226 | mark is skipped, in all other modes, it is copied to the output |
| 5227 | stream as-is (giving a ZWNBSP character). */ |
| 5228 | if (bo == 0) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5229 | if (size >= 2) { |
| 5230 | const Py_UNICODE bom = (q[ihi] << 8) | q[ilo]; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5231 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5232 | if (bom == 0xFEFF) { |
| 5233 | q += 2; |
| 5234 | bo = -1; |
| 5235 | } |
| 5236 | else if (bom == 0xFFFE) { |
| 5237 | q += 2; |
| 5238 | bo = 1; |
| 5239 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5240 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5241 | if (bom == 0xFEFF) { |
| 5242 | q += 2; |
| 5243 | bo = 1; |
| 5244 | } |
| 5245 | else if (bom == 0xFFFE) { |
| 5246 | q += 2; |
| 5247 | bo = -1; |
| 5248 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5249 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5250 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5251 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5252 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5253 | if (bo == -1) { |
| 5254 | /* force LE */ |
| 5255 | ihi = 1; |
| 5256 | ilo = 0; |
| 5257 | } |
| 5258 | else if (bo == 1) { |
| 5259 | /* force BE */ |
| 5260 | ihi = 0; |
| 5261 | ilo = 1; |
| 5262 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5263 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5264 | native_ordering = ilo < ihi; |
| 5265 | #else |
| 5266 | native_ordering = ilo > ihi; |
| 5267 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5268 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5269 | aligned_end = (const unsigned char *) ((size_t) e & ~LONG_PTR_MASK); |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5270 | while (q < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5271 | Py_UNICODE ch; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5272 | /* First check for possible aligned read of a C 'long'. Unaligned |
| 5273 | reads are more expensive, better to defer to another iteration. */ |
| 5274 | if (!((size_t) q & LONG_PTR_MASK)) { |
| 5275 | /* Fast path for runs of non-surrogate chars. */ |
| 5276 | register const unsigned char *_q = q; |
| 5277 | Py_UNICODE *_p = p; |
| 5278 | if (native_ordering) { |
| 5279 | /* Native ordering is simple: as long as the input cannot |
| 5280 | possibly contain a surrogate char, do an unrolled copy |
| 5281 | of several 16-bit code points to the target object. |
| 5282 | The non-surrogate check is done on several input bytes |
| 5283 | at a time (as many as a C 'long' can contain). */ |
| 5284 | while (_q < aligned_end) { |
| 5285 | unsigned long data = * (unsigned long *) _q; |
| 5286 | if (data & FAST_CHAR_MASK) |
| 5287 | break; |
| 5288 | _p[0] = ((unsigned short *) _q)[0]; |
| 5289 | _p[1] = ((unsigned short *) _q)[1]; |
| 5290 | #if (SIZEOF_LONG == 8) |
| 5291 | _p[2] = ((unsigned short *) _q)[2]; |
| 5292 | _p[3] = ((unsigned short *) _q)[3]; |
| 5293 | #endif |
| 5294 | _q += SIZEOF_LONG; |
| 5295 | _p += SIZEOF_LONG / 2; |
| 5296 | } |
| 5297 | } |
| 5298 | else { |
| 5299 | /* Byteswapped ordering is similar, but we must decompose |
| 5300 | the copy bytewise, and take care of zero'ing out the |
| 5301 | upper bytes if the target object is in 32-bit units |
| 5302 | (that is, in UCS-4 builds). */ |
| 5303 | while (_q < aligned_end) { |
| 5304 | unsigned long data = * (unsigned long *) _q; |
| 5305 | if (data & SWAPPED_FAST_CHAR_MASK) |
| 5306 | break; |
| 5307 | /* Zero upper bytes in UCS-4 builds */ |
| 5308 | #if (Py_UNICODE_SIZE > 2) |
| 5309 | _p[0] = 0; |
| 5310 | _p[1] = 0; |
| 5311 | #if (SIZEOF_LONG == 8) |
| 5312 | _p[2] = 0; |
| 5313 | _p[3] = 0; |
| 5314 | #endif |
| 5315 | #endif |
Antoine Pitrou | d6e8de1 | 2009-01-11 23:56:55 +0000 | [diff] [blame] | 5316 | /* Issue #4916; UCS-4 builds on big endian machines must |
| 5317 | fill the two last bytes of each 4-byte unit. */ |
| 5318 | #if (!defined(BYTEORDER_IS_LITTLE_ENDIAN) && Py_UNICODE_SIZE > 2) |
| 5319 | # define OFF 2 |
| 5320 | #else |
| 5321 | # define OFF 0 |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5322 | #endif |
Antoine Pitrou | d6e8de1 | 2009-01-11 23:56:55 +0000 | [diff] [blame] | 5323 | ((unsigned char *) _p)[OFF + 1] = _q[0]; |
| 5324 | ((unsigned char *) _p)[OFF + 0] = _q[1]; |
| 5325 | ((unsigned char *) _p)[OFF + 1 + Py_UNICODE_SIZE] = _q[2]; |
| 5326 | ((unsigned char *) _p)[OFF + 0 + Py_UNICODE_SIZE] = _q[3]; |
| 5327 | #if (SIZEOF_LONG == 8) |
| 5328 | ((unsigned char *) _p)[OFF + 1 + 2 * Py_UNICODE_SIZE] = _q[4]; |
| 5329 | ((unsigned char *) _p)[OFF + 0 + 2 * Py_UNICODE_SIZE] = _q[5]; |
| 5330 | ((unsigned char *) _p)[OFF + 1 + 3 * Py_UNICODE_SIZE] = _q[6]; |
| 5331 | ((unsigned char *) _p)[OFF + 0 + 3 * Py_UNICODE_SIZE] = _q[7]; |
| 5332 | #endif |
| 5333 | #undef OFF |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5334 | _q += SIZEOF_LONG; |
| 5335 | _p += SIZEOF_LONG / 2; |
| 5336 | } |
| 5337 | } |
| 5338 | p = _p; |
| 5339 | q = _q; |
| 5340 | if (q >= e) |
| 5341 | break; |
| 5342 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5343 | ch = (q[ihi] << 8) | q[ilo]; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5344 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5345 | q += 2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5346 | |
| 5347 | if (ch < 0xD800 || ch > 0xDFFF) { |
| 5348 | *p++ = ch; |
| 5349 | continue; |
| 5350 | } |
| 5351 | |
| 5352 | /* UTF-16 code pair: */ |
| 5353 | if (q > e) { |
| 5354 | errmsg = "unexpected end of data"; |
| 5355 | startinpos = (((const char *)q) - 2) - starts; |
| 5356 | endinpos = ((const char *)e) + 1 - starts; |
| 5357 | goto utf16Error; |
| 5358 | } |
| 5359 | if (0xD800 <= ch && ch <= 0xDBFF) { |
| 5360 | Py_UNICODE ch2 = (q[ihi] << 8) | q[ilo]; |
| 5361 | q += 2; |
| 5362 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 5363 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5364 | *p++ = ch; |
| 5365 | *p++ = ch2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5366 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5367 | *p++ = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5368 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5369 | continue; |
| 5370 | } |
| 5371 | else { |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5372 | errmsg = "illegal UTF-16 surrogate"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5373 | startinpos = (((const char *)q)-4)-starts; |
| 5374 | endinpos = startinpos+2; |
| 5375 | goto utf16Error; |
| 5376 | } |
| 5377 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5378 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5379 | errmsg = "illegal encoding"; |
| 5380 | startinpos = (((const char *)q)-2)-starts; |
| 5381 | endinpos = startinpos+2; |
| 5382 | /* Fall through to report the error */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5383 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5384 | utf16Error: |
| 5385 | outpos = p - PyUnicode_AS_UNICODE(unicode); |
| 5386 | if (unicode_decode_call_errorhandler( |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5387 | errors, |
| 5388 | &errorHandler, |
| 5389 | "utf16", errmsg, |
| 5390 | &starts, |
| 5391 | (const char **)&e, |
| 5392 | &startinpos, |
| 5393 | &endinpos, |
| 5394 | &exc, |
| 5395 | (const char **)&q, |
| 5396 | &unicode, |
| 5397 | &outpos, |
| 5398 | &p)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5399 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5400 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5401 | /* remaining byte at the end? (size should be even) */ |
| 5402 | if (e == q) { |
| 5403 | if (!consumed) { |
| 5404 | errmsg = "truncated data"; |
| 5405 | startinpos = ((const char *)q) - starts; |
| 5406 | endinpos = ((const char *)e) + 1 - starts; |
| 5407 | outpos = p - PyUnicode_AS_UNICODE(unicode); |
| 5408 | if (unicode_decode_call_errorhandler( |
| 5409 | errors, |
| 5410 | &errorHandler, |
| 5411 | "utf16", errmsg, |
| 5412 | &starts, |
| 5413 | (const char **)&e, |
| 5414 | &startinpos, |
| 5415 | &endinpos, |
| 5416 | &exc, |
| 5417 | (const char **)&q, |
| 5418 | &unicode, |
| 5419 | &outpos, |
| 5420 | &p)) |
| 5421 | goto onError; |
| 5422 | /* The remaining input chars are ignored if the callback |
| 5423 | chooses to skip the input */ |
| 5424 | } |
| 5425 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5426 | |
| 5427 | if (byteorder) |
| 5428 | *byteorder = bo; |
| 5429 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5430 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5431 | *consumed = (const char *)q-starts; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5432 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5433 | /* Adjust length */ |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 5434 | if (PyUnicode_Resize((PyObject**)&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5435 | goto onError; |
| 5436 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5437 | Py_XDECREF(errorHandler); |
| 5438 | Py_XDECREF(exc); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 5439 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 5440 | if (_PyUnicode_READY_REPLACE(&unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5441 | Py_DECREF(unicode); |
| 5442 | return NULL; |
| 5443 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 5444 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 5445 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5446 | return (PyObject *)unicode; |
| 5447 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5448 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5449 | Py_DECREF(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5450 | Py_XDECREF(errorHandler); |
| 5451 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5452 | return NULL; |
| 5453 | } |
| 5454 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5455 | #undef FAST_CHAR_MASK |
| 5456 | #undef SWAPPED_FAST_CHAR_MASK |
| 5457 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5458 | PyObject * |
| 5459 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5460 | Py_ssize_t size, |
| 5461 | const char *errors, |
| 5462 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5463 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5464 | PyObject *v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5465 | unsigned char *p; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5466 | Py_ssize_t nsize, bytesize; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 5467 | #ifdef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5468 | Py_ssize_t i, pairs; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 5469 | #else |
| 5470 | const int pairs = 0; |
| 5471 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5472 | /* Offsets from p for storing byte pairs in the right order. */ |
| 5473 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5474 | int ihi = 1, ilo = 0; |
| 5475 | #else |
| 5476 | int ihi = 0, ilo = 1; |
| 5477 | #endif |
| 5478 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5479 | #define STORECHAR(CH) \ |
| 5480 | do { \ |
| 5481 | p[ihi] = ((CH) >> 8) & 0xff; \ |
| 5482 | p[ilo] = (CH) & 0xff; \ |
| 5483 | p += 2; \ |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5484 | } while(0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5485 | |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 5486 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5487 | for (i = pairs = 0; i < size; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5488 | if (s[i] >= 0x10000) |
| 5489 | pairs++; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 5490 | #endif |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5491 | /* 2 * (size + pairs + (byteorder == 0)) */ |
| 5492 | if (size > PY_SSIZE_T_MAX || |
| 5493 | size > PY_SSIZE_T_MAX - pairs - (byteorder == 0)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5494 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5495 | nsize = size + pairs + (byteorder == 0); |
| 5496 | bytesize = nsize * 2; |
| 5497 | if (bytesize / 2 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5498 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5499 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5500 | if (v == NULL) |
| 5501 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5502 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5503 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5504 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5505 | STORECHAR(0xFEFF); |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 5506 | if (size == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5507 | goto done; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5508 | |
| 5509 | if (byteorder == -1) { |
| 5510 | /* force LE */ |
| 5511 | ihi = 1; |
| 5512 | ilo = 0; |
| 5513 | } |
| 5514 | else if (byteorder == 1) { |
| 5515 | /* force BE */ |
| 5516 | ihi = 0; |
| 5517 | ilo = 1; |
| 5518 | } |
| 5519 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5520 | while (size-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5521 | Py_UNICODE ch = *s++; |
| 5522 | Py_UNICODE ch2 = 0; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 5523 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5524 | if (ch >= 0x10000) { |
| 5525 | ch2 = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 5526 | ch = 0xD800 | ((ch-0x10000) >> 10); |
| 5527 | } |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 5528 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5529 | STORECHAR(ch); |
| 5530 | if (ch2) |
| 5531 | STORECHAR(ch2); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5532 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5533 | |
| 5534 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5535 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5536 | #undef STORECHAR |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5537 | } |
| 5538 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5539 | PyObject * |
| 5540 | PyUnicode_AsUTF16String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5541 | { |
| 5542 | if (!PyUnicode_Check(unicode)) { |
| 5543 | PyErr_BadArgument(); |
| 5544 | return NULL; |
| 5545 | } |
| 5546 | return PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5547 | PyUnicode_GET_SIZE(unicode), |
| 5548 | NULL, |
| 5549 | 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5550 | } |
| 5551 | |
| 5552 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 5553 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5554 | /* Helper function for PyUnicode_DecodeUnicodeEscape, determines |
| 5555 | if all the escapes in the string make it still a valid ASCII string. |
| 5556 | Returns -1 if any escapes were found which cause the string to |
| 5557 | pop out of ASCII range. Otherwise returns the length of the |
| 5558 | required buffer to hold the string. |
| 5559 | */ |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 5560 | static Py_ssize_t |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5561 | length_of_escaped_ascii_string(const char *s, Py_ssize_t size) |
| 5562 | { |
| 5563 | const unsigned char *p = (const unsigned char *)s; |
| 5564 | const unsigned char *end = p + size; |
| 5565 | Py_ssize_t length = 0; |
| 5566 | |
| 5567 | if (size < 0) |
| 5568 | return -1; |
| 5569 | |
| 5570 | for (; p < end; ++p) { |
| 5571 | if (*p > 127) { |
| 5572 | /* Non-ASCII */ |
| 5573 | return -1; |
| 5574 | } |
| 5575 | else if (*p != '\\') { |
| 5576 | /* Normal character */ |
| 5577 | ++length; |
| 5578 | } |
| 5579 | else { |
| 5580 | /* Backslash-escape, check next char */ |
| 5581 | ++p; |
| 5582 | /* Escape sequence reaches till end of string or |
| 5583 | non-ASCII follow-up. */ |
| 5584 | if (p >= end || *p > 127) |
| 5585 | return -1; |
| 5586 | switch (*p) { |
| 5587 | case '\n': |
| 5588 | /* backslash + \n result in zero characters */ |
| 5589 | break; |
| 5590 | case '\\': case '\'': case '\"': |
| 5591 | case 'b': case 'f': case 't': |
| 5592 | case 'n': case 'r': case 'v': case 'a': |
| 5593 | ++length; |
| 5594 | break; |
| 5595 | case '0': case '1': case '2': case '3': |
| 5596 | case '4': case '5': case '6': case '7': |
| 5597 | case 'x': case 'u': case 'U': case 'N': |
| 5598 | /* these do not guarantee ASCII characters */ |
| 5599 | return -1; |
| 5600 | default: |
| 5601 | /* count the backslash + the other character */ |
| 5602 | length += 2; |
| 5603 | } |
| 5604 | } |
| 5605 | } |
| 5606 | return length; |
| 5607 | } |
| 5608 | |
| 5609 | /* Similar to PyUnicode_WRITE but either write into wstr field |
| 5610 | or treat string as ASCII. */ |
| 5611 | #define WRITE_ASCII_OR_WSTR(kind, buf, index, value) \ |
| 5612 | do { \ |
| 5613 | if ((kind) != PyUnicode_WCHAR_KIND) \ |
| 5614 | ((unsigned char *)(buf))[(index)] = (unsigned char)(value); \ |
| 5615 | else \ |
| 5616 | ((Py_UNICODE *)(buf))[(index)] = (Py_UNICODE)(value); \ |
| 5617 | } while (0) |
| 5618 | |
| 5619 | #define WRITE_WSTR(buf, index, value) \ |
| 5620 | assert(kind == PyUnicode_WCHAR_KIND), \ |
| 5621 | ((Py_UNICODE *)(buf))[(index)] = (Py_UNICODE)(value) |
| 5622 | |
| 5623 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5624 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 5625 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5626 | PyObject * |
| 5627 | PyUnicode_DecodeUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5628 | Py_ssize_t size, |
Victor Stinner | c17f540 | 2011-09-29 00:16:58 +0200 | [diff] [blame] | 5629 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5630 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5631 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5632 | Py_ssize_t startinpos; |
| 5633 | Py_ssize_t endinpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5634 | int j; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5635 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5636 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5637 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5638 | char* message; |
| 5639 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5640 | PyObject *errorHandler = NULL; |
| 5641 | PyObject *exc = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5642 | Py_ssize_t ascii_length; |
| 5643 | Py_ssize_t i; |
| 5644 | int kind; |
| 5645 | void *data; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5646 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5647 | ascii_length = length_of_escaped_ascii_string(s, size); |
| 5648 | |
| 5649 | /* After length_of_escaped_ascii_string() there are two alternatives, |
| 5650 | either the string is pure ASCII with named escapes like \n, etc. |
| 5651 | and we determined it's exact size (common case) |
| 5652 | or it contains \x, \u, ... escape sequences. then we create a |
| 5653 | legacy wchar string and resize it at the end of this function. */ |
| 5654 | if (ascii_length >= 0) { |
| 5655 | v = (PyUnicodeObject *)PyUnicode_New(ascii_length, 127); |
| 5656 | if (!v) |
| 5657 | goto onError; |
| 5658 | assert(PyUnicode_KIND(v) == PyUnicode_1BYTE_KIND); |
| 5659 | kind = PyUnicode_1BYTE_KIND; |
| 5660 | data = PyUnicode_DATA(v); |
| 5661 | } |
| 5662 | else { |
| 5663 | /* Escaped strings will always be longer than the resulting |
| 5664 | Unicode string, so we start with size here and then reduce the |
| 5665 | length after conversion to the true value. |
| 5666 | (but if the error callback returns a long replacement string |
| 5667 | we'll have to allocate more space) */ |
| 5668 | v = _PyUnicode_New(size); |
| 5669 | if (!v) |
| 5670 | goto onError; |
| 5671 | kind = PyUnicode_WCHAR_KIND; |
| 5672 | data = PyUnicode_AS_UNICODE(v); |
| 5673 | } |
| 5674 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5675 | if (size == 0) |
| 5676 | return (PyObject *)v; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5677 | i = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5678 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5679 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5680 | while (s < end) { |
| 5681 | unsigned char c; |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 5682 | Py_UNICODE x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5683 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5684 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5685 | if (kind == PyUnicode_WCHAR_KIND) { |
| 5686 | assert(i < _PyUnicode_WSTR_LENGTH(v)); |
| 5687 | } |
| 5688 | else { |
| 5689 | /* The only case in which i == ascii_length is a backslash |
| 5690 | followed by a newline. */ |
| 5691 | assert(i <= ascii_length); |
| 5692 | } |
| 5693 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5694 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 5695 | if (*s != '\\') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5696 | WRITE_ASCII_OR_WSTR(kind, data, i++, (unsigned char) *s++); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5697 | continue; |
| 5698 | } |
| 5699 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5700 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5701 | /* \ - Escapes */ |
| 5702 | s++; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5703 | c = *s++; |
| 5704 | if (s > end) |
| 5705 | c = '\0'; /* Invalid after \ */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5706 | |
| 5707 | if (kind == PyUnicode_WCHAR_KIND) { |
| 5708 | assert(i < _PyUnicode_WSTR_LENGTH(v)); |
| 5709 | } |
| 5710 | else { |
| 5711 | /* The only case in which i == ascii_length is a backslash |
| 5712 | followed by a newline. */ |
| 5713 | assert(i < ascii_length || (i == ascii_length && c == '\n')); |
| 5714 | } |
| 5715 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5716 | switch (c) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5717 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5718 | /* \x escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5719 | case '\n': break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5720 | case '\\': WRITE_ASCII_OR_WSTR(kind, data, i++, '\\'); break; |
| 5721 | case '\'': WRITE_ASCII_OR_WSTR(kind, data, i++, '\''); break; |
| 5722 | case '\"': WRITE_ASCII_OR_WSTR(kind, data, i++, '\"'); break; |
| 5723 | case 'b': WRITE_ASCII_OR_WSTR(kind, data, i++, '\b'); break; |
| 5724 | /* FF */ |
| 5725 | case 'f': WRITE_ASCII_OR_WSTR(kind, data, i++, '\014'); break; |
| 5726 | case 't': WRITE_ASCII_OR_WSTR(kind, data, i++, '\t'); break; |
| 5727 | case 'n': WRITE_ASCII_OR_WSTR(kind, data, i++, '\n'); break; |
| 5728 | case 'r': WRITE_ASCII_OR_WSTR(kind, data, i++, '\r'); break; |
| 5729 | /* VT */ |
| 5730 | case 'v': WRITE_ASCII_OR_WSTR(kind, data, i++, '\013'); break; |
| 5731 | /* BEL, not classic C */ |
| 5732 | case 'a': WRITE_ASCII_OR_WSTR(kind, data, i++, '\007'); break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5733 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5734 | /* \OOO (octal) escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5735 | case '0': case '1': case '2': case '3': |
| 5736 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5737 | x = s[-1] - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5738 | if (s < end && '0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5739 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5740 | if (s < end && '0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5741 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5742 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5743 | WRITE_WSTR(data, i++, x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5744 | break; |
| 5745 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5746 | /* hex escapes */ |
| 5747 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5748 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5749 | digits = 2; |
| 5750 | message = "truncated \\xXX escape"; |
| 5751 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5752 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5753 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5754 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5755 | digits = 4; |
| 5756 | message = "truncated \\uXXXX escape"; |
| 5757 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5758 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5759 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5760 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5761 | digits = 8; |
| 5762 | message = "truncated \\UXXXXXXXX escape"; |
| 5763 | hexescape: |
| 5764 | chr = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5765 | p = PyUnicode_AS_UNICODE(v) + i; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5766 | if (s+digits>end) { |
| 5767 | endinpos = size; |
| 5768 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5769 | errors, &errorHandler, |
| 5770 | "unicodeescape", "end of string in escape sequence", |
| 5771 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5772 | &v, &i, &p)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5773 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5774 | data = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5775 | goto nextByte; |
| 5776 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5777 | for (j = 0; j < digits; ++j) { |
| 5778 | c = (unsigned char) s[j]; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 5779 | if (!Py_ISXDIGIT(c)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5780 | endinpos = (s+j+1)-starts; |
| 5781 | p = PyUnicode_AS_UNICODE(v) + i; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5782 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5783 | errors, &errorHandler, |
| 5784 | "unicodeescape", message, |
| 5785 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5786 | &v, &i, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5787 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5788 | data = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5789 | goto nextByte; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5790 | } |
| 5791 | chr = (chr<<4) & ~0xF; |
| 5792 | if (c >= '0' && c <= '9') |
| 5793 | chr += c - '0'; |
| 5794 | else if (c >= 'a' && c <= 'f') |
| 5795 | chr += 10 + c - 'a'; |
| 5796 | else |
| 5797 | chr += 10 + c - 'A'; |
| 5798 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5799 | s += j; |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 5800 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5801 | /* _decoding_error will have already written into the |
| 5802 | target buffer. */ |
| 5803 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5804 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5805 | /* when we get here, chr is a 32-bit unicode character */ |
| 5806 | if (chr <= 0xffff) |
| 5807 | /* UCS-2 character */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5808 | WRITE_WSTR(data, i++, chr); |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5809 | else if (chr <= 0x10ffff) { |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5810 | /* UCS-4 character. Either store directly, or as |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5811 | surrogate pair. */ |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 5812 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5813 | WRITE_WSTR(data, i++, chr); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5814 | #else |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5815 | chr -= 0x10000L; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5816 | WRITE_WSTR(data, i++, 0xD800 + (Py_UNICODE) (chr >> 10)); |
| 5817 | WRITE_WSTR(data, i++, 0xDC00 + (Py_UNICODE) (chr & 0x03FF)); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5818 | #endif |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5819 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5820 | endinpos = s-starts; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5821 | p = PyUnicode_AS_UNICODE(v) + i; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5822 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5823 | errors, &errorHandler, |
| 5824 | "unicodeescape", "illegal Unicode character", |
| 5825 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5826 | &v, &i, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5827 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5828 | data = PyUnicode_AS_UNICODE(v); |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5829 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5830 | break; |
| 5831 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5832 | /* \N{name} */ |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5833 | case 'N': |
| 5834 | message = "malformed \\N character escape"; |
| 5835 | if (ucnhash_CAPI == NULL) { |
| 5836 | /* load the unicode data module */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5837 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCapsule_Import( |
| 5838 | PyUnicodeData_CAPSULE_NAME, 1); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5839 | if (ucnhash_CAPI == NULL) |
| 5840 | goto ucnhashError; |
| 5841 | } |
| 5842 | if (*s == '{') { |
| 5843 | const char *start = s+1; |
| 5844 | /* look for the closing brace */ |
| 5845 | while (*s != '}' && s < end) |
| 5846 | s++; |
| 5847 | if (s > start && s < end && *s == '}') { |
| 5848 | /* found a name. look it up in the unicode database */ |
| 5849 | message = "unknown Unicode character name"; |
| 5850 | s++; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5851 | if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), |
| 5852 | &chr)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5853 | goto store; |
| 5854 | } |
| 5855 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5856 | endinpos = s-starts; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5857 | p = PyUnicode_AS_UNICODE(v) + i; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5858 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5859 | errors, &errorHandler, |
| 5860 | "unicodeescape", message, |
| 5861 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5862 | &v, &i, &p)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5863 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5864 | data = PyUnicode_AS_UNICODE(v); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5865 | break; |
| 5866 | |
| 5867 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5868 | if (s > end) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5869 | assert(kind == PyUnicode_WCHAR_KIND); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5870 | message = "\\ at end of string"; |
| 5871 | s--; |
| 5872 | endinpos = s-starts; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5873 | p = PyUnicode_AS_UNICODE(v) + i; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5874 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5875 | errors, &errorHandler, |
| 5876 | "unicodeescape", message, |
| 5877 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5878 | &v, &i, &p)) |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5879 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5880 | data = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5881 | } |
| 5882 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5883 | WRITE_ASCII_OR_WSTR(kind, data, i++, '\\'); |
| 5884 | WRITE_ASCII_OR_WSTR(kind, data, i++, (unsigned char)s[-1]); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5885 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5886 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5887 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5888 | nextByte: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5889 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5890 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5891 | /* Ensure the length prediction worked in case of ASCII strings */ |
| 5892 | assert(kind == PyUnicode_WCHAR_KIND || i == ascii_length); |
| 5893 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 5894 | if (kind == PyUnicode_WCHAR_KIND) |
| 5895 | { |
| 5896 | if (PyUnicode_Resize((PyObject**)&v, i) < 0) |
| 5897 | goto onError; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 5898 | } |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 5899 | Py_XDECREF(errorHandler); |
| 5900 | Py_XDECREF(exc); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 5901 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 5902 | if (_PyUnicode_READY_REPLACE(&v)) { |
| 5903 | Py_DECREF(v); |
| 5904 | return NULL; |
| 5905 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 5906 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 5907 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5908 | return (PyObject *)v; |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5909 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5910 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5911 | PyErr_SetString( |
| 5912 | PyExc_UnicodeError, |
| 5913 | "\\N escapes not supported (can't load unicodedata module)" |
| 5914 | ); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 5915 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5916 | Py_XDECREF(errorHandler); |
| 5917 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 5918 | return NULL; |
| 5919 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5920 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5921 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5922 | Py_XDECREF(errorHandler); |
| 5923 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5924 | return NULL; |
| 5925 | } |
| 5926 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5927 | #undef WRITE_ASCII_OR_WSTR |
| 5928 | #undef WRITE_WSTR |
| 5929 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5930 | /* Return a Unicode-Escape string version of the Unicode object. |
| 5931 | |
| 5932 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 5933 | appropriate. |
| 5934 | |
| 5935 | */ |
| 5936 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5937 | static const char *hexdigits = "0123456789abcdef"; |
| 5938 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5939 | PyObject * |
| 5940 | PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5941 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5942 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5943 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5944 | char *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5945 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5946 | #ifdef Py_UNICODE_WIDE |
| 5947 | const Py_ssize_t expandsize = 10; |
| 5948 | #else |
| 5949 | const Py_ssize_t expandsize = 6; |
| 5950 | #endif |
| 5951 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5952 | /* XXX(nnorwitz): rather than over-allocating, it would be |
| 5953 | better to choose a different scheme. Perhaps scan the |
| 5954 | first N-chars of the string and allocate based on that size. |
| 5955 | */ |
| 5956 | /* Initial allocation is based on the longest-possible unichr |
| 5957 | escape. |
| 5958 | |
| 5959 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 5960 | unichr, so in this case it's the longest unichr escape. In |
| 5961 | narrow (UTF-16) builds this is five chars per source unichr |
| 5962 | since there are two unichrs in the surrogate pair, so in narrow |
| 5963 | (UTF-16) builds it's not the longest unichr escape. |
| 5964 | |
| 5965 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 5966 | so in the narrow (UTF-16) build case it's the longest unichr |
| 5967 | escape. |
| 5968 | */ |
| 5969 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5970 | if (size == 0) |
| 5971 | return PyBytes_FromStringAndSize(NULL, 0); |
| 5972 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5973 | if (size > (PY_SSIZE_T_MAX - 2 - 1) / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5974 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5975 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5976 | repr = PyBytes_FromStringAndSize(NULL, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5977 | 2 |
| 5978 | + expandsize*size |
| 5979 | + 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5980 | if (repr == NULL) |
| 5981 | return NULL; |
| 5982 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5983 | p = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5984 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5985 | while (size-- > 0) { |
| 5986 | Py_UNICODE ch = *s++; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5987 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5988 | /* Escape backslashes */ |
| 5989 | if (ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5990 | *p++ = '\\'; |
| 5991 | *p++ = (char) ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5992 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5993 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5994 | |
Guido van Rossum | 0d42e0c | 2001-07-20 16:36:21 +0000 | [diff] [blame] | 5995 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5996 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 5997 | else if (ch >= 0x10000) { |
| 5998 | *p++ = '\\'; |
| 5999 | *p++ = 'U'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 6000 | *p++ = hexdigits[(ch >> 28) & 0x0000000F]; |
| 6001 | *p++ = hexdigits[(ch >> 24) & 0x0000000F]; |
| 6002 | *p++ = hexdigits[(ch >> 20) & 0x0000000F]; |
| 6003 | *p++ = hexdigits[(ch >> 16) & 0x0000000F]; |
| 6004 | *p++ = hexdigits[(ch >> 12) & 0x0000000F]; |
| 6005 | *p++ = hexdigits[(ch >> 8) & 0x0000000F]; |
| 6006 | *p++ = hexdigits[(ch >> 4) & 0x0000000F]; |
| 6007 | *p++ = hexdigits[ch & 0x0000000F]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6008 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 6009 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6010 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6011 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 6012 | else if (ch >= 0xD800 && ch < 0xDC00) { |
| 6013 | Py_UNICODE ch2; |
| 6014 | Py_UCS4 ucs; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6015 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6016 | ch2 = *s++; |
| 6017 | size--; |
Georg Brandl | 78eef3de | 2010-08-01 20:51:02 +0000 | [diff] [blame] | 6018 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6019 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 6020 | *p++ = '\\'; |
| 6021 | *p++ = 'U'; |
| 6022 | *p++ = hexdigits[(ucs >> 28) & 0x0000000F]; |
| 6023 | *p++ = hexdigits[(ucs >> 24) & 0x0000000F]; |
| 6024 | *p++ = hexdigits[(ucs >> 20) & 0x0000000F]; |
| 6025 | *p++ = hexdigits[(ucs >> 16) & 0x0000000F]; |
| 6026 | *p++ = hexdigits[(ucs >> 12) & 0x0000000F]; |
| 6027 | *p++ = hexdigits[(ucs >> 8) & 0x0000000F]; |
| 6028 | *p++ = hexdigits[(ucs >> 4) & 0x0000000F]; |
| 6029 | *p++ = hexdigits[ucs & 0x0000000F]; |
| 6030 | continue; |
| 6031 | } |
| 6032 | /* Fall through: isolated surrogates are copied as-is */ |
| 6033 | s--; |
| 6034 | size++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6035 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6036 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 6037 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6038 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 6039 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6040 | *p++ = '\\'; |
| 6041 | *p++ = 'u'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 6042 | *p++ = hexdigits[(ch >> 12) & 0x000F]; |
| 6043 | *p++ = hexdigits[(ch >> 8) & 0x000F]; |
| 6044 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 6045 | *p++ = hexdigits[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6046 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 6047 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 6048 | /* Map special whitespace to '\t', \n', '\r' */ |
| 6049 | else if (ch == '\t') { |
| 6050 | *p++ = '\\'; |
| 6051 | *p++ = 't'; |
| 6052 | } |
| 6053 | else if (ch == '\n') { |
| 6054 | *p++ = '\\'; |
| 6055 | *p++ = 'n'; |
| 6056 | } |
| 6057 | else if (ch == '\r') { |
| 6058 | *p++ = '\\'; |
| 6059 | *p++ = 'r'; |
| 6060 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 6061 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 6062 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 6063 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6064 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 6065 | *p++ = 'x'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 6066 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 6067 | *p++ = hexdigits[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6068 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 6069 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6070 | /* Copy everything else as-is */ |
| 6071 | else |
| 6072 | *p++ = (char) ch; |
| 6073 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6074 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6075 | assert(p - PyBytes_AS_STRING(repr) > 0); |
| 6076 | if (_PyBytes_Resize(&repr, p - PyBytes_AS_STRING(repr)) < 0) |
| 6077 | return NULL; |
| 6078 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6079 | } |
| 6080 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6081 | PyObject * |
| 6082 | PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6083 | { |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 6084 | PyObject *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6085 | if (!PyUnicode_Check(unicode)) { |
| 6086 | PyErr_BadArgument(); |
| 6087 | return NULL; |
| 6088 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 6089 | s = PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 6090 | PyUnicode_GET_SIZE(unicode)); |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 6091 | return s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6092 | } |
| 6093 | |
| 6094 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 6095 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6096 | PyObject * |
| 6097 | PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6098 | Py_ssize_t size, |
| 6099 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6100 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6101 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6102 | Py_ssize_t startinpos; |
| 6103 | Py_ssize_t endinpos; |
| 6104 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6105 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6106 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6107 | const char *end; |
| 6108 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6109 | PyObject *errorHandler = NULL; |
| 6110 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6111 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6112 | /* Escaped strings will always be longer than the resulting |
| 6113 | 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] | 6114 | length after conversion to the true value. (But decoding error |
| 6115 | handler might have to resize the string) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6116 | v = _PyUnicode_New(size); |
| 6117 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6118 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6119 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6120 | return (PyObject *)v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6121 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6122 | end = s + size; |
| 6123 | while (s < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6124 | unsigned char c; |
| 6125 | Py_UCS4 x; |
| 6126 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6127 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6128 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6129 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 6130 | if (*s != '\\') { |
| 6131 | *p++ = (unsigned char)*s++; |
| 6132 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6133 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6134 | startinpos = s-starts; |
| 6135 | |
| 6136 | /* \u-escapes are only interpreted iff the number of leading |
| 6137 | backslashes if odd */ |
| 6138 | bs = s; |
| 6139 | for (;s < end;) { |
| 6140 | if (*s != '\\') |
| 6141 | break; |
| 6142 | *p++ = (unsigned char)*s++; |
| 6143 | } |
| 6144 | if (((s - bs) & 1) == 0 || |
| 6145 | s >= end || |
| 6146 | (*s != 'u' && *s != 'U')) { |
| 6147 | continue; |
| 6148 | } |
| 6149 | p--; |
| 6150 | count = *s=='u' ? 4 : 8; |
| 6151 | s++; |
| 6152 | |
| 6153 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
| 6154 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 6155 | for (x = 0, i = 0; i < count; ++i, ++s) { |
| 6156 | c = (unsigned char)*s; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 6157 | if (!Py_ISXDIGIT(c)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6158 | endinpos = s-starts; |
| 6159 | if (unicode_decode_call_errorhandler( |
| 6160 | errors, &errorHandler, |
| 6161 | "rawunicodeescape", "truncated \\uXXXX", |
| 6162 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 6163 | &v, &outpos, &p)) |
| 6164 | goto onError; |
| 6165 | goto nextByte; |
| 6166 | } |
| 6167 | x = (x<<4) & ~0xF; |
| 6168 | if (c >= '0' && c <= '9') |
| 6169 | x += c - '0'; |
| 6170 | else if (c >= 'a' && c <= 'f') |
| 6171 | x += 10 + c - 'a'; |
| 6172 | else |
| 6173 | x += 10 + c - 'A'; |
| 6174 | } |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 6175 | if (x <= 0xffff) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6176 | /* UCS-2 character */ |
| 6177 | *p++ = (Py_UNICODE) x; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 6178 | else if (x <= 0x10ffff) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6179 | /* UCS-4 character. Either store directly, or as |
| 6180 | surrogate pair. */ |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 6181 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6182 | *p++ = (Py_UNICODE) x; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 6183 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6184 | x -= 0x10000L; |
| 6185 | *p++ = 0xD800 + (Py_UNICODE) (x >> 10); |
| 6186 | *p++ = 0xDC00 + (Py_UNICODE) (x & 0x03FF); |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 6187 | #endif |
| 6188 | } else { |
| 6189 | endinpos = s-starts; |
| 6190 | outpos = p-PyUnicode_AS_UNICODE(v); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6191 | if (unicode_decode_call_errorhandler( |
| 6192 | errors, &errorHandler, |
| 6193 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6194 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 6195 | &v, &outpos, &p)) |
| 6196 | goto onError; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6197 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6198 | nextByte: |
| 6199 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6200 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 6201 | if (PyUnicode_Resize((PyObject**)&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6202 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6203 | Py_XDECREF(errorHandler); |
| 6204 | Py_XDECREF(exc); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 6205 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 6206 | if (_PyUnicode_READY_REPLACE(&v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6207 | Py_DECREF(v); |
| 6208 | return NULL; |
| 6209 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 6210 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 6211 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6212 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6213 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6214 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6215 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6216 | Py_XDECREF(errorHandler); |
| 6217 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6218 | return NULL; |
| 6219 | } |
| 6220 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6221 | PyObject * |
| 6222 | PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6223 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6224 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6225 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6226 | char *p; |
| 6227 | char *q; |
| 6228 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6229 | #ifdef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 6230 | const Py_ssize_t expandsize = 10; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6231 | #else |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 6232 | const Py_ssize_t expandsize = 6; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6233 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6234 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 6235 | if (size > PY_SSIZE_T_MAX / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6236 | return PyErr_NoMemory(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6237 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6238 | repr = PyBytes_FromStringAndSize(NULL, expandsize * size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6239 | if (repr == NULL) |
| 6240 | return NULL; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 6241 | if (size == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6242 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6243 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6244 | p = q = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6245 | while (size-- > 0) { |
| 6246 | Py_UNICODE ch = *s++; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6247 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6248 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 6249 | if (ch >= 0x10000) { |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6250 | *p++ = '\\'; |
| 6251 | *p++ = 'U'; |
Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 6252 | *p++ = hexdigits[(ch >> 28) & 0xf]; |
| 6253 | *p++ = hexdigits[(ch >> 24) & 0xf]; |
| 6254 | *p++ = hexdigits[(ch >> 20) & 0xf]; |
| 6255 | *p++ = hexdigits[(ch >> 16) & 0xf]; |
| 6256 | *p++ = hexdigits[(ch >> 12) & 0xf]; |
| 6257 | *p++ = hexdigits[(ch >> 8) & 0xf]; |
| 6258 | *p++ = hexdigits[(ch >> 4) & 0xf]; |
| 6259 | *p++ = hexdigits[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6260 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6261 | else |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 6262 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6263 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 6264 | if (ch >= 0xD800 && ch < 0xDC00) { |
| 6265 | Py_UNICODE ch2; |
| 6266 | Py_UCS4 ucs; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 6267 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6268 | ch2 = *s++; |
| 6269 | size--; |
Georg Brandl | 78eef3de | 2010-08-01 20:51:02 +0000 | [diff] [blame] | 6270 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6271 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 6272 | *p++ = '\\'; |
| 6273 | *p++ = 'U'; |
| 6274 | *p++ = hexdigits[(ucs >> 28) & 0xf]; |
| 6275 | *p++ = hexdigits[(ucs >> 24) & 0xf]; |
| 6276 | *p++ = hexdigits[(ucs >> 20) & 0xf]; |
| 6277 | *p++ = hexdigits[(ucs >> 16) & 0xf]; |
| 6278 | *p++ = hexdigits[(ucs >> 12) & 0xf]; |
| 6279 | *p++ = hexdigits[(ucs >> 8) & 0xf]; |
| 6280 | *p++ = hexdigits[(ucs >> 4) & 0xf]; |
| 6281 | *p++ = hexdigits[ucs & 0xf]; |
| 6282 | continue; |
| 6283 | } |
| 6284 | /* Fall through: isolated surrogates are copied as-is */ |
| 6285 | s--; |
| 6286 | size++; |
| 6287 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6288 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6289 | /* Map 16-bit characters to '\uxxxx' */ |
| 6290 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6291 | *p++ = '\\'; |
| 6292 | *p++ = 'u'; |
Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 6293 | *p++ = hexdigits[(ch >> 12) & 0xf]; |
| 6294 | *p++ = hexdigits[(ch >> 8) & 0xf]; |
| 6295 | *p++ = hexdigits[(ch >> 4) & 0xf]; |
| 6296 | *p++ = hexdigits[ch & 15]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6297 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6298 | /* Copy everything else as-is */ |
| 6299 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6300 | *p++ = (char) ch; |
| 6301 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6302 | size = p - q; |
| 6303 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6304 | assert(size > 0); |
| 6305 | if (_PyBytes_Resize(&repr, size) < 0) |
| 6306 | return NULL; |
| 6307 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6308 | } |
| 6309 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6310 | PyObject * |
| 6311 | PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6312 | { |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 6313 | PyObject *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6314 | if (!PyUnicode_Check(unicode)) { |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 6315 | PyErr_BadArgument(); |
| 6316 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6317 | } |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 6318 | s = PyUnicode_EncodeRawUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 6319 | PyUnicode_GET_SIZE(unicode)); |
| 6320 | |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 6321 | return s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6322 | } |
| 6323 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6324 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 6325 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6326 | PyObject * |
| 6327 | _PyUnicode_DecodeUnicodeInternal(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6328 | Py_ssize_t size, |
| 6329 | const char *errors) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6330 | { |
| 6331 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6332 | Py_ssize_t startinpos; |
| 6333 | Py_ssize_t endinpos; |
| 6334 | Py_ssize_t outpos; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6335 | PyUnicodeObject *v; |
| 6336 | Py_UNICODE *p; |
| 6337 | const char *end; |
| 6338 | const char *reason; |
| 6339 | PyObject *errorHandler = NULL; |
| 6340 | PyObject *exc = NULL; |
| 6341 | |
Neal Norwitz | d43069c | 2006-01-08 01:12:10 +0000 | [diff] [blame] | 6342 | #ifdef Py_UNICODE_WIDE |
| 6343 | Py_UNICODE unimax = PyUnicode_GetMax(); |
| 6344 | #endif |
| 6345 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6346 | /* XXX overflow detection missing */ |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6347 | v = _PyUnicode_New((size+Py_UNICODE_SIZE-1)/ Py_UNICODE_SIZE); |
| 6348 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6349 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6350 | /* Intentionally PyUnicode_GET_SIZE instead of PyUnicode_GET_LENGTH |
| 6351 | as string was created with the old API. */ |
| 6352 | if (PyUnicode_GET_SIZE(v) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6353 | return (PyObject *)v; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6354 | p = PyUnicode_AS_UNICODE(v); |
| 6355 | end = s + size; |
| 6356 | |
| 6357 | while (s < end) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6358 | memcpy(p, s, sizeof(Py_UNICODE)); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6359 | /* We have to sanity check the raw data, otherwise doom looms for |
| 6360 | some malformed UCS-4 data. */ |
| 6361 | if ( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6362 | #ifdef Py_UNICODE_WIDE |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6363 | *p > unimax || *p < 0 || |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6364 | #endif |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6365 | end-s < Py_UNICODE_SIZE |
| 6366 | ) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6367 | { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6368 | startinpos = s - starts; |
| 6369 | if (end-s < Py_UNICODE_SIZE) { |
| 6370 | endinpos = end-starts; |
| 6371 | reason = "truncated input"; |
| 6372 | } |
| 6373 | else { |
| 6374 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 6375 | reason = "illegal code point (> 0x10FFFF)"; |
| 6376 | } |
| 6377 | outpos = p - PyUnicode_AS_UNICODE(v); |
| 6378 | if (unicode_decode_call_errorhandler( |
| 6379 | errors, &errorHandler, |
| 6380 | "unicode_internal", reason, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 6381 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 6382 | &v, &outpos, &p)) { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6383 | goto onError; |
| 6384 | } |
| 6385 | } |
| 6386 | else { |
| 6387 | p++; |
| 6388 | s += Py_UNICODE_SIZE; |
| 6389 | } |
| 6390 | } |
| 6391 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 6392 | if (PyUnicode_Resize((PyObject**)&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6393 | goto onError; |
| 6394 | Py_XDECREF(errorHandler); |
| 6395 | Py_XDECREF(exc); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 6396 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 6397 | if (_PyUnicode_READY_REPLACE(&v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6398 | Py_DECREF(v); |
| 6399 | return NULL; |
| 6400 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 6401 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 6402 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6403 | return (PyObject *)v; |
| 6404 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6405 | onError: |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6406 | Py_XDECREF(v); |
| 6407 | Py_XDECREF(errorHandler); |
| 6408 | Py_XDECREF(exc); |
| 6409 | return NULL; |
| 6410 | } |
| 6411 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6412 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 6413 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6414 | PyObject * |
| 6415 | PyUnicode_DecodeLatin1(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6416 | Py_ssize_t size, |
| 6417 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6418 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6419 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 6420 | return _PyUnicode_FromUCS1((unsigned char*)s, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6421 | } |
| 6422 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6423 | /* create or adjust a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6424 | static void |
| 6425 | make_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6426 | const char *encoding, |
| 6427 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 6428 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6429 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6430 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6431 | if (*exceptionObject == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6432 | *exceptionObject = PyUnicodeEncodeError_Create( |
| 6433 | encoding, unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6434 | } |
| 6435 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6436 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 6437 | goto onError; |
| 6438 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 6439 | goto onError; |
| 6440 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 6441 | goto onError; |
| 6442 | return; |
| 6443 | onError: |
| 6444 | Py_DECREF(*exceptionObject); |
| 6445 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6446 | } |
| 6447 | } |
| 6448 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6449 | /* raises a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6450 | static void |
| 6451 | raise_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6452 | const char *encoding, |
| 6453 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 6454 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6455 | const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6456 | { |
| 6457 | make_encode_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6458 | encoding, unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6459 | if (*exceptionObject != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6460 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6461 | } |
| 6462 | |
| 6463 | /* error handling callback helper: |
| 6464 | build arguments, call the callback and check the arguments, |
| 6465 | put the result into newpos and return the replacement string, which |
| 6466 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6467 | static PyObject * |
| 6468 | unicode_encode_call_errorhandler(const char *errors, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6469 | PyObject **errorHandler, |
| 6470 | const char *encoding, const char *reason, |
| 6471 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 6472 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6473 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6474 | { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6475 | 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] | 6476 | |
| 6477 | PyObject *restuple; |
| 6478 | PyObject *resunicode; |
| 6479 | |
| 6480 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6481 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6482 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6483 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6484 | } |
| 6485 | |
| 6486 | make_encode_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6487 | encoding, unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6488 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6489 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6490 | |
| 6491 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6492 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6493 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6494 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6495 | if (!PyTuple_Check(restuple)) { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6496 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6497 | Py_DECREF(restuple); |
| 6498 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6499 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6500 | if (!PyArg_ParseTuple(restuple, argparse, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6501 | &resunicode, newpos)) { |
| 6502 | Py_DECREF(restuple); |
| 6503 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6504 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6505 | if (!PyUnicode_Check(resunicode) && !PyBytes_Check(resunicode)) { |
| 6506 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
| 6507 | Py_DECREF(restuple); |
| 6508 | return NULL; |
| 6509 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6510 | if (*newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6511 | *newpos = size+*newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 6512 | if (*newpos<0 || *newpos>size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6513 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 6514 | Py_DECREF(restuple); |
| 6515 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 6516 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6517 | Py_INCREF(resunicode); |
| 6518 | Py_DECREF(restuple); |
| 6519 | return resunicode; |
| 6520 | } |
| 6521 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6522 | static PyObject * |
| 6523 | unicode_encode_ucs1(const Py_UNICODE *p, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6524 | Py_ssize_t size, |
| 6525 | const char *errors, |
| 6526 | int limit) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6527 | { |
| 6528 | /* output object */ |
| 6529 | PyObject *res; |
| 6530 | /* pointers to the beginning and end+1 of input */ |
| 6531 | const Py_UNICODE *startp = p; |
| 6532 | const Py_UNICODE *endp = p + size; |
| 6533 | /* pointer to the beginning of the unencodable characters */ |
| 6534 | /* const Py_UNICODE *badp = NULL; */ |
| 6535 | /* pointer into the output */ |
| 6536 | char *str; |
| 6537 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6538 | Py_ssize_t ressize; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6539 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 6540 | 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] | 6541 | PyObject *errorHandler = NULL; |
| 6542 | PyObject *exc = NULL; |
| 6543 | /* the following variable is used for caching string comparisons |
| 6544 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 6545 | int known_errorHandler = -1; |
| 6546 | |
| 6547 | /* allocate enough for a simple encoding without |
| 6548 | replacements, if we need more, we'll resize */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6549 | if (size == 0) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6550 | return PyBytes_FromStringAndSize(NULL, 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6551 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6552 | if (res == NULL) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6553 | return NULL; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6554 | str = PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6555 | ressize = size; |
| 6556 | |
| 6557 | while (p<endp) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6558 | Py_UNICODE c = *p; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6559 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6560 | /* can we encode this? */ |
| 6561 | if (c<limit) { |
| 6562 | /* no overflow check, because we know that the space is enough */ |
| 6563 | *str++ = (char)c; |
| 6564 | ++p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6565 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6566 | else { |
| 6567 | Py_ssize_t unicodepos = p-startp; |
| 6568 | Py_ssize_t requiredsize; |
| 6569 | PyObject *repunicode; |
| 6570 | Py_ssize_t repsize; |
| 6571 | Py_ssize_t newpos; |
| 6572 | Py_ssize_t respos; |
| 6573 | Py_UNICODE *uni2; |
| 6574 | /* startpos for collecting unencodable chars */ |
| 6575 | const Py_UNICODE *collstart = p; |
| 6576 | const Py_UNICODE *collend = p; |
| 6577 | /* find all unecodable characters */ |
| 6578 | while ((collend < endp) && ((*collend)>=limit)) |
| 6579 | ++collend; |
| 6580 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 6581 | if (known_errorHandler==-1) { |
| 6582 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 6583 | known_errorHandler = 1; |
| 6584 | else if (!strcmp(errors, "replace")) |
| 6585 | known_errorHandler = 2; |
| 6586 | else if (!strcmp(errors, "ignore")) |
| 6587 | known_errorHandler = 3; |
| 6588 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 6589 | known_errorHandler = 4; |
| 6590 | else |
| 6591 | known_errorHandler = 0; |
| 6592 | } |
| 6593 | switch (known_errorHandler) { |
| 6594 | case 1: /* strict */ |
| 6595 | raise_encode_exception(&exc, encoding, startp, size, collstart-startp, collend-startp, reason); |
| 6596 | goto onError; |
| 6597 | case 2: /* replace */ |
| 6598 | while (collstart++<collend) |
| 6599 | *str++ = '?'; /* fall through */ |
| 6600 | case 3: /* ignore */ |
| 6601 | p = collend; |
| 6602 | break; |
| 6603 | case 4: /* xmlcharrefreplace */ |
| 6604 | respos = str - PyBytes_AS_STRING(res); |
| 6605 | /* determine replacement size (temporarily (mis)uses p) */ |
| 6606 | for (p = collstart, repsize = 0; p < collend; ++p) { |
| 6607 | if (*p<10) |
| 6608 | repsize += 2+1+1; |
| 6609 | else if (*p<100) |
| 6610 | repsize += 2+2+1; |
| 6611 | else if (*p<1000) |
| 6612 | repsize += 2+3+1; |
| 6613 | else if (*p<10000) |
| 6614 | repsize += 2+4+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 6615 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6616 | else |
| 6617 | repsize += 2+5+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 6618 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6619 | else if (*p<100000) |
| 6620 | repsize += 2+5+1; |
| 6621 | else if (*p<1000000) |
| 6622 | repsize += 2+6+1; |
| 6623 | else |
| 6624 | repsize += 2+7+1; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 6625 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6626 | } |
| 6627 | requiredsize = respos+repsize+(endp-collend); |
| 6628 | if (requiredsize > ressize) { |
| 6629 | if (requiredsize<2*ressize) |
| 6630 | requiredsize = 2*ressize; |
| 6631 | if (_PyBytes_Resize(&res, requiredsize)) |
| 6632 | goto onError; |
| 6633 | str = PyBytes_AS_STRING(res) + respos; |
| 6634 | ressize = requiredsize; |
| 6635 | } |
| 6636 | /* generate replacement (temporarily (mis)uses p) */ |
| 6637 | for (p = collstart; p < collend; ++p) { |
| 6638 | str += sprintf(str, "&#%d;", (int)*p); |
| 6639 | } |
| 6640 | p = collend; |
| 6641 | break; |
| 6642 | default: |
| 6643 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 6644 | encoding, reason, startp, size, &exc, |
| 6645 | collstart-startp, collend-startp, &newpos); |
| 6646 | if (repunicode == NULL) |
| 6647 | goto onError; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6648 | if (PyBytes_Check(repunicode)) { |
| 6649 | /* Directly copy bytes result to output. */ |
| 6650 | repsize = PyBytes_Size(repunicode); |
| 6651 | if (repsize > 1) { |
| 6652 | /* Make room for all additional bytes. */ |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6653 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6654 | if (_PyBytes_Resize(&res, ressize+repsize-1)) { |
| 6655 | Py_DECREF(repunicode); |
| 6656 | goto onError; |
| 6657 | } |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6658 | str = PyBytes_AS_STRING(res) + respos; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6659 | ressize += repsize-1; |
| 6660 | } |
| 6661 | memcpy(str, PyBytes_AsString(repunicode), repsize); |
| 6662 | str += repsize; |
| 6663 | p = startp + newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6664 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6665 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6666 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6667 | /* need more space? (at least enough for what we |
| 6668 | have+the replacement+the rest of the string, so |
| 6669 | we won't have to check space for encodable characters) */ |
| 6670 | respos = str - PyBytes_AS_STRING(res); |
| 6671 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 6672 | requiredsize = respos+repsize+(endp-collend); |
| 6673 | if (requiredsize > ressize) { |
| 6674 | if (requiredsize<2*ressize) |
| 6675 | requiredsize = 2*ressize; |
| 6676 | if (_PyBytes_Resize(&res, requiredsize)) { |
| 6677 | Py_DECREF(repunicode); |
| 6678 | goto onError; |
| 6679 | } |
| 6680 | str = PyBytes_AS_STRING(res) + respos; |
| 6681 | ressize = requiredsize; |
| 6682 | } |
| 6683 | /* check if there is anything unencodable in the replacement |
| 6684 | and copy it to the output */ |
| 6685 | for (uni2 = PyUnicode_AS_UNICODE(repunicode);repsize-->0; ++uni2, ++str) { |
| 6686 | c = *uni2; |
| 6687 | if (c >= limit) { |
| 6688 | raise_encode_exception(&exc, encoding, startp, size, |
| 6689 | unicodepos, unicodepos+1, reason); |
| 6690 | Py_DECREF(repunicode); |
| 6691 | goto onError; |
| 6692 | } |
| 6693 | *str = (char)c; |
| 6694 | } |
| 6695 | p = startp + newpos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6696 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6697 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6698 | } |
| 6699 | } |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6700 | /* Resize if we allocated to much */ |
| 6701 | size = str - PyBytes_AS_STRING(res); |
| 6702 | if (size < ressize) { /* If this falls res will be NULL */ |
Alexandre Vassalotti | bad1b92 | 2008-12-27 09:49:09 +0000 | [diff] [blame] | 6703 | assert(size >= 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6704 | if (_PyBytes_Resize(&res, size) < 0) |
| 6705 | goto onError; |
| 6706 | } |
| 6707 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6708 | Py_XDECREF(errorHandler); |
| 6709 | Py_XDECREF(exc); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6710 | return res; |
| 6711 | |
| 6712 | onError: |
| 6713 | Py_XDECREF(res); |
| 6714 | Py_XDECREF(errorHandler); |
| 6715 | Py_XDECREF(exc); |
| 6716 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6717 | } |
| 6718 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6719 | PyObject * |
| 6720 | PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6721 | Py_ssize_t size, |
| 6722 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6723 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6724 | return unicode_encode_ucs1(p, size, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6725 | } |
| 6726 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6727 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6728 | _PyUnicode_AsLatin1String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6729 | { |
| 6730 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6731 | PyErr_BadArgument(); |
| 6732 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6733 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6734 | if (PyUnicode_READY(unicode) == -1) |
| 6735 | return NULL; |
| 6736 | /* Fast path: if it is a one-byte string, construct |
| 6737 | bytes object directly. */ |
| 6738 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) |
| 6739 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6740 | PyUnicode_GET_LENGTH(unicode)); |
| 6741 | /* Non-Latin-1 characters present. Defer to above function to |
| 6742 | raise the exception. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6743 | return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6744 | PyUnicode_GET_SIZE(unicode), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6745 | errors); |
| 6746 | } |
| 6747 | |
| 6748 | PyObject* |
| 6749 | PyUnicode_AsLatin1String(PyObject *unicode) |
| 6750 | { |
| 6751 | return _PyUnicode_AsLatin1String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6752 | } |
| 6753 | |
| 6754 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 6755 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6756 | PyObject * |
| 6757 | PyUnicode_DecodeASCII(const char *s, |
| 6758 | Py_ssize_t size, |
| 6759 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6760 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6761 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6762 | PyUnicodeObject *v; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6763 | Py_UNICODE *u; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6764 | Py_ssize_t startinpos; |
| 6765 | Py_ssize_t endinpos; |
| 6766 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6767 | const char *e; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6768 | int has_error; |
| 6769 | const unsigned char *p = (const unsigned char *)s; |
| 6770 | const unsigned char *end = p + size; |
| 6771 | 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] | 6772 | PyObject *errorHandler = NULL; |
| 6773 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6774 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6775 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6776 | if (size == 1 && (unsigned char)s[0] < 128) |
| 6777 | return get_latin1_char((unsigned char)s[0]); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6778 | |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6779 | has_error = 0; |
| 6780 | while (p < end && !has_error) { |
| 6781 | /* Fast path, see below in PyUnicode_DecodeUTF8Stateful for |
| 6782 | an explanation. */ |
| 6783 | if (!((size_t) p & LONG_PTR_MASK)) { |
| 6784 | /* Help register allocation */ |
| 6785 | register const unsigned char *_p = p; |
| 6786 | while (_p < aligned_end) { |
| 6787 | unsigned long value = *(unsigned long *) _p; |
| 6788 | if (value & ASCII_CHAR_MASK) { |
| 6789 | has_error = 1; |
| 6790 | break; |
| 6791 | } |
| 6792 | _p += SIZEOF_LONG; |
| 6793 | } |
| 6794 | if (_p == end) |
| 6795 | break; |
| 6796 | if (has_error) |
| 6797 | break; |
| 6798 | p = _p; |
| 6799 | } |
| 6800 | if (*p & 0x80) { |
| 6801 | has_error = 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6802 | break; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6803 | } |
| 6804 | else { |
| 6805 | ++p; |
| 6806 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 6807 | } |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6808 | if (!has_error) |
| 6809 | return unicode_fromascii((const unsigned char *)s, size); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6810 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6811 | v = _PyUnicode_New(size); |
| 6812 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6813 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6814 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6815 | return (PyObject *)v; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6816 | u = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6817 | e = s + size; |
| 6818 | while (s < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6819 | register unsigned char c = (unsigned char)*s; |
| 6820 | if (c < 128) { |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6821 | *u++ = c; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6822 | ++s; |
| 6823 | } |
| 6824 | else { |
| 6825 | startinpos = s-starts; |
| 6826 | endinpos = startinpos + 1; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6827 | outpos = u - (Py_UNICODE *)PyUnicode_AS_UNICODE(v); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6828 | if (unicode_decode_call_errorhandler( |
| 6829 | errors, &errorHandler, |
| 6830 | "ascii", "ordinal not in range(128)", |
| 6831 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6832 | &v, &outpos, &u)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6833 | goto onError; |
| 6834 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6835 | } |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6836 | if (u - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
| 6837 | if (PyUnicode_Resize((PyObject**)&v, u - PyUnicode_AS_UNICODE(v)) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6838 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6839 | Py_XDECREF(errorHandler); |
| 6840 | Py_XDECREF(exc); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 6841 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 6842 | if (_PyUnicode_READY_REPLACE(&v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6843 | Py_DECREF(v); |
| 6844 | return NULL; |
| 6845 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 6846 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 6847 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6848 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6849 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6850 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6851 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6852 | Py_XDECREF(errorHandler); |
| 6853 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6854 | return NULL; |
| 6855 | } |
| 6856 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6857 | PyObject * |
| 6858 | PyUnicode_EncodeASCII(const Py_UNICODE *p, |
| 6859 | Py_ssize_t size, |
| 6860 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6861 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6862 | return unicode_encode_ucs1(p, size, errors, 128); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6863 | } |
| 6864 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6865 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6866 | _PyUnicode_AsASCIIString(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6867 | { |
| 6868 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6869 | PyErr_BadArgument(); |
| 6870 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6871 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6872 | if (PyUnicode_READY(unicode) == -1) |
| 6873 | return NULL; |
| 6874 | /* Fast path: if it is an ASCII-only string, construct bytes object |
| 6875 | directly. Else defer to above function to raise the exception. */ |
| 6876 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) |
| 6877 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6878 | PyUnicode_GET_LENGTH(unicode)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6879 | return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6880 | PyUnicode_GET_SIZE(unicode), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6881 | errors); |
| 6882 | } |
| 6883 | |
| 6884 | PyObject * |
| 6885 | PyUnicode_AsASCIIString(PyObject *unicode) |
| 6886 | { |
| 6887 | return _PyUnicode_AsASCIIString(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6888 | } |
| 6889 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 6890 | #ifdef HAVE_MBCS |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6891 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6892 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6893 | |
Hirokazu Yamamoto | 3530246 | 2009-03-21 13:23:27 +0000 | [diff] [blame] | 6894 | #if SIZEOF_INT < SIZEOF_SIZE_T |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6895 | #define NEED_RETRY |
| 6896 | #endif |
| 6897 | |
| 6898 | /* XXX This code is limited to "true" double-byte encodings, as |
| 6899 | a) it assumes an incomplete character consists of a single byte, and |
| 6900 | b) IsDBCSLeadByte (probably) does not work for non-DBCS multi-byte |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6901 | encodings, see IsDBCSLeadByteEx documentation. */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6902 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6903 | static int |
| 6904 | is_dbcs_lead_byte(const char *s, int offset) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6905 | { |
| 6906 | const char *curr = s + offset; |
| 6907 | |
| 6908 | if (IsDBCSLeadByte(*curr)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6909 | const char *prev = CharPrev(s, curr); |
| 6910 | return (prev == curr) || !IsDBCSLeadByte(*prev) || (curr - prev == 2); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6911 | } |
| 6912 | return 0; |
| 6913 | } |
| 6914 | |
| 6915 | /* |
| 6916 | * Decode MBCS string into unicode object. If 'final' is set, converts |
| 6917 | * trailing lead-byte too. Returns consumed size if succeed, -1 otherwise. |
| 6918 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6919 | static int |
| 6920 | decode_mbcs(PyUnicodeObject **v, |
| 6921 | const char *s, /* MBCS string */ |
| 6922 | int size, /* sizeof MBCS string */ |
| 6923 | int final, |
| 6924 | const char *errors) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6925 | { |
| 6926 | Py_UNICODE *p; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6927 | Py_ssize_t n; |
| 6928 | DWORD usize; |
| 6929 | DWORD flags; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6930 | |
| 6931 | assert(size >= 0); |
| 6932 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6933 | /* check and handle 'errors' arg */ |
| 6934 | if (errors==NULL || strcmp(errors, "strict")==0) |
| 6935 | flags = MB_ERR_INVALID_CHARS; |
| 6936 | else if (strcmp(errors, "ignore")==0) |
| 6937 | flags = 0; |
| 6938 | else { |
| 6939 | PyErr_Format(PyExc_ValueError, |
| 6940 | "mbcs encoding does not support errors='%s'", |
| 6941 | errors); |
| 6942 | return -1; |
| 6943 | } |
| 6944 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6945 | /* Skip trailing lead-byte unless 'final' is set */ |
| 6946 | if (!final && size >= 1 && is_dbcs_lead_byte(s, size - 1)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6947 | --size; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6948 | |
| 6949 | /* First get the size of the result */ |
| 6950 | if (size > 0) { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6951 | usize = MultiByteToWideChar(CP_ACP, flags, s, size, NULL, 0); |
| 6952 | if (usize==0) |
| 6953 | goto mbcs_decode_error; |
| 6954 | } else |
| 6955 | usize = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6956 | |
| 6957 | if (*v == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6958 | /* Create unicode object */ |
| 6959 | *v = _PyUnicode_New(usize); |
| 6960 | if (*v == NULL) |
| 6961 | return -1; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6962 | n = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6963 | } |
| 6964 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6965 | /* Extend unicode object */ |
| 6966 | n = PyUnicode_GET_SIZE(*v); |
Victor Stinner | 2fd8227 | 2011-10-03 04:06:05 +0200 | [diff] [blame] | 6967 | if (PyUnicode_Resize((PyObject**)v, n + usize) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6968 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6969 | } |
| 6970 | |
| 6971 | /* Do the conversion */ |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6972 | if (usize > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6973 | p = PyUnicode_AS_UNICODE(*v) + n; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6974 | if (0 == MultiByteToWideChar(CP_ACP, flags, s, size, p, usize)) { |
| 6975 | goto mbcs_decode_error; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6976 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6977 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6978 | return size; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6979 | |
| 6980 | mbcs_decode_error: |
| 6981 | /* If the last error was ERROR_NO_UNICODE_TRANSLATION, then |
| 6982 | we raise a UnicodeDecodeError - else it is a 'generic' |
| 6983 | windows error |
| 6984 | */ |
| 6985 | if (GetLastError()==ERROR_NO_UNICODE_TRANSLATION) { |
| 6986 | /* Ideally, we should get reason from FormatMessage - this |
| 6987 | is the Windows 2000 English version of the message |
| 6988 | */ |
| 6989 | PyObject *exc = NULL; |
| 6990 | const char *reason = "No mapping for the Unicode character exists " |
| 6991 | "in the target multi-byte code page."; |
| 6992 | make_decode_exception(&exc, "mbcs", s, size, 0, 0, reason); |
| 6993 | if (exc != NULL) { |
| 6994 | PyCodec_StrictErrors(exc); |
| 6995 | Py_DECREF(exc); |
| 6996 | } |
| 6997 | } else { |
| 6998 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 6999 | } |
| 7000 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7001 | } |
| 7002 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7003 | PyObject * |
| 7004 | PyUnicode_DecodeMBCSStateful(const char *s, |
| 7005 | Py_ssize_t size, |
| 7006 | const char *errors, |
| 7007 | Py_ssize_t *consumed) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7008 | { |
| 7009 | PyUnicodeObject *v = NULL; |
| 7010 | int done; |
| 7011 | |
| 7012 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7013 | *consumed = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7014 | |
| 7015 | #ifdef NEED_RETRY |
| 7016 | retry: |
| 7017 | if (size > INT_MAX) |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7018 | done = decode_mbcs(&v, s, INT_MAX, 0, errors); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7019 | else |
| 7020 | #endif |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7021 | done = decode_mbcs(&v, s, (int)size, !consumed, errors); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7022 | |
| 7023 | if (done < 0) { |
| 7024 | Py_XDECREF(v); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7025 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7026 | } |
| 7027 | |
| 7028 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7029 | *consumed += done; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7030 | |
| 7031 | #ifdef NEED_RETRY |
| 7032 | if (size > INT_MAX) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7033 | s += done; |
| 7034 | size -= done; |
| 7035 | goto retry; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7036 | } |
| 7037 | #endif |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 7038 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 7039 | if (_PyUnicode_READY_REPLACE(&v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7040 | Py_DECREF(v); |
| 7041 | return NULL; |
| 7042 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 7043 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 7044 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7045 | return (PyObject *)v; |
| 7046 | } |
| 7047 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7048 | PyObject * |
| 7049 | PyUnicode_DecodeMBCS(const char *s, |
| 7050 | Py_ssize_t size, |
| 7051 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7052 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7053 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 7054 | } |
| 7055 | |
| 7056 | /* |
| 7057 | * Convert unicode into string object (MBCS). |
| 7058 | * Returns 0 if succeed, -1 otherwise. |
| 7059 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7060 | static int |
| 7061 | encode_mbcs(PyObject **repr, |
| 7062 | const Py_UNICODE *p, /* unicode */ |
| 7063 | int size, /* size of unicode */ |
| 7064 | const char* errors) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7065 | { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7066 | BOOL usedDefaultChar = FALSE; |
| 7067 | BOOL *pusedDefaultChar; |
| 7068 | int mbcssize; |
| 7069 | Py_ssize_t n; |
| 7070 | PyObject *exc = NULL; |
| 7071 | DWORD flags; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7072 | |
| 7073 | assert(size >= 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7074 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7075 | /* check and handle 'errors' arg */ |
| 7076 | if (errors==NULL || strcmp(errors, "strict")==0) { |
| 7077 | flags = WC_NO_BEST_FIT_CHARS; |
| 7078 | pusedDefaultChar = &usedDefaultChar; |
| 7079 | } else if (strcmp(errors, "replace")==0) { |
| 7080 | flags = 0; |
| 7081 | pusedDefaultChar = NULL; |
| 7082 | } else { |
| 7083 | PyErr_Format(PyExc_ValueError, |
| 7084 | "mbcs encoding does not support errors='%s'", |
| 7085 | errors); |
| 7086 | return -1; |
| 7087 | } |
| 7088 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7089 | /* First get the size of the result */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7090 | if (size > 0) { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7091 | mbcssize = WideCharToMultiByte(CP_ACP, flags, p, size, NULL, 0, |
| 7092 | NULL, pusedDefaultChar); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7093 | if (mbcssize == 0) { |
| 7094 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 7095 | return -1; |
| 7096 | } |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7097 | /* If we used a default char, then we failed! */ |
| 7098 | if (pusedDefaultChar && *pusedDefaultChar) |
| 7099 | goto mbcs_encode_error; |
| 7100 | } else { |
| 7101 | mbcssize = 0; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7102 | } |
| 7103 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7104 | if (*repr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7105 | /* Create string object */ |
| 7106 | *repr = PyBytes_FromStringAndSize(NULL, mbcssize); |
| 7107 | if (*repr == NULL) |
| 7108 | return -1; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7109 | n = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7110 | } |
| 7111 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7112 | /* Extend string object */ |
| 7113 | n = PyBytes_Size(*repr); |
| 7114 | if (_PyBytes_Resize(repr, n + mbcssize) < 0) |
| 7115 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7116 | } |
| 7117 | |
| 7118 | /* Do the conversion */ |
| 7119 | if (size > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7120 | char *s = PyBytes_AS_STRING(*repr) + n; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7121 | if (0 == WideCharToMultiByte(CP_ACP, flags, p, size, s, mbcssize, |
| 7122 | NULL, pusedDefaultChar)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7123 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 7124 | return -1; |
| 7125 | } |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7126 | if (pusedDefaultChar && *pusedDefaultChar) |
| 7127 | goto mbcs_encode_error; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7128 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7129 | return 0; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7130 | |
| 7131 | mbcs_encode_error: |
| 7132 | raise_encode_exception(&exc, "mbcs", p, size, 0, 0, "invalid character"); |
| 7133 | Py_XDECREF(exc); |
| 7134 | return -1; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7135 | } |
| 7136 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7137 | PyObject * |
| 7138 | PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
| 7139 | Py_ssize_t size, |
| 7140 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7141 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7142 | PyObject *repr = NULL; |
| 7143 | int ret; |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 7144 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7145 | #ifdef NEED_RETRY |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7146 | retry: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7147 | if (size > INT_MAX) |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7148 | ret = encode_mbcs(&repr, p, INT_MAX, errors); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7149 | else |
| 7150 | #endif |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7151 | ret = encode_mbcs(&repr, p, (int)size, errors); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7152 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7153 | if (ret < 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7154 | Py_XDECREF(repr); |
| 7155 | return NULL; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7156 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7157 | |
| 7158 | #ifdef NEED_RETRY |
| 7159 | if (size > INT_MAX) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7160 | p += INT_MAX; |
| 7161 | size -= INT_MAX; |
| 7162 | goto retry; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7163 | } |
| 7164 | #endif |
| 7165 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7166 | return repr; |
| 7167 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 7168 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7169 | PyObject * |
| 7170 | PyUnicode_AsMBCSString(PyObject *unicode) |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7171 | { |
| 7172 | if (!PyUnicode_Check(unicode)) { |
| 7173 | PyErr_BadArgument(); |
| 7174 | return NULL; |
| 7175 | } |
| 7176 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7177 | PyUnicode_GET_SIZE(unicode), |
| 7178 | NULL); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7179 | } |
| 7180 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7181 | #undef NEED_RETRY |
| 7182 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 7183 | #endif /* HAVE_MBCS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7184 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7185 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 7186 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7187 | PyObject * |
| 7188 | PyUnicode_DecodeCharmap(const char *s, |
| 7189 | Py_ssize_t size, |
| 7190 | PyObject *mapping, |
| 7191 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7192 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7193 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7194 | Py_ssize_t startinpos; |
| 7195 | Py_ssize_t endinpos; |
| 7196 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7197 | const char *e; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7198 | PyUnicodeObject *v; |
| 7199 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7200 | Py_ssize_t extrachars = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7201 | PyObject *errorHandler = NULL; |
| 7202 | PyObject *exc = NULL; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7203 | Py_UNICODE *mapstring = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7204 | Py_ssize_t maplen = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7205 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7206 | /* Default to Latin-1 */ |
| 7207 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7208 | return PyUnicode_DecodeLatin1(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7209 | |
| 7210 | v = _PyUnicode_New(size); |
| 7211 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7212 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7213 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7214 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7215 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7216 | e = s + size; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7217 | if (PyUnicode_CheckExact(mapping)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7218 | mapstring = PyUnicode_AS_UNICODE(mapping); |
| 7219 | maplen = PyUnicode_GET_SIZE(mapping); |
| 7220 | while (s < e) { |
| 7221 | unsigned char ch = *s; |
| 7222 | Py_UNICODE x = 0xfffe; /* illegal value */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7223 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7224 | if (ch < maplen) |
| 7225 | x = mapstring[ch]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7226 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7227 | if (x == 0xfffe) { |
| 7228 | /* undefined mapping */ |
| 7229 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 7230 | startinpos = s-starts; |
| 7231 | endinpos = startinpos+1; |
| 7232 | if (unicode_decode_call_errorhandler( |
| 7233 | errors, &errorHandler, |
| 7234 | "charmap", "character maps to <undefined>", |
| 7235 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 7236 | &v, &outpos, &p)) { |
| 7237 | goto onError; |
| 7238 | } |
| 7239 | continue; |
| 7240 | } |
| 7241 | *p++ = x; |
| 7242 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7243 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7244 | } |
| 7245 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7246 | while (s < e) { |
| 7247 | unsigned char ch = *s; |
| 7248 | PyObject *w, *x; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7249 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7250 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 7251 | w = PyLong_FromLong((long)ch); |
| 7252 | if (w == NULL) |
| 7253 | goto onError; |
| 7254 | x = PyObject_GetItem(mapping, w); |
| 7255 | Py_DECREF(w); |
| 7256 | if (x == NULL) { |
| 7257 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 7258 | /* No mapping found means: mapping is undefined. */ |
| 7259 | PyErr_Clear(); |
| 7260 | x = Py_None; |
| 7261 | Py_INCREF(x); |
| 7262 | } else |
| 7263 | goto onError; |
| 7264 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7265 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7266 | /* Apply mapping */ |
| 7267 | if (PyLong_Check(x)) { |
| 7268 | long value = PyLong_AS_LONG(x); |
| 7269 | if (value < 0 || value > 65535) { |
| 7270 | PyErr_SetString(PyExc_TypeError, |
| 7271 | "character mapping must be in range(65536)"); |
| 7272 | Py_DECREF(x); |
| 7273 | goto onError; |
| 7274 | } |
| 7275 | *p++ = (Py_UNICODE)value; |
| 7276 | } |
| 7277 | else if (x == Py_None) { |
| 7278 | /* undefined mapping */ |
| 7279 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 7280 | startinpos = s-starts; |
| 7281 | endinpos = startinpos+1; |
| 7282 | if (unicode_decode_call_errorhandler( |
| 7283 | errors, &errorHandler, |
| 7284 | "charmap", "character maps to <undefined>", |
| 7285 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 7286 | &v, &outpos, &p)) { |
| 7287 | Py_DECREF(x); |
| 7288 | goto onError; |
| 7289 | } |
| 7290 | Py_DECREF(x); |
| 7291 | continue; |
| 7292 | } |
| 7293 | else if (PyUnicode_Check(x)) { |
| 7294 | Py_ssize_t targetsize = PyUnicode_GET_SIZE(x); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7295 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7296 | if (targetsize == 1) |
| 7297 | /* 1-1 mapping */ |
| 7298 | *p++ = *PyUnicode_AS_UNICODE(x); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7299 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7300 | else if (targetsize > 1) { |
| 7301 | /* 1-n mapping */ |
| 7302 | if (targetsize > extrachars) { |
| 7303 | /* resize first */ |
| 7304 | Py_ssize_t oldpos = p - PyUnicode_AS_UNICODE(v); |
| 7305 | Py_ssize_t needed = (targetsize - extrachars) + \ |
| 7306 | (targetsize << 2); |
| 7307 | extrachars += needed; |
| 7308 | /* XXX overflow detection missing */ |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 7309 | if (PyUnicode_Resize((PyObject**)&v, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7310 | PyUnicode_GET_SIZE(v) + needed) < 0) { |
| 7311 | Py_DECREF(x); |
| 7312 | goto onError; |
| 7313 | } |
| 7314 | p = PyUnicode_AS_UNICODE(v) + oldpos; |
| 7315 | } |
| 7316 | Py_UNICODE_COPY(p, |
| 7317 | PyUnicode_AS_UNICODE(x), |
| 7318 | targetsize); |
| 7319 | p += targetsize; |
| 7320 | extrachars -= targetsize; |
| 7321 | } |
| 7322 | /* 1-0 mapping: skip the character */ |
| 7323 | } |
| 7324 | else { |
| 7325 | /* wrong return value */ |
| 7326 | PyErr_SetString(PyExc_TypeError, |
| 7327 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7328 | Py_DECREF(x); |
| 7329 | goto onError; |
| 7330 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7331 | Py_DECREF(x); |
| 7332 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7333 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7334 | } |
| 7335 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 7336 | if (PyUnicode_Resize((PyObject**)&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7337 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7338 | Py_XDECREF(errorHandler); |
| 7339 | Py_XDECREF(exc); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 7340 | #ifndef DONT_MAKE_RESULT_READY |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 7341 | if (_PyUnicode_READY_REPLACE(&v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7342 | Py_DECREF(v); |
| 7343 | return NULL; |
| 7344 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 7345 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 7346 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7347 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7348 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7349 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7350 | Py_XDECREF(errorHandler); |
| 7351 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7352 | Py_XDECREF(v); |
| 7353 | return NULL; |
| 7354 | } |
| 7355 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7356 | /* Charmap encoding: the lookup table */ |
| 7357 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7358 | struct encoding_map { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7359 | PyObject_HEAD |
| 7360 | unsigned char level1[32]; |
| 7361 | int count2, count3; |
| 7362 | unsigned char level23[1]; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7363 | }; |
| 7364 | |
| 7365 | static PyObject* |
| 7366 | encoding_map_size(PyObject *obj, PyObject* args) |
| 7367 | { |
| 7368 | struct encoding_map *map = (struct encoding_map*)obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7369 | return PyLong_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7370 | 128*map->count3); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7371 | } |
| 7372 | |
| 7373 | static PyMethodDef encoding_map_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7374 | {"size", encoding_map_size, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7375 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 7376 | { 0 } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7377 | }; |
| 7378 | |
| 7379 | static void |
| 7380 | encoding_map_dealloc(PyObject* o) |
| 7381 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7382 | PyObject_FREE(o); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7383 | } |
| 7384 | |
| 7385 | static PyTypeObject EncodingMapType = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7386 | PyVarObject_HEAD_INIT(NULL, 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7387 | "EncodingMap", /*tp_name*/ |
| 7388 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 7389 | 0, /*tp_itemsize*/ |
| 7390 | /* methods */ |
| 7391 | encoding_map_dealloc, /*tp_dealloc*/ |
| 7392 | 0, /*tp_print*/ |
| 7393 | 0, /*tp_getattr*/ |
| 7394 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 7395 | 0, /*tp_reserved*/ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7396 | 0, /*tp_repr*/ |
| 7397 | 0, /*tp_as_number*/ |
| 7398 | 0, /*tp_as_sequence*/ |
| 7399 | 0, /*tp_as_mapping*/ |
| 7400 | 0, /*tp_hash*/ |
| 7401 | 0, /*tp_call*/ |
| 7402 | 0, /*tp_str*/ |
| 7403 | 0, /*tp_getattro*/ |
| 7404 | 0, /*tp_setattro*/ |
| 7405 | 0, /*tp_as_buffer*/ |
| 7406 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 7407 | 0, /*tp_doc*/ |
| 7408 | 0, /*tp_traverse*/ |
| 7409 | 0, /*tp_clear*/ |
| 7410 | 0, /*tp_richcompare*/ |
| 7411 | 0, /*tp_weaklistoffset*/ |
| 7412 | 0, /*tp_iter*/ |
| 7413 | 0, /*tp_iternext*/ |
| 7414 | encoding_map_methods, /*tp_methods*/ |
| 7415 | 0, /*tp_members*/ |
| 7416 | 0, /*tp_getset*/ |
| 7417 | 0, /*tp_base*/ |
| 7418 | 0, /*tp_dict*/ |
| 7419 | 0, /*tp_descr_get*/ |
| 7420 | 0, /*tp_descr_set*/ |
| 7421 | 0, /*tp_dictoffset*/ |
| 7422 | 0, /*tp_init*/ |
| 7423 | 0, /*tp_alloc*/ |
| 7424 | 0, /*tp_new*/ |
| 7425 | 0, /*tp_free*/ |
| 7426 | 0, /*tp_is_gc*/ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7427 | }; |
| 7428 | |
| 7429 | PyObject* |
| 7430 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 7431 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7432 | PyObject *result; |
| 7433 | struct encoding_map *mresult; |
| 7434 | int i; |
| 7435 | int need_dict = 0; |
| 7436 | unsigned char level1[32]; |
| 7437 | unsigned char level2[512]; |
| 7438 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 7439 | int count2 = 0, count3 = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7440 | int kind; |
| 7441 | void *data; |
| 7442 | Py_UCS4 ch; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7443 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7444 | if (!PyUnicode_Check(string) || PyUnicode_GET_LENGTH(string) != 256) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7445 | PyErr_BadArgument(); |
| 7446 | return NULL; |
| 7447 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7448 | kind = PyUnicode_KIND(string); |
| 7449 | data = PyUnicode_DATA(string); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7450 | memset(level1, 0xFF, sizeof level1); |
| 7451 | memset(level2, 0xFF, sizeof level2); |
| 7452 | |
| 7453 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 7454 | or if there are non-BMP characters, we need to use |
| 7455 | a mapping dictionary. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7456 | if (PyUnicode_READ(kind, data, 0) != 0) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7457 | need_dict = 1; |
| 7458 | for (i = 1; i < 256; i++) { |
| 7459 | int l1, l2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7460 | ch = PyUnicode_READ(kind, data, i); |
| 7461 | if (ch == 0 || ch > 0xFFFF) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7462 | need_dict = 1; |
| 7463 | break; |
| 7464 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7465 | if (ch == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7466 | /* unmapped character */ |
| 7467 | continue; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7468 | l1 = ch >> 11; |
| 7469 | l2 = ch >> 7; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7470 | if (level1[l1] == 0xFF) |
| 7471 | level1[l1] = count2++; |
| 7472 | if (level2[l2] == 0xFF) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7473 | level2[l2] = count3++; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7474 | } |
| 7475 | |
| 7476 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 7477 | need_dict = 1; |
| 7478 | |
| 7479 | if (need_dict) { |
| 7480 | PyObject *result = PyDict_New(); |
| 7481 | PyObject *key, *value; |
| 7482 | if (!result) |
| 7483 | return NULL; |
| 7484 | for (i = 0; i < 256; i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7485 | key = PyLong_FromLong(PyUnicode_READ(kind, data, i)); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7486 | value = PyLong_FromLong(i); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7487 | if (!key || !value) |
| 7488 | goto failed1; |
| 7489 | if (PyDict_SetItem(result, key, value) == -1) |
| 7490 | goto failed1; |
| 7491 | Py_DECREF(key); |
| 7492 | Py_DECREF(value); |
| 7493 | } |
| 7494 | return result; |
| 7495 | failed1: |
| 7496 | Py_XDECREF(key); |
| 7497 | Py_XDECREF(value); |
| 7498 | Py_DECREF(result); |
| 7499 | return NULL; |
| 7500 | } |
| 7501 | |
| 7502 | /* Create a three-level trie */ |
| 7503 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 7504 | 16*count2 + 128*count3 - 1); |
| 7505 | if (!result) |
| 7506 | return PyErr_NoMemory(); |
| 7507 | PyObject_Init(result, &EncodingMapType); |
| 7508 | mresult = (struct encoding_map*)result; |
| 7509 | mresult->count2 = count2; |
| 7510 | mresult->count3 = count3; |
| 7511 | mlevel1 = mresult->level1; |
| 7512 | mlevel2 = mresult->level23; |
| 7513 | mlevel3 = mresult->level23 + 16*count2; |
| 7514 | memcpy(mlevel1, level1, 32); |
| 7515 | memset(mlevel2, 0xFF, 16*count2); |
| 7516 | memset(mlevel3, 0, 128*count3); |
| 7517 | count3 = 0; |
| 7518 | for (i = 1; i < 256; i++) { |
| 7519 | int o1, o2, o3, i2, i3; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7520 | if (PyUnicode_READ(kind, data, i) == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7521 | /* unmapped character */ |
| 7522 | continue; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7523 | o1 = PyUnicode_READ(kind, data, i)>>11; |
| 7524 | o2 = (PyUnicode_READ(kind, data, i)>>7) & 0xF; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7525 | i2 = 16*mlevel1[o1] + o2; |
| 7526 | if (mlevel2[i2] == 0xFF) |
| 7527 | mlevel2[i2] = count3++; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7528 | o3 = PyUnicode_READ(kind, data, i) & 0x7F; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7529 | i3 = 128*mlevel2[i2] + o3; |
| 7530 | mlevel3[i3] = i; |
| 7531 | } |
| 7532 | return result; |
| 7533 | } |
| 7534 | |
| 7535 | static int |
| 7536 | encoding_map_lookup(Py_UNICODE c, PyObject *mapping) |
| 7537 | { |
| 7538 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 7539 | int l1 = c>>11; |
| 7540 | int l2 = (c>>7) & 0xF; |
| 7541 | int l3 = c & 0x7F; |
| 7542 | int i; |
| 7543 | |
| 7544 | #ifdef Py_UNICODE_WIDE |
| 7545 | if (c > 0xFFFF) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7546 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7547 | } |
| 7548 | #endif |
| 7549 | if (c == 0) |
| 7550 | return 0; |
| 7551 | /* level 1*/ |
| 7552 | i = map->level1[l1]; |
| 7553 | if (i == 0xFF) { |
| 7554 | return -1; |
| 7555 | } |
| 7556 | /* level 2*/ |
| 7557 | i = map->level23[16*i+l2]; |
| 7558 | if (i == 0xFF) { |
| 7559 | return -1; |
| 7560 | } |
| 7561 | /* level 3 */ |
| 7562 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 7563 | if (i == 0) { |
| 7564 | return -1; |
| 7565 | } |
| 7566 | return i; |
| 7567 | } |
| 7568 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7569 | /* Lookup the character ch in the mapping. If the character |
| 7570 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 7571 | error occurred). */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7572 | static PyObject * |
| 7573 | charmapencode_lookup(Py_UNICODE c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7574 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7575 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7576 | PyObject *x; |
| 7577 | |
| 7578 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7579 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7580 | x = PyObject_GetItem(mapping, w); |
| 7581 | Py_DECREF(w); |
| 7582 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7583 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 7584 | /* No mapping found means: mapping is undefined. */ |
| 7585 | PyErr_Clear(); |
| 7586 | x = Py_None; |
| 7587 | Py_INCREF(x); |
| 7588 | return x; |
| 7589 | } else |
| 7590 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7591 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 7592 | else if (x == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7593 | return x; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7594 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7595 | long value = PyLong_AS_LONG(x); |
| 7596 | if (value < 0 || value > 255) { |
| 7597 | PyErr_SetString(PyExc_TypeError, |
| 7598 | "character mapping must be in range(256)"); |
| 7599 | Py_DECREF(x); |
| 7600 | return NULL; |
| 7601 | } |
| 7602 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7603 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7604 | else if (PyBytes_Check(x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7605 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7606 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7607 | /* wrong return value */ |
| 7608 | PyErr_Format(PyExc_TypeError, |
| 7609 | "character mapping must return integer, bytes or None, not %.400s", |
| 7610 | x->ob_type->tp_name); |
| 7611 | Py_DECREF(x); |
| 7612 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7613 | } |
| 7614 | } |
| 7615 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7616 | static int |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 7617 | charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7618 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7619 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
| 7620 | /* exponentially overallocate to minimize reallocations */ |
| 7621 | if (requiredsize < 2*outsize) |
| 7622 | requiredsize = 2*outsize; |
| 7623 | if (_PyBytes_Resize(outobj, requiredsize)) |
| 7624 | return -1; |
| 7625 | return 0; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7626 | } |
| 7627 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7628 | typedef enum charmapencode_result { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7629 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7630 | } charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7631 | /* 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] | 7632 | various state variables. Resize the output bytes object if not enough |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7633 | space is available. Return a new reference to the object that |
| 7634 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 7635 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 7636 | reallocation error occurred. The caller must decref the result */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7637 | static charmapencode_result |
| 7638 | charmapencode_output(Py_UNICODE c, PyObject *mapping, |
| 7639 | PyObject **outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7640 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7641 | PyObject *rep; |
| 7642 | char *outstart; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7643 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7644 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7645 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7646 | int res = encoding_map_lookup(c, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7647 | Py_ssize_t requiredsize = *outpos+1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7648 | if (res == -1) |
| 7649 | return enc_FAILED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7650 | if (outsize<requiredsize) |
| 7651 | if (charmapencode_resize(outobj, outpos, requiredsize)) |
| 7652 | return enc_EXCEPTION; |
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 | outstart[(*outpos)++] = (char)res; |
| 7655 | return enc_SUCCESS; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7656 | } |
| 7657 | |
| 7658 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7659 | if (rep==NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7660 | return enc_EXCEPTION; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7661 | else if (rep==Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7662 | Py_DECREF(rep); |
| 7663 | return enc_FAILED; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7664 | } else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7665 | if (PyLong_Check(rep)) { |
| 7666 | Py_ssize_t requiredsize = *outpos+1; |
| 7667 | if (outsize<requiredsize) |
| 7668 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 7669 | Py_DECREF(rep); |
| 7670 | return enc_EXCEPTION; |
| 7671 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7672 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7673 | outstart[(*outpos)++] = (char)PyLong_AS_LONG(rep); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7674 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7675 | else { |
| 7676 | const char *repchars = PyBytes_AS_STRING(rep); |
| 7677 | Py_ssize_t repsize = PyBytes_GET_SIZE(rep); |
| 7678 | Py_ssize_t requiredsize = *outpos+repsize; |
| 7679 | if (outsize<requiredsize) |
| 7680 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 7681 | Py_DECREF(rep); |
| 7682 | return enc_EXCEPTION; |
| 7683 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7684 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7685 | memcpy(outstart + *outpos, repchars, repsize); |
| 7686 | *outpos += repsize; |
| 7687 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7688 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7689 | Py_DECREF(rep); |
| 7690 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7691 | } |
| 7692 | |
| 7693 | /* handle an error in PyUnicode_EncodeCharmap |
| 7694 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7695 | static int |
| 7696 | charmap_encoding_error( |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7697 | 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] | 7698 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 7699 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 7700 | PyObject **res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7701 | { |
| 7702 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7703 | Py_ssize_t repsize; |
| 7704 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7705 | Py_UNICODE *uni2; |
| 7706 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7707 | Py_ssize_t collstartpos = *inpos; |
| 7708 | Py_ssize_t collendpos = *inpos+1; |
| 7709 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7710 | char *encoding = "charmap"; |
| 7711 | char *reason = "character maps to <undefined>"; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7712 | charmapencode_result x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7713 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7714 | /* find all unencodable characters */ |
| 7715 | while (collendpos < size) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7716 | PyObject *rep; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7717 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7718 | int res = encoding_map_lookup(p[collendpos], mapping); |
| 7719 | if (res != -1) |
| 7720 | break; |
| 7721 | ++collendpos; |
| 7722 | continue; |
| 7723 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7724 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7725 | rep = charmapencode_lookup(p[collendpos], mapping); |
| 7726 | if (rep==NULL) |
| 7727 | return -1; |
| 7728 | else if (rep!=Py_None) { |
| 7729 | Py_DECREF(rep); |
| 7730 | break; |
| 7731 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7732 | Py_DECREF(rep); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7733 | ++collendpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7734 | } |
| 7735 | /* cache callback name lookup |
| 7736 | * (if not done yet, i.e. it's the first error) */ |
| 7737 | if (*known_errorHandler==-1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7738 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 7739 | *known_errorHandler = 1; |
| 7740 | else if (!strcmp(errors, "replace")) |
| 7741 | *known_errorHandler = 2; |
| 7742 | else if (!strcmp(errors, "ignore")) |
| 7743 | *known_errorHandler = 3; |
| 7744 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 7745 | *known_errorHandler = 4; |
| 7746 | else |
| 7747 | *known_errorHandler = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7748 | } |
| 7749 | switch (*known_errorHandler) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7750 | case 1: /* strict */ |
| 7751 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 7752 | return -1; |
| 7753 | case 2: /* replace */ |
| 7754 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7755 | x = charmapencode_output('?', mapping, res, respos); |
| 7756 | if (x==enc_EXCEPTION) { |
| 7757 | return -1; |
| 7758 | } |
| 7759 | else if (x==enc_FAILED) { |
| 7760 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 7761 | return -1; |
| 7762 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7763 | } |
| 7764 | /* fall through */ |
| 7765 | case 3: /* ignore */ |
| 7766 | *inpos = collendpos; |
| 7767 | break; |
| 7768 | case 4: /* xmlcharrefreplace */ |
| 7769 | /* generate replacement (temporarily (mis)uses p) */ |
| 7770 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7771 | char buffer[2+29+1+1]; |
| 7772 | char *cp; |
| 7773 | sprintf(buffer, "&#%d;", (int)p[collpos]); |
| 7774 | for (cp = buffer; *cp; ++cp) { |
| 7775 | x = charmapencode_output(*cp, mapping, res, respos); |
| 7776 | if (x==enc_EXCEPTION) |
| 7777 | return -1; |
| 7778 | else if (x==enc_FAILED) { |
| 7779 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 7780 | return -1; |
| 7781 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7782 | } |
| 7783 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7784 | *inpos = collendpos; |
| 7785 | break; |
| 7786 | default: |
| 7787 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7788 | encoding, reason, p, size, exceptionObject, |
| 7789 | collstartpos, collendpos, &newpos); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7790 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7791 | return -1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 7792 | if (PyBytes_Check(repunicode)) { |
| 7793 | /* Directly copy bytes result to output. */ |
| 7794 | Py_ssize_t outsize = PyBytes_Size(*res); |
| 7795 | Py_ssize_t requiredsize; |
| 7796 | repsize = PyBytes_Size(repunicode); |
| 7797 | requiredsize = *respos + repsize; |
| 7798 | if (requiredsize > outsize) |
| 7799 | /* Make room for all additional bytes. */ |
| 7800 | if (charmapencode_resize(res, respos, requiredsize)) { |
| 7801 | Py_DECREF(repunicode); |
| 7802 | return -1; |
| 7803 | } |
| 7804 | memcpy(PyBytes_AsString(*res) + *respos, |
| 7805 | PyBytes_AsString(repunicode), repsize); |
| 7806 | *respos += repsize; |
| 7807 | *inpos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 7808 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 7809 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 7810 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7811 | /* generate replacement */ |
| 7812 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 7813 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7814 | x = charmapencode_output(*uni2, mapping, res, respos); |
| 7815 | if (x==enc_EXCEPTION) { |
| 7816 | return -1; |
| 7817 | } |
| 7818 | else if (x==enc_FAILED) { |
| 7819 | Py_DECREF(repunicode); |
| 7820 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 7821 | return -1; |
| 7822 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7823 | } |
| 7824 | *inpos = newpos; |
| 7825 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7826 | } |
| 7827 | return 0; |
| 7828 | } |
| 7829 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7830 | PyObject * |
| 7831 | PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
| 7832 | Py_ssize_t size, |
| 7833 | PyObject *mapping, |
| 7834 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7835 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7836 | /* output object */ |
| 7837 | PyObject *res = NULL; |
| 7838 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7839 | Py_ssize_t inpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7840 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7841 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7842 | PyObject *errorHandler = NULL; |
| 7843 | PyObject *exc = NULL; |
| 7844 | /* the following variable is used for caching string comparisons |
| 7845 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 7846 | * 3=ignore, 4=xmlcharrefreplace */ |
| 7847 | int known_errorHandler = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7848 | |
| 7849 | /* Default to Latin-1 */ |
| 7850 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7851 | return PyUnicode_EncodeLatin1(p, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7852 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7853 | /* allocate enough for a simple encoding without |
| 7854 | replacements, if we need more, we'll resize */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7855 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7856 | if (res == NULL) |
| 7857 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 7858 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7859 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7860 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7861 | while (inpos<size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7862 | /* try to encode it */ |
| 7863 | charmapencode_result x = charmapencode_output(p[inpos], mapping, &res, &respos); |
| 7864 | if (x==enc_EXCEPTION) /* error */ |
| 7865 | goto onError; |
| 7866 | if (x==enc_FAILED) { /* unencodable character */ |
| 7867 | if (charmap_encoding_error(p, size, &inpos, mapping, |
| 7868 | &exc, |
| 7869 | &known_errorHandler, &errorHandler, errors, |
| 7870 | &res, &respos)) { |
| 7871 | goto onError; |
| 7872 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7873 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7874 | else |
| 7875 | /* done with this character => adjust input position */ |
| 7876 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7877 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7878 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7879 | /* Resize if we allocated to much */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7880 | if (respos<PyBytes_GET_SIZE(res)) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 7881 | if (_PyBytes_Resize(&res, respos) < 0) |
| 7882 | goto onError; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 7883 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7884 | Py_XDECREF(exc); |
| 7885 | Py_XDECREF(errorHandler); |
| 7886 | return res; |
| 7887 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7888 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7889 | Py_XDECREF(res); |
| 7890 | Py_XDECREF(exc); |
| 7891 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7892 | return NULL; |
| 7893 | } |
| 7894 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7895 | PyObject * |
| 7896 | PyUnicode_AsCharmapString(PyObject *unicode, |
| 7897 | PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7898 | { |
| 7899 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7900 | PyErr_BadArgument(); |
| 7901 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7902 | } |
| 7903 | return PyUnicode_EncodeCharmap(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7904 | PyUnicode_GET_SIZE(unicode), |
| 7905 | mapping, |
| 7906 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7907 | } |
| 7908 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7909 | /* create or adjust a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7910 | static void |
| 7911 | make_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7912 | PyObject *unicode, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7913 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 7914 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7915 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7916 | if (*exceptionObject == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7917 | *exceptionObject = _PyUnicodeTranslateError_Create( |
| 7918 | unicode, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7919 | } |
| 7920 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7921 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 7922 | goto onError; |
| 7923 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 7924 | goto onError; |
| 7925 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 7926 | goto onError; |
| 7927 | return; |
| 7928 | onError: |
| 7929 | Py_DECREF(*exceptionObject); |
| 7930 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7931 | } |
| 7932 | } |
| 7933 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7934 | /* raises a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7935 | static void |
| 7936 | raise_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7937 | PyObject *unicode, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7938 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 7939 | const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7940 | { |
| 7941 | make_translate_exception(exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7942 | unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7943 | if (*exceptionObject != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7944 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7945 | } |
| 7946 | |
| 7947 | /* error handling callback helper: |
| 7948 | build arguments, call the callback and check the arguments, |
| 7949 | put the result into newpos and return the replacement string, which |
| 7950 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7951 | static PyObject * |
| 7952 | unicode_translate_call_errorhandler(const char *errors, |
| 7953 | PyObject **errorHandler, |
| 7954 | const char *reason, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7955 | PyObject *unicode, PyObject **exceptionObject, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7956 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 7957 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7958 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 7959 | 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] | 7960 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7961 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7962 | PyObject *restuple; |
| 7963 | PyObject *resunicode; |
| 7964 | |
| 7965 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7966 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7967 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7968 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7969 | } |
| 7970 | |
| 7971 | make_translate_exception(exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7972 | unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7973 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7974 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7975 | |
| 7976 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7977 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7978 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7979 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7980 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 7981 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7982 | Py_DECREF(restuple); |
| 7983 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7984 | } |
| 7985 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7986 | &resunicode, &i_newpos)) { |
| 7987 | Py_DECREF(restuple); |
| 7988 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7989 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7990 | if (i_newpos<0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7991 | *newpos = PyUnicode_GET_LENGTH(unicode)+i_newpos; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7992 | else |
| 7993 | *newpos = i_newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7994 | if (*newpos<0 || *newpos>PyUnicode_GET_LENGTH(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7995 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 7996 | Py_DECREF(restuple); |
| 7997 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 7998 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7999 | Py_INCREF(resunicode); |
| 8000 | Py_DECREF(restuple); |
| 8001 | return resunicode; |
| 8002 | } |
| 8003 | |
| 8004 | /* Lookup the character ch in the mapping and put the result in result, |
| 8005 | which must be decrefed by the caller. |
| 8006 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8007 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8008 | charmaptranslate_lookup(Py_UCS4 c, PyObject *mapping, PyObject **result) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8009 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8010 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8011 | PyObject *x; |
| 8012 | |
| 8013 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8014 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8015 | x = PyObject_GetItem(mapping, w); |
| 8016 | Py_DECREF(w); |
| 8017 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8018 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 8019 | /* No mapping found means: use 1:1 mapping. */ |
| 8020 | PyErr_Clear(); |
| 8021 | *result = NULL; |
| 8022 | return 0; |
| 8023 | } else |
| 8024 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8025 | } |
| 8026 | else if (x == Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8027 | *result = x; |
| 8028 | return 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8029 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8030 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8031 | long value = PyLong_AS_LONG(x); |
| 8032 | long max = PyUnicode_GetMax(); |
| 8033 | if (value < 0 || value > max) { |
| 8034 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5a2f7e60 | 2007-10-24 21:13:09 +0000 | [diff] [blame] | 8035 | "character mapping must be in range(0x%x)", max+1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8036 | Py_DECREF(x); |
| 8037 | return -1; |
| 8038 | } |
| 8039 | *result = x; |
| 8040 | return 0; |
| 8041 | } |
| 8042 | else if (PyUnicode_Check(x)) { |
| 8043 | *result = x; |
| 8044 | return 0; |
| 8045 | } |
| 8046 | else { |
| 8047 | /* wrong return value */ |
| 8048 | PyErr_SetString(PyExc_TypeError, |
| 8049 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8050 | Py_DECREF(x); |
| 8051 | return -1; |
| 8052 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8053 | } |
| 8054 | /* ensure that *outobj is at least requiredsize characters long, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8055 | if not reallocate and adjust various state variables. |
| 8056 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8057 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8058 | charmaptranslate_makespace(Py_UCS4 **outobj, Py_ssize_t *psize, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8059 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8060 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8061 | Py_ssize_t oldsize = *psize; |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 8062 | if (requiredsize > oldsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8063 | /* exponentially overallocate to minimize reallocations */ |
| 8064 | if (requiredsize < 2 * oldsize) |
| 8065 | requiredsize = 2 * oldsize; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8066 | *outobj = PyMem_Realloc(*outobj, requiredsize * sizeof(Py_UCS4)); |
| 8067 | if (*outobj == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8068 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8069 | *psize = requiredsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8070 | } |
| 8071 | return 0; |
| 8072 | } |
| 8073 | /* lookup the character, put the result in the output string and adjust |
| 8074 | various state variables. Return a new reference to the object that |
| 8075 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 8076 | undefined (in which case no character was written). |
| 8077 | The called must decref result. |
| 8078 | Return 0 on success, -1 on error. */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8079 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8080 | charmaptranslate_output(PyObject *input, Py_ssize_t ipos, |
| 8081 | PyObject *mapping, Py_UCS4 **output, |
| 8082 | Py_ssize_t *osize, Py_ssize_t *opos, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8083 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8084 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8085 | Py_UCS4 curinp = PyUnicode_READ_CHAR(input, ipos); |
| 8086 | if (charmaptranslate_lookup(curinp, mapping, res)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8087 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8088 | if (*res==NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8089 | /* not found => default to 1:1 mapping */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8090 | (*output)[(*opos)++] = curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8091 | } |
| 8092 | else if (*res==Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8093 | ; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8094 | else if (PyLong_Check(*res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8095 | /* 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] | 8096 | (*output)[(*opos)++] = (Py_UCS4)PyLong_AS_LONG(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8097 | } |
| 8098 | else if (PyUnicode_Check(*res)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8099 | Py_ssize_t repsize; |
| 8100 | if (PyUnicode_READY(*res) == -1) |
| 8101 | return -1; |
| 8102 | repsize = PyUnicode_GET_LENGTH(*res); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8103 | if (repsize==1) { |
| 8104 | /* 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] | 8105 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8106 | } |
| 8107 | else if (repsize!=0) { |
| 8108 | /* more than one character */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8109 | Py_ssize_t requiredsize = *opos + |
| 8110 | (PyUnicode_GET_LENGTH(input) - ipos) + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8111 | repsize - 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8112 | Py_ssize_t i; |
| 8113 | if (charmaptranslate_makespace(output, osize, requiredsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8114 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8115 | for(i = 0; i < repsize; i++) |
| 8116 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, i); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8117 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8118 | } |
| 8119 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8120 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8121 | return 0; |
| 8122 | } |
| 8123 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8124 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8125 | _PyUnicode_TranslateCharmap(PyObject *input, |
| 8126 | PyObject *mapping, |
| 8127 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8128 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8129 | /* input object */ |
| 8130 | char *idata; |
| 8131 | Py_ssize_t size, i; |
| 8132 | int kind; |
| 8133 | /* output buffer */ |
| 8134 | Py_UCS4 *output = NULL; |
| 8135 | Py_ssize_t osize; |
| 8136 | PyObject *res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8137 | /* current output position */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8138 | Py_ssize_t opos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8139 | char *reason = "character maps to <undefined>"; |
| 8140 | PyObject *errorHandler = NULL; |
| 8141 | PyObject *exc = NULL; |
| 8142 | /* the following variable is used for caching string comparisons |
| 8143 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 8144 | * 3=ignore, 4=xmlcharrefreplace */ |
| 8145 | int known_errorHandler = -1; |
| 8146 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8147 | if (mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8148 | PyErr_BadArgument(); |
| 8149 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8150 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8151 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8152 | if (PyUnicode_READY(input) == -1) |
| 8153 | return NULL; |
| 8154 | idata = (char*)PyUnicode_DATA(input); |
| 8155 | kind = PyUnicode_KIND(input); |
| 8156 | size = PyUnicode_GET_LENGTH(input); |
| 8157 | i = 0; |
| 8158 | |
| 8159 | if (size == 0) { |
| 8160 | Py_INCREF(input); |
| 8161 | return input; |
| 8162 | } |
| 8163 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8164 | /* allocate enough for a simple 1:1 translation without |
| 8165 | replacements, if we need more, we'll resize */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8166 | osize = size; |
| 8167 | output = PyMem_Malloc(osize * sizeof(Py_UCS4)); |
| 8168 | opos = 0; |
| 8169 | if (output == NULL) { |
| 8170 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8171 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8172 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8173 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8174 | while (i<size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8175 | /* try to encode it */ |
| 8176 | PyObject *x = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8177 | if (charmaptranslate_output(input, i, mapping, |
| 8178 | &output, &osize, &opos, &x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8179 | Py_XDECREF(x); |
| 8180 | goto onError; |
| 8181 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8182 | Py_XDECREF(x); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8183 | if (x!=Py_None) /* it worked => adjust input pointer */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8184 | ++i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8185 | else { /* untranslatable character */ |
| 8186 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
| 8187 | Py_ssize_t repsize; |
| 8188 | Py_ssize_t newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8189 | Py_ssize_t uni2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8190 | /* startpos for collecting untranslatable chars */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8191 | Py_ssize_t collstart = i; |
| 8192 | Py_ssize_t collend = i+1; |
| 8193 | Py_ssize_t coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8194 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8195 | /* find all untranslatable characters */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8196 | while (collend < size) { |
| 8197 | if (charmaptranslate_lookup(PyUnicode_READ(kind,idata, collend), mapping, &x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8198 | goto onError; |
| 8199 | Py_XDECREF(x); |
| 8200 | if (x!=Py_None) |
| 8201 | break; |
| 8202 | ++collend; |
| 8203 | } |
| 8204 | /* cache callback name lookup |
| 8205 | * (if not done yet, i.e. it's the first error) */ |
| 8206 | if (known_errorHandler==-1) { |
| 8207 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 8208 | known_errorHandler = 1; |
| 8209 | else if (!strcmp(errors, "replace")) |
| 8210 | known_errorHandler = 2; |
| 8211 | else if (!strcmp(errors, "ignore")) |
| 8212 | known_errorHandler = 3; |
| 8213 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 8214 | known_errorHandler = 4; |
| 8215 | else |
| 8216 | known_errorHandler = 0; |
| 8217 | } |
| 8218 | switch (known_errorHandler) { |
| 8219 | case 1: /* strict */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8220 | raise_translate_exception(&exc, input, collstart, |
| 8221 | collend, reason); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8222 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8223 | case 2: /* replace */ |
| 8224 | /* 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] | 8225 | for (coll = collstart; coll<collend; coll++) |
| 8226 | output[opos++] = '?'; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8227 | /* fall through */ |
| 8228 | case 3: /* ignore */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8229 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8230 | break; |
| 8231 | case 4: /* xmlcharrefreplace */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8232 | /* generate replacement (temporarily (mis)uses i) */ |
| 8233 | for (i = collstart; i < collend; ++i) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8234 | char buffer[2+29+1+1]; |
| 8235 | char *cp; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8236 | sprintf(buffer, "&#%d;", PyUnicode_READ(kind, idata, i)); |
| 8237 | if (charmaptranslate_makespace(&output, &osize, |
| 8238 | opos+strlen(buffer)+(size-collend))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8239 | goto onError; |
| 8240 | for (cp = buffer; *cp; ++cp) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8241 | output[opos++] = *cp; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8242 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8243 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8244 | break; |
| 8245 | default: |
| 8246 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8247 | reason, input, &exc, |
| 8248 | collstart, collend, &newpos); |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 8249 | if (repunicode == NULL || _PyUnicode_READY_REPLACE(&repunicode)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8250 | goto onError; |
| 8251 | /* generate replacement */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8252 | repsize = PyUnicode_GET_LENGTH(repunicode); |
| 8253 | if (charmaptranslate_makespace(&output, &osize, |
| 8254 | opos+repsize+(size-collend))) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8255 | Py_DECREF(repunicode); |
| 8256 | goto onError; |
| 8257 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8258 | for (uni2 = 0; repsize-->0; ++uni2) |
| 8259 | output[opos++] = PyUnicode_READ_CHAR(repunicode, uni2); |
| 8260 | i = newpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8261 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8262 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8263 | } |
| 8264 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8265 | res = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, output, opos); |
| 8266 | if (!res) |
| 8267 | goto onError; |
| 8268 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8269 | Py_XDECREF(exc); |
| 8270 | Py_XDECREF(errorHandler); |
| 8271 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8272 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8273 | onError: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8274 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8275 | Py_XDECREF(exc); |
| 8276 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8277 | return NULL; |
| 8278 | } |
| 8279 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8280 | /* Deprecated. Use PyUnicode_Translate instead. */ |
| 8281 | PyObject * |
| 8282 | PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
| 8283 | Py_ssize_t size, |
| 8284 | PyObject *mapping, |
| 8285 | const char *errors) |
| 8286 | { |
| 8287 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 8288 | if (!unicode) |
| 8289 | return NULL; |
| 8290 | return _PyUnicode_TranslateCharmap(unicode, mapping, errors); |
| 8291 | } |
| 8292 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8293 | PyObject * |
| 8294 | PyUnicode_Translate(PyObject *str, |
| 8295 | PyObject *mapping, |
| 8296 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8297 | { |
| 8298 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8299 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8300 | str = PyUnicode_FromObject(str); |
| 8301 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8302 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8303 | result = _PyUnicode_TranslateCharmap(str, mapping, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8304 | Py_DECREF(str); |
| 8305 | return result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8306 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8307 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8308 | Py_XDECREF(str); |
| 8309 | return NULL; |
| 8310 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8311 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8312 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8313 | fix_decimal_and_space_to_ascii(PyObject *self) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8314 | { |
| 8315 | /* No need to call PyUnicode_READY(self) because this function is only |
| 8316 | called as a callback from fixup() which does it already. */ |
| 8317 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8318 | const int kind = PyUnicode_KIND(self); |
| 8319 | void *data = PyUnicode_DATA(self); |
| 8320 | Py_UCS4 maxchar = 0, ch, fixed; |
| 8321 | Py_ssize_t i; |
| 8322 | |
| 8323 | for (i = 0; i < len; ++i) { |
| 8324 | ch = PyUnicode_READ(kind, data, i); |
| 8325 | fixed = 0; |
| 8326 | if (ch > 127) { |
| 8327 | if (Py_UNICODE_ISSPACE(ch)) |
| 8328 | fixed = ' '; |
| 8329 | else { |
| 8330 | const int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8331 | if (decimal >= 0) |
| 8332 | fixed = '0' + decimal; |
| 8333 | } |
| 8334 | if (fixed != 0) { |
| 8335 | if (fixed > maxchar) |
| 8336 | maxchar = fixed; |
| 8337 | PyUnicode_WRITE(kind, data, i, fixed); |
| 8338 | } |
| 8339 | else if (ch > maxchar) |
| 8340 | maxchar = ch; |
| 8341 | } |
| 8342 | else if (ch > maxchar) |
| 8343 | maxchar = ch; |
| 8344 | } |
| 8345 | |
| 8346 | return maxchar; |
| 8347 | } |
| 8348 | |
| 8349 | PyObject * |
| 8350 | _PyUnicode_TransformDecimalAndSpaceToASCII(PyObject *unicode) |
| 8351 | { |
| 8352 | if (!PyUnicode_Check(unicode)) { |
| 8353 | PyErr_BadInternalCall(); |
| 8354 | return NULL; |
| 8355 | } |
| 8356 | if (PyUnicode_READY(unicode) == -1) |
| 8357 | return NULL; |
| 8358 | if (PyUnicode_MAX_CHAR_VALUE(unicode) <= 127) { |
| 8359 | /* If the string is already ASCII, just return the same string */ |
| 8360 | Py_INCREF(unicode); |
| 8361 | return unicode; |
| 8362 | } |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8363 | return fixup(unicode, fix_decimal_and_space_to_ascii); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8364 | } |
| 8365 | |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8366 | PyObject * |
| 8367 | PyUnicode_TransformDecimalToASCII(Py_UNICODE *s, |
| 8368 | Py_ssize_t length) |
| 8369 | { |
| 8370 | PyObject *result; |
| 8371 | Py_UNICODE *p; /* write pointer into result */ |
| 8372 | Py_ssize_t i; |
| 8373 | /* Copy to a new string */ |
| 8374 | result = (PyObject *)_PyUnicode_New(length); |
| 8375 | Py_UNICODE_COPY(PyUnicode_AS_UNICODE(result), s, length); |
| 8376 | if (result == NULL) |
| 8377 | return result; |
| 8378 | p = PyUnicode_AS_UNICODE(result); |
| 8379 | /* Iterate over code points */ |
| 8380 | for (i = 0; i < length; i++) { |
| 8381 | Py_UNICODE ch =s[i]; |
| 8382 | if (ch > 127) { |
| 8383 | int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8384 | if (decimal >= 0) |
| 8385 | p[i] = '0' + decimal; |
| 8386 | } |
| 8387 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 8388 | #ifndef DONT_MAKE_RESULT_READY |
| 8389 | if (_PyUnicode_READY_REPLACE(&result)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8390 | Py_DECREF(result); |
| 8391 | return NULL; |
| 8392 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 8393 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 8394 | assert(_PyUnicode_CheckConsistency(result, 1)); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8395 | return result; |
| 8396 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8397 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 8398 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8399 | int |
| 8400 | PyUnicode_EncodeDecimal(Py_UNICODE *s, |
| 8401 | Py_ssize_t length, |
| 8402 | char *output, |
| 8403 | const char *errors) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8404 | { |
| 8405 | Py_UNICODE *p, *end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8406 | PyObject *errorHandler = NULL; |
| 8407 | PyObject *exc = NULL; |
| 8408 | const char *encoding = "decimal"; |
| 8409 | const char *reason = "invalid decimal Unicode string"; |
| 8410 | /* the following variable is used for caching string comparisons |
| 8411 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 8412 | int known_errorHandler = -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8413 | |
| 8414 | if (output == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8415 | PyErr_BadArgument(); |
| 8416 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8417 | } |
| 8418 | |
| 8419 | p = s; |
| 8420 | end = s + length; |
| 8421 | while (p < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8422 | register Py_UNICODE ch = *p; |
| 8423 | int decimal; |
| 8424 | PyObject *repunicode; |
| 8425 | Py_ssize_t repsize; |
| 8426 | Py_ssize_t newpos; |
| 8427 | Py_UNICODE *uni2; |
| 8428 | Py_UNICODE *collstart; |
| 8429 | Py_UNICODE *collend; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8430 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8431 | if (Py_UNICODE_ISSPACE(ch)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8432 | *output++ = ' '; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8433 | ++p; |
| 8434 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8435 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8436 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 8437 | if (decimal >= 0) { |
| 8438 | *output++ = '0' + decimal; |
| 8439 | ++p; |
| 8440 | continue; |
| 8441 | } |
| 8442 | if (0 < ch && ch < 256) { |
| 8443 | *output++ = (char)ch; |
| 8444 | ++p; |
| 8445 | continue; |
| 8446 | } |
| 8447 | /* All other characters are considered unencodable */ |
| 8448 | collstart = p; |
| 8449 | collend = p+1; |
| 8450 | while (collend < end) { |
| 8451 | if ((0 < *collend && *collend < 256) || |
| 8452 | !Py_UNICODE_ISSPACE(*collend) || |
| 8453 | Py_UNICODE_TODECIMAL(*collend)) |
| 8454 | break; |
| 8455 | } |
| 8456 | /* cache callback name lookup |
| 8457 | * (if not done yet, i.e. it's the first error) */ |
| 8458 | if (known_errorHandler==-1) { |
| 8459 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 8460 | known_errorHandler = 1; |
| 8461 | else if (!strcmp(errors, "replace")) |
| 8462 | known_errorHandler = 2; |
| 8463 | else if (!strcmp(errors, "ignore")) |
| 8464 | known_errorHandler = 3; |
| 8465 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 8466 | known_errorHandler = 4; |
| 8467 | else |
| 8468 | known_errorHandler = 0; |
| 8469 | } |
| 8470 | switch (known_errorHandler) { |
| 8471 | case 1: /* strict */ |
| 8472 | raise_encode_exception(&exc, encoding, s, length, collstart-s, collend-s, reason); |
| 8473 | goto onError; |
| 8474 | case 2: /* replace */ |
| 8475 | for (p = collstart; p < collend; ++p) |
| 8476 | *output++ = '?'; |
| 8477 | /* fall through */ |
| 8478 | case 3: /* ignore */ |
| 8479 | p = collend; |
| 8480 | break; |
| 8481 | case 4: /* xmlcharrefreplace */ |
| 8482 | /* generate replacement (temporarily (mis)uses p) */ |
| 8483 | for (p = collstart; p < collend; ++p) |
| 8484 | output += sprintf(output, "&#%d;", (int)*p); |
| 8485 | p = collend; |
| 8486 | break; |
| 8487 | default: |
| 8488 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 8489 | encoding, reason, s, length, &exc, |
| 8490 | collstart-s, collend-s, &newpos); |
| 8491 | if (repunicode == NULL) |
| 8492 | goto onError; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8493 | if (!PyUnicode_Check(repunicode)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8494 | /* Byte results not supported, since they have no decimal property. */ |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8495 | PyErr_SetString(PyExc_TypeError, "error handler should return unicode"); |
| 8496 | Py_DECREF(repunicode); |
| 8497 | goto onError; |
| 8498 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8499 | /* generate replacement */ |
| 8500 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 8501 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 8502 | Py_UNICODE ch = *uni2; |
| 8503 | if (Py_UNICODE_ISSPACE(ch)) |
| 8504 | *output++ = ' '; |
| 8505 | else { |
| 8506 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 8507 | if (decimal >= 0) |
| 8508 | *output++ = '0' + decimal; |
| 8509 | else if (0 < ch && ch < 256) |
| 8510 | *output++ = (char)ch; |
| 8511 | else { |
| 8512 | Py_DECREF(repunicode); |
| 8513 | raise_encode_exception(&exc, encoding, |
| 8514 | s, length, collstart-s, collend-s, reason); |
| 8515 | goto onError; |
| 8516 | } |
| 8517 | } |
| 8518 | } |
| 8519 | p = s + newpos; |
| 8520 | Py_DECREF(repunicode); |
| 8521 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8522 | } |
| 8523 | /* 0-terminate the output string */ |
| 8524 | *output++ = '\0'; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8525 | Py_XDECREF(exc); |
| 8526 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8527 | return 0; |
| 8528 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8529 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8530 | Py_XDECREF(exc); |
| 8531 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8532 | return -1; |
| 8533 | } |
| 8534 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8535 | /* --- Helpers ------------------------------------------------------------ */ |
| 8536 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8537 | static Py_ssize_t |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 8538 | any_find_slice(int direction, PyObject* s1, PyObject* s2, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8539 | Py_ssize_t start, |
| 8540 | Py_ssize_t end) |
| 8541 | { |
| 8542 | int kind1, kind2, kind; |
| 8543 | void *buf1, *buf2; |
| 8544 | Py_ssize_t len1, len2, result; |
| 8545 | |
| 8546 | kind1 = PyUnicode_KIND(s1); |
| 8547 | kind2 = PyUnicode_KIND(s2); |
| 8548 | kind = kind1 > kind2 ? kind1 : kind2; |
| 8549 | buf1 = PyUnicode_DATA(s1); |
| 8550 | buf2 = PyUnicode_DATA(s2); |
| 8551 | if (kind1 != kind) |
| 8552 | buf1 = _PyUnicode_AsKind(s1, kind); |
| 8553 | if (!buf1) |
| 8554 | return -2; |
| 8555 | if (kind2 != kind) |
| 8556 | buf2 = _PyUnicode_AsKind(s2, kind); |
| 8557 | if (!buf2) { |
| 8558 | if (kind1 != kind) PyMem_Free(buf1); |
| 8559 | return -2; |
| 8560 | } |
| 8561 | len1 = PyUnicode_GET_LENGTH(s1); |
| 8562 | len2 = PyUnicode_GET_LENGTH(s2); |
| 8563 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 8564 | if (direction > 0) { |
| 8565 | switch(kind) { |
| 8566 | case PyUnicode_1BYTE_KIND: |
| 8567 | if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2)) |
| 8568 | result = asciilib_find_slice(buf1, len1, buf2, len2, start, end); |
| 8569 | else |
| 8570 | result = ucs1lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 8571 | break; |
| 8572 | case PyUnicode_2BYTE_KIND: |
| 8573 | result = ucs2lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 8574 | break; |
| 8575 | case PyUnicode_4BYTE_KIND: |
| 8576 | result = ucs4lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 8577 | break; |
| 8578 | default: |
| 8579 | assert(0); result = -2; |
| 8580 | } |
| 8581 | } |
| 8582 | else { |
| 8583 | switch(kind) { |
| 8584 | case PyUnicode_1BYTE_KIND: |
| 8585 | if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2)) |
| 8586 | result = asciilib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 8587 | else |
| 8588 | result = ucs1lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 8589 | break; |
| 8590 | case PyUnicode_2BYTE_KIND: |
| 8591 | result = ucs2lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 8592 | break; |
| 8593 | case PyUnicode_4BYTE_KIND: |
| 8594 | result = ucs4lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 8595 | break; |
| 8596 | default: |
| 8597 | assert(0); result = -2; |
| 8598 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8599 | } |
| 8600 | |
| 8601 | if (kind1 != kind) |
| 8602 | PyMem_Free(buf1); |
| 8603 | if (kind2 != kind) |
| 8604 | PyMem_Free(buf2); |
| 8605 | |
| 8606 | return result; |
| 8607 | } |
| 8608 | |
| 8609 | Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8610 | _PyUnicode_InsertThousandsGrouping(PyObject *unicode, int kind, void *data, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8611 | Py_ssize_t n_buffer, |
| 8612 | void *digits, Py_ssize_t n_digits, |
| 8613 | Py_ssize_t min_width, |
| 8614 | const char *grouping, |
| 8615 | const char *thousands_sep) |
| 8616 | { |
| 8617 | switch(kind) { |
| 8618 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8619 | if (unicode != NULL && PyUnicode_IS_ASCII(unicode)) |
| 8620 | return _PyUnicode_ascii_InsertThousandsGrouping( |
| 8621 | (Py_UCS1*)data, n_buffer, (Py_UCS1*)digits, n_digits, |
| 8622 | min_width, grouping, thousands_sep); |
| 8623 | else |
| 8624 | return _PyUnicode_ucs1_InsertThousandsGrouping( |
| 8625 | (Py_UCS1*)data, n_buffer, (Py_UCS1*)digits, n_digits, |
| 8626 | min_width, grouping, thousands_sep); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8627 | case PyUnicode_2BYTE_KIND: |
| 8628 | return _PyUnicode_ucs2_InsertThousandsGrouping( |
| 8629 | (Py_UCS2*)data, n_buffer, (Py_UCS2*)digits, n_digits, |
| 8630 | min_width, grouping, thousands_sep); |
| 8631 | case PyUnicode_4BYTE_KIND: |
| 8632 | return _PyUnicode_ucs4_InsertThousandsGrouping( |
| 8633 | (Py_UCS4*)data, n_buffer, (Py_UCS4*)digits, n_digits, |
| 8634 | min_width, grouping, thousands_sep); |
| 8635 | } |
| 8636 | assert(0); |
| 8637 | return -1; |
| 8638 | } |
| 8639 | |
| 8640 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 8641 | #include "stringlib/unicodedefs.h" |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8642 | #include "stringlib/fastsearch.h" |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 8643 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8644 | #include "stringlib/count.h" |
| 8645 | #include "stringlib/find.h" |
Eric Smith | 5807c41 | 2008-05-11 21:00:57 +0000 | [diff] [blame] | 8646 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8647 | /* helper macro to fixup start/end slice values */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 8648 | #define ADJUST_INDICES(start, end, len) \ |
| 8649 | if (end > len) \ |
| 8650 | end = len; \ |
| 8651 | else if (end < 0) { \ |
| 8652 | end += len; \ |
| 8653 | if (end < 0) \ |
| 8654 | end = 0; \ |
| 8655 | } \ |
| 8656 | if (start < 0) { \ |
| 8657 | start += len; \ |
| 8658 | if (start < 0) \ |
| 8659 | start = 0; \ |
| 8660 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8661 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8662 | Py_ssize_t |
| 8663 | PyUnicode_Count(PyObject *str, |
| 8664 | PyObject *substr, |
| 8665 | Py_ssize_t start, |
| 8666 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8667 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8668 | Py_ssize_t result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8669 | PyUnicodeObject* str_obj; |
| 8670 | PyUnicodeObject* sub_obj; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8671 | int kind1, kind2, kind; |
| 8672 | void *buf1 = NULL, *buf2 = NULL; |
| 8673 | Py_ssize_t len1, len2; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8674 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8675 | str_obj = (PyUnicodeObject*) PyUnicode_FromObject(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8676 | if (!str_obj || PyUnicode_READY(str_obj) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8677 | return -1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8678 | sub_obj = (PyUnicodeObject*) PyUnicode_FromObject(substr); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 8679 | if (!sub_obj || PyUnicode_READY(sub_obj) == -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8680 | Py_DECREF(str_obj); |
| 8681 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8682 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8683 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8684 | kind1 = PyUnicode_KIND(str_obj); |
| 8685 | kind2 = PyUnicode_KIND(sub_obj); |
| 8686 | kind = kind1 > kind2 ? kind1 : kind2; |
| 8687 | buf1 = PyUnicode_DATA(str_obj); |
| 8688 | if (kind1 != kind) |
| 8689 | buf1 = _PyUnicode_AsKind((PyObject*)str_obj, kind); |
| 8690 | if (!buf1) |
| 8691 | goto onError; |
| 8692 | buf2 = PyUnicode_DATA(sub_obj); |
| 8693 | if (kind2 != kind) |
| 8694 | buf2 = _PyUnicode_AsKind((PyObject*)sub_obj, kind); |
| 8695 | if (!buf2) |
| 8696 | goto onError; |
| 8697 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 8698 | len2 = PyUnicode_GET_LENGTH(sub_obj); |
| 8699 | |
| 8700 | ADJUST_INDICES(start, end, len1); |
| 8701 | switch(kind) { |
| 8702 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8703 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sub_obj)) |
| 8704 | result = asciilib_count( |
| 8705 | ((Py_UCS1*)buf1) + start, end - start, |
| 8706 | buf2, len2, PY_SSIZE_T_MAX |
| 8707 | ); |
| 8708 | else |
| 8709 | result = ucs1lib_count( |
| 8710 | ((Py_UCS1*)buf1) + start, end - start, |
| 8711 | buf2, len2, PY_SSIZE_T_MAX |
| 8712 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8713 | break; |
| 8714 | case PyUnicode_2BYTE_KIND: |
| 8715 | result = ucs2lib_count( |
| 8716 | ((Py_UCS2*)buf1) + start, end - start, |
| 8717 | buf2, len2, PY_SSIZE_T_MAX |
| 8718 | ); |
| 8719 | break; |
| 8720 | case PyUnicode_4BYTE_KIND: |
| 8721 | result = ucs4lib_count( |
| 8722 | ((Py_UCS4*)buf1) + start, end - start, |
| 8723 | buf2, len2, PY_SSIZE_T_MAX |
| 8724 | ); |
| 8725 | break; |
| 8726 | default: |
| 8727 | assert(0); result = 0; |
| 8728 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8729 | |
| 8730 | Py_DECREF(sub_obj); |
| 8731 | Py_DECREF(str_obj); |
| 8732 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8733 | if (kind1 != kind) |
| 8734 | PyMem_Free(buf1); |
| 8735 | if (kind2 != kind) |
| 8736 | PyMem_Free(buf2); |
| 8737 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8738 | return result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8739 | onError: |
| 8740 | Py_DECREF(sub_obj); |
| 8741 | Py_DECREF(str_obj); |
| 8742 | if (kind1 != kind && buf1) |
| 8743 | PyMem_Free(buf1); |
| 8744 | if (kind2 != kind && buf2) |
| 8745 | PyMem_Free(buf2); |
| 8746 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8747 | } |
| 8748 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8749 | Py_ssize_t |
| 8750 | PyUnicode_Find(PyObject *str, |
| 8751 | PyObject *sub, |
| 8752 | Py_ssize_t start, |
| 8753 | Py_ssize_t end, |
| 8754 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8755 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8756 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8757 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8758 | str = PyUnicode_FromObject(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8759 | if (!str || PyUnicode_READY(str) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8760 | return -2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8761 | sub = PyUnicode_FromObject(sub); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8762 | if (!sub || PyUnicode_READY(sub) == -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8763 | Py_DECREF(str); |
| 8764 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8765 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8766 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 8767 | result = any_find_slice(direction, |
| 8768 | str, sub, start, end |
| 8769 | ); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8770 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8771 | Py_DECREF(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8772 | Py_DECREF(sub); |
| 8773 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8774 | return result; |
| 8775 | } |
| 8776 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8777 | Py_ssize_t |
| 8778 | PyUnicode_FindChar(PyObject *str, Py_UCS4 ch, |
| 8779 | Py_ssize_t start, Py_ssize_t end, |
| 8780 | int direction) |
| 8781 | { |
| 8782 | char *result; |
| 8783 | int kind; |
| 8784 | if (PyUnicode_READY(str) == -1) |
| 8785 | return -2; |
Victor Stinner | 267aa24 | 2011-10-02 01:08:37 +0200 | [diff] [blame] | 8786 | if (start < 0 || end < 0) { |
| 8787 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 8788 | return -2; |
| 8789 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8790 | if (end > PyUnicode_GET_LENGTH(str)) |
| 8791 | end = PyUnicode_GET_LENGTH(str); |
| 8792 | kind = PyUnicode_KIND(str); |
| 8793 | result = findchar(PyUnicode_1BYTE_DATA(str) |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 8794 | + kind*start, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8795 | kind, |
| 8796 | end-start, ch, direction); |
| 8797 | if (!result) |
| 8798 | return -1; |
| 8799 | return (result-(char*)PyUnicode_DATA(str)) >> (kind-1); |
| 8800 | } |
| 8801 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8802 | static int |
| 8803 | tailmatch(PyUnicodeObject *self, |
| 8804 | PyUnicodeObject *substring, |
| 8805 | Py_ssize_t start, |
| 8806 | Py_ssize_t end, |
| 8807 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8808 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8809 | int kind_self; |
| 8810 | int kind_sub; |
| 8811 | void *data_self; |
| 8812 | void *data_sub; |
| 8813 | Py_ssize_t offset; |
| 8814 | Py_ssize_t i; |
| 8815 | Py_ssize_t end_sub; |
| 8816 | |
| 8817 | if (PyUnicode_READY(self) == -1 || |
| 8818 | PyUnicode_READY(substring) == -1) |
| 8819 | return 0; |
| 8820 | |
| 8821 | if (PyUnicode_GET_LENGTH(substring) == 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8822 | return 1; |
| 8823 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8824 | ADJUST_INDICES(start, end, PyUnicode_GET_LENGTH(self)); |
| 8825 | end -= PyUnicode_GET_LENGTH(substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8826 | if (end < start) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8827 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8828 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8829 | kind_self = PyUnicode_KIND(self); |
| 8830 | data_self = PyUnicode_DATA(self); |
| 8831 | kind_sub = PyUnicode_KIND(substring); |
| 8832 | data_sub = PyUnicode_DATA(substring); |
| 8833 | end_sub = PyUnicode_GET_LENGTH(substring) - 1; |
| 8834 | |
| 8835 | if (direction > 0) |
| 8836 | offset = end; |
| 8837 | else |
| 8838 | offset = start; |
| 8839 | |
| 8840 | if (PyUnicode_READ(kind_self, data_self, offset) == |
| 8841 | PyUnicode_READ(kind_sub, data_sub, 0) && |
| 8842 | PyUnicode_READ(kind_self, data_self, offset + end_sub) == |
| 8843 | PyUnicode_READ(kind_sub, data_sub, end_sub)) { |
| 8844 | /* If both are of the same kind, memcmp is sufficient */ |
| 8845 | if (kind_self == kind_sub) { |
| 8846 | return ! memcmp((char *)data_self + |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 8847 | (offset * PyUnicode_KIND(substring)), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8848 | data_sub, |
| 8849 | PyUnicode_GET_LENGTH(substring) * |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 8850 | PyUnicode_KIND(substring)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8851 | } |
| 8852 | /* otherwise we have to compare each character by first accesing it */ |
| 8853 | else { |
| 8854 | /* We do not need to compare 0 and len(substring)-1 because |
| 8855 | the if statement above ensured already that they are equal |
| 8856 | when we end up here. */ |
| 8857 | // TODO: honor direction and do a forward or backwards search |
| 8858 | for (i = 1; i < end_sub; ++i) { |
| 8859 | if (PyUnicode_READ(kind_self, data_self, offset + i) != |
| 8860 | PyUnicode_READ(kind_sub, data_sub, i)) |
| 8861 | return 0; |
| 8862 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8863 | return 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8864 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8865 | } |
| 8866 | |
| 8867 | return 0; |
| 8868 | } |
| 8869 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8870 | Py_ssize_t |
| 8871 | PyUnicode_Tailmatch(PyObject *str, |
| 8872 | PyObject *substr, |
| 8873 | Py_ssize_t start, |
| 8874 | Py_ssize_t end, |
| 8875 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8876 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8877 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8878 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8879 | str = PyUnicode_FromObject(str); |
| 8880 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8881 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8882 | substr = PyUnicode_FromObject(substr); |
| 8883 | if (substr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8884 | Py_DECREF(str); |
| 8885 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8886 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8887 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8888 | result = tailmatch((PyUnicodeObject *)str, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8889 | (PyUnicodeObject *)substr, |
| 8890 | start, end, direction); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8891 | Py_DECREF(str); |
| 8892 | Py_DECREF(substr); |
| 8893 | return result; |
| 8894 | } |
| 8895 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8896 | /* Apply fixfct filter to the Unicode object self and return a |
| 8897 | reference to the modified object */ |
| 8898 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8899 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8900 | fixup(PyObject *self, |
| 8901 | Py_UCS4 (*fixfct)(PyObject *s)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8902 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8903 | PyObject *u; |
| 8904 | Py_UCS4 maxchar_old, maxchar_new = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8905 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8906 | if (PyUnicode_READY(self) == -1) |
| 8907 | return NULL; |
| 8908 | maxchar_old = PyUnicode_MAX_CHAR_VALUE(self); |
| 8909 | u = PyUnicode_New(PyUnicode_GET_LENGTH(self), |
| 8910 | maxchar_old); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8911 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8912 | return NULL; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8913 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8914 | Py_MEMCPY(PyUnicode_1BYTE_DATA(u), PyUnicode_1BYTE_DATA(self), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 8915 | PyUnicode_GET_LENGTH(u) * PyUnicode_KIND(u)); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8916 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8917 | /* fix functions return the new maximum character in a string, |
| 8918 | if the kind of the resulting unicode object does not change, |
| 8919 | everything is fine. Otherwise we need to change the string kind |
| 8920 | and re-run the fix function. */ |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8921 | maxchar_new = fixfct(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8922 | if (maxchar_new == 0) |
| 8923 | /* do nothing, keep maxchar_new at 0 which means no changes. */; |
| 8924 | else if (maxchar_new <= 127) |
| 8925 | maxchar_new = 127; |
| 8926 | else if (maxchar_new <= 255) |
| 8927 | maxchar_new = 255; |
| 8928 | else if (maxchar_new <= 65535) |
| 8929 | maxchar_new = 65535; |
| 8930 | else |
| 8931 | maxchar_new = 1114111; /* 0x10ffff */ |
| 8932 | |
| 8933 | if (!maxchar_new && PyUnicode_CheckExact(self)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8934 | /* fixfct should return TRUE if it modified the buffer. If |
| 8935 | FALSE, return a reference to the original buffer instead |
| 8936 | (to save space, not time) */ |
| 8937 | Py_INCREF(self); |
| 8938 | Py_DECREF(u); |
| 8939 | return (PyObject*) self; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8940 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8941 | else if (maxchar_new == maxchar_old) { |
| 8942 | return u; |
| 8943 | } |
| 8944 | else { |
| 8945 | /* In case the maximum character changed, we need to |
| 8946 | convert the string to the new category. */ |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 8947 | PyObject *v = PyUnicode_New(PyUnicode_GET_LENGTH(self), maxchar_new); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8948 | if (v == NULL) { |
| 8949 | Py_DECREF(u); |
| 8950 | return NULL; |
| 8951 | } |
| 8952 | if (maxchar_new > maxchar_old) { |
| 8953 | /* If the maxchar increased so that the kind changed, not all |
| 8954 | characters are representable anymore and we need to fix the |
| 8955 | string again. This only happens in very few cases. */ |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 8956 | copy_characters(v, 0, self, 0, PyUnicode_GET_LENGTH(self)); |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8957 | maxchar_old = fixfct(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8958 | assert(maxchar_old > 0 && maxchar_old <= maxchar_new); |
| 8959 | } |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 8960 | else { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 8961 | copy_characters(v, 0, u, 0, PyUnicode_GET_LENGTH(self)); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 8962 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8963 | |
| 8964 | Py_DECREF(u); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 8965 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8966 | return v; |
| 8967 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8968 | } |
| 8969 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8970 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8971 | fixupper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8972 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8973 | /* No need to call PyUnicode_READY(self) because this function is only |
| 8974 | called as a callback from fixup() which does it already. */ |
| 8975 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8976 | const int kind = PyUnicode_KIND(self); |
| 8977 | void *data = PyUnicode_DATA(self); |
| 8978 | int touched = 0; |
| 8979 | Py_UCS4 maxchar = 0; |
| 8980 | Py_ssize_t i; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8981 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8982 | for (i = 0; i < len; ++i) { |
| 8983 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 8984 | const Py_UCS4 up = Py_UNICODE_TOUPPER(ch); |
| 8985 | if (up != ch) { |
| 8986 | if (up > maxchar) |
| 8987 | maxchar = up; |
| 8988 | PyUnicode_WRITE(kind, data, i, up); |
| 8989 | touched = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8990 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8991 | else if (ch > maxchar) |
| 8992 | maxchar = ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8993 | } |
| 8994 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8995 | if (touched) |
| 8996 | return maxchar; |
| 8997 | else |
| 8998 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8999 | } |
| 9000 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9001 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9002 | fixlower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9003 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9004 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 9005 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9006 | const int kind = PyUnicode_KIND(self); |
| 9007 | void *data = PyUnicode_DATA(self); |
| 9008 | int touched = 0; |
| 9009 | Py_UCS4 maxchar = 0; |
| 9010 | Py_ssize_t i; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9011 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9012 | for(i = 0; i < len; ++i) { |
| 9013 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 9014 | const Py_UCS4 lo = Py_UNICODE_TOLOWER(ch); |
| 9015 | if (lo != ch) { |
| 9016 | if (lo > maxchar) |
| 9017 | maxchar = lo; |
| 9018 | PyUnicode_WRITE(kind, data, i, lo); |
| 9019 | touched = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9020 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9021 | else if (ch > maxchar) |
| 9022 | maxchar = ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9023 | } |
| 9024 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9025 | if (touched) |
| 9026 | return maxchar; |
| 9027 | else |
| 9028 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9029 | } |
| 9030 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9031 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9032 | fixswapcase(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9033 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9034 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 9035 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9036 | const int kind = PyUnicode_KIND(self); |
| 9037 | void *data = PyUnicode_DATA(self); |
| 9038 | int touched = 0; |
| 9039 | Py_UCS4 maxchar = 0; |
| 9040 | Py_ssize_t i; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9041 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9042 | for(i = 0; i < len; ++i) { |
| 9043 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 9044 | Py_UCS4 nu = 0; |
| 9045 | |
| 9046 | if (Py_UNICODE_ISUPPER(ch)) |
| 9047 | nu = Py_UNICODE_TOLOWER(ch); |
| 9048 | else if (Py_UNICODE_ISLOWER(ch)) |
| 9049 | nu = Py_UNICODE_TOUPPER(ch); |
| 9050 | |
| 9051 | if (nu != 0) { |
| 9052 | if (nu > maxchar) |
| 9053 | maxchar = nu; |
| 9054 | PyUnicode_WRITE(kind, data, i, nu); |
| 9055 | touched = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9056 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9057 | else if (ch > maxchar) |
| 9058 | maxchar = ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9059 | } |
| 9060 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9061 | if (touched) |
| 9062 | return maxchar; |
| 9063 | else |
| 9064 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9065 | } |
| 9066 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9067 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9068 | fixcapitalize(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9069 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9070 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 9071 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9072 | const int kind = PyUnicode_KIND(self); |
| 9073 | void *data = PyUnicode_DATA(self); |
| 9074 | int touched = 0; |
| 9075 | Py_UCS4 maxchar = 0; |
| 9076 | Py_ssize_t i = 0; |
| 9077 | Py_UCS4 ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9078 | |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9079 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9080 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9081 | |
| 9082 | ch = PyUnicode_READ(kind, data, i); |
| 9083 | if (!Py_UNICODE_ISUPPER(ch)) { |
| 9084 | maxchar = Py_UNICODE_TOUPPER(ch); |
| 9085 | PyUnicode_WRITE(kind, data, i, maxchar); |
| 9086 | touched = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9087 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9088 | ++i; |
| 9089 | for(; i < len; ++i) { |
| 9090 | ch = PyUnicode_READ(kind, data, i); |
| 9091 | if (!Py_UNICODE_ISLOWER(ch)) { |
| 9092 | const Py_UCS4 lo = Py_UNICODE_TOLOWER(ch); |
| 9093 | if (lo > maxchar) |
| 9094 | maxchar = lo; |
| 9095 | PyUnicode_WRITE(kind, data, i, lo); |
| 9096 | touched = 1; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9097 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9098 | else if (ch > maxchar) |
| 9099 | maxchar = ch; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9100 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9101 | |
| 9102 | if (touched) |
| 9103 | return maxchar; |
| 9104 | else |
| 9105 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9106 | } |
| 9107 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9108 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9109 | fixtitle(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9110 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9111 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 9112 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9113 | const int kind = PyUnicode_KIND(self); |
| 9114 | void *data = PyUnicode_DATA(self); |
| 9115 | Py_UCS4 maxchar = 0; |
| 9116 | Py_ssize_t i = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9117 | int previous_is_cased; |
| 9118 | |
| 9119 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9120 | if (len == 1) { |
| 9121 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 9122 | const Py_UCS4 ti = Py_UNICODE_TOTITLE(ch); |
| 9123 | if (ti != ch) { |
| 9124 | PyUnicode_WRITE(kind, data, i, ti); |
| 9125 | return ti; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9126 | } |
| 9127 | else |
| 9128 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9129 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9130 | previous_is_cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9131 | for(; i < len; ++i) { |
| 9132 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 9133 | Py_UCS4 nu; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9134 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9135 | if (previous_is_cased) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9136 | nu = Py_UNICODE_TOLOWER(ch); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9137 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9138 | nu = Py_UNICODE_TOTITLE(ch); |
| 9139 | |
| 9140 | if (nu > maxchar) |
| 9141 | maxchar = nu; |
| 9142 | PyUnicode_WRITE(kind, data, i, nu); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9143 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9144 | if (Py_UNICODE_ISLOWER(ch) || |
| 9145 | Py_UNICODE_ISUPPER(ch) || |
| 9146 | Py_UNICODE_ISTITLE(ch)) |
| 9147 | previous_is_cased = 1; |
| 9148 | else |
| 9149 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9150 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9151 | return maxchar; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9152 | } |
| 9153 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9154 | PyObject * |
| 9155 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9156 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9157 | PyObject *sep = NULL; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9158 | Py_ssize_t seplen; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9159 | PyObject *res = NULL; /* the result */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9160 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9161 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
| 9162 | PyObject **items; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9163 | PyObject *item; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9164 | Py_ssize_t sz, i, res_offset; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9165 | Py_UCS4 maxchar; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9166 | Py_UCS4 item_maxchar; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9167 | int use_memcpy; |
| 9168 | unsigned char *res_data = NULL, *sep_data = NULL; |
| 9169 | PyObject *last_obj; |
| 9170 | unsigned int kind = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9171 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9172 | fseq = PySequence_Fast(seq, ""); |
| 9173 | if (fseq == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9174 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9175 | } |
| 9176 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9177 | /* NOTE: the following code can't call back into Python code, |
| 9178 | * so we are sure that fseq won't be mutated. |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9179 | */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9180 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9181 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 9182 | /* If empty sequence, return u"". */ |
| 9183 | if (seqlen == 0) { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9184 | Py_DECREF(fseq); |
| 9185 | Py_INCREF(unicode_empty); |
| 9186 | res = unicode_empty; |
| 9187 | return res; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9188 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9189 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9190 | /* If singleton sequence with an exact Unicode, return that. */ |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9191 | last_obj = NULL; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9192 | items = PySequence_Fast_ITEMS(fseq); |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9193 | if (seqlen == 1) { |
| 9194 | if (PyUnicode_CheckExact(items[0])) { |
| 9195 | res = items[0]; |
| 9196 | Py_INCREF(res); |
| 9197 | Py_DECREF(fseq); |
| 9198 | return res; |
| 9199 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9200 | seplen = 0; |
Victor Stinner | c6f0df7 | 2011-10-06 15:58:54 +0200 | [diff] [blame] | 9201 | maxchar = 0; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9202 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9203 | else { |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9204 | /* Set up sep and seplen */ |
| 9205 | if (separator == NULL) { |
| 9206 | /* fall back to a blank space separator */ |
| 9207 | sep = PyUnicode_FromOrdinal(' '); |
| 9208 | if (!sep) |
| 9209 | goto onError; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9210 | seplen = 1; |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9211 | maxchar = 32; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9212 | } |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9213 | else { |
| 9214 | if (!PyUnicode_Check(separator)) { |
| 9215 | PyErr_Format(PyExc_TypeError, |
| 9216 | "separator: expected str instance," |
| 9217 | " %.80s found", |
| 9218 | Py_TYPE(separator)->tp_name); |
| 9219 | goto onError; |
| 9220 | } |
| 9221 | if (PyUnicode_READY(separator)) |
| 9222 | goto onError; |
| 9223 | sep = separator; |
| 9224 | seplen = PyUnicode_GET_LENGTH(separator); |
| 9225 | maxchar = PyUnicode_MAX_CHAR_VALUE(separator); |
| 9226 | /* inc refcount to keep this code path symmetric with the |
| 9227 | above case of a blank separator */ |
| 9228 | Py_INCREF(sep); |
| 9229 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9230 | last_obj = sep; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9231 | } |
| 9232 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9233 | /* There are at least two things to join, or else we have a subclass |
| 9234 | * of str in the sequence. |
| 9235 | * Do a pre-pass to figure out the total amount of space we'll |
| 9236 | * need (sz), and see whether all argument are strings. |
| 9237 | */ |
| 9238 | sz = 0; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9239 | #ifdef Py_DEBUG |
| 9240 | use_memcpy = 0; |
| 9241 | #else |
| 9242 | use_memcpy = 1; |
| 9243 | #endif |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9244 | for (i = 0; i < seqlen; i++) { |
| 9245 | const Py_ssize_t old_sz = sz; |
| 9246 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9247 | if (!PyUnicode_Check(item)) { |
| 9248 | PyErr_Format(PyExc_TypeError, |
| 9249 | "sequence item %zd: expected str instance," |
| 9250 | " %.80s found", |
| 9251 | i, Py_TYPE(item)->tp_name); |
| 9252 | goto onError; |
| 9253 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9254 | if (PyUnicode_READY(item) == -1) |
| 9255 | goto onError; |
| 9256 | sz += PyUnicode_GET_LENGTH(item); |
| 9257 | item_maxchar = PyUnicode_MAX_CHAR_VALUE(item); |
Victor Stinner | c6f0df7 | 2011-10-06 15:58:54 +0200 | [diff] [blame] | 9258 | maxchar = Py_MAX(maxchar, item_maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9259 | if (i != 0) |
| 9260 | sz += seplen; |
| 9261 | if (sz < old_sz || sz > PY_SSIZE_T_MAX) { |
| 9262 | PyErr_SetString(PyExc_OverflowError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9263 | "join() result is too long for a Python string"); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9264 | goto onError; |
| 9265 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9266 | if (use_memcpy && last_obj != NULL) { |
| 9267 | if (PyUnicode_KIND(last_obj) != PyUnicode_KIND(item)) |
| 9268 | use_memcpy = 0; |
| 9269 | } |
| 9270 | last_obj = item; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9271 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9272 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9273 | res = PyUnicode_New(sz, maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9274 | if (res == NULL) |
| 9275 | goto onError; |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9276 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9277 | /* Catenate everything. */ |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9278 | #ifdef Py_DEBUG |
| 9279 | use_memcpy = 0; |
| 9280 | #else |
| 9281 | if (use_memcpy) { |
| 9282 | res_data = PyUnicode_1BYTE_DATA(res); |
| 9283 | kind = PyUnicode_KIND(res); |
| 9284 | if (seplen != 0) |
| 9285 | sep_data = PyUnicode_1BYTE_DATA(sep); |
| 9286 | } |
| 9287 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9288 | for (i = 0, res_offset = 0; i < seqlen; ++i) { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9289 | Py_ssize_t itemlen; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9290 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9291 | /* Copy item, and maybe the separator. */ |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9292 | if (i && seplen != 0) { |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9293 | if (use_memcpy) { |
| 9294 | Py_MEMCPY(res_data, |
| 9295 | sep_data, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9296 | kind * seplen); |
| 9297 | res_data += kind * seplen; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9298 | } |
| 9299 | else { |
| 9300 | copy_characters(res, res_offset, sep, 0, seplen); |
| 9301 | res_offset += seplen; |
| 9302 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9303 | } |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9304 | itemlen = PyUnicode_GET_LENGTH(item); |
| 9305 | if (itemlen != 0) { |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9306 | if (use_memcpy) { |
| 9307 | Py_MEMCPY(res_data, |
| 9308 | PyUnicode_DATA(item), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9309 | kind * itemlen); |
| 9310 | res_data += kind * itemlen; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9311 | } |
| 9312 | else { |
| 9313 | copy_characters(res, res_offset, item, 0, itemlen); |
| 9314 | res_offset += itemlen; |
| 9315 | } |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9316 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9317 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9318 | if (use_memcpy) |
| 9319 | assert(res_data == PyUnicode_1BYTE_DATA(res) |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9320 | + kind * PyUnicode_GET_LENGTH(res)); |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9321 | else |
| 9322 | assert(res_offset == PyUnicode_GET_LENGTH(res)); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9323 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9324 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9325 | Py_XDECREF(sep); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9326 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9327 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9328 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9329 | onError: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9330 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9331 | Py_XDECREF(sep); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9332 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9333 | return NULL; |
| 9334 | } |
| 9335 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9336 | #define FILL(kind, data, value, start, length) \ |
| 9337 | do { \ |
| 9338 | Py_ssize_t i_ = 0; \ |
| 9339 | assert(kind != PyUnicode_WCHAR_KIND); \ |
| 9340 | switch ((kind)) { \ |
| 9341 | case PyUnicode_1BYTE_KIND: { \ |
| 9342 | unsigned char * to_ = (unsigned char *)((data)) + (start); \ |
| 9343 | memset(to_, (unsigned char)value, length); \ |
| 9344 | break; \ |
| 9345 | } \ |
| 9346 | case PyUnicode_2BYTE_KIND: { \ |
| 9347 | Py_UCS2 * to_ = (Py_UCS2 *)((data)) + (start); \ |
| 9348 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 9349 | break; \ |
| 9350 | } \ |
| 9351 | default: { \ |
| 9352 | Py_UCS4 * to_ = (Py_UCS4 *)((data)) + (start); \ |
| 9353 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 9354 | break; \ |
| 9355 | } \ |
| 9356 | } \ |
| 9357 | } while (0) |
| 9358 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9359 | static PyObject * |
| 9360 | pad(PyObject *self, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9361 | Py_ssize_t left, |
| 9362 | Py_ssize_t right, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9363 | Py_UCS4 fill) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9364 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9365 | PyObject *u; |
| 9366 | Py_UCS4 maxchar; |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9367 | int kind; |
| 9368 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9369 | |
| 9370 | if (left < 0) |
| 9371 | left = 0; |
| 9372 | if (right < 0) |
| 9373 | right = 0; |
| 9374 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 9375 | if (left == 0 && right == 0 && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9376 | Py_INCREF(self); |
| 9377 | return self; |
| 9378 | } |
| 9379 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9380 | if (left > PY_SSIZE_T_MAX - _PyUnicode_LENGTH(self) || |
| 9381 | right > PY_SSIZE_T_MAX - (left + _PyUnicode_LENGTH(self))) { |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 9382 | PyErr_SetString(PyExc_OverflowError, "padded string is too long"); |
| 9383 | return NULL; |
| 9384 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9385 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
| 9386 | if (fill > maxchar) |
| 9387 | maxchar = fill; |
| 9388 | u = PyUnicode_New(left + _PyUnicode_LENGTH(self) + right, maxchar); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9389 | if (!u) |
| 9390 | return NULL; |
| 9391 | |
| 9392 | kind = PyUnicode_KIND(u); |
| 9393 | data = PyUnicode_DATA(u); |
| 9394 | if (left) |
| 9395 | FILL(kind, data, fill, 0, left); |
| 9396 | if (right) |
| 9397 | FILL(kind, data, fill, left + _PyUnicode_LENGTH(self), right); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9398 | copy_characters(u, left, self, 0, _PyUnicode_LENGTH(self)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9399 | assert(_PyUnicode_CheckConsistency(u, 1)); |
| 9400 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9401 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9402 | #undef FILL |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9403 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9404 | PyObject * |
| 9405 | PyUnicode_Splitlines(PyObject *string, int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9406 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9407 | PyObject *list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9408 | |
| 9409 | string = PyUnicode_FromObject(string); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9410 | if (string == NULL || PyUnicode_READY(string) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9411 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9412 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9413 | switch(PyUnicode_KIND(string)) { |
| 9414 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9415 | if (PyUnicode_IS_ASCII(string)) |
| 9416 | list = asciilib_splitlines( |
| 9417 | (PyObject*) string, PyUnicode_1BYTE_DATA(string), |
| 9418 | PyUnicode_GET_LENGTH(string), keepends); |
| 9419 | else |
| 9420 | list = ucs1lib_splitlines( |
| 9421 | (PyObject*) string, PyUnicode_1BYTE_DATA(string), |
| 9422 | PyUnicode_GET_LENGTH(string), keepends); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9423 | break; |
| 9424 | case PyUnicode_2BYTE_KIND: |
| 9425 | list = ucs2lib_splitlines( |
| 9426 | (PyObject*) string, PyUnicode_2BYTE_DATA(string), |
| 9427 | PyUnicode_GET_LENGTH(string), keepends); |
| 9428 | break; |
| 9429 | case PyUnicode_4BYTE_KIND: |
| 9430 | list = ucs4lib_splitlines( |
| 9431 | (PyObject*) string, PyUnicode_4BYTE_DATA(string), |
| 9432 | PyUnicode_GET_LENGTH(string), keepends); |
| 9433 | break; |
| 9434 | default: |
| 9435 | assert(0); |
| 9436 | list = 0; |
| 9437 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9438 | Py_DECREF(string); |
| 9439 | return list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9440 | } |
| 9441 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9442 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9443 | split(PyObject *self, |
| 9444 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9445 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9446 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9447 | int kind1, kind2, kind; |
| 9448 | void *buf1, *buf2; |
| 9449 | Py_ssize_t len1, len2; |
| 9450 | PyObject* out; |
| 9451 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9452 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9453 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9454 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9455 | if (PyUnicode_READY(self) == -1) |
| 9456 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9457 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9458 | if (substring == NULL) |
| 9459 | switch(PyUnicode_KIND(self)) { |
| 9460 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9461 | if (PyUnicode_IS_ASCII(self)) |
| 9462 | return asciilib_split_whitespace( |
| 9463 | (PyObject*) self, PyUnicode_1BYTE_DATA(self), |
| 9464 | PyUnicode_GET_LENGTH(self), maxcount |
| 9465 | ); |
| 9466 | else |
| 9467 | return ucs1lib_split_whitespace( |
| 9468 | (PyObject*) self, PyUnicode_1BYTE_DATA(self), |
| 9469 | PyUnicode_GET_LENGTH(self), maxcount |
| 9470 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9471 | case PyUnicode_2BYTE_KIND: |
| 9472 | return ucs2lib_split_whitespace( |
| 9473 | (PyObject*) self, PyUnicode_2BYTE_DATA(self), |
| 9474 | PyUnicode_GET_LENGTH(self), maxcount |
| 9475 | ); |
| 9476 | case PyUnicode_4BYTE_KIND: |
| 9477 | return ucs4lib_split_whitespace( |
| 9478 | (PyObject*) self, PyUnicode_4BYTE_DATA(self), |
| 9479 | PyUnicode_GET_LENGTH(self), maxcount |
| 9480 | ); |
| 9481 | default: |
| 9482 | assert(0); |
| 9483 | return NULL; |
| 9484 | } |
| 9485 | |
| 9486 | if (PyUnicode_READY(substring) == -1) |
| 9487 | return NULL; |
| 9488 | |
| 9489 | kind1 = PyUnicode_KIND(self); |
| 9490 | kind2 = PyUnicode_KIND(substring); |
| 9491 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9492 | buf1 = PyUnicode_DATA(self); |
| 9493 | buf2 = PyUnicode_DATA(substring); |
| 9494 | if (kind1 != kind) |
| 9495 | buf1 = _PyUnicode_AsKind((PyObject*)self, kind); |
| 9496 | if (!buf1) |
| 9497 | return NULL; |
| 9498 | if (kind2 != kind) |
| 9499 | buf2 = _PyUnicode_AsKind((PyObject*)substring, kind); |
| 9500 | if (!buf2) { |
| 9501 | if (kind1 != kind) PyMem_Free(buf1); |
| 9502 | return NULL; |
| 9503 | } |
| 9504 | len1 = PyUnicode_GET_LENGTH(self); |
| 9505 | len2 = PyUnicode_GET_LENGTH(substring); |
| 9506 | |
| 9507 | switch(kind) { |
| 9508 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9509 | if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) |
| 9510 | out = asciilib_split( |
| 9511 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 9512 | else |
| 9513 | out = ucs1lib_split( |
| 9514 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9515 | break; |
| 9516 | case PyUnicode_2BYTE_KIND: |
| 9517 | out = ucs2lib_split( |
| 9518 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 9519 | break; |
| 9520 | case PyUnicode_4BYTE_KIND: |
| 9521 | out = ucs4lib_split( |
| 9522 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 9523 | break; |
| 9524 | default: |
| 9525 | out = NULL; |
| 9526 | } |
| 9527 | if (kind1 != kind) |
| 9528 | PyMem_Free(buf1); |
| 9529 | if (kind2 != kind) |
| 9530 | PyMem_Free(buf2); |
| 9531 | return out; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9532 | } |
| 9533 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9534 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9535 | rsplit(PyObject *self, |
| 9536 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9537 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9538 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9539 | int kind1, kind2, kind; |
| 9540 | void *buf1, *buf2; |
| 9541 | Py_ssize_t len1, len2; |
| 9542 | PyObject* out; |
| 9543 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9544 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9545 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9546 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9547 | if (PyUnicode_READY(self) == -1) |
| 9548 | return NULL; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9549 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9550 | if (substring == NULL) |
| 9551 | switch(PyUnicode_KIND(self)) { |
| 9552 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9553 | if (PyUnicode_IS_ASCII(self)) |
| 9554 | return asciilib_rsplit_whitespace( |
| 9555 | (PyObject*) self, PyUnicode_1BYTE_DATA(self), |
| 9556 | PyUnicode_GET_LENGTH(self), maxcount |
| 9557 | ); |
| 9558 | else |
| 9559 | return ucs1lib_rsplit_whitespace( |
| 9560 | (PyObject*) self, PyUnicode_1BYTE_DATA(self), |
| 9561 | PyUnicode_GET_LENGTH(self), maxcount |
| 9562 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9563 | case PyUnicode_2BYTE_KIND: |
| 9564 | return ucs2lib_rsplit_whitespace( |
| 9565 | (PyObject*) self, PyUnicode_2BYTE_DATA(self), |
| 9566 | PyUnicode_GET_LENGTH(self), maxcount |
| 9567 | ); |
| 9568 | case PyUnicode_4BYTE_KIND: |
| 9569 | return ucs4lib_rsplit_whitespace( |
| 9570 | (PyObject*) self, PyUnicode_4BYTE_DATA(self), |
| 9571 | PyUnicode_GET_LENGTH(self), maxcount |
| 9572 | ); |
| 9573 | default: |
| 9574 | assert(0); |
| 9575 | return NULL; |
| 9576 | } |
| 9577 | |
| 9578 | if (PyUnicode_READY(substring) == -1) |
| 9579 | return NULL; |
| 9580 | |
| 9581 | kind1 = PyUnicode_KIND(self); |
| 9582 | kind2 = PyUnicode_KIND(substring); |
| 9583 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9584 | buf1 = PyUnicode_DATA(self); |
| 9585 | buf2 = PyUnicode_DATA(substring); |
| 9586 | if (kind1 != kind) |
| 9587 | buf1 = _PyUnicode_AsKind((PyObject*)self, kind); |
| 9588 | if (!buf1) |
| 9589 | return NULL; |
| 9590 | if (kind2 != kind) |
| 9591 | buf2 = _PyUnicode_AsKind((PyObject*)substring, kind); |
| 9592 | if (!buf2) { |
| 9593 | if (kind1 != kind) PyMem_Free(buf1); |
| 9594 | return NULL; |
| 9595 | } |
| 9596 | len1 = PyUnicode_GET_LENGTH(self); |
| 9597 | len2 = PyUnicode_GET_LENGTH(substring); |
| 9598 | |
| 9599 | switch(kind) { |
| 9600 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9601 | if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) |
| 9602 | out = asciilib_rsplit( |
| 9603 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 9604 | else |
| 9605 | out = ucs1lib_rsplit( |
| 9606 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9607 | break; |
| 9608 | case PyUnicode_2BYTE_KIND: |
| 9609 | out = ucs2lib_rsplit( |
| 9610 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 9611 | break; |
| 9612 | case PyUnicode_4BYTE_KIND: |
| 9613 | out = ucs4lib_rsplit( |
| 9614 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 9615 | break; |
| 9616 | default: |
| 9617 | out = NULL; |
| 9618 | } |
| 9619 | if (kind1 != kind) |
| 9620 | PyMem_Free(buf1); |
| 9621 | if (kind2 != kind) |
| 9622 | PyMem_Free(buf2); |
| 9623 | return out; |
| 9624 | } |
| 9625 | |
| 9626 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9627 | anylib_find(int kind, PyObject *str1, void *buf1, Py_ssize_t len1, |
| 9628 | 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] | 9629 | { |
| 9630 | switch(kind) { |
| 9631 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9632 | if (PyUnicode_IS_ASCII(str1) && PyUnicode_IS_ASCII(str2)) |
| 9633 | return asciilib_find(buf1, len1, buf2, len2, offset); |
| 9634 | else |
| 9635 | return ucs1lib_find(buf1, len1, buf2, len2, offset); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9636 | case PyUnicode_2BYTE_KIND: |
| 9637 | return ucs2lib_find(buf1, len1, buf2, len2, offset); |
| 9638 | case PyUnicode_4BYTE_KIND: |
| 9639 | return ucs4lib_find(buf1, len1, buf2, len2, offset); |
| 9640 | } |
| 9641 | assert(0); |
| 9642 | return -1; |
| 9643 | } |
| 9644 | |
| 9645 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9646 | anylib_count(int kind, PyObject *sstr, void* sbuf, Py_ssize_t slen, |
| 9647 | 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] | 9648 | { |
| 9649 | switch(kind) { |
| 9650 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9651 | if (PyUnicode_IS_ASCII(sstr) && PyUnicode_IS_ASCII(str1)) |
| 9652 | return asciilib_count(sbuf, slen, buf1, len1, maxcount); |
| 9653 | else |
| 9654 | return ucs1lib_count(sbuf, slen, buf1, len1, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9655 | case PyUnicode_2BYTE_KIND: |
| 9656 | return ucs2lib_count(sbuf, slen, buf1, len1, maxcount); |
| 9657 | case PyUnicode_4BYTE_KIND: |
| 9658 | return ucs4lib_count(sbuf, slen, buf1, len1, maxcount); |
| 9659 | } |
| 9660 | assert(0); |
| 9661 | return 0; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9662 | } |
| 9663 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9664 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9665 | replace(PyObject *self, PyObject *str1, |
| 9666 | PyObject *str2, Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9667 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9668 | PyObject *u; |
| 9669 | char *sbuf = PyUnicode_DATA(self); |
| 9670 | char *buf1 = PyUnicode_DATA(str1); |
| 9671 | char *buf2 = PyUnicode_DATA(str2); |
| 9672 | int srelease = 0, release1 = 0, release2 = 0; |
| 9673 | int skind = PyUnicode_KIND(self); |
| 9674 | int kind1 = PyUnicode_KIND(str1); |
| 9675 | int kind2 = PyUnicode_KIND(str2); |
| 9676 | Py_ssize_t slen = PyUnicode_GET_LENGTH(self); |
| 9677 | Py_ssize_t len1 = PyUnicode_GET_LENGTH(str1); |
| 9678 | Py_ssize_t len2 = PyUnicode_GET_LENGTH(str2); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 9679 | int mayshrink; |
| 9680 | Py_UCS4 maxchar, maxchar_str2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9681 | |
| 9682 | if (maxcount < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9683 | maxcount = PY_SSIZE_T_MAX; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9684 | else if (maxcount == 0 || slen == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9685 | goto nothing; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9686 | |
Victor Stinner | 59de0ee | 2011-10-07 10:01:28 +0200 | [diff] [blame] | 9687 | if (str1 == str2) |
| 9688 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9689 | if (skind < kind1) |
| 9690 | /* substring too wide to be present */ |
| 9691 | goto nothing; |
| 9692 | |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 9693 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
| 9694 | maxchar_str2 = PyUnicode_MAX_CHAR_VALUE(str2); |
| 9695 | /* Replacing str1 with str2 may cause a maxchar reduction in the |
| 9696 | result string. */ |
| 9697 | mayshrink = (maxchar_str2 < maxchar); |
| 9698 | maxchar = Py_MAX(maxchar, maxchar_str2); |
| 9699 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9700 | if (len1 == len2) { |
Antoine Pitrou | cbfdee3 | 2010-01-13 08:58:08 +0000 | [diff] [blame] | 9701 | Py_ssize_t i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9702 | /* same length */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9703 | if (len1 == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9704 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9705 | if (len1 == 1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9706 | /* replace characters */ |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 9707 | Py_UCS4 u1, u2; |
| 9708 | int rkind; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9709 | u1 = PyUnicode_READ_CHAR(str1, 0); |
| 9710 | if (!findchar(sbuf, PyUnicode_KIND(self), |
| 9711 | slen, u1, 1)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9712 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9713 | u2 = PyUnicode_READ_CHAR(str2, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9714 | u = PyUnicode_New(slen, maxchar); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9715 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9716 | goto error; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9717 | copy_characters(u, 0, self, 0, slen); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9718 | rkind = PyUnicode_KIND(u); |
| 9719 | for (i = 0; i < PyUnicode_GET_LENGTH(u); i++) |
| 9720 | if (PyUnicode_READ(rkind, PyUnicode_DATA(u), i) == u1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9721 | if (--maxcount < 0) |
| 9722 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9723 | PyUnicode_WRITE(rkind, PyUnicode_DATA(u), i, u2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9724 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 9725 | } |
| 9726 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9727 | int rkind = skind; |
| 9728 | char *res; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 9729 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9730 | if (kind1 < rkind) { |
| 9731 | /* widen substring */ |
| 9732 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 9733 | if (!buf1) goto error; |
| 9734 | release1 = 1; |
| 9735 | } |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9736 | i = anylib_find(rkind, self, sbuf, slen, str1, buf1, len1, 0); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9737 | if (i < 0) |
| 9738 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9739 | if (rkind > kind2) { |
| 9740 | /* widen replacement */ |
| 9741 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 9742 | if (!buf2) goto error; |
| 9743 | release2 = 1; |
| 9744 | } |
| 9745 | else if (rkind < kind2) { |
| 9746 | /* widen self and buf1 */ |
| 9747 | rkind = kind2; |
| 9748 | if (release1) PyMem_Free(buf1); |
| 9749 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 9750 | if (!sbuf) goto error; |
| 9751 | srelease = 1; |
| 9752 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 9753 | if (!buf1) goto error; |
| 9754 | release1 = 1; |
| 9755 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 9756 | u = PyUnicode_New(slen, maxchar); |
| 9757 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9758 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 9759 | assert(PyUnicode_KIND(u) == rkind); |
| 9760 | res = PyUnicode_DATA(u); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 9761 | |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9762 | memcpy(res, sbuf, rkind * slen); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9763 | /* change everything in-place, starting with this one */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9764 | memcpy(res + rkind * i, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9765 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9766 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9767 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9768 | |
| 9769 | while ( --maxcount > 0) { |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9770 | i = anylib_find(rkind, self, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9771 | sbuf+rkind*i, slen-i, |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9772 | str1, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9773 | if (i == -1) |
| 9774 | break; |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9775 | memcpy(res + rkind * i, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9776 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9777 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9778 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9779 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9780 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 9781 | } |
| 9782 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9783 | Py_ssize_t n, i, j, ires; |
| 9784 | Py_ssize_t product, new_size; |
| 9785 | int rkind = skind; |
| 9786 | char *res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9787 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9788 | if (kind1 < rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 9789 | /* widen substring */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9790 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 9791 | if (!buf1) goto error; |
| 9792 | release1 = 1; |
| 9793 | } |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9794 | n = anylib_count(rkind, self, sbuf, slen, str1, buf1, len1, maxcount); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9795 | if (n == 0) |
| 9796 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9797 | if (kind2 < rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 9798 | /* widen replacement */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9799 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 9800 | if (!buf2) goto error; |
| 9801 | release2 = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9802 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9803 | else if (kind2 > rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 9804 | /* widen self and buf1 */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9805 | rkind = kind2; |
| 9806 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 9807 | if (!sbuf) goto error; |
| 9808 | srelease = 1; |
| 9809 | if (release1) PyMem_Free(buf1); |
| 9810 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 9811 | if (!buf1) goto error; |
| 9812 | release1 = 1; |
| 9813 | } |
| 9814 | /* new_size = PyUnicode_GET_LENGTH(self) + n * (PyUnicode_GET_LENGTH(str2) - |
| 9815 | PyUnicode_GET_LENGTH(str1))); */ |
| 9816 | product = n * (len2-len1); |
| 9817 | if ((product / (len2-len1)) != n) { |
| 9818 | PyErr_SetString(PyExc_OverflowError, |
| 9819 | "replace string is too long"); |
| 9820 | goto error; |
| 9821 | } |
| 9822 | new_size = slen + product; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 9823 | if (new_size == 0) { |
| 9824 | Py_INCREF(unicode_empty); |
| 9825 | u = unicode_empty; |
| 9826 | goto done; |
| 9827 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9828 | if (new_size < 0 || new_size > (PY_SSIZE_T_MAX >> (rkind-1))) { |
| 9829 | PyErr_SetString(PyExc_OverflowError, |
| 9830 | "replace string is too long"); |
| 9831 | goto error; |
| 9832 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 9833 | u = PyUnicode_New(new_size, maxchar); |
| 9834 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9835 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 9836 | assert(PyUnicode_KIND(u) == rkind); |
| 9837 | res = PyUnicode_DATA(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9838 | ires = i = 0; |
| 9839 | if (len1 > 0) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9840 | while (n-- > 0) { |
| 9841 | /* look for next match */ |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9842 | j = anylib_find(rkind, self, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9843 | sbuf + rkind * i, slen-i, |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9844 | str1, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9845 | if (j == -1) |
| 9846 | break; |
| 9847 | else if (j > i) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9848 | /* copy unchanged part [i:j] */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9849 | memcpy(res + rkind * ires, |
| 9850 | sbuf + rkind * i, |
| 9851 | rkind * (j-i)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9852 | ires += j - i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9853 | } |
| 9854 | /* copy substitution string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9855 | if (len2 > 0) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9856 | memcpy(res + rkind * ires, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9857 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9858 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9859 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9860 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9861 | i = j + len1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9862 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9863 | if (i < slen) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9864 | /* copy tail [i:] */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9865 | memcpy(res + rkind * ires, |
| 9866 | sbuf + rkind * i, |
| 9867 | rkind * (slen-i)); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 9868 | } |
| 9869 | else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9870 | /* interleave */ |
| 9871 | while (n > 0) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9872 | memcpy(res + rkind * ires, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9873 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9874 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9875 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9876 | if (--n <= 0) |
| 9877 | break; |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9878 | memcpy(res + rkind * ires, |
| 9879 | sbuf + rkind * i, |
| 9880 | rkind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9881 | ires++; |
| 9882 | i++; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9883 | } |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9884 | memcpy(res + rkind * ires, |
| 9885 | sbuf + rkind * i, |
| 9886 | rkind * (slen-i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9887 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 9888 | } |
| 9889 | |
| 9890 | if (mayshrink) { |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 9891 | unicode_adjust_maxchar(&u); |
| 9892 | if (u == NULL) |
| 9893 | goto error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9894 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 9895 | |
| 9896 | done: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9897 | if (srelease) |
| 9898 | PyMem_FREE(sbuf); |
| 9899 | if (release1) |
| 9900 | PyMem_FREE(buf1); |
| 9901 | if (release2) |
| 9902 | PyMem_FREE(buf2); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9903 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9904 | return u; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9905 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9906 | nothing: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9907 | /* nothing to replace; return original string (when possible) */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9908 | if (srelease) |
| 9909 | PyMem_FREE(sbuf); |
| 9910 | if (release1) |
| 9911 | PyMem_FREE(buf1); |
| 9912 | if (release2) |
| 9913 | PyMem_FREE(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9914 | if (PyUnicode_CheckExact(self)) { |
| 9915 | Py_INCREF(self); |
| 9916 | return (PyObject *) self; |
| 9917 | } |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 9918 | return PyUnicode_Copy(self); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9919 | error: |
| 9920 | if (srelease && sbuf) |
| 9921 | PyMem_FREE(sbuf); |
| 9922 | if (release1 && buf1) |
| 9923 | PyMem_FREE(buf1); |
| 9924 | if (release2 && buf2) |
| 9925 | PyMem_FREE(buf2); |
| 9926 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9927 | } |
| 9928 | |
| 9929 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 9930 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9931 | PyDoc_STRVAR(title__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9932 | "S.title() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9933 | \n\ |
| 9934 | 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] | 9935 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9936 | |
| 9937 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9938 | unicode_title(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9939 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9940 | return fixup(self, fixtitle); |
| 9941 | } |
| 9942 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9943 | PyDoc_STRVAR(capitalize__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9944 | "S.capitalize() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9945 | \n\ |
| 9946 | 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] | 9947 | have upper case and the rest lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9948 | |
| 9949 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9950 | unicode_capitalize(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9951 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9952 | return fixup(self, fixcapitalize); |
| 9953 | } |
| 9954 | |
| 9955 | #if 0 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9956 | PyDoc_STRVAR(capwords__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9957 | "S.capwords() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9958 | \n\ |
| 9959 | 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] | 9960 | normalized whitespace (all whitespace strings are replaced by ' ')."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9961 | |
| 9962 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 9963 | unicode_capwords(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9964 | { |
| 9965 | PyObject *list; |
| 9966 | PyObject *item; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9967 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9968 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9969 | /* Split into words */ |
| 9970 | list = split(self, NULL, -1); |
| 9971 | if (!list) |
| 9972 | return NULL; |
| 9973 | |
| 9974 | /* Capitalize each word */ |
| 9975 | for (i = 0; i < PyList_GET_SIZE(list); i++) { |
| 9976 | item = fixup((PyUnicodeObject *)PyList_GET_ITEM(list, i), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9977 | fixcapitalize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9978 | if (item == NULL) |
| 9979 | goto onError; |
| 9980 | Py_DECREF(PyList_GET_ITEM(list, i)); |
| 9981 | PyList_SET_ITEM(list, i, item); |
| 9982 | } |
| 9983 | |
| 9984 | /* Join the words to form a new string */ |
| 9985 | item = PyUnicode_Join(NULL, list); |
| 9986 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9987 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9988 | Py_DECREF(list); |
| 9989 | return (PyObject *)item; |
| 9990 | } |
| 9991 | #endif |
| 9992 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 9993 | /* Argument converter. Coerces to a single unicode character */ |
| 9994 | |
| 9995 | static int |
| 9996 | convert_uc(PyObject *obj, void *addr) |
| 9997 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9998 | Py_UCS4 *fillcharloc = (Py_UCS4 *)addr; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9999 | PyObject *uniobj; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10000 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10001 | uniobj = PyUnicode_FromObject(obj); |
| 10002 | if (uniobj == NULL) { |
| 10003 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10004 | "The fill character cannot be converted to Unicode"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10005 | return 0; |
| 10006 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10007 | if (PyUnicode_GET_LENGTH(uniobj) != 1) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10008 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10009 | "The fill character must be exactly one character long"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10010 | Py_DECREF(uniobj); |
| 10011 | return 0; |
| 10012 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10013 | *fillcharloc = PyUnicode_READ_CHAR(uniobj, 0); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10014 | Py_DECREF(uniobj); |
| 10015 | return 1; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10016 | } |
| 10017 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10018 | PyDoc_STRVAR(center__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10019 | "S.center(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10020 | \n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10021 | Return S centered in a string of length width. Padding is\n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10022 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10023 | |
| 10024 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10025 | unicode_center(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10026 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10027 | Py_ssize_t marg, left; |
| 10028 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10029 | Py_UCS4 fillchar = ' '; |
| 10030 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 10031 | 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] | 10032 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10033 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 10034 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10035 | return NULL; |
| 10036 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10037 | if (_PyUnicode_LENGTH(self) >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10038 | Py_INCREF(self); |
| 10039 | return (PyObject*) self; |
| 10040 | } |
| 10041 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10042 | marg = width - _PyUnicode_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10043 | left = marg / 2 + (marg & width & 1); |
| 10044 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10045 | return pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10046 | } |
| 10047 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10048 | /* This function assumes that str1 and str2 are readied by the caller. */ |
| 10049 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10050 | static int |
| 10051 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 10052 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10053 | int kind1, kind2; |
| 10054 | void *data1, *data2; |
| 10055 | Py_ssize_t len1, len2, i; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10056 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10057 | kind1 = PyUnicode_KIND(str1); |
| 10058 | kind2 = PyUnicode_KIND(str2); |
| 10059 | data1 = PyUnicode_DATA(str1); |
| 10060 | data2 = PyUnicode_DATA(str2); |
| 10061 | len1 = PyUnicode_GET_LENGTH(str1); |
| 10062 | len2 = PyUnicode_GET_LENGTH(str2); |
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 | for (i = 0; i < len1 && i < len2; ++i) { |
| 10065 | Py_UCS4 c1, c2; |
| 10066 | c1 = PyUnicode_READ(kind1, data1, i); |
| 10067 | c2 = PyUnicode_READ(kind2, data2, i); |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 10068 | |
| 10069 | if (c1 != c2) |
| 10070 | return (c1 < c2) ? -1 : 1; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10071 | } |
| 10072 | |
| 10073 | return (len1 < len2) ? -1 : (len1 != len2); |
| 10074 | } |
| 10075 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10076 | int |
| 10077 | PyUnicode_Compare(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10078 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10079 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 10080 | if (PyUnicode_READY(left) == -1 || |
| 10081 | PyUnicode_READY(right) == -1) |
| 10082 | return -1; |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 10083 | return unicode_compare((PyUnicodeObject *)left, |
| 10084 | (PyUnicodeObject *)right); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10085 | } |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 10086 | PyErr_Format(PyExc_TypeError, |
| 10087 | "Can't compare %.100s and %.100s", |
| 10088 | left->ob_type->tp_name, |
| 10089 | right->ob_type->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10090 | return -1; |
| 10091 | } |
| 10092 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10093 | int |
| 10094 | PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str) |
| 10095 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10096 | Py_ssize_t i; |
| 10097 | int kind; |
| 10098 | void *data; |
| 10099 | Py_UCS4 chr; |
| 10100 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 10101 | assert(_PyUnicode_CHECK(uni)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10102 | if (PyUnicode_READY(uni) == -1) |
| 10103 | return -1; |
| 10104 | kind = PyUnicode_KIND(uni); |
| 10105 | data = PyUnicode_DATA(uni); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10106 | /* Compare Unicode string and source character set string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10107 | for (i = 0; (chr = PyUnicode_READ(kind, data, i)) && str[i]; i++) |
| 10108 | if (chr != str[i]) |
| 10109 | return (chr < (unsigned char)(str[i])) ? -1 : 1; |
Benjamin Peterson | 8667a9b | 2010-01-09 21:45:28 +0000 | [diff] [blame] | 10110 | /* This check keeps Python strings that end in '\0' from comparing equal |
| 10111 | to C strings identical up to that point. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10112 | if (PyUnicode_GET_LENGTH(uni) != i || chr) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10113 | return 1; /* uni is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10114 | if (str[i]) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10115 | return -1; /* str is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10116 | return 0; |
| 10117 | } |
| 10118 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10119 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10120 | #define TEST_COND(cond) \ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10121 | ((cond) ? Py_True : Py_False) |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10122 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10123 | PyObject * |
| 10124 | PyUnicode_RichCompare(PyObject *left, PyObject *right, int op) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10125 | { |
| 10126 | int result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10127 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10128 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 10129 | PyObject *v; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10130 | if (PyUnicode_READY(left) == -1 || |
| 10131 | PyUnicode_READY(right) == -1) |
| 10132 | return NULL; |
| 10133 | if (PyUnicode_GET_LENGTH(left) != PyUnicode_GET_LENGTH(right) || |
| 10134 | PyUnicode_KIND(left) != PyUnicode_KIND(right)) { |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10135 | if (op == Py_EQ) { |
| 10136 | Py_INCREF(Py_False); |
| 10137 | return Py_False; |
| 10138 | } |
| 10139 | if (op == Py_NE) { |
| 10140 | Py_INCREF(Py_True); |
| 10141 | return Py_True; |
| 10142 | } |
| 10143 | } |
| 10144 | if (left == right) |
| 10145 | result = 0; |
| 10146 | else |
| 10147 | result = unicode_compare((PyUnicodeObject *)left, |
| 10148 | (PyUnicodeObject *)right); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10149 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10150 | /* Convert the return value to a Boolean */ |
| 10151 | switch (op) { |
| 10152 | case Py_EQ: |
| 10153 | v = TEST_COND(result == 0); |
| 10154 | break; |
| 10155 | case Py_NE: |
| 10156 | v = TEST_COND(result != 0); |
| 10157 | break; |
| 10158 | case Py_LE: |
| 10159 | v = TEST_COND(result <= 0); |
| 10160 | break; |
| 10161 | case Py_GE: |
| 10162 | v = TEST_COND(result >= 0); |
| 10163 | break; |
| 10164 | case Py_LT: |
| 10165 | v = TEST_COND(result == -1); |
| 10166 | break; |
| 10167 | case Py_GT: |
| 10168 | v = TEST_COND(result == 1); |
| 10169 | break; |
| 10170 | default: |
| 10171 | PyErr_BadArgument(); |
| 10172 | return NULL; |
| 10173 | } |
| 10174 | Py_INCREF(v); |
| 10175 | return v; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10176 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10177 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 10178 | Py_RETURN_NOTIMPLEMENTED; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10179 | } |
| 10180 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10181 | int |
| 10182 | PyUnicode_Contains(PyObject *container, PyObject *element) |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10183 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10184 | PyObject *str, *sub; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10185 | int kind1, kind2, kind; |
| 10186 | void *buf1, *buf2; |
| 10187 | Py_ssize_t len1, len2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10188 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10189 | |
| 10190 | /* Coerce the two arguments */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10191 | sub = PyUnicode_FromObject(element); |
| 10192 | if (!sub) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10193 | PyErr_Format(PyExc_TypeError, |
| 10194 | "'in <string>' requires string as left operand, not %s", |
| 10195 | element->ob_type->tp_name); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10196 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10197 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10198 | if (PyUnicode_READY(sub) == -1) |
| 10199 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10200 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10201 | str = PyUnicode_FromObject(container); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 10202 | if (!str || PyUnicode_READY(str) == -1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10203 | Py_DECREF(sub); |
| 10204 | return -1; |
| 10205 | } |
| 10206 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10207 | kind1 = PyUnicode_KIND(str); |
| 10208 | kind2 = PyUnicode_KIND(sub); |
| 10209 | kind = kind1 > kind2 ? kind1 : kind2; |
| 10210 | buf1 = PyUnicode_DATA(str); |
| 10211 | buf2 = PyUnicode_DATA(sub); |
| 10212 | if (kind1 != kind) |
| 10213 | buf1 = _PyUnicode_AsKind((PyObject*)str, kind); |
| 10214 | if (!buf1) { |
| 10215 | Py_DECREF(sub); |
| 10216 | return -1; |
| 10217 | } |
| 10218 | if (kind2 != kind) |
| 10219 | buf2 = _PyUnicode_AsKind((PyObject*)sub, kind); |
| 10220 | if (!buf2) { |
| 10221 | Py_DECREF(sub); |
| 10222 | if (kind1 != kind) PyMem_Free(buf1); |
| 10223 | return -1; |
| 10224 | } |
| 10225 | len1 = PyUnicode_GET_LENGTH(str); |
| 10226 | len2 = PyUnicode_GET_LENGTH(sub); |
| 10227 | |
| 10228 | switch(kind) { |
| 10229 | case PyUnicode_1BYTE_KIND: |
| 10230 | result = ucs1lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10231 | break; |
| 10232 | case PyUnicode_2BYTE_KIND: |
| 10233 | result = ucs2lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10234 | break; |
| 10235 | case PyUnicode_4BYTE_KIND: |
| 10236 | result = ucs4lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10237 | break; |
| 10238 | default: |
| 10239 | result = -1; |
| 10240 | assert(0); |
| 10241 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10242 | |
| 10243 | Py_DECREF(str); |
| 10244 | Py_DECREF(sub); |
| 10245 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10246 | if (kind1 != kind) |
| 10247 | PyMem_Free(buf1); |
| 10248 | if (kind2 != kind) |
| 10249 | PyMem_Free(buf2); |
| 10250 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10251 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10252 | } |
| 10253 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10254 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 10255 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10256 | PyObject * |
| 10257 | PyUnicode_Concat(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10258 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10259 | PyObject *u = NULL, *v = NULL, *w; |
Victor Stinner | 127226b | 2011-10-13 01:12:34 +0200 | [diff] [blame] | 10260 | Py_UCS4 maxchar, maxchar2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10261 | |
| 10262 | /* Coerce the two arguments */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10263 | u = PyUnicode_FromObject(left); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10264 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10265 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10266 | v = PyUnicode_FromObject(right); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10267 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10268 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10269 | |
| 10270 | /* Shortcuts */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 10271 | if (v == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10272 | Py_DECREF(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10273 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10274 | } |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 10275 | if (u == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10276 | Py_DECREF(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10277 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10278 | } |
| 10279 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10280 | maxchar = PyUnicode_MAX_CHAR_VALUE(u); |
Victor Stinner | 127226b | 2011-10-13 01:12:34 +0200 | [diff] [blame] | 10281 | maxchar2 = PyUnicode_MAX_CHAR_VALUE(v); |
| 10282 | maxchar = Py_MAX(maxchar, maxchar2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10283 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10284 | /* Concat the two Unicode strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10285 | w = PyUnicode_New( |
| 10286 | PyUnicode_GET_LENGTH(u) + PyUnicode_GET_LENGTH(v), |
| 10287 | maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10288 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10289 | goto onError; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 10290 | copy_characters(w, 0, u, 0, PyUnicode_GET_LENGTH(u)); |
| 10291 | 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] | 10292 | Py_DECREF(u); |
| 10293 | Py_DECREF(v); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10294 | assert(_PyUnicode_CheckConsistency(w, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10295 | return w; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10296 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10297 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10298 | Py_XDECREF(u); |
| 10299 | Py_XDECREF(v); |
| 10300 | return NULL; |
| 10301 | } |
| 10302 | |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10303 | static void |
| 10304 | unicode_append_inplace(PyObject **p_left, PyObject *right) |
| 10305 | { |
| 10306 | Py_ssize_t left_len, right_len, new_len; |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10307 | |
| 10308 | assert(PyUnicode_IS_READY(*p_left)); |
| 10309 | assert(PyUnicode_IS_READY(right)); |
| 10310 | |
| 10311 | left_len = PyUnicode_GET_LENGTH(*p_left); |
| 10312 | right_len = PyUnicode_GET_LENGTH(right); |
| 10313 | if (left_len > PY_SSIZE_T_MAX - right_len) { |
| 10314 | PyErr_SetString(PyExc_OverflowError, |
| 10315 | "strings are too large to concat"); |
| 10316 | goto error; |
| 10317 | } |
| 10318 | new_len = left_len + right_len; |
| 10319 | |
| 10320 | /* Now we own the last reference to 'left', so we can resize it |
| 10321 | * in-place. |
| 10322 | */ |
| 10323 | if (unicode_resize(p_left, new_len) != 0) { |
| 10324 | /* XXX if _PyUnicode_Resize() fails, 'left' has been |
| 10325 | * deallocated so it cannot be put back into |
| 10326 | * 'variable'. The MemoryError is raised when there |
| 10327 | * is no value in 'variable', which might (very |
| 10328 | * remotely) be a cause of incompatibilities. |
| 10329 | */ |
| 10330 | goto error; |
| 10331 | } |
| 10332 | /* copy 'right' into the newly allocated area of 'left' */ |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 10333 | copy_characters(*p_left, left_len, right, 0, right_len); |
| 10334 | _PyUnicode_DIRTY(*p_left); |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10335 | return; |
| 10336 | |
| 10337 | error: |
| 10338 | Py_DECREF(*p_left); |
| 10339 | *p_left = NULL; |
| 10340 | } |
| 10341 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10342 | void |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10343 | PyUnicode_Append(PyObject **p_left, PyObject *right) |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10344 | { |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10345 | PyObject *left, *res; |
| 10346 | |
| 10347 | if (p_left == NULL) { |
| 10348 | if (!PyErr_Occurred()) |
| 10349 | PyErr_BadInternalCall(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10350 | return; |
| 10351 | } |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10352 | left = *p_left; |
| 10353 | if (right == NULL || !PyUnicode_Check(left)) { |
| 10354 | if (!PyErr_Occurred()) |
| 10355 | PyErr_BadInternalCall(); |
| 10356 | goto error; |
| 10357 | } |
| 10358 | |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 10359 | if (PyUnicode_READY(left)) |
| 10360 | goto error; |
| 10361 | if (PyUnicode_READY(right)) |
| 10362 | goto error; |
| 10363 | |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10364 | if (PyUnicode_CheckExact(left) && left != unicode_empty |
| 10365 | && PyUnicode_CheckExact(right) && right != unicode_empty |
| 10366 | && unicode_resizable(left) |
| 10367 | && (_PyUnicode_KIND(right) <= _PyUnicode_KIND(left) |
| 10368 | || _PyUnicode_WSTR(left) != NULL)) |
| 10369 | { |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10370 | /* Don't resize for ascii += latin1. Convert ascii to latin1 requires |
| 10371 | to change the structure size, but characters are stored just after |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 10372 | the structure, and so it requires to move all characters which is |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10373 | not so different than duplicating the string. */ |
| 10374 | if (!(PyUnicode_IS_ASCII(left) && !PyUnicode_IS_ASCII(right))) |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10375 | { |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10376 | unicode_append_inplace(p_left, right); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10377 | if (p_left != NULL) |
| 10378 | assert(_PyUnicode_CheckConsistency(*p_left, 1)); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10379 | return; |
| 10380 | } |
| 10381 | } |
| 10382 | |
| 10383 | res = PyUnicode_Concat(left, right); |
| 10384 | if (res == NULL) |
| 10385 | goto error; |
| 10386 | Py_DECREF(left); |
| 10387 | *p_left = res; |
| 10388 | return; |
| 10389 | |
| 10390 | error: |
| 10391 | Py_DECREF(*p_left); |
| 10392 | *p_left = NULL; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10393 | } |
| 10394 | |
| 10395 | void |
| 10396 | PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right) |
| 10397 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10398 | PyUnicode_Append(pleft, right); |
| 10399 | Py_XDECREF(right); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10400 | } |
| 10401 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10402 | PyDoc_STRVAR(count__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10403 | "S.count(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10404 | \n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10405 | Return the number of non-overlapping occurrences of substring sub in\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10406 | 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] | 10407 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10408 | |
| 10409 | static PyObject * |
| 10410 | unicode_count(PyUnicodeObject *self, PyObject *args) |
| 10411 | { |
| 10412 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10413 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10414 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10415 | PyObject *result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10416 | int kind1, kind2, kind; |
| 10417 | void *buf1, *buf2; |
| 10418 | Py_ssize_t len1, len2, iresult; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10419 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10420 | if (!stringlib_parse_args_finds_unicode("count", args, &substring, |
| 10421 | &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10422 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10423 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10424 | kind1 = PyUnicode_KIND(self); |
| 10425 | kind2 = PyUnicode_KIND(substring); |
| 10426 | kind = kind1 > kind2 ? kind1 : kind2; |
| 10427 | buf1 = PyUnicode_DATA(self); |
| 10428 | buf2 = PyUnicode_DATA(substring); |
| 10429 | if (kind1 != kind) |
| 10430 | buf1 = _PyUnicode_AsKind((PyObject*)self, kind); |
| 10431 | if (!buf1) { |
| 10432 | Py_DECREF(substring); |
| 10433 | return NULL; |
| 10434 | } |
| 10435 | if (kind2 != kind) |
| 10436 | buf2 = _PyUnicode_AsKind((PyObject*)substring, kind); |
| 10437 | if (!buf2) { |
| 10438 | Py_DECREF(substring); |
| 10439 | if (kind1 != kind) PyMem_Free(buf1); |
| 10440 | return NULL; |
| 10441 | } |
| 10442 | len1 = PyUnicode_GET_LENGTH(self); |
| 10443 | len2 = PyUnicode_GET_LENGTH(substring); |
| 10444 | |
| 10445 | ADJUST_INDICES(start, end, len1); |
| 10446 | switch(kind) { |
| 10447 | case PyUnicode_1BYTE_KIND: |
| 10448 | iresult = ucs1lib_count( |
| 10449 | ((Py_UCS1*)buf1) + start, end - start, |
| 10450 | buf2, len2, PY_SSIZE_T_MAX |
| 10451 | ); |
| 10452 | break; |
| 10453 | case PyUnicode_2BYTE_KIND: |
| 10454 | iresult = ucs2lib_count( |
| 10455 | ((Py_UCS2*)buf1) + start, end - start, |
| 10456 | buf2, len2, PY_SSIZE_T_MAX |
| 10457 | ); |
| 10458 | break; |
| 10459 | case PyUnicode_4BYTE_KIND: |
| 10460 | iresult = ucs4lib_count( |
| 10461 | ((Py_UCS4*)buf1) + start, end - start, |
| 10462 | buf2, len2, PY_SSIZE_T_MAX |
| 10463 | ); |
| 10464 | break; |
| 10465 | default: |
| 10466 | assert(0); iresult = 0; |
| 10467 | } |
| 10468 | |
| 10469 | result = PyLong_FromSsize_t(iresult); |
| 10470 | |
| 10471 | if (kind1 != kind) |
| 10472 | PyMem_Free(buf1); |
| 10473 | if (kind2 != kind) |
| 10474 | PyMem_Free(buf2); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10475 | |
| 10476 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10477 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10478 | return result; |
| 10479 | } |
| 10480 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10481 | PyDoc_STRVAR(encode__doc__, |
Victor Stinner | c911bbf | 2010-11-07 19:04:46 +0000 | [diff] [blame] | 10482 | "S.encode(encoding='utf-8', errors='strict') -> bytes\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10483 | \n\ |
Victor Stinner | e14e212 | 2010-11-07 18:41:46 +0000 | [diff] [blame] | 10484 | Encode S using the codec registered for encoding. Default encoding\n\ |
| 10485 | 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] | 10486 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 10487 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 10488 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 10489 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10490 | |
| 10491 | static PyObject * |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 10492 | unicode_encode(PyUnicodeObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10493 | { |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 10494 | static char *kwlist[] = {"encoding", "errors", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10495 | char *encoding = NULL; |
| 10496 | char *errors = NULL; |
Guido van Rossum | 35d9428 | 2007-08-27 18:20:11 +0000 | [diff] [blame] | 10497 | |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 10498 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:encode", |
| 10499 | kwlist, &encoding, &errors)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10500 | return NULL; |
Georg Brandl | 3b9406b | 2010-12-03 07:54:09 +0000 | [diff] [blame] | 10501 | return PyUnicode_AsEncodedString((PyObject *)self, encoding, errors); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 10502 | } |
| 10503 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10504 | PyDoc_STRVAR(expandtabs__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10505 | "S.expandtabs([tabsize]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10506 | \n\ |
| 10507 | 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] | 10508 | 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] | 10509 | |
| 10510 | static PyObject* |
| 10511 | unicode_expandtabs(PyUnicodeObject *self, PyObject *args) |
| 10512 | { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10513 | Py_ssize_t i, j, line_pos, src_len, incr; |
| 10514 | Py_UCS4 ch; |
| 10515 | PyObject *u; |
| 10516 | void *src_data, *dest_data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10517 | int tabsize = 8; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10518 | int kind; |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10519 | int found; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10520 | |
| 10521 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10522 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10523 | |
Antoine Pitrou | 2242522 | 2011-10-04 19:10:51 +0200 | [diff] [blame] | 10524 | if (PyUnicode_READY(self) == -1) |
| 10525 | return NULL; |
| 10526 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 10527 | /* First pass: determine size of output string */ |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10528 | src_len = PyUnicode_GET_LENGTH(self); |
| 10529 | i = j = line_pos = 0; |
| 10530 | kind = PyUnicode_KIND(self); |
| 10531 | src_data = PyUnicode_DATA(self); |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10532 | found = 0; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10533 | for (; i < src_len; i++) { |
| 10534 | ch = PyUnicode_READ(kind, src_data, i); |
| 10535 | if (ch == '\t') { |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10536 | found = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10537 | if (tabsize > 0) { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10538 | incr = tabsize - (line_pos % tabsize); /* cannot overflow */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10539 | if (j > PY_SSIZE_T_MAX - incr) |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10540 | goto overflow; |
| 10541 | line_pos += incr; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10542 | j += incr; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10543 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10544 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10545 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10546 | if (j > PY_SSIZE_T_MAX - 1) |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10547 | goto overflow; |
| 10548 | line_pos++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10549 | j++; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10550 | if (ch == '\n' || ch == '\r') |
| 10551 | line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10552 | } |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10553 | } |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10554 | if (!found && PyUnicode_CheckExact(self)) { |
| 10555 | Py_INCREF((PyObject *) self); |
| 10556 | return (PyObject *) self; |
| 10557 | } |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 10558 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10559 | /* Second pass: create output string and fill it */ |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10560 | u = PyUnicode_New(j, PyUnicode_MAX_CHAR_VALUE(self)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10561 | if (!u) |
| 10562 | return NULL; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10563 | dest_data = PyUnicode_DATA(u); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10564 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10565 | i = j = line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10566 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10567 | for (; i < src_len; i++) { |
| 10568 | ch = PyUnicode_READ(kind, src_data, i); |
| 10569 | if (ch == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10570 | if (tabsize > 0) { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10571 | incr = tabsize - (line_pos % tabsize); |
| 10572 | line_pos += incr; |
| 10573 | while (incr--) { |
| 10574 | PyUnicode_WRITE(kind, dest_data, j, ' '); |
| 10575 | j++; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10576 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10577 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10578 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10579 | else { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10580 | line_pos++; |
| 10581 | PyUnicode_WRITE(kind, dest_data, j, ch); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10582 | j++; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10583 | if (ch == '\n' || ch == '\r') |
| 10584 | line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10585 | } |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10586 | } |
| 10587 | assert (j == PyUnicode_GET_LENGTH(u)); |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 10588 | #ifndef DONT_MAKE_RESULT_READY |
| 10589 | if (_PyUnicode_READY_REPLACE(&u)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10590 | Py_DECREF(u); |
| 10591 | return NULL; |
| 10592 | } |
Victor Stinner | 17efeed | 2011-10-04 20:05:46 +0200 | [diff] [blame] | 10593 | #endif |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10594 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10595 | return (PyObject*) u; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10596 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10597 | overflow: |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10598 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 10599 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10600 | } |
| 10601 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10602 | PyDoc_STRVAR(find__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10603 | "S.find(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10604 | \n\ |
| 10605 | Return the lowest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 10606 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10607 | arguments start and end are interpreted as in slice notation.\n\ |
| 10608 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10609 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10610 | |
| 10611 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10612 | unicode_find(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10613 | { |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10614 | PyUnicodeObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 10615 | Py_ssize_t start; |
| 10616 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10617 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10618 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10619 | if (!stringlib_parse_args_finds_unicode("find", args, &substring, |
| 10620 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10621 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10622 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10623 | if (PyUnicode_READY(self) == -1) |
| 10624 | return NULL; |
| 10625 | if (PyUnicode_READY(substring) == -1) |
| 10626 | return NULL; |
| 10627 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 10628 | result = any_find_slice(1, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10629 | self, (PyObject*)substring, start, end |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10630 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10631 | |
| 10632 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10633 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10634 | if (result == -2) |
| 10635 | return NULL; |
| 10636 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 10637 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10638 | } |
| 10639 | |
| 10640 | static PyObject * |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 10641 | unicode_getitem(PyObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10642 | { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 10643 | Py_UCS4 ch = PyUnicode_ReadChar(self, index); |
| 10644 | if (ch == (Py_UCS4)-1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10645 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10646 | return PyUnicode_FromOrdinal(ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10647 | } |
| 10648 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 10649 | /* Believe it or not, this produces the same value for ASCII strings |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 10650 | as bytes_hash(). */ |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 10651 | static Py_hash_t |
Neil Schemenauer | f8c37d1 | 2007-09-07 20:49:04 +0000 | [diff] [blame] | 10652 | unicode_hash(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10653 | { |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 10654 | Py_ssize_t len; |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 10655 | Py_uhash_t x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 10656 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10657 | if (_PyUnicode_HASH(self) != -1) |
| 10658 | return _PyUnicode_HASH(self); |
| 10659 | if (PyUnicode_READY(self) == -1) |
| 10660 | return -1; |
| 10661 | len = PyUnicode_GET_LENGTH(self); |
| 10662 | |
| 10663 | /* The hash function as a macro, gets expanded three times below. */ |
| 10664 | #define HASH(P) \ |
| 10665 | x = (Py_uhash_t)*P << 7; \ |
| 10666 | while (--len >= 0) \ |
| 10667 | x = (1000003*x) ^ (Py_uhash_t)*P++; |
| 10668 | |
| 10669 | switch (PyUnicode_KIND(self)) { |
| 10670 | case PyUnicode_1BYTE_KIND: { |
| 10671 | const unsigned char *c = PyUnicode_1BYTE_DATA(self); |
| 10672 | HASH(c); |
| 10673 | break; |
| 10674 | } |
| 10675 | case PyUnicode_2BYTE_KIND: { |
| 10676 | const Py_UCS2 *s = PyUnicode_2BYTE_DATA(self); |
| 10677 | HASH(s); |
| 10678 | break; |
| 10679 | } |
| 10680 | default: { |
| 10681 | Py_UCS4 *l; |
| 10682 | assert(PyUnicode_KIND(self) == PyUnicode_4BYTE_KIND && |
| 10683 | "Impossible switch case in unicode_hash"); |
| 10684 | l = PyUnicode_4BYTE_DATA(self); |
| 10685 | HASH(l); |
| 10686 | break; |
| 10687 | } |
| 10688 | } |
| 10689 | x ^= (Py_uhash_t)PyUnicode_GET_LENGTH(self); |
| 10690 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 10691 | if (x == -1) |
| 10692 | x = -2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10693 | _PyUnicode_HASH(self) = x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 10694 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10695 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10696 | #undef HASH |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10697 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10698 | PyDoc_STRVAR(index__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10699 | "S.index(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10700 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10701 | 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] | 10702 | |
| 10703 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10704 | unicode_index(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10705 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10706 | Py_ssize_t result; |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10707 | PyUnicodeObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 10708 | Py_ssize_t start; |
| 10709 | Py_ssize_t end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10710 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10711 | if (!stringlib_parse_args_finds_unicode("index", args, &substring, |
| 10712 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10713 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10714 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10715 | if (PyUnicode_READY(self) == -1) |
| 10716 | return NULL; |
| 10717 | if (PyUnicode_READY(substring) == -1) |
| 10718 | return NULL; |
| 10719 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 10720 | result = any_find_slice(1, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10721 | self, (PyObject*)substring, start, end |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10722 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10723 | |
| 10724 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10725 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10726 | if (result == -2) |
| 10727 | return NULL; |
| 10728 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10729 | if (result < 0) { |
| 10730 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 10731 | return NULL; |
| 10732 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10733 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 10734 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10735 | } |
| 10736 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10737 | PyDoc_STRVAR(islower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10738 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10739 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10740 | 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] | 10741 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10742 | |
| 10743 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10744 | unicode_islower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10745 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10746 | Py_ssize_t i, length; |
| 10747 | int kind; |
| 10748 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10749 | int cased; |
| 10750 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10751 | if (PyUnicode_READY(self) == -1) |
| 10752 | return NULL; |
| 10753 | length = PyUnicode_GET_LENGTH(self); |
| 10754 | kind = PyUnicode_KIND(self); |
| 10755 | data = PyUnicode_DATA(self); |
| 10756 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10757 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10758 | if (length == 1) |
| 10759 | return PyBool_FromLong( |
| 10760 | Py_UNICODE_ISLOWER(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10761 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10762 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10763 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10764 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10765 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10766 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10767 | for (i = 0; i < length; i++) { |
| 10768 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10769 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10770 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 10771 | return PyBool_FromLong(0); |
| 10772 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 10773 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10774 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10775 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10776 | } |
| 10777 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10778 | PyDoc_STRVAR(isupper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10779 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10780 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 10781 | 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] | 10782 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10783 | |
| 10784 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10785 | unicode_isupper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10786 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10787 | Py_ssize_t i, length; |
| 10788 | int kind; |
| 10789 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10790 | int cased; |
| 10791 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10792 | if (PyUnicode_READY(self) == -1) |
| 10793 | return NULL; |
| 10794 | length = PyUnicode_GET_LENGTH(self); |
| 10795 | kind = PyUnicode_KIND(self); |
| 10796 | data = PyUnicode_DATA(self); |
| 10797 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10798 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10799 | if (length == 1) |
| 10800 | return PyBool_FromLong( |
| 10801 | Py_UNICODE_ISUPPER(PyUnicode_READ(kind, data, 0)) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10802 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10803 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10804 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10805 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10806 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10807 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10808 | for (i = 0; i < length; i++) { |
| 10809 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10810 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10811 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 10812 | return PyBool_FromLong(0); |
| 10813 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 10814 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10815 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10816 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10817 | } |
| 10818 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10819 | PyDoc_STRVAR(istitle__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10820 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10821 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 10822 | Return True if S is a titlecased string and there is at least one\n\ |
| 10823 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 10824 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 10825 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10826 | |
| 10827 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10828 | unicode_istitle(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10829 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10830 | Py_ssize_t i, length; |
| 10831 | int kind; |
| 10832 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10833 | int cased, previous_is_cased; |
| 10834 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10835 | if (PyUnicode_READY(self) == -1) |
| 10836 | return NULL; |
| 10837 | length = PyUnicode_GET_LENGTH(self); |
| 10838 | kind = PyUnicode_KIND(self); |
| 10839 | data = PyUnicode_DATA(self); |
| 10840 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10841 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10842 | if (length == 1) { |
| 10843 | Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 10844 | return PyBool_FromLong((Py_UNICODE_ISTITLE(ch) != 0) || |
| 10845 | (Py_UNICODE_ISUPPER(ch) != 0)); |
| 10846 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10847 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10848 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10849 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10850 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10851 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10852 | cased = 0; |
| 10853 | previous_is_cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10854 | for (i = 0; i < length; i++) { |
| 10855 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10856 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10857 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 10858 | if (previous_is_cased) |
| 10859 | return PyBool_FromLong(0); |
| 10860 | previous_is_cased = 1; |
| 10861 | cased = 1; |
| 10862 | } |
| 10863 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 10864 | if (!previous_is_cased) |
| 10865 | return PyBool_FromLong(0); |
| 10866 | previous_is_cased = 1; |
| 10867 | cased = 1; |
| 10868 | } |
| 10869 | else |
| 10870 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10871 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10872 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10873 | } |
| 10874 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10875 | PyDoc_STRVAR(isspace__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10876 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10877 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 10878 | Return True if all characters in S are whitespace\n\ |
| 10879 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10880 | |
| 10881 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10882 | unicode_isspace(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10883 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10884 | Py_ssize_t i, length; |
| 10885 | int kind; |
| 10886 | void *data; |
| 10887 | |
| 10888 | if (PyUnicode_READY(self) == -1) |
| 10889 | return NULL; |
| 10890 | length = PyUnicode_GET_LENGTH(self); |
| 10891 | kind = PyUnicode_KIND(self); |
| 10892 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10893 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10894 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10895 | if (length == 1) |
| 10896 | return PyBool_FromLong( |
| 10897 | Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10898 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10899 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10900 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10901 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10902 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10903 | for (i = 0; i < length; i++) { |
| 10904 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 10905 | if (!Py_UNICODE_ISSPACE(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10906 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10907 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10908 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10909 | } |
| 10910 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10911 | PyDoc_STRVAR(isalpha__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10912 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10913 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 10914 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10915 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10916 | |
| 10917 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10918 | unicode_isalpha(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10919 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10920 | Py_ssize_t i, length; |
| 10921 | int kind; |
| 10922 | void *data; |
| 10923 | |
| 10924 | if (PyUnicode_READY(self) == -1) |
| 10925 | return NULL; |
| 10926 | length = PyUnicode_GET_LENGTH(self); |
| 10927 | kind = PyUnicode_KIND(self); |
| 10928 | data = PyUnicode_DATA(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10929 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10930 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10931 | if (length == 1) |
| 10932 | return PyBool_FromLong( |
| 10933 | Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, 0))); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10934 | |
| 10935 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10936 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10937 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10938 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10939 | for (i = 0; i < length; i++) { |
| 10940 | if (!Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10941 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10942 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10943 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10944 | } |
| 10945 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10946 | PyDoc_STRVAR(isalnum__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10947 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10948 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 10949 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10950 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10951 | |
| 10952 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10953 | unicode_isalnum(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10954 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10955 | int kind; |
| 10956 | void *data; |
| 10957 | Py_ssize_t len, i; |
| 10958 | |
| 10959 | if (PyUnicode_READY(self) == -1) |
| 10960 | return NULL; |
| 10961 | |
| 10962 | kind = PyUnicode_KIND(self); |
| 10963 | data = PyUnicode_DATA(self); |
| 10964 | len = PyUnicode_GET_LENGTH(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10965 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10966 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10967 | if (len == 1) { |
| 10968 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 10969 | return PyBool_FromLong(Py_UNICODE_ISALNUM(ch)); |
| 10970 | } |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10971 | |
| 10972 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10973 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10974 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10975 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10976 | for (i = 0; i < len; i++) { |
| 10977 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 10978 | if (!Py_UNICODE_ISALNUM(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10979 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10980 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10981 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10982 | } |
| 10983 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10984 | PyDoc_STRVAR(isdecimal__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10985 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10986 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10987 | 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] | 10988 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10989 | |
| 10990 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10991 | unicode_isdecimal(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10992 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10993 | Py_ssize_t i, length; |
| 10994 | int kind; |
| 10995 | void *data; |
| 10996 | |
| 10997 | if (PyUnicode_READY(self) == -1) |
| 10998 | return NULL; |
| 10999 | length = PyUnicode_GET_LENGTH(self); |
| 11000 | kind = PyUnicode_KIND(self); |
| 11001 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11002 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11003 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11004 | if (length == 1) |
| 11005 | return PyBool_FromLong( |
| 11006 | Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11007 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11008 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11009 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11010 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11011 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11012 | for (i = 0; i < length; i++) { |
| 11013 | if (!Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11014 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11015 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11016 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11017 | } |
| 11018 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11019 | PyDoc_STRVAR(isdigit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11020 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11021 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11022 | Return True if all characters in S are digits\n\ |
| 11023 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11024 | |
| 11025 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11026 | unicode_isdigit(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11027 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11028 | Py_ssize_t i, length; |
| 11029 | int kind; |
| 11030 | void *data; |
| 11031 | |
| 11032 | if (PyUnicode_READY(self) == -1) |
| 11033 | return NULL; |
| 11034 | length = PyUnicode_GET_LENGTH(self); |
| 11035 | kind = PyUnicode_KIND(self); |
| 11036 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11037 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11038 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11039 | if (length == 1) { |
| 11040 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11041 | return PyBool_FromLong(Py_UNICODE_ISDIGIT(ch)); |
| 11042 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11043 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11044 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11045 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11046 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11047 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11048 | for (i = 0; i < length; i++) { |
| 11049 | if (!Py_UNICODE_ISDIGIT(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11050 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11051 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11052 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11053 | } |
| 11054 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11055 | PyDoc_STRVAR(isnumeric__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11056 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11057 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11058 | 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] | 11059 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11060 | |
| 11061 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11062 | unicode_isnumeric(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11063 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11064 | Py_ssize_t i, length; |
| 11065 | int kind; |
| 11066 | void *data; |
| 11067 | |
| 11068 | if (PyUnicode_READY(self) == -1) |
| 11069 | return NULL; |
| 11070 | length = PyUnicode_GET_LENGTH(self); |
| 11071 | kind = PyUnicode_KIND(self); |
| 11072 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11073 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11074 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11075 | if (length == 1) |
| 11076 | return PyBool_FromLong( |
| 11077 | Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11078 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11079 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11080 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11081 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11082 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11083 | for (i = 0; i < length; i++) { |
| 11084 | if (!Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11085 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11086 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11087 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11088 | } |
| 11089 | |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11090 | int |
| 11091 | PyUnicode_IsIdentifier(PyObject *self) |
| 11092 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11093 | int kind; |
| 11094 | void *data; |
| 11095 | Py_ssize_t i; |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11096 | Py_UCS4 first; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11097 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11098 | if (PyUnicode_READY(self) == -1) { |
| 11099 | Py_FatalError("identifier not ready"); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11100 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11101 | } |
| 11102 | |
| 11103 | /* Special case for empty strings */ |
| 11104 | if (PyUnicode_GET_LENGTH(self) == 0) |
| 11105 | return 0; |
| 11106 | kind = PyUnicode_KIND(self); |
| 11107 | data = PyUnicode_DATA(self); |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11108 | |
| 11109 | /* PEP 3131 says that the first character must be in |
| 11110 | XID_Start and subsequent characters in XID_Continue, |
| 11111 | and for the ASCII range, the 2.x rules apply (i.e |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11112 | start with letters and underscore, continue with |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11113 | letters, digits, underscore). However, given the current |
| 11114 | definition of XID_Start and XID_Continue, it is sufficient |
| 11115 | to check just for these, except that _ must be allowed |
| 11116 | as starting an identifier. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11117 | first = PyUnicode_READ(kind, data, 0); |
Benjamin Peterson | f413b80 | 2011-08-12 22:17:18 -0500 | [diff] [blame] | 11118 | if (!_PyUnicode_IsXidStart(first) && first != 0x5F /* LOW LINE */) |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11119 | return 0; |
| 11120 | |
Benjamin Peterson | 9c6e6a0 | 2011-09-28 08:09:05 -0400 | [diff] [blame] | 11121 | for (i = 1; i < PyUnicode_GET_LENGTH(self); i++) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11122 | if (!_PyUnicode_IsXidContinue(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11123 | return 0; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11124 | return 1; |
| 11125 | } |
| 11126 | |
| 11127 | PyDoc_STRVAR(isidentifier__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11128 | "S.isidentifier() -> bool\n\ |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11129 | \n\ |
| 11130 | Return True if S is a valid identifier according\n\ |
| 11131 | to the language definition."); |
| 11132 | |
| 11133 | static PyObject* |
| 11134 | unicode_isidentifier(PyObject *self) |
| 11135 | { |
| 11136 | return PyBool_FromLong(PyUnicode_IsIdentifier(self)); |
| 11137 | } |
| 11138 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11139 | PyDoc_STRVAR(isprintable__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11140 | "S.isprintable() -> bool\n\ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11141 | \n\ |
| 11142 | Return True if all characters in S are considered\n\ |
| 11143 | printable in repr() or S is empty, False otherwise."); |
| 11144 | |
| 11145 | static PyObject* |
| 11146 | unicode_isprintable(PyObject *self) |
| 11147 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11148 | Py_ssize_t i, length; |
| 11149 | int kind; |
| 11150 | void *data; |
| 11151 | |
| 11152 | if (PyUnicode_READY(self) == -1) |
| 11153 | return NULL; |
| 11154 | length = PyUnicode_GET_LENGTH(self); |
| 11155 | kind = PyUnicode_KIND(self); |
| 11156 | data = PyUnicode_DATA(self); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11157 | |
| 11158 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11159 | if (length == 1) |
| 11160 | return PyBool_FromLong( |
| 11161 | Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, 0))); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11162 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11163 | for (i = 0; i < length; i++) { |
| 11164 | if (!Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, i))) { |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11165 | Py_RETURN_FALSE; |
| 11166 | } |
| 11167 | } |
| 11168 | Py_RETURN_TRUE; |
| 11169 | } |
| 11170 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11171 | PyDoc_STRVAR(join__doc__, |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 11172 | "S.join(iterable) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11173 | \n\ |
| 11174 | 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] | 11175 | iterable. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11176 | |
| 11177 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11178 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11179 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11180 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11181 | } |
| 11182 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11183 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11184 | unicode_length(PyUnicodeObject *self) |
| 11185 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11186 | if (PyUnicode_READY(self) == -1) |
| 11187 | return -1; |
| 11188 | return PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11189 | } |
| 11190 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11191 | PyDoc_STRVAR(ljust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11192 | "S.ljust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11193 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 11194 | 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] | 11195 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11196 | |
| 11197 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11198 | unicode_ljust(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11199 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11200 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11201 | Py_UCS4 fillchar = ' '; |
| 11202 | |
| 11203 | if (PyUnicode_READY(self) == -1) |
| 11204 | return NULL; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 11205 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11206 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11207 | return NULL; |
| 11208 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11209 | if (_PyUnicode_LENGTH(self) >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11210 | Py_INCREF(self); |
| 11211 | return (PyObject*) self; |
| 11212 | } |
| 11213 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11214 | return (PyObject*) pad(self, 0, width - _PyUnicode_LENGTH(self), fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11215 | } |
| 11216 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11217 | PyDoc_STRVAR(lower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11218 | "S.lower() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11219 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11220 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11221 | |
| 11222 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11223 | unicode_lower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11224 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11225 | return fixup(self, fixlower); |
| 11226 | } |
| 11227 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11228 | #define LEFTSTRIP 0 |
| 11229 | #define RIGHTSTRIP 1 |
| 11230 | #define BOTHSTRIP 2 |
| 11231 | |
| 11232 | /* Arrays indexed by above */ |
| 11233 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 11234 | |
| 11235 | #define STRIPNAME(i) (stripformat[i]+3) |
| 11236 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11237 | /* externally visible for str.strip(unicode) */ |
| 11238 | PyObject * |
| 11239 | _PyUnicode_XStrip(PyUnicodeObject *self, int striptype, PyObject *sepobj) |
| 11240 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11241 | void *data; |
| 11242 | int kind; |
| 11243 | Py_ssize_t i, j, len; |
| 11244 | BLOOM_MASK sepmask; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11245 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11246 | if (PyUnicode_READY(self) == -1 || PyUnicode_READY(sepobj) == -1) |
| 11247 | return NULL; |
| 11248 | |
| 11249 | kind = PyUnicode_KIND(self); |
| 11250 | data = PyUnicode_DATA(self); |
| 11251 | len = PyUnicode_GET_LENGTH(self); |
| 11252 | sepmask = make_bloom_mask(PyUnicode_KIND(sepobj), |
| 11253 | PyUnicode_DATA(sepobj), |
| 11254 | PyUnicode_GET_LENGTH(sepobj)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11255 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11256 | i = 0; |
| 11257 | if (striptype != RIGHTSTRIP) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11258 | while (i < len && |
| 11259 | BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, i), sepobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11260 | i++; |
| 11261 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11262 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11263 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11264 | j = len; |
| 11265 | if (striptype != LEFTSTRIP) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11266 | do { |
| 11267 | j--; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11268 | } while (j >= i && |
| 11269 | BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, j), sepobj)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11270 | j++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11271 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11272 | |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11273 | return PyUnicode_Substring((PyObject*)self, i, j); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11274 | } |
| 11275 | |
| 11276 | PyObject* |
| 11277 | PyUnicode_Substring(PyObject *self, Py_ssize_t start, Py_ssize_t end) |
| 11278 | { |
| 11279 | unsigned char *data; |
| 11280 | int kind; |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11281 | Py_ssize_t length; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11282 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 11283 | if (PyUnicode_READY(self) == -1) |
| 11284 | return NULL; |
| 11285 | |
| 11286 | end = Py_MIN(end, PyUnicode_GET_LENGTH(self)); |
| 11287 | |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11288 | if (start == 0 && end == PyUnicode_GET_LENGTH(self)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11289 | { |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11290 | if (PyUnicode_CheckExact(self)) { |
| 11291 | Py_INCREF(self); |
| 11292 | return self; |
| 11293 | } |
| 11294 | else |
| 11295 | return PyUnicode_Copy(self); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11296 | } |
| 11297 | |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11298 | length = end - start; |
| 11299 | if (length == 1) |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 11300 | return unicode_getitem(self, start); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11301 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 11302 | if (start < 0 || end < 0) { |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11303 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 11304 | return NULL; |
| 11305 | } |
| 11306 | |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11307 | if (PyUnicode_IS_ASCII(self)) { |
| 11308 | kind = PyUnicode_KIND(self); |
| 11309 | data = PyUnicode_1BYTE_DATA(self); |
| 11310 | return unicode_fromascii(data + start, length); |
| 11311 | } |
| 11312 | else { |
| 11313 | kind = PyUnicode_KIND(self); |
| 11314 | data = PyUnicode_1BYTE_DATA(self); |
| 11315 | return PyUnicode_FromKindAndData(kind, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 11316 | data + kind * start, |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11317 | length); |
| 11318 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11319 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11320 | |
| 11321 | static PyObject * |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11322 | do_strip(PyUnicodeObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11323 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11324 | int kind; |
| 11325 | void *data; |
| 11326 | Py_ssize_t len, i, j; |
| 11327 | |
| 11328 | if (PyUnicode_READY(self) == -1) |
| 11329 | return NULL; |
| 11330 | |
| 11331 | kind = PyUnicode_KIND(self); |
| 11332 | data = PyUnicode_DATA(self); |
| 11333 | len = PyUnicode_GET_LENGTH(self); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11334 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11335 | i = 0; |
| 11336 | if (striptype != RIGHTSTRIP) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11337 | while (i < len && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, i))) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11338 | i++; |
| 11339 | } |
| 11340 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11341 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11342 | j = len; |
| 11343 | if (striptype != LEFTSTRIP) { |
| 11344 | do { |
| 11345 | j--; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11346 | } while (j >= i && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, j))); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11347 | j++; |
| 11348 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11349 | |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11350 | return PyUnicode_Substring((PyObject*)self, i, j); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11351 | } |
| 11352 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11353 | |
| 11354 | static PyObject * |
| 11355 | do_argstrip(PyUnicodeObject *self, int striptype, PyObject *args) |
| 11356 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11357 | PyObject *sep = NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11358 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11359 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
| 11360 | return NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11361 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11362 | if (sep != NULL && sep != Py_None) { |
| 11363 | if (PyUnicode_Check(sep)) |
| 11364 | return _PyUnicode_XStrip(self, striptype, sep); |
| 11365 | else { |
| 11366 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11367 | "%s arg must be None or str", |
| 11368 | STRIPNAME(striptype)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11369 | return NULL; |
| 11370 | } |
| 11371 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11372 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11373 | return do_strip(self, striptype); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11374 | } |
| 11375 | |
| 11376 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11377 | PyDoc_STRVAR(strip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11378 | "S.strip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11379 | \n\ |
| 11380 | Return a copy of the string S with leading and trailing\n\ |
| 11381 | whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11382 | 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] | 11383 | |
| 11384 | static PyObject * |
| 11385 | unicode_strip(PyUnicodeObject *self, PyObject *args) |
| 11386 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11387 | if (PyTuple_GET_SIZE(args) == 0) |
| 11388 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 11389 | else |
| 11390 | return do_argstrip(self, BOTHSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11391 | } |
| 11392 | |
| 11393 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11394 | PyDoc_STRVAR(lstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11395 | "S.lstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11396 | \n\ |
| 11397 | Return a copy of the string S with leading whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11398 | 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] | 11399 | |
| 11400 | static PyObject * |
| 11401 | unicode_lstrip(PyUnicodeObject *self, PyObject *args) |
| 11402 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11403 | if (PyTuple_GET_SIZE(args) == 0) |
| 11404 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 11405 | else |
| 11406 | return do_argstrip(self, LEFTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11407 | } |
| 11408 | |
| 11409 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11410 | PyDoc_STRVAR(rstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11411 | "S.rstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11412 | \n\ |
| 11413 | Return a copy of the string S with trailing whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11414 | 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] | 11415 | |
| 11416 | static PyObject * |
| 11417 | unicode_rstrip(PyUnicodeObject *self, PyObject *args) |
| 11418 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11419 | if (PyTuple_GET_SIZE(args) == 0) |
| 11420 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 11421 | else |
| 11422 | return do_argstrip(self, RIGHTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11423 | } |
| 11424 | |
| 11425 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11426 | static PyObject* |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11427 | unicode_repeat(PyUnicodeObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11428 | { |
| 11429 | PyUnicodeObject *u; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11430 | Py_ssize_t nchars, n; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11431 | |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 11432 | if (len < 1) { |
| 11433 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 11434 | return unicode_empty; |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 11435 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11436 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 11437 | if (len == 1 && PyUnicode_CheckExact(str)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11438 | /* no repeat, return original string */ |
| 11439 | Py_INCREF(str); |
| 11440 | return (PyObject*) str; |
| 11441 | } |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 11442 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11443 | if (PyUnicode_READY(str) == -1) |
| 11444 | return NULL; |
| 11445 | |
Victor Stinner | c759f3e | 2011-10-01 03:09:58 +0200 | [diff] [blame] | 11446 | if (PyUnicode_GET_LENGTH(str) > PY_SSIZE_T_MAX / len) { |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11447 | PyErr_SetString(PyExc_OverflowError, |
| 11448 | "repeated string is too long"); |
| 11449 | return NULL; |
| 11450 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11451 | nchars = len * PyUnicode_GET_LENGTH(str); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11452 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11453 | u = (PyUnicodeObject *)PyUnicode_New(nchars, PyUnicode_MAX_CHAR_VALUE(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11454 | if (!u) |
| 11455 | return NULL; |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11456 | assert(PyUnicode_KIND(u) == PyUnicode_KIND(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11457 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11458 | if (PyUnicode_GET_LENGTH(str) == 1) { |
| 11459 | const int kind = PyUnicode_KIND(str); |
| 11460 | const Py_UCS4 fill_char = PyUnicode_READ(kind, PyUnicode_DATA(str), 0); |
| 11461 | void *to = PyUnicode_DATA(u); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11462 | if (kind == PyUnicode_1BYTE_KIND) |
| 11463 | memset(to, (unsigned char)fill_char, len); |
| 11464 | else { |
| 11465 | for (n = 0; n < len; ++n) |
| 11466 | PyUnicode_WRITE(kind, to, n, fill_char); |
| 11467 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11468 | } |
| 11469 | else { |
| 11470 | /* number of characters copied this far */ |
| 11471 | Py_ssize_t done = PyUnicode_GET_LENGTH(str); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 11472 | const Py_ssize_t char_size = PyUnicode_KIND(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11473 | char *to = (char *) PyUnicode_DATA(u); |
| 11474 | Py_MEMCPY(to, PyUnicode_DATA(str), |
| 11475 | PyUnicode_GET_LENGTH(str) * char_size); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11476 | while (done < nchars) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11477 | n = (done <= nchars-done) ? done : nchars-done; |
| 11478 | Py_MEMCPY(to + (done * char_size), to, n * char_size); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11479 | done += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11480 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11481 | } |
| 11482 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 11483 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11484 | return (PyObject*) u; |
| 11485 | } |
| 11486 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11487 | PyObject * |
| 11488 | PyUnicode_Replace(PyObject *obj, |
| 11489 | PyObject *subobj, |
| 11490 | PyObject *replobj, |
| 11491 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11492 | { |
| 11493 | PyObject *self; |
| 11494 | PyObject *str1; |
| 11495 | PyObject *str2; |
| 11496 | PyObject *result; |
| 11497 | |
| 11498 | self = PyUnicode_FromObject(obj); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11499 | if (self == NULL || PyUnicode_READY(self) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11500 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11501 | str1 = PyUnicode_FromObject(subobj); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11502 | if (str1 == NULL || PyUnicode_READY(str1) == -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11503 | Py_DECREF(self); |
| 11504 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11505 | } |
| 11506 | str2 = PyUnicode_FromObject(replobj); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11507 | if (str2 == NULL || PyUnicode_READY(str2)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11508 | Py_DECREF(self); |
| 11509 | Py_DECREF(str1); |
| 11510 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11511 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11512 | result = replace(self, str1, str2, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11513 | Py_DECREF(self); |
| 11514 | Py_DECREF(str1); |
| 11515 | Py_DECREF(str2); |
| 11516 | return result; |
| 11517 | } |
| 11518 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11519 | PyDoc_STRVAR(replace__doc__, |
Ezio Melotti | c1897e7 | 2010-06-26 18:50:39 +0000 | [diff] [blame] | 11520 | "S.replace(old, new[, count]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11521 | \n\ |
| 11522 | Return a copy of S with all occurrences of substring\n\ |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 11523 | old replaced by new. If the optional argument count is\n\ |
| 11524 | given, only the first count occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11525 | |
| 11526 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11527 | unicode_replace(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11528 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11529 | PyObject *str1; |
| 11530 | PyObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11531 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11532 | PyObject *result; |
| 11533 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11534 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11535 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11536 | if (!PyUnicode_READY(self) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11537 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11538 | str1 = PyUnicode_FromObject(str1); |
| 11539 | if (str1 == NULL || PyUnicode_READY(str1) == -1) |
| 11540 | return NULL; |
| 11541 | str2 = PyUnicode_FromObject(str2); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11542 | if (str2 == NULL || PyUnicode_READY(str2) == -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11543 | Py_DECREF(str1); |
| 11544 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 11545 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11546 | |
| 11547 | result = replace(self, str1, str2, maxcount); |
| 11548 | |
| 11549 | Py_DECREF(str1); |
| 11550 | Py_DECREF(str2); |
| 11551 | return result; |
| 11552 | } |
| 11553 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11554 | static PyObject * |
| 11555 | unicode_repr(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11556 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11557 | PyObject *repr; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11558 | Py_ssize_t isize; |
| 11559 | Py_ssize_t osize, squote, dquote, i, o; |
| 11560 | Py_UCS4 max, quote; |
| 11561 | int ikind, okind; |
| 11562 | void *idata, *odata; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11563 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11564 | if (PyUnicode_READY(unicode) == -1) |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11565 | return NULL; |
| 11566 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11567 | isize = PyUnicode_GET_LENGTH(unicode); |
| 11568 | idata = PyUnicode_DATA(unicode); |
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 | /* Compute length of output, quote characters, and |
| 11571 | maximum character */ |
| 11572 | osize = 2; /* quotes */ |
| 11573 | max = 127; |
| 11574 | squote = dquote = 0; |
| 11575 | ikind = PyUnicode_KIND(unicode); |
| 11576 | for (i = 0; i < isize; i++) { |
| 11577 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
| 11578 | switch (ch) { |
| 11579 | case '\'': squote++; osize++; break; |
| 11580 | case '"': dquote++; osize++; break; |
| 11581 | case '\\': case '\t': case '\r': case '\n': |
| 11582 | osize += 2; break; |
| 11583 | default: |
| 11584 | /* Fast-path ASCII */ |
| 11585 | if (ch < ' ' || ch == 0x7f) |
| 11586 | osize += 4; /* \xHH */ |
| 11587 | else if (ch < 0x7f) |
| 11588 | osize++; |
| 11589 | else if (Py_UNICODE_ISPRINTABLE(ch)) { |
| 11590 | osize++; |
| 11591 | max = ch > max ? ch : max; |
| 11592 | } |
| 11593 | else if (ch < 0x100) |
| 11594 | osize += 4; /* \xHH */ |
| 11595 | else if (ch < 0x10000) |
| 11596 | osize += 6; /* \uHHHH */ |
| 11597 | else |
| 11598 | osize += 10; /* \uHHHHHHHH */ |
| 11599 | } |
| 11600 | } |
| 11601 | |
| 11602 | quote = '\''; |
| 11603 | if (squote) { |
| 11604 | if (dquote) |
| 11605 | /* Both squote and dquote present. Use squote, |
| 11606 | and escape them */ |
| 11607 | osize += squote; |
| 11608 | else |
| 11609 | quote = '"'; |
| 11610 | } |
| 11611 | |
| 11612 | repr = PyUnicode_New(osize, max); |
| 11613 | if (repr == NULL) |
| 11614 | return NULL; |
| 11615 | okind = PyUnicode_KIND(repr); |
| 11616 | odata = PyUnicode_DATA(repr); |
| 11617 | |
| 11618 | PyUnicode_WRITE(okind, odata, 0, quote); |
| 11619 | PyUnicode_WRITE(okind, odata, osize-1, quote); |
| 11620 | |
| 11621 | for (i = 0, o = 1; i < isize; i++) { |
| 11622 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11623 | |
| 11624 | /* Escape quotes and backslashes */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11625 | if ((ch == quote) || (ch == '\\')) { |
| 11626 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11627 | PyUnicode_WRITE(okind, odata, o++, ch); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11628 | continue; |
| 11629 | } |
| 11630 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11631 | /* Map special whitespace to '\t', \n', '\r' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11632 | if (ch == '\t') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11633 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11634 | PyUnicode_WRITE(okind, odata, o++, 't'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11635 | } |
| 11636 | else if (ch == '\n') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11637 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11638 | PyUnicode_WRITE(okind, odata, o++, 'n'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11639 | } |
| 11640 | else if (ch == '\r') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11641 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11642 | PyUnicode_WRITE(okind, odata, o++, 'r'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11643 | } |
| 11644 | |
| 11645 | /* Map non-printable US ASCII to '\xhh' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11646 | else if (ch < ' ' || ch == 0x7F) { |
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++, 'x'); |
| 11649 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 4) & 0x000F]); |
| 11650 | PyUnicode_WRITE(okind, odata, o++, hexdigits[ch & 0x000F]); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11651 | } |
| 11652 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11653 | /* Copy ASCII characters as-is */ |
| 11654 | else if (ch < 0x7F) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11655 | PyUnicode_WRITE(okind, odata, o++, ch); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11656 | } |
| 11657 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11658 | /* Non-ASCII characters */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11659 | else { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11660 | /* Map Unicode whitespace and control characters |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11661 | (categories Z* and C* except ASCII space) |
| 11662 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11663 | if (!Py_UNICODE_ISPRINTABLE(ch)) { |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11664 | /* Map 8-bit characters to '\xhh' */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11665 | if (ch <= 0xff) { |
| 11666 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11667 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
| 11668 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 4) & 0x000F]); |
| 11669 | PyUnicode_WRITE(okind, odata, o++, hexdigits[ch & 0x000F]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11670 | } |
| 11671 | /* Map 21-bit characters to '\U00xxxxxx' */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11672 | else if (ch >= 0x10000) { |
| 11673 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11674 | PyUnicode_WRITE(okind, odata, o++, 'U'); |
| 11675 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 28) & 0xF]); |
| 11676 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 24) & 0xF]); |
| 11677 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 20) & 0xF]); |
| 11678 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 16) & 0xF]); |
| 11679 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 12) & 0xF]); |
| 11680 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 8) & 0xF]); |
| 11681 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 4) & 0xF]); |
| 11682 | PyUnicode_WRITE(okind, odata, o++, hexdigits[ch & 0xF]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11683 | } |
| 11684 | /* Map 16-bit characters to '\uxxxx' */ |
| 11685 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11686 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11687 | PyUnicode_WRITE(okind, odata, o++, 'u'); |
| 11688 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 12) & 0xF]); |
| 11689 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 8) & 0xF]); |
| 11690 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 4) & 0xF]); |
| 11691 | PyUnicode_WRITE(okind, odata, o++, hexdigits[ch & 0xF]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11692 | } |
| 11693 | } |
| 11694 | /* Copy characters as-is */ |
| 11695 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11696 | PyUnicode_WRITE(okind, odata, o++, ch); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11697 | } |
| 11698 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11699 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11700 | /* Closing quote already added at the beginning */ |
Victor Stinner | 05d1189 | 2011-10-06 01:13:58 +0200 | [diff] [blame] | 11701 | assert(_PyUnicode_CheckConsistency(repr, 1)); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11702 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11703 | } |
| 11704 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11705 | PyDoc_STRVAR(rfind__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11706 | "S.rfind(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11707 | \n\ |
| 11708 | Return the highest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 11709 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11710 | arguments start and end are interpreted as in slice notation.\n\ |
| 11711 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11712 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11713 | |
| 11714 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11715 | unicode_rfind(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11716 | { |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11717 | PyUnicodeObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 11718 | Py_ssize_t start; |
| 11719 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11720 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11721 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11722 | if (!stringlib_parse_args_finds_unicode("rfind", args, &substring, |
| 11723 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11724 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11725 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11726 | if (PyUnicode_READY(self) == -1) |
| 11727 | return NULL; |
| 11728 | if (PyUnicode_READY(substring) == -1) |
| 11729 | return NULL; |
| 11730 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 11731 | result = any_find_slice(-1, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11732 | self, (PyObject*)substring, start, end |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11733 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11734 | |
| 11735 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11736 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11737 | if (result == -2) |
| 11738 | return NULL; |
| 11739 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11740 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11741 | } |
| 11742 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11743 | PyDoc_STRVAR(rindex__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11744 | "S.rindex(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11745 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11746 | 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] | 11747 | |
| 11748 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11749 | unicode_rindex(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11750 | { |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11751 | PyUnicodeObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 11752 | Py_ssize_t start; |
| 11753 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11754 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11755 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11756 | if (!stringlib_parse_args_finds_unicode("rindex", args, &substring, |
| 11757 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11758 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11759 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11760 | if (PyUnicode_READY(self) == -1) |
| 11761 | return NULL; |
| 11762 | if (PyUnicode_READY(substring) == -1) |
| 11763 | return NULL; |
| 11764 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 11765 | result = any_find_slice(-1, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11766 | self, (PyObject*)substring, start, end |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11767 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11768 | |
| 11769 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11770 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11771 | if (result == -2) |
| 11772 | return NULL; |
| 11773 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11774 | if (result < 0) { |
| 11775 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 11776 | return NULL; |
| 11777 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11778 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11779 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11780 | } |
| 11781 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11782 | PyDoc_STRVAR(rjust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11783 | "S.rjust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11784 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 11785 | 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] | 11786 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11787 | |
| 11788 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11789 | unicode_rjust(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11790 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11791 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11792 | Py_UCS4 fillchar = ' '; |
| 11793 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11794 | 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] | 11795 | return NULL; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 11796 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11797 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11798 | return NULL; |
| 11799 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11800 | if (_PyUnicode_LENGTH(self) >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11801 | Py_INCREF(self); |
| 11802 | return (PyObject*) self; |
| 11803 | } |
| 11804 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11805 | return (PyObject*) pad(self, width - _PyUnicode_LENGTH(self), 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11806 | } |
| 11807 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11808 | PyObject * |
| 11809 | PyUnicode_Split(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11810 | { |
| 11811 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11812 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11813 | s = PyUnicode_FromObject(s); |
| 11814 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11815 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11816 | if (sep != NULL) { |
| 11817 | sep = PyUnicode_FromObject(sep); |
| 11818 | if (sep == NULL) { |
| 11819 | Py_DECREF(s); |
| 11820 | return NULL; |
| 11821 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11822 | } |
| 11823 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11824 | result = split(s, sep, maxsplit); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11825 | |
| 11826 | Py_DECREF(s); |
| 11827 | Py_XDECREF(sep); |
| 11828 | return result; |
| 11829 | } |
| 11830 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11831 | PyDoc_STRVAR(split__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11832 | "S.split([sep[, maxsplit]]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11833 | \n\ |
| 11834 | Return a list of the words in S, using sep as the\n\ |
| 11835 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 11836 | 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] | 11837 | whitespace string is a separator and empty strings are\n\ |
| 11838 | removed from the result."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11839 | |
| 11840 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11841 | unicode_split(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11842 | { |
| 11843 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11844 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11845 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11846 | if (!PyArg_ParseTuple(args, "|On:split", &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11847 | return NULL; |
| 11848 | |
| 11849 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11850 | return split(self, NULL, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11851 | else if (PyUnicode_Check(substring)) |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11852 | return split(self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11853 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11854 | return PyUnicode_Split((PyObject *)self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11855 | } |
| 11856 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11857 | PyObject * |
| 11858 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 11859 | { |
| 11860 | PyObject* str_obj; |
| 11861 | PyObject* sep_obj; |
| 11862 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11863 | int kind1, kind2, kind; |
| 11864 | void *buf1 = NULL, *buf2 = NULL; |
| 11865 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11866 | |
| 11867 | str_obj = PyUnicode_FromObject(str_in); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11868 | if (!str_obj || PyUnicode_READY(str_obj) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11869 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11870 | sep_obj = PyUnicode_FromObject(sep_in); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11871 | if (!sep_obj || PyUnicode_READY(sep_obj) == -1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11872 | Py_DECREF(str_obj); |
| 11873 | return NULL; |
| 11874 | } |
| 11875 | |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 11876 | kind1 = PyUnicode_KIND(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11877 | kind2 = PyUnicode_KIND(sep_obj); |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 11878 | kind = Py_MAX(kind1, kind2); |
| 11879 | buf1 = PyUnicode_DATA(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11880 | if (kind1 != kind) |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 11881 | buf1 = _PyUnicode_AsKind(str_obj, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11882 | if (!buf1) |
| 11883 | goto onError; |
| 11884 | buf2 = PyUnicode_DATA(sep_obj); |
| 11885 | if (kind2 != kind) |
| 11886 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 11887 | if (!buf2) |
| 11888 | goto onError; |
| 11889 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 11890 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 11891 | |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 11892 | switch(PyUnicode_KIND(str_obj)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11893 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 11894 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) |
| 11895 | out = asciilib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 11896 | else |
| 11897 | 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] | 11898 | break; |
| 11899 | case PyUnicode_2BYTE_KIND: |
| 11900 | out = ucs2lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 11901 | break; |
| 11902 | case PyUnicode_4BYTE_KIND: |
| 11903 | out = ucs4lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 11904 | break; |
| 11905 | default: |
| 11906 | assert(0); |
| 11907 | out = 0; |
| 11908 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11909 | |
| 11910 | Py_DECREF(sep_obj); |
| 11911 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11912 | if (kind1 != kind) |
| 11913 | PyMem_Free(buf1); |
| 11914 | if (kind2 != kind) |
| 11915 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11916 | |
| 11917 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11918 | onError: |
| 11919 | Py_DECREF(sep_obj); |
| 11920 | Py_DECREF(str_obj); |
| 11921 | if (kind1 != kind && buf1) |
| 11922 | PyMem_Free(buf1); |
| 11923 | if (kind2 != kind && buf2) |
| 11924 | PyMem_Free(buf2); |
| 11925 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11926 | } |
| 11927 | |
| 11928 | |
| 11929 | PyObject * |
| 11930 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 11931 | { |
| 11932 | PyObject* str_obj; |
| 11933 | PyObject* sep_obj; |
| 11934 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11935 | int kind1, kind2, kind; |
| 11936 | void *buf1 = NULL, *buf2 = NULL; |
| 11937 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11938 | |
| 11939 | str_obj = PyUnicode_FromObject(str_in); |
| 11940 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11941 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11942 | sep_obj = PyUnicode_FromObject(sep_in); |
| 11943 | if (!sep_obj) { |
| 11944 | Py_DECREF(str_obj); |
| 11945 | return NULL; |
| 11946 | } |
| 11947 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11948 | kind1 = PyUnicode_KIND(str_in); |
| 11949 | kind2 = PyUnicode_KIND(sep_obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 11950 | kind = Py_MAX(kind1, kind2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11951 | buf1 = PyUnicode_DATA(str_in); |
| 11952 | if (kind1 != kind) |
| 11953 | buf1 = _PyUnicode_AsKind(str_in, kind); |
| 11954 | if (!buf1) |
| 11955 | goto onError; |
| 11956 | buf2 = PyUnicode_DATA(sep_obj); |
| 11957 | if (kind2 != kind) |
| 11958 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 11959 | if (!buf2) |
| 11960 | goto onError; |
| 11961 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 11962 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 11963 | |
| 11964 | switch(PyUnicode_KIND(str_in)) { |
| 11965 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 11966 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) |
| 11967 | out = asciilib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 11968 | else |
| 11969 | 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] | 11970 | break; |
| 11971 | case PyUnicode_2BYTE_KIND: |
| 11972 | out = ucs2lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 11973 | break; |
| 11974 | case PyUnicode_4BYTE_KIND: |
| 11975 | out = ucs4lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 11976 | break; |
| 11977 | default: |
| 11978 | assert(0); |
| 11979 | out = 0; |
| 11980 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11981 | |
| 11982 | Py_DECREF(sep_obj); |
| 11983 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11984 | if (kind1 != kind) |
| 11985 | PyMem_Free(buf1); |
| 11986 | if (kind2 != kind) |
| 11987 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11988 | |
| 11989 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11990 | onError: |
| 11991 | Py_DECREF(sep_obj); |
| 11992 | Py_DECREF(str_obj); |
| 11993 | if (kind1 != kind && buf1) |
| 11994 | PyMem_Free(buf1); |
| 11995 | if (kind2 != kind && buf2) |
| 11996 | PyMem_Free(buf2); |
| 11997 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11998 | } |
| 11999 | |
| 12000 | PyDoc_STRVAR(partition__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12001 | "S.partition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12002 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 12003 | 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] | 12004 | 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] | 12005 | found, return S and two empty strings."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12006 | |
| 12007 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12008 | unicode_partition(PyObject *self, PyObject *separator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12009 | { |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12010 | return PyUnicode_Partition(self, separator); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12011 | } |
| 12012 | |
| 12013 | PyDoc_STRVAR(rpartition__doc__, |
Ezio Melotti | 5b2b242 | 2010-01-25 11:58:28 +0000 | [diff] [blame] | 12014 | "S.rpartition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12015 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 12016 | 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] | 12017 | 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] | 12018 | separator is not found, return two empty strings and S."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12019 | |
| 12020 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12021 | unicode_rpartition(PyObject *self, PyObject *separator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12022 | { |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12023 | return PyUnicode_RPartition(self, separator); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12024 | } |
| 12025 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12026 | PyObject * |
| 12027 | PyUnicode_RSplit(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12028 | { |
| 12029 | PyObject *result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12030 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12031 | s = PyUnicode_FromObject(s); |
| 12032 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12033 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12034 | if (sep != NULL) { |
| 12035 | sep = PyUnicode_FromObject(sep); |
| 12036 | if (sep == NULL) { |
| 12037 | Py_DECREF(s); |
| 12038 | return NULL; |
| 12039 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12040 | } |
| 12041 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12042 | result = rsplit(s, sep, maxsplit); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12043 | |
| 12044 | Py_DECREF(s); |
| 12045 | Py_XDECREF(sep); |
| 12046 | return result; |
| 12047 | } |
| 12048 | |
| 12049 | PyDoc_STRVAR(rsplit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12050 | "S.rsplit([sep[, maxsplit]]) -> list of strings\n\ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12051 | \n\ |
| 12052 | Return a list of the words in S, using sep as the\n\ |
| 12053 | delimiter string, starting at the end of the string and\n\ |
| 12054 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 12055 | splits are done. If sep is not specified, any whitespace string\n\ |
| 12056 | is a separator."); |
| 12057 | |
| 12058 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12059 | unicode_rsplit(PyObject *self, PyObject *args) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12060 | { |
| 12061 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12062 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12063 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12064 | if (!PyArg_ParseTuple(args, "|On:rsplit", &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12065 | return NULL; |
| 12066 | |
| 12067 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12068 | return rsplit(self, NULL, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12069 | else if (PyUnicode_Check(substring)) |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12070 | return rsplit(self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12071 | else |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12072 | return PyUnicode_RSplit(self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12073 | } |
| 12074 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12075 | PyDoc_STRVAR(splitlines__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12076 | "S.splitlines([keepends]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12077 | \n\ |
| 12078 | 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] | 12079 | 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] | 12080 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12081 | |
| 12082 | static PyObject* |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12083 | unicode_splitlines(PyUnicodeObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12084 | { |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12085 | static char *kwlist[] = {"keepends", 0}; |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 12086 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12087 | |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12088 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:splitlines", |
| 12089 | kwlist, &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12090 | return NULL; |
| 12091 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 12092 | return PyUnicode_Splitlines((PyObject *)self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12093 | } |
| 12094 | |
| 12095 | static |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 12096 | PyObject *unicode_str(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12097 | { |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 12098 | if (PyUnicode_CheckExact(self)) { |
| 12099 | Py_INCREF(self); |
| 12100 | return self; |
| 12101 | } else |
| 12102 | /* Subtype -- return genuine unicode string with the same value. */ |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 12103 | return PyUnicode_Copy(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12104 | } |
| 12105 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12106 | PyDoc_STRVAR(swapcase__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12107 | "S.swapcase() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12108 | \n\ |
| 12109 | 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] | 12110 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12111 | |
| 12112 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12113 | unicode_swapcase(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12114 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12115 | return fixup(self, fixswapcase); |
| 12116 | } |
| 12117 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12118 | PyDoc_STRVAR(maketrans__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12119 | "str.maketrans(x[, y[, z]]) -> dict (static method)\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12120 | \n\ |
| 12121 | Return a translation table usable for str.translate().\n\ |
| 12122 | If there is only one argument, it must be a dictionary mapping Unicode\n\ |
| 12123 | ordinals (integers) or characters to Unicode ordinals, strings or None.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12124 | Character keys will be then converted to ordinals.\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12125 | If there are two arguments, they must be strings of equal length, and\n\ |
| 12126 | in the resulting dictionary, each character in x will be mapped to the\n\ |
| 12127 | character at the same position in y. If there is a third argument, it\n\ |
| 12128 | must be a string, whose characters will be mapped to None in the result."); |
| 12129 | |
| 12130 | static PyObject* |
| 12131 | unicode_maketrans(PyUnicodeObject *null, PyObject *args) |
| 12132 | { |
| 12133 | PyObject *x, *y = NULL, *z = NULL; |
| 12134 | PyObject *new = NULL, *key, *value; |
| 12135 | Py_ssize_t i = 0; |
| 12136 | int res; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12137 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12138 | if (!PyArg_ParseTuple(args, "O|UU:maketrans", &x, &y, &z)) |
| 12139 | return NULL; |
| 12140 | new = PyDict_New(); |
| 12141 | if (!new) |
| 12142 | return NULL; |
| 12143 | if (y != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12144 | int x_kind, y_kind, z_kind; |
| 12145 | void *x_data, *y_data, *z_data; |
| 12146 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12147 | /* x must be a string too, of equal length */ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12148 | if (!PyUnicode_Check(x)) { |
| 12149 | PyErr_SetString(PyExc_TypeError, "first maketrans argument must " |
| 12150 | "be a string if there is a second argument"); |
| 12151 | goto err; |
| 12152 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12153 | if (PyUnicode_GET_LENGTH(x) != PyUnicode_GET_LENGTH(y)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12154 | PyErr_SetString(PyExc_ValueError, "the first two maketrans " |
| 12155 | "arguments must have equal length"); |
| 12156 | goto err; |
| 12157 | } |
| 12158 | /* 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] | 12159 | x_kind = PyUnicode_KIND(x); |
| 12160 | y_kind = PyUnicode_KIND(y); |
| 12161 | x_data = PyUnicode_DATA(x); |
| 12162 | y_data = PyUnicode_DATA(y); |
| 12163 | for (i = 0; i < PyUnicode_GET_LENGTH(x); i++) { |
| 12164 | key = PyLong_FromLong(PyUnicode_READ(x_kind, x_data, i)); |
| 12165 | value = PyLong_FromLong(PyUnicode_READ(y_kind, y_data, i)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12166 | if (!key || !value) |
| 12167 | goto err; |
| 12168 | res = PyDict_SetItem(new, key, value); |
| 12169 | Py_DECREF(key); |
| 12170 | Py_DECREF(value); |
| 12171 | if (res < 0) |
| 12172 | goto err; |
| 12173 | } |
| 12174 | /* create entries for deleting chars in z */ |
| 12175 | if (z != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12176 | z_kind = PyUnicode_KIND(z); |
| 12177 | z_data = PyUnicode_DATA(z); |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 12178 | for (i = 0; i < PyUnicode_GET_LENGTH(z); i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12179 | key = PyLong_FromLong(PyUnicode_READ(z_kind, z_data, i)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12180 | if (!key) |
| 12181 | goto err; |
| 12182 | res = PyDict_SetItem(new, key, Py_None); |
| 12183 | Py_DECREF(key); |
| 12184 | if (res < 0) |
| 12185 | goto err; |
| 12186 | } |
| 12187 | } |
| 12188 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12189 | int kind; |
| 12190 | void *data; |
| 12191 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12192 | /* x must be a dict */ |
Raymond Hettinger | 3ad0576 | 2009-05-29 22:11:22 +0000 | [diff] [blame] | 12193 | if (!PyDict_CheckExact(x)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12194 | PyErr_SetString(PyExc_TypeError, "if you give only one argument " |
| 12195 | "to maketrans it must be a dict"); |
| 12196 | goto err; |
| 12197 | } |
| 12198 | /* copy entries into the new dict, converting string keys to int keys */ |
| 12199 | while (PyDict_Next(x, &i, &key, &value)) { |
| 12200 | if (PyUnicode_Check(key)) { |
| 12201 | /* convert string keys to integer keys */ |
| 12202 | PyObject *newkey; |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 12203 | if (PyUnicode_GET_LENGTH(key) != 1) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12204 | PyErr_SetString(PyExc_ValueError, "string keys in translate " |
| 12205 | "table must be of length 1"); |
| 12206 | goto err; |
| 12207 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12208 | kind = PyUnicode_KIND(key); |
| 12209 | data = PyUnicode_DATA(key); |
| 12210 | newkey = PyLong_FromLong(PyUnicode_READ(kind, data, 0)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12211 | if (!newkey) |
| 12212 | goto err; |
| 12213 | res = PyDict_SetItem(new, newkey, value); |
| 12214 | Py_DECREF(newkey); |
| 12215 | if (res < 0) |
| 12216 | goto err; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12217 | } else if (PyLong_Check(key)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12218 | /* just keep integer keys */ |
| 12219 | if (PyDict_SetItem(new, key, value) < 0) |
| 12220 | goto err; |
| 12221 | } else { |
| 12222 | PyErr_SetString(PyExc_TypeError, "keys in translate table must " |
| 12223 | "be strings or integers"); |
| 12224 | goto err; |
| 12225 | } |
| 12226 | } |
| 12227 | } |
| 12228 | return new; |
| 12229 | err: |
| 12230 | Py_DECREF(new); |
| 12231 | return NULL; |
| 12232 | } |
| 12233 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12234 | PyDoc_STRVAR(translate__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12235 | "S.translate(table) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12236 | \n\ |
| 12237 | Return a copy of the string S, where all characters have been mapped\n\ |
| 12238 | through the given translation table, which must be a mapping of\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12239 | Unicode ordinals to Unicode ordinals, strings, or None.\n\ |
Walter Dörwald | 5c1ee17 | 2002-09-04 20:31:32 +0000 | [diff] [blame] | 12240 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 12241 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12242 | |
| 12243 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12244 | unicode_translate(PyObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12245 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12246 | return _PyUnicode_TranslateCharmap(self, table, "ignore"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12247 | } |
| 12248 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12249 | PyDoc_STRVAR(upper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12250 | "S.upper() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12251 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12252 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12253 | |
| 12254 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12255 | unicode_upper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12256 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12257 | return fixup(self, fixupper); |
| 12258 | } |
| 12259 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12260 | PyDoc_STRVAR(zfill__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12261 | "S.zfill(width) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12262 | \n\ |
Benjamin Peterson | 9aa4299 | 2008-09-10 21:57:34 +0000 | [diff] [blame] | 12263 | Pad a numeric string S with zeros on the left, to fill a field\n\ |
| 12264 | of the specified width. The string S is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12265 | |
| 12266 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12267 | unicode_zfill(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12268 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12269 | Py_ssize_t fill; |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12270 | PyObject *u; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12271 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12272 | int kind; |
| 12273 | void *data; |
| 12274 | Py_UCS4 chr; |
| 12275 | |
| 12276 | if (PyUnicode_READY(self) == -1) |
| 12277 | return NULL; |
| 12278 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12279 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12280 | return NULL; |
| 12281 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12282 | if (PyUnicode_GET_LENGTH(self) >= width) { |
Walter Dörwald | 0fe940c | 2002-04-15 18:42:15 +0000 | [diff] [blame] | 12283 | if (PyUnicode_CheckExact(self)) { |
| 12284 | Py_INCREF(self); |
| 12285 | return (PyObject*) self; |
| 12286 | } |
| 12287 | else |
Victor Stinner | 2219e0a | 2011-10-01 01:16:59 +0200 | [diff] [blame] | 12288 | return PyUnicode_Copy((PyObject*)self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12289 | } |
| 12290 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12291 | fill = width - _PyUnicode_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12292 | |
| 12293 | u = pad(self, fill, 0, '0'); |
| 12294 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 12295 | if (u == NULL) |
| 12296 | return NULL; |
| 12297 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12298 | kind = PyUnicode_KIND(u); |
| 12299 | data = PyUnicode_DATA(u); |
| 12300 | chr = PyUnicode_READ(kind, data, fill); |
| 12301 | |
| 12302 | if (chr == '+' || chr == '-') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12303 | /* move sign to beginning of string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12304 | PyUnicode_WRITE(kind, data, 0, chr); |
| 12305 | PyUnicode_WRITE(kind, data, fill, '0'); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12306 | } |
| 12307 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12308 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12309 | return (PyObject*) u; |
| 12310 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12311 | |
| 12312 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12313 | static PyObject * |
| 12314 | unicode__decimal2ascii(PyObject *self) |
| 12315 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12316 | return PyUnicode_TransformDecimalAndSpaceToASCII(self); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12317 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12318 | #endif |
| 12319 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12320 | PyDoc_STRVAR(startswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12321 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12322 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 12323 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 12324 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12325 | With optional end, stop comparing S at that position.\n\ |
| 12326 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12327 | |
| 12328 | static PyObject * |
| 12329 | unicode_startswith(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12330 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12331 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12332 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12333 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12334 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12335 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12336 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12337 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12338 | if (!stringlib_parse_args_finds("startswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12339 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12340 | if (PyTuple_Check(subobj)) { |
| 12341 | Py_ssize_t i; |
| 12342 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 12343 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12344 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12345 | if (substring == NULL) |
| 12346 | return NULL; |
| 12347 | result = tailmatch(self, substring, start, end, -1); |
| 12348 | Py_DECREF(substring); |
| 12349 | if (result) { |
| 12350 | Py_RETURN_TRUE; |
| 12351 | } |
| 12352 | } |
| 12353 | /* nothing matched */ |
| 12354 | Py_RETURN_FALSE; |
| 12355 | } |
| 12356 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12357 | if (substring == NULL) { |
| 12358 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 12359 | PyErr_Format(PyExc_TypeError, "startswith first arg must be str or " |
| 12360 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12361 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12362 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12363 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12364 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12365 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12366 | } |
| 12367 | |
| 12368 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12369 | PyDoc_STRVAR(endswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12370 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12371 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 12372 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 12373 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12374 | With optional end, stop comparing S at that position.\n\ |
| 12375 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12376 | |
| 12377 | static PyObject * |
| 12378 | unicode_endswith(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12379 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12380 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12381 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12382 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12383 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12384 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12385 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12386 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12387 | if (!stringlib_parse_args_finds("endswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12388 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12389 | if (PyTuple_Check(subobj)) { |
| 12390 | Py_ssize_t i; |
| 12391 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 12392 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12393 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12394 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12395 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12396 | result = tailmatch(self, substring, start, end, +1); |
| 12397 | Py_DECREF(substring); |
| 12398 | if (result) { |
| 12399 | Py_RETURN_TRUE; |
| 12400 | } |
| 12401 | } |
| 12402 | Py_RETURN_FALSE; |
| 12403 | } |
| 12404 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12405 | if (substring == NULL) { |
| 12406 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 12407 | PyErr_Format(PyExc_TypeError, "endswith first arg must be str or " |
| 12408 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12409 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12410 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12411 | result = tailmatch(self, substring, start, end, +1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12412 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12413 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12414 | } |
| 12415 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12416 | #include "stringlib/unicode_format.h" |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12417 | |
| 12418 | PyDoc_STRVAR(format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12419 | "S.format(*args, **kwargs) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12420 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 12421 | Return a formatted version of S, using substitutions from args and kwargs.\n\ |
| 12422 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12423 | |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 12424 | PyDoc_STRVAR(format_map__doc__, |
| 12425 | "S.format_map(mapping) -> str\n\ |
| 12426 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 12427 | Return a formatted version of S, using substitutions from mapping.\n\ |
| 12428 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 12429 | |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12430 | static PyObject * |
| 12431 | unicode__format__(PyObject* self, PyObject* args) |
| 12432 | { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12433 | PyObject *format_spec, *out; |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12434 | |
| 12435 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
| 12436 | return NULL; |
| 12437 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12438 | out = _PyUnicode_FormatAdvanced(self, format_spec, 0, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12439 | PyUnicode_GET_LENGTH(format_spec)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12440 | return out; |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12441 | } |
| 12442 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12443 | PyDoc_STRVAR(p_format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12444 | "S.__format__(format_spec) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12445 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 12446 | Return a formatted version of S as described by format_spec."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12447 | |
| 12448 | static PyObject * |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12449 | unicode__sizeof__(PyUnicodeObject *v) |
| 12450 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12451 | Py_ssize_t size; |
| 12452 | |
| 12453 | /* If it's a compact object, account for base structure + |
| 12454 | character data. */ |
| 12455 | if (PyUnicode_IS_COMPACT_ASCII(v)) |
| 12456 | size = sizeof(PyASCIIObject) + PyUnicode_GET_LENGTH(v) + 1; |
| 12457 | else if (PyUnicode_IS_COMPACT(v)) |
| 12458 | size = sizeof(PyCompactUnicodeObject) + |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 12459 | (PyUnicode_GET_LENGTH(v) + 1) * PyUnicode_KIND(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12460 | else { |
| 12461 | /* If it is a two-block object, account for base object, and |
| 12462 | for character block if present. */ |
| 12463 | size = sizeof(PyUnicodeObject); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 12464 | if (_PyUnicode_DATA_ANY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12465 | size += (PyUnicode_GET_LENGTH(v) + 1) * |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 12466 | PyUnicode_KIND(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12467 | } |
| 12468 | /* 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] | 12469 | with the data pointer. Check if the data is not shared. */ |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 12470 | if (_PyUnicode_HAS_WSTR_MEMORY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12471 | size += (PyUnicode_WSTR_LENGTH(v) + 1) * sizeof(wchar_t); |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 12472 | if (_PyUnicode_HAS_UTF8_MEMORY(v)) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 12473 | size += PyUnicode_UTF8_LENGTH(v) + 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12474 | |
| 12475 | return PyLong_FromSsize_t(size); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12476 | } |
| 12477 | |
| 12478 | PyDoc_STRVAR(sizeof__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12479 | "S.__sizeof__() -> size of S in memory, in bytes"); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12480 | |
| 12481 | static PyObject * |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 12482 | unicode_getnewargs(PyObject *v) |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 12483 | { |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 12484 | PyObject *copy = PyUnicode_Copy(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12485 | if (!copy) |
| 12486 | return NULL; |
| 12487 | return Py_BuildValue("(N)", copy); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 12488 | } |
| 12489 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12490 | static PyMethodDef unicode_methods[] = { |
| 12491 | |
| 12492 | /* Order is according to common usage: often used methods should |
| 12493 | appear first, since lookup is done sequentially. */ |
| 12494 | |
Benjamin Peterson | 28a4dce | 2010-12-12 01:33:04 +0000 | [diff] [blame] | 12495 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS | METH_KEYWORDS, encode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12496 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
| 12497 | {"split", (PyCFunction) unicode_split, METH_VARARGS, split__doc__}, |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12498 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12499 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 12500 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
| 12501 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 12502 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 12503 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 12504 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 12505 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12506 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12507 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 12508 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 12509 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12510 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12511 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 12512 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 12513 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12514 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12515 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12516 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS | METH_KEYWORDS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12517 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12518 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 12519 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 12520 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 12521 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 12522 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 12523 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 12524 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 12525 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 12526 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 12527 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 12528 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 12529 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 12530 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 12531 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 12532 | {"isidentifier", (PyCFunction) unicode_isidentifier, METH_NOARGS, isidentifier__doc__}, |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12533 | {"isprintable", (PyCFunction) unicode_isprintable, METH_NOARGS, isprintable__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12534 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Eric Smith | 9cd1e09 | 2007-08-31 18:39:38 +0000 | [diff] [blame] | 12535 | {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 12536 | {"format_map", (PyCFunction) do_string_format_map, METH_O, format_map__doc__}, |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12537 | {"__format__", (PyCFunction) unicode__format__, METH_VARARGS, p_format__doc__}, |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12538 | {"maketrans", (PyCFunction) unicode_maketrans, |
| 12539 | METH_VARARGS | METH_STATIC, maketrans__doc__}, |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12540 | {"__sizeof__", (PyCFunction) unicode__sizeof__, METH_NOARGS, sizeof__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 12541 | #if 0 |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12542 | {"capwords", (PyCFunction) unicode_capwords, METH_NOARGS, capwords__doc__}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12543 | #endif |
| 12544 | |
| 12545 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12546 | /* These methods are just used for debugging the implementation. */ |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12547 | {"_decimal2ascii", (PyCFunction) unicode__decimal2ascii, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12548 | #endif |
| 12549 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12550 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12551 | {NULL, NULL} |
| 12552 | }; |
| 12553 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 12554 | static PyObject * |
| 12555 | unicode_mod(PyObject *v, PyObject *w) |
| 12556 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 12557 | if (!PyUnicode_Check(v)) |
| 12558 | Py_RETURN_NOTIMPLEMENTED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12559 | return PyUnicode_Format(v, w); |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 12560 | } |
| 12561 | |
| 12562 | static PyNumberMethods unicode_as_number = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12563 | 0, /*nb_add*/ |
| 12564 | 0, /*nb_subtract*/ |
| 12565 | 0, /*nb_multiply*/ |
| 12566 | unicode_mod, /*nb_remainder*/ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 12567 | }; |
| 12568 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12569 | static PySequenceMethods unicode_as_sequence = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12570 | (lenfunc) unicode_length, /* sq_length */ |
| 12571 | PyUnicode_Concat, /* sq_concat */ |
| 12572 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 12573 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
| 12574 | 0, /* sq_slice */ |
| 12575 | 0, /* sq_ass_item */ |
| 12576 | 0, /* sq_ass_slice */ |
| 12577 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12578 | }; |
| 12579 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12580 | static PyObject* |
| 12581 | unicode_subscript(PyUnicodeObject* self, PyObject* item) |
| 12582 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12583 | if (PyUnicode_READY(self) == -1) |
| 12584 | return NULL; |
| 12585 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 12586 | if (PyIndex_Check(item)) { |
| 12587 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12588 | if (i == -1 && PyErr_Occurred()) |
| 12589 | return NULL; |
| 12590 | if (i < 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12591 | i += PyUnicode_GET_LENGTH(self); |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 12592 | return unicode_getitem((PyObject*)self, i); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12593 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12594 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 12595 | PyObject *result; |
| 12596 | void *src_data, *dest_data; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 12597 | int src_kind, dest_kind; |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 12598 | Py_UCS4 ch, max_char, kind_limit; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12599 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12600 | if (PySlice_GetIndicesEx(item, PyUnicode_GET_LENGTH(self), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12601 | &start, &stop, &step, &slicelength) < 0) { |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12602 | return NULL; |
| 12603 | } |
| 12604 | |
| 12605 | if (slicelength <= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12606 | return PyUnicode_New(0, 0); |
| 12607 | } else if (start == 0 && step == 1 && |
| 12608 | slicelength == PyUnicode_GET_LENGTH(self) && |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 12609 | PyUnicode_CheckExact(self)) { |
| 12610 | Py_INCREF(self); |
| 12611 | return (PyObject *)self; |
| 12612 | } else if (step == 1) { |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 12613 | return PyUnicode_Substring((PyObject*)self, |
| 12614 | start, start + slicelength); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12615 | } |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 12616 | /* General case */ |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 12617 | src_kind = PyUnicode_KIND(self); |
| 12618 | src_data = PyUnicode_DATA(self); |
Victor Stinner | 55c9911 | 2011-10-13 01:17:06 +0200 | [diff] [blame^] | 12619 | if (!PyUnicode_IS_ASCII(self)) { |
| 12620 | kind_limit = kind_maxchar_limit(src_kind); |
| 12621 | max_char = 0; |
| 12622 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 12623 | ch = PyUnicode_READ(src_kind, src_data, cur); |
| 12624 | if (ch > max_char) { |
| 12625 | max_char = ch; |
| 12626 | if (max_char >= kind_limit) |
| 12627 | break; |
| 12628 | } |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 12629 | } |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 12630 | } |
Victor Stinner | 55c9911 | 2011-10-13 01:17:06 +0200 | [diff] [blame^] | 12631 | else |
| 12632 | max_char = 127; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 12633 | result = PyUnicode_New(slicelength, max_char); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 12634 | if (result == NULL) |
| 12635 | return NULL; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 12636 | dest_kind = PyUnicode_KIND(result); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 12637 | dest_data = PyUnicode_DATA(result); |
| 12638 | |
| 12639 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 12640 | Py_UCS4 ch = PyUnicode_READ(src_kind, src_data, cur); |
| 12641 | PyUnicode_WRITE(dest_kind, dest_data, i, ch); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 12642 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12643 | assert(_PyUnicode_CheckConsistency(result, 1)); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 12644 | return result; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12645 | } else { |
| 12646 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 12647 | return NULL; |
| 12648 | } |
| 12649 | } |
| 12650 | |
| 12651 | static PyMappingMethods unicode_as_mapping = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12652 | (lenfunc)unicode_length, /* mp_length */ |
| 12653 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 12654 | (objobjargproc)0, /* mp_ass_subscript */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12655 | }; |
| 12656 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12657 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12658 | /* Helpers for PyUnicode_Format() */ |
| 12659 | |
| 12660 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12661 | 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] | 12662 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12663 | Py_ssize_t argidx = *p_argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12664 | if (argidx < arglen) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12665 | (*p_argidx)++; |
| 12666 | if (arglen < 0) |
| 12667 | return args; |
| 12668 | else |
| 12669 | return PyTuple_GetItem(args, argidx); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12670 | } |
| 12671 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12672 | "not enough arguments for format string"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12673 | return NULL; |
| 12674 | } |
| 12675 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 12676 | /* 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] | 12677 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 12678 | static PyObject * |
| 12679 | formatfloat(PyObject *v, int flags, int prec, int type) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12680 | { |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 12681 | char *p; |
| 12682 | PyObject *result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12683 | double x; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 12684 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12685 | x = PyFloat_AsDouble(v); |
| 12686 | if (x == -1.0 && PyErr_Occurred()) |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 12687 | return NULL; |
| 12688 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12689 | if (prec < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12690 | prec = 6; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 12691 | |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 12692 | p = PyOS_double_to_string(x, type, prec, |
| 12693 | (flags & F_ALT) ? Py_DTSF_ALT : 0, NULL); |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 12694 | if (p == NULL) |
| 12695 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12696 | result = PyUnicode_DecodeASCII(p, strlen(p), NULL); |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 12697 | PyMem_Free(p); |
| 12698 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12699 | } |
| 12700 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 12701 | static PyObject* |
| 12702 | formatlong(PyObject *val, int flags, int prec, int type) |
| 12703 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12704 | char *buf; |
| 12705 | int len; |
| 12706 | PyObject *str; /* temporary string object. */ |
| 12707 | PyObject *result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 12708 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12709 | str = _PyBytes_FormatLong(val, flags, prec, type, &buf, &len); |
| 12710 | if (!str) |
| 12711 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12712 | result = PyUnicode_DecodeASCII(buf, len, NULL); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12713 | Py_DECREF(str); |
| 12714 | return result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 12715 | } |
| 12716 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 12717 | static Py_UCS4 |
| 12718 | formatchar(PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12719 | { |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 12720 | /* presume that the buffer is at least 3 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 12721 | if (PyUnicode_Check(v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12722 | if (PyUnicode_GET_LENGTH(v) == 1) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 12723 | return PyUnicode_READ_CHAR(v, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12724 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12725 | goto onError; |
| 12726 | } |
| 12727 | else { |
| 12728 | /* Integer input truncated to a character */ |
| 12729 | long x; |
| 12730 | x = PyLong_AsLong(v); |
| 12731 | if (x == -1 && PyErr_Occurred()) |
| 12732 | goto onError; |
| 12733 | |
| 12734 | if (x < 0 || x > 0x10ffff) { |
| 12735 | PyErr_SetString(PyExc_OverflowError, |
| 12736 | "%c arg not in range(0x110000)"); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 12737 | return (Py_UCS4) -1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12738 | } |
| 12739 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 12740 | return (Py_UCS4) x; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12741 | } |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 12742 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12743 | onError: |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 12744 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12745 | "%c requires int or char"); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 12746 | return (Py_UCS4) -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12747 | } |
| 12748 | |
Antoine Pitrou | 978b9d2 | 2011-10-07 12:35:48 +0200 | [diff] [blame] | 12749 | static int |
| 12750 | repeat_accumulate(_PyAccu *acc, PyObject *obj, Py_ssize_t count) |
| 12751 | { |
| 12752 | int r; |
| 12753 | assert(count > 0); |
| 12754 | assert(PyUnicode_Check(obj)); |
| 12755 | if (count > 5) { |
| 12756 | PyObject *repeated = unicode_repeat((PyUnicodeObject *) obj, count); |
| 12757 | if (repeated == NULL) |
| 12758 | return -1; |
| 12759 | r = _PyAccu_Accumulate(acc, repeated); |
| 12760 | Py_DECREF(repeated); |
| 12761 | return r; |
| 12762 | } |
| 12763 | else { |
| 12764 | do { |
| 12765 | if (_PyAccu_Accumulate(acc, obj)) |
| 12766 | return -1; |
| 12767 | } while (--count); |
| 12768 | return 0; |
| 12769 | } |
| 12770 | } |
| 12771 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12772 | PyObject * |
| 12773 | PyUnicode_Format(PyObject *format, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12774 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12775 | void *fmt; |
| 12776 | int fmtkind; |
| 12777 | PyObject *result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12778 | int kind; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 12779 | int r; |
| 12780 | Py_ssize_t fmtcnt, fmtpos, arglen, argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12781 | int args_owned = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12782 | PyObject *dict = NULL; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 12783 | PyObject *temp = NULL; |
| 12784 | PyObject *second = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12785 | PyUnicodeObject *uformat; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 12786 | _PyAccu acc; |
| 12787 | static PyObject *plus, *minus, *blank, *zero, *percent; |
| 12788 | |
| 12789 | if (!plus && !(plus = get_latin1_char('+'))) |
| 12790 | return NULL; |
| 12791 | if (!minus && !(minus = get_latin1_char('-'))) |
| 12792 | return NULL; |
| 12793 | if (!blank && !(blank = get_latin1_char(' '))) |
| 12794 | return NULL; |
| 12795 | if (!zero && !(zero = get_latin1_char('0'))) |
| 12796 | return NULL; |
| 12797 | if (!percent && !(percent = get_latin1_char('%'))) |
| 12798 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 12799 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12800 | if (format == NULL || args == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12801 | PyErr_BadInternalCall(); |
| 12802 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12803 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12804 | uformat = (PyUnicodeObject*)PyUnicode_FromObject(format); |
| 12805 | if (uformat == NULL || PyUnicode_READY(uformat) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12806 | return NULL; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 12807 | if (_PyAccu_Init(&acc)) |
| 12808 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12809 | fmt = PyUnicode_DATA(uformat); |
| 12810 | fmtkind = PyUnicode_KIND(uformat); |
| 12811 | fmtcnt = PyUnicode_GET_LENGTH(uformat); |
| 12812 | fmtpos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12813 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12814 | if (PyTuple_Check(args)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12815 | arglen = PyTuple_Size(args); |
| 12816 | argidx = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12817 | } |
| 12818 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12819 | arglen = -1; |
| 12820 | argidx = -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12821 | } |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 12822 | if (Py_TYPE(args)->tp_as_mapping && !PyTuple_Check(args) && |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 12823 | !PyUnicode_Check(args)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12824 | dict = args; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12825 | |
| 12826 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12827 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) != '%') { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 12828 | PyObject *nonfmt; |
| 12829 | Py_ssize_t nonfmtpos; |
| 12830 | nonfmtpos = fmtpos++; |
| 12831 | while (fmtcnt >= 0 && |
| 12832 | PyUnicode_READ(fmtkind, fmt, fmtpos) != '%') { |
| 12833 | fmtpos++; |
| 12834 | fmtcnt--; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12835 | } |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 12836 | nonfmt = PyUnicode_Substring((PyObject *) uformat, nonfmtpos, fmtpos); |
| 12837 | if (nonfmt == NULL) |
| 12838 | goto onError; |
| 12839 | r = _PyAccu_Accumulate(&acc, nonfmt); |
| 12840 | Py_DECREF(nonfmt); |
| 12841 | if (r) |
| 12842 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12843 | } |
| 12844 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12845 | /* Got a format specifier */ |
| 12846 | int flags = 0; |
| 12847 | Py_ssize_t width = -1; |
| 12848 | int prec = -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12849 | Py_UCS4 c = '\0'; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 12850 | Py_UCS4 fill, sign; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12851 | int isnumok; |
| 12852 | PyObject *v = NULL; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 12853 | void *pbuf = NULL; |
| 12854 | Py_ssize_t pindex, len; |
| 12855 | PyObject *signobj = NULL, *fillobj = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12856 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12857 | fmtpos++; |
| 12858 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) == '(') { |
| 12859 | Py_ssize_t keystart; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12860 | Py_ssize_t keylen; |
| 12861 | PyObject *key; |
| 12862 | int pcount = 1; |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 12863 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12864 | if (dict == NULL) { |
| 12865 | PyErr_SetString(PyExc_TypeError, |
| 12866 | "format requires a mapping"); |
| 12867 | goto onError; |
| 12868 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12869 | ++fmtpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12870 | --fmtcnt; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12871 | keystart = fmtpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12872 | /* Skip over balanced parentheses */ |
| 12873 | while (pcount > 0 && --fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12874 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) == ')') |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12875 | --pcount; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12876 | else 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 | fmtpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12879 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12880 | keylen = fmtpos - keystart - 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12881 | if (fmtcnt < 0 || pcount > 0) { |
| 12882 | PyErr_SetString(PyExc_ValueError, |
| 12883 | "incomplete format key"); |
| 12884 | goto onError; |
| 12885 | } |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 12886 | key = PyUnicode_Substring((PyObject*)uformat, |
| 12887 | keystart, keystart + keylen); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12888 | if (key == NULL) |
| 12889 | goto onError; |
| 12890 | if (args_owned) { |
| 12891 | Py_DECREF(args); |
| 12892 | args_owned = 0; |
| 12893 | } |
| 12894 | args = PyObject_GetItem(dict, key); |
| 12895 | Py_DECREF(key); |
| 12896 | if (args == NULL) { |
| 12897 | goto onError; |
| 12898 | } |
| 12899 | args_owned = 1; |
| 12900 | arglen = -1; |
| 12901 | argidx = -2; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12902 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12903 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12904 | switch (c = PyUnicode_READ(fmtkind, fmt, fmtpos++)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12905 | case '-': flags |= F_LJUST; continue; |
| 12906 | case '+': flags |= F_SIGN; continue; |
| 12907 | case ' ': flags |= F_BLANK; continue; |
| 12908 | case '#': flags |= F_ALT; continue; |
| 12909 | case '0': flags |= F_ZERO; continue; |
| 12910 | } |
| 12911 | break; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12912 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12913 | if (c == '*') { |
| 12914 | v = getnextarg(args, arglen, &argidx); |
| 12915 | if (v == NULL) |
| 12916 | goto onError; |
| 12917 | if (!PyLong_Check(v)) { |
| 12918 | PyErr_SetString(PyExc_TypeError, |
| 12919 | "* wants int"); |
| 12920 | goto onError; |
| 12921 | } |
| 12922 | width = PyLong_AsLong(v); |
| 12923 | if (width == -1 && PyErr_Occurred()) |
| 12924 | goto onError; |
| 12925 | if (width < 0) { |
| 12926 | flags |= F_LJUST; |
| 12927 | width = -width; |
| 12928 | } |
| 12929 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12930 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12931 | } |
| 12932 | else if (c >= '0' && c <= '9') { |
| 12933 | width = c - '0'; |
| 12934 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12935 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12936 | if (c < '0' || c > '9') |
| 12937 | break; |
| 12938 | if ((width*10) / 10 != width) { |
| 12939 | PyErr_SetString(PyExc_ValueError, |
| 12940 | "width too big"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12941 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12942 | } |
| 12943 | width = width*10 + (c - '0'); |
| 12944 | } |
| 12945 | } |
| 12946 | if (c == '.') { |
| 12947 | prec = 0; |
| 12948 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12949 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12950 | if (c == '*') { |
| 12951 | v = getnextarg(args, arglen, &argidx); |
| 12952 | if (v == NULL) |
| 12953 | goto onError; |
| 12954 | if (!PyLong_Check(v)) { |
| 12955 | PyErr_SetString(PyExc_TypeError, |
| 12956 | "* wants int"); |
| 12957 | goto onError; |
| 12958 | } |
| 12959 | prec = PyLong_AsLong(v); |
| 12960 | if (prec == -1 && PyErr_Occurred()) |
| 12961 | goto onError; |
| 12962 | if (prec < 0) |
| 12963 | prec = 0; |
| 12964 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12965 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12966 | } |
| 12967 | else if (c >= '0' && c <= '9') { |
| 12968 | prec = c - '0'; |
| 12969 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12970 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12971 | if (c < '0' || c > '9') |
| 12972 | break; |
| 12973 | if ((prec*10) / 10 != prec) { |
| 12974 | PyErr_SetString(PyExc_ValueError, |
| 12975 | "prec too big"); |
| 12976 | goto onError; |
| 12977 | } |
| 12978 | prec = prec*10 + (c - '0'); |
| 12979 | } |
| 12980 | } |
| 12981 | } /* prec */ |
| 12982 | if (fmtcnt >= 0) { |
| 12983 | if (c == 'h' || c == 'l' || c == 'L') { |
| 12984 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12985 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12986 | } |
| 12987 | } |
| 12988 | if (fmtcnt < 0) { |
| 12989 | PyErr_SetString(PyExc_ValueError, |
| 12990 | "incomplete format"); |
| 12991 | goto onError; |
| 12992 | } |
| 12993 | if (c != '%') { |
| 12994 | v = getnextarg(args, arglen, &argidx); |
| 12995 | if (v == NULL) |
| 12996 | goto onError; |
| 12997 | } |
| 12998 | sign = 0; |
| 12999 | fill = ' '; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13000 | fillobj = blank; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13001 | switch (c) { |
| 13002 | |
| 13003 | case '%': |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13004 | _PyAccu_Accumulate(&acc, percent); |
| 13005 | continue; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13006 | |
| 13007 | case 's': |
| 13008 | case 'r': |
| 13009 | case 'a': |
Victor Stinner | 808fc0a | 2010-03-22 12:50:40 +0000 | [diff] [blame] | 13010 | if (PyUnicode_CheckExact(v) && c == 's') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13011 | temp = v; |
| 13012 | Py_INCREF(temp); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13013 | } |
| 13014 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13015 | if (c == 's') |
| 13016 | temp = PyObject_Str(v); |
| 13017 | else if (c == 'r') |
| 13018 | temp = PyObject_Repr(v); |
| 13019 | else |
| 13020 | temp = PyObject_ASCII(v); |
| 13021 | if (temp == NULL) |
| 13022 | goto onError; |
| 13023 | if (PyUnicode_Check(temp)) |
| 13024 | /* nothing to do */; |
| 13025 | else { |
| 13026 | Py_DECREF(temp); |
| 13027 | PyErr_SetString(PyExc_TypeError, |
| 13028 | "%s argument has non-string str()"); |
| 13029 | goto onError; |
| 13030 | } |
| 13031 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13032 | if (PyUnicode_READY(temp) == -1) { |
| 13033 | Py_CLEAR(temp); |
| 13034 | goto onError; |
| 13035 | } |
| 13036 | pbuf = PyUnicode_DATA(temp); |
| 13037 | kind = PyUnicode_KIND(temp); |
| 13038 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13039 | if (prec >= 0 && len > prec) |
| 13040 | len = prec; |
| 13041 | break; |
| 13042 | |
| 13043 | case 'i': |
| 13044 | case 'd': |
| 13045 | case 'u': |
| 13046 | case 'o': |
| 13047 | case 'x': |
| 13048 | case 'X': |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13049 | isnumok = 0; |
| 13050 | if (PyNumber_Check(v)) { |
| 13051 | PyObject *iobj=NULL; |
| 13052 | |
| 13053 | if (PyLong_Check(v)) { |
| 13054 | iobj = v; |
| 13055 | Py_INCREF(iobj); |
| 13056 | } |
| 13057 | else { |
| 13058 | iobj = PyNumber_Long(v); |
| 13059 | } |
| 13060 | if (iobj!=NULL) { |
| 13061 | if (PyLong_Check(iobj)) { |
| 13062 | isnumok = 1; |
Senthil Kumaran | 9ebe08d | 2011-07-03 21:03:16 -0700 | [diff] [blame] | 13063 | temp = formatlong(iobj, flags, prec, (c == 'i'? 'd': c)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13064 | Py_DECREF(iobj); |
| 13065 | if (!temp) |
| 13066 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13067 | if (PyUnicode_READY(temp) == -1) { |
| 13068 | Py_CLEAR(temp); |
| 13069 | goto onError; |
| 13070 | } |
| 13071 | pbuf = PyUnicode_DATA(temp); |
| 13072 | kind = PyUnicode_KIND(temp); |
| 13073 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13074 | sign = 1; |
| 13075 | } |
| 13076 | else { |
| 13077 | Py_DECREF(iobj); |
| 13078 | } |
| 13079 | } |
| 13080 | } |
| 13081 | if (!isnumok) { |
| 13082 | PyErr_Format(PyExc_TypeError, |
| 13083 | "%%%c format: a number is required, " |
| 13084 | "not %.200s", (char)c, Py_TYPE(v)->tp_name); |
| 13085 | goto onError; |
| 13086 | } |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13087 | if (flags & F_ZERO) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13088 | fill = '0'; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13089 | fillobj = zero; |
| 13090 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13091 | break; |
| 13092 | |
| 13093 | case 'e': |
| 13094 | case 'E': |
| 13095 | case 'f': |
| 13096 | case 'F': |
| 13097 | case 'g': |
| 13098 | case 'G': |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13099 | temp = formatfloat(v, flags, prec, c); |
| 13100 | if (!temp) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13101 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13102 | if (PyUnicode_READY(temp) == -1) { |
| 13103 | Py_CLEAR(temp); |
| 13104 | goto onError; |
| 13105 | } |
| 13106 | pbuf = PyUnicode_DATA(temp); |
| 13107 | kind = PyUnicode_KIND(temp); |
| 13108 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13109 | sign = 1; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13110 | if (flags & F_ZERO) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13111 | fill = '0'; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13112 | fillobj = zero; |
| 13113 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13114 | break; |
| 13115 | |
| 13116 | case 'c': |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13117 | { |
| 13118 | Py_UCS4 ch = formatchar(v); |
| 13119 | if (ch == (Py_UCS4) -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13120 | goto onError; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13121 | temp = _PyUnicode_FromUCS4(&ch, 1); |
| 13122 | if (temp == NULL) |
| 13123 | goto onError; |
| 13124 | pbuf = PyUnicode_DATA(temp); |
| 13125 | kind = PyUnicode_KIND(temp); |
| 13126 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13127 | break; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13128 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13129 | |
| 13130 | default: |
| 13131 | PyErr_Format(PyExc_ValueError, |
| 13132 | "unsupported format character '%c' (0x%x) " |
| 13133 | "at index %zd", |
| 13134 | (31<=c && c<=126) ? (char)c : '?', |
| 13135 | (int)c, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13136 | fmtpos - 1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13137 | goto onError; |
| 13138 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13139 | /* pbuf is initialized here. */ |
| 13140 | pindex = 0; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13141 | if (sign) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13142 | if (PyUnicode_READ(kind, pbuf, pindex) == '-') { |
| 13143 | signobj = minus; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13144 | len--; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13145 | pindex++; |
| 13146 | } |
| 13147 | else if (PyUnicode_READ(kind, pbuf, pindex) == '+') { |
| 13148 | signobj = plus; |
| 13149 | len--; |
| 13150 | pindex++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13151 | } |
| 13152 | else if (flags & F_SIGN) |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13153 | signobj = plus; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13154 | else if (flags & F_BLANK) |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13155 | signobj = blank; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13156 | else |
| 13157 | sign = 0; |
| 13158 | } |
| 13159 | if (width < len) |
| 13160 | width = len; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13161 | if (sign) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13162 | if (fill != ' ') { |
| 13163 | assert(signobj != NULL); |
| 13164 | if (_PyAccu_Accumulate(&acc, signobj)) |
| 13165 | goto onError; |
| 13166 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13167 | if (width > len) |
| 13168 | width--; |
| 13169 | } |
| 13170 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13171 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13172 | assert(PyUnicode_READ(kind, pbuf, pindex + 1) == c); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13173 | if (fill != ' ') { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13174 | second = get_latin1_char( |
| 13175 | PyUnicode_READ(kind, pbuf, pindex + 1)); |
| 13176 | pindex += 2; |
| 13177 | if (second == NULL || |
| 13178 | _PyAccu_Accumulate(&acc, zero) || |
| 13179 | _PyAccu_Accumulate(&acc, second)) |
| 13180 | goto onError; |
| 13181 | Py_CLEAR(second); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13182 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13183 | width -= 2; |
| 13184 | if (width < 0) |
| 13185 | width = 0; |
| 13186 | len -= 2; |
| 13187 | } |
| 13188 | if (width > len && !(flags & F_LJUST)) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13189 | assert(fillobj != NULL); |
Antoine Pitrou | 978b9d2 | 2011-10-07 12:35:48 +0200 | [diff] [blame] | 13190 | if (repeat_accumulate(&acc, fillobj, width - len)) |
| 13191 | goto onError; |
| 13192 | width = len; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13193 | } |
| 13194 | if (fill == ' ') { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13195 | if (sign) { |
| 13196 | assert(signobj != NULL); |
| 13197 | if (_PyAccu_Accumulate(&acc, signobj)) |
| 13198 | goto onError; |
| 13199 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13200 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13201 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
| 13202 | assert(PyUnicode_READ(kind, pbuf, pindex+1) == c); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13203 | second = get_latin1_char( |
| 13204 | PyUnicode_READ(kind, pbuf, pindex + 1)); |
| 13205 | pindex += 2; |
| 13206 | if (second == NULL || |
| 13207 | _PyAccu_Accumulate(&acc, zero) || |
| 13208 | _PyAccu_Accumulate(&acc, second)) |
| 13209 | goto onError; |
| 13210 | Py_CLEAR(second); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13211 | } |
| 13212 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13213 | /* Copy all characters, preserving len */ |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13214 | if (temp != NULL) { |
| 13215 | assert(pbuf == PyUnicode_DATA(temp)); |
| 13216 | v = PyUnicode_Substring(temp, pindex, pindex + len); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13217 | } |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13218 | else { |
| 13219 | const char *p = (const char *) pbuf; |
| 13220 | assert(pbuf != NULL); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 13221 | p += kind * pindex; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13222 | v = PyUnicode_FromKindAndData(kind, p, len); |
| 13223 | } |
| 13224 | if (v == NULL) |
| 13225 | goto onError; |
| 13226 | r = _PyAccu_Accumulate(&acc, v); |
| 13227 | Py_DECREF(v); |
| 13228 | if (r) |
| 13229 | goto onError; |
Antoine Pitrou | 978b9d2 | 2011-10-07 12:35:48 +0200 | [diff] [blame] | 13230 | if (width > len && repeat_accumulate(&acc, blank, width - len)) |
| 13231 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13232 | if (dict && (argidx < arglen) && c != '%') { |
| 13233 | PyErr_SetString(PyExc_TypeError, |
| 13234 | "not all arguments converted during string formatting"); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13235 | goto onError; |
| 13236 | } |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13237 | Py_CLEAR(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13238 | } /* '%' */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13239 | } /* until end */ |
| 13240 | if (argidx < arglen && !dict) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13241 | PyErr_SetString(PyExc_TypeError, |
| 13242 | "not all arguments converted during string formatting"); |
| 13243 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13244 | } |
| 13245 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13246 | result = _PyAccu_Finish(&acc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13247 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13248 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13249 | } |
| 13250 | Py_DECREF(uformat); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13251 | Py_XDECREF(temp); |
| 13252 | Py_XDECREF(second); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13253 | return (PyObject *)result; |
| 13254 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13255 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13256 | Py_DECREF(uformat); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13257 | Py_XDECREF(temp); |
| 13258 | Py_XDECREF(second); |
| 13259 | _PyAccu_Destroy(&acc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13260 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13261 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13262 | } |
| 13263 | return NULL; |
| 13264 | } |
| 13265 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 13266 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 13267 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 13268 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13269 | static PyObject * |
| 13270 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 13271 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13272 | PyObject *x = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13273 | static char *kwlist[] = {"object", "encoding", "errors", 0}; |
| 13274 | char *encoding = NULL; |
| 13275 | char *errors = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13276 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13277 | if (type != &PyUnicode_Type) |
| 13278 | return unicode_subtype_new(type, args, kwds); |
| 13279 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:str", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13280 | kwlist, &x, &encoding, &errors)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13281 | return NULL; |
| 13282 | if (x == NULL) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13283 | return (PyObject *)PyUnicode_New(0, 0); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13284 | if (encoding == NULL && errors == NULL) |
| 13285 | return PyObject_Str(x); |
| 13286 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13287 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13288 | } |
| 13289 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 13290 | static PyObject * |
| 13291 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 13292 | { |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13293 | PyUnicodeObject *unicode, *self; |
| 13294 | Py_ssize_t length, char_size; |
| 13295 | int share_wstr, share_utf8; |
| 13296 | unsigned int kind; |
| 13297 | void *data; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 13298 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13299 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13300 | |
| 13301 | unicode = (PyUnicodeObject *)unicode_new(&PyUnicode_Type, args, kwds); |
| 13302 | if (unicode == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13303 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 13304 | assert(_PyUnicode_CHECK(unicode)); |
Victor Stinner | e06e145 | 2011-10-04 20:52:31 +0200 | [diff] [blame] | 13305 | if (PyUnicode_READY(unicode)) |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13306 | return NULL; |
| 13307 | |
| 13308 | self = (PyUnicodeObject *) type->tp_alloc(type, 0); |
| 13309 | if (self == NULL) { |
| 13310 | Py_DECREF(unicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13311 | return NULL; |
| 13312 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13313 | kind = PyUnicode_KIND(unicode); |
| 13314 | length = PyUnicode_GET_LENGTH(unicode); |
| 13315 | |
| 13316 | _PyUnicode_LENGTH(self) = length; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 13317 | #ifdef Py_DEBUG |
| 13318 | _PyUnicode_HASH(self) = -1; |
| 13319 | #else |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13320 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 13321 | #endif |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13322 | _PyUnicode_STATE(self).interned = 0; |
| 13323 | _PyUnicode_STATE(self).kind = kind; |
| 13324 | _PyUnicode_STATE(self).compact = 0; |
Victor Stinner | 3cf4637 | 2011-10-03 14:42:15 +0200 | [diff] [blame] | 13325 | _PyUnicode_STATE(self).ascii = _PyUnicode_STATE(unicode).ascii; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13326 | _PyUnicode_STATE(self).ready = 1; |
| 13327 | _PyUnicode_WSTR(self) = NULL; |
| 13328 | _PyUnicode_UTF8_LENGTH(self) = 0; |
| 13329 | _PyUnicode_UTF8(self) = NULL; |
| 13330 | _PyUnicode_WSTR_LENGTH(self) = 0; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 13331 | _PyUnicode_DATA_ANY(self) = NULL; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13332 | |
| 13333 | share_utf8 = 0; |
| 13334 | share_wstr = 0; |
| 13335 | if (kind == PyUnicode_1BYTE_KIND) { |
| 13336 | char_size = 1; |
| 13337 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) |
| 13338 | share_utf8 = 1; |
| 13339 | } |
| 13340 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 13341 | char_size = 2; |
| 13342 | if (sizeof(wchar_t) == 2) |
| 13343 | share_wstr = 1; |
| 13344 | } |
| 13345 | else { |
| 13346 | assert(kind == PyUnicode_4BYTE_KIND); |
| 13347 | char_size = 4; |
| 13348 | if (sizeof(wchar_t) == 4) |
| 13349 | share_wstr = 1; |
| 13350 | } |
| 13351 | |
| 13352 | /* Ensure we won't overflow the length. */ |
| 13353 | if (length > (PY_SSIZE_T_MAX / char_size - 1)) { |
| 13354 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13355 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13356 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13357 | data = PyObject_MALLOC((length + 1) * char_size); |
| 13358 | if (data == NULL) { |
| 13359 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13360 | goto onError; |
| 13361 | } |
| 13362 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 13363 | _PyUnicode_DATA_ANY(self) = data; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13364 | if (share_utf8) { |
| 13365 | _PyUnicode_UTF8_LENGTH(self) = length; |
| 13366 | _PyUnicode_UTF8(self) = data; |
| 13367 | } |
| 13368 | if (share_wstr) { |
| 13369 | _PyUnicode_WSTR_LENGTH(self) = length; |
| 13370 | _PyUnicode_WSTR(self) = (wchar_t *)data; |
| 13371 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13372 | |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13373 | Py_MEMCPY(data, PyUnicode_DATA(unicode), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 13374 | kind * (length + 1)); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13375 | Py_DECREF(unicode); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13376 | assert(_PyUnicode_CheckConsistency(self, 1)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 13377 | #ifdef Py_DEBUG |
| 13378 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
| 13379 | #endif |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13380 | return (PyObject *)self; |
| 13381 | |
| 13382 | onError: |
| 13383 | Py_DECREF(unicode); |
| 13384 | Py_DECREF(self); |
| 13385 | return NULL; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 13386 | } |
| 13387 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13388 | PyDoc_STRVAR(unicode_doc, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13389 | "str(string[, encoding[, errors]]) -> str\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13390 | \n\ |
Collin Winter | d474ce8 | 2007-08-07 19:42:11 +0000 | [diff] [blame] | 13391 | Create a new string object from the given encoded string.\n\ |
Skip Montanaro | 35b37a5 | 2002-07-26 16:22:46 +0000 | [diff] [blame] | 13392 | encoding defaults to the current default string encoding.\n\ |
| 13393 | errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13394 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13395 | static PyObject *unicode_iter(PyObject *seq); |
| 13396 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13397 | PyTypeObject PyUnicode_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 13398 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13399 | "str", /* tp_name */ |
| 13400 | sizeof(PyUnicodeObject), /* tp_size */ |
| 13401 | 0, /* tp_itemsize */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13402 | /* Slots */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13403 | (destructor)unicode_dealloc, /* tp_dealloc */ |
| 13404 | 0, /* tp_print */ |
| 13405 | 0, /* tp_getattr */ |
| 13406 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 13407 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13408 | unicode_repr, /* tp_repr */ |
| 13409 | &unicode_as_number, /* tp_as_number */ |
| 13410 | &unicode_as_sequence, /* tp_as_sequence */ |
| 13411 | &unicode_as_mapping, /* tp_as_mapping */ |
| 13412 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 13413 | 0, /* tp_call*/ |
| 13414 | (reprfunc) unicode_str, /* tp_str */ |
| 13415 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 13416 | 0, /* tp_setattro */ |
| 13417 | 0, /* tp_as_buffer */ |
| 13418 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13419 | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13420 | unicode_doc, /* tp_doc */ |
| 13421 | 0, /* tp_traverse */ |
| 13422 | 0, /* tp_clear */ |
| 13423 | PyUnicode_RichCompare, /* tp_richcompare */ |
| 13424 | 0, /* tp_weaklistoffset */ |
| 13425 | unicode_iter, /* tp_iter */ |
| 13426 | 0, /* tp_iternext */ |
| 13427 | unicode_methods, /* tp_methods */ |
| 13428 | 0, /* tp_members */ |
| 13429 | 0, /* tp_getset */ |
| 13430 | &PyBaseObject_Type, /* tp_base */ |
| 13431 | 0, /* tp_dict */ |
| 13432 | 0, /* tp_descr_get */ |
| 13433 | 0, /* tp_descr_set */ |
| 13434 | 0, /* tp_dictoffset */ |
| 13435 | 0, /* tp_init */ |
| 13436 | 0, /* tp_alloc */ |
| 13437 | unicode_new, /* tp_new */ |
| 13438 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13439 | }; |
| 13440 | |
| 13441 | /* Initialize the Unicode implementation */ |
| 13442 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 13443 | void _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13444 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13445 | int i; |
| 13446 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13447 | /* XXX - move this array to unicodectype.c ? */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13448 | Py_UCS2 linebreak[] = { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13449 | 0x000A, /* LINE FEED */ |
| 13450 | 0x000D, /* CARRIAGE RETURN */ |
| 13451 | 0x001C, /* FILE SEPARATOR */ |
| 13452 | 0x001D, /* GROUP SEPARATOR */ |
| 13453 | 0x001E, /* RECORD SEPARATOR */ |
| 13454 | 0x0085, /* NEXT LINE */ |
| 13455 | 0x2028, /* LINE SEPARATOR */ |
| 13456 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 13457 | }; |
| 13458 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 13459 | /* Init the implementation */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 13460 | unicode_empty = PyUnicode_New(0, 0); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13461 | assert(_PyUnicode_CheckConsistency(unicode_empty, 1)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13462 | if (!unicode_empty) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13463 | Py_FatalError("Can't create empty string"); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13464 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13465 | for (i = 0; i < 256; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13466 | unicode_latin1[i] = NULL; |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 13467 | if (PyType_Ready(&PyUnicode_Type) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13468 | Py_FatalError("Can't initialize 'unicode'"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13469 | |
| 13470 | /* initialize the linebreak bloom filter */ |
| 13471 | bloom_linebreak = make_bloom_mask( |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13472 | PyUnicode_2BYTE_KIND, linebreak, |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 13473 | Py_ARRAY_LENGTH(linebreak)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13474 | |
| 13475 | PyType_Ready(&EncodingMapType); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13476 | } |
| 13477 | |
| 13478 | /* Finalize the Unicode implementation */ |
| 13479 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 13480 | int |
| 13481 | PyUnicode_ClearFreeList(void) |
| 13482 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13483 | return 0; |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 13484 | } |
| 13485 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13486 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 13487 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13488 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13489 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13490 | |
Guido van Rossum | 4ae8ef8 | 2000-10-03 18:09:04 +0000 | [diff] [blame] | 13491 | Py_XDECREF(unicode_empty); |
| 13492 | unicode_empty = NULL; |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 13493 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13494 | for (i = 0; i < 256; i++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13495 | if (unicode_latin1[i]) { |
| 13496 | Py_DECREF(unicode_latin1[i]); |
| 13497 | unicode_latin1[i] = NULL; |
| 13498 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13499 | } |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 13500 | _PyUnicode_ClearStaticStrings(); |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 13501 | (void)PyUnicode_ClearFreeList(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13502 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 13503 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13504 | void |
| 13505 | PyUnicode_InternInPlace(PyObject **p) |
| 13506 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13507 | register PyUnicodeObject *s = (PyUnicodeObject *)(*p); |
| 13508 | PyObject *t; |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 13509 | #ifdef Py_DEBUG |
| 13510 | assert(s != NULL); |
| 13511 | assert(_PyUnicode_CHECK(s)); |
| 13512 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13513 | if (s == NULL || !PyUnicode_Check(s)) |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 13514 | return; |
| 13515 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13516 | /* If it's a subclass, we don't really know what putting |
| 13517 | it in the interned dict might do. */ |
| 13518 | if (!PyUnicode_CheckExact(s)) |
| 13519 | return; |
| 13520 | if (PyUnicode_CHECK_INTERNED(s)) |
| 13521 | return; |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 13522 | if (_PyUnicode_READY_REPLACE(p)) { |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 13523 | assert(0 && "_PyUnicode_READY_REPLACE fail in PyUnicode_InternInPlace"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13524 | return; |
| 13525 | } |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 13526 | s = (PyUnicodeObject *)(*p); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13527 | if (interned == NULL) { |
| 13528 | interned = PyDict_New(); |
| 13529 | if (interned == NULL) { |
| 13530 | PyErr_Clear(); /* Don't leave an exception */ |
| 13531 | return; |
| 13532 | } |
| 13533 | } |
| 13534 | /* It might be that the GetItem call fails even |
| 13535 | though the key is present in the dictionary, |
| 13536 | namely when this happens during a stack overflow. */ |
| 13537 | Py_ALLOW_RECURSION |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13538 | t = PyDict_GetItem(interned, (PyObject *)s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13539 | Py_END_ALLOW_RECURSION |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 13540 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13541 | if (t) { |
| 13542 | Py_INCREF(t); |
| 13543 | Py_DECREF(*p); |
| 13544 | *p = t; |
| 13545 | return; |
| 13546 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13547 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13548 | PyThreadState_GET()->recursion_critical = 1; |
| 13549 | if (PyDict_SetItem(interned, (PyObject *)s, (PyObject *)s) < 0) { |
| 13550 | PyErr_Clear(); |
| 13551 | PyThreadState_GET()->recursion_critical = 0; |
| 13552 | return; |
| 13553 | } |
| 13554 | PyThreadState_GET()->recursion_critical = 0; |
| 13555 | /* The two references in interned are not counted by refcnt. |
| 13556 | The deallocator will take care of this */ |
| 13557 | Py_REFCNT(s) -= 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13558 | _PyUnicode_STATE(s).interned = SSTATE_INTERNED_MORTAL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13559 | } |
| 13560 | |
| 13561 | void |
| 13562 | PyUnicode_InternImmortal(PyObject **p) |
| 13563 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13564 | PyUnicodeObject *u = (PyUnicodeObject *)*p; |
| 13565 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13566 | PyUnicode_InternInPlace(p); |
| 13567 | if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13568 | _PyUnicode_STATE(u).interned = SSTATE_INTERNED_IMMORTAL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13569 | Py_INCREF(*p); |
| 13570 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13571 | } |
| 13572 | |
| 13573 | PyObject * |
| 13574 | PyUnicode_InternFromString(const char *cp) |
| 13575 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13576 | PyObject *s = PyUnicode_FromString(cp); |
| 13577 | if (s == NULL) |
| 13578 | return NULL; |
| 13579 | PyUnicode_InternInPlace(&s); |
| 13580 | return s; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13581 | } |
| 13582 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 13583 | void |
| 13584 | _Py_ReleaseInternedUnicodeStrings(void) |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13585 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13586 | PyObject *keys; |
| 13587 | PyUnicodeObject *s; |
| 13588 | Py_ssize_t i, n; |
| 13589 | Py_ssize_t immortal_size = 0, mortal_size = 0; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13590 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13591 | if (interned == NULL || !PyDict_Check(interned)) |
| 13592 | return; |
| 13593 | keys = PyDict_Keys(interned); |
| 13594 | if (keys == NULL || !PyList_Check(keys)) { |
| 13595 | PyErr_Clear(); |
| 13596 | return; |
| 13597 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13598 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13599 | /* Since _Py_ReleaseInternedUnicodeStrings() is intended to help a leak |
| 13600 | detector, interned unicode strings are not forcibly deallocated; |
| 13601 | rather, we give them their stolen references back, and then clear |
| 13602 | and DECREF the interned dict. */ |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13603 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13604 | n = PyList_GET_SIZE(keys); |
| 13605 | fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13606 | n); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13607 | for (i = 0; i < n; i++) { |
| 13608 | s = (PyUnicodeObject *) PyList_GET_ITEM(keys, i); |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 13609 | if (PyUnicode_READY(s) == -1) { |
| 13610 | assert(0 && "could not ready string"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13611 | fprintf(stderr, "could not ready string\n"); |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 13612 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13613 | switch (PyUnicode_CHECK_INTERNED(s)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13614 | case SSTATE_NOT_INTERNED: |
| 13615 | /* XXX Shouldn't happen */ |
| 13616 | break; |
| 13617 | case SSTATE_INTERNED_IMMORTAL: |
| 13618 | Py_REFCNT(s) += 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13619 | immortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13620 | break; |
| 13621 | case SSTATE_INTERNED_MORTAL: |
| 13622 | Py_REFCNT(s) += 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13623 | mortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13624 | break; |
| 13625 | default: |
| 13626 | Py_FatalError("Inconsistent interned string state."); |
| 13627 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13628 | _PyUnicode_STATE(s).interned = SSTATE_NOT_INTERNED; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13629 | } |
| 13630 | fprintf(stderr, "total size of all interned strings: " |
| 13631 | "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " |
| 13632 | "mortal/immortal\n", mortal_size, immortal_size); |
| 13633 | Py_DECREF(keys); |
| 13634 | PyDict_Clear(interned); |
| 13635 | Py_DECREF(interned); |
| 13636 | interned = NULL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13637 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13638 | |
| 13639 | |
| 13640 | /********************* Unicode Iterator **************************/ |
| 13641 | |
| 13642 | typedef struct { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13643 | PyObject_HEAD |
| 13644 | Py_ssize_t it_index; |
| 13645 | PyUnicodeObject *it_seq; /* Set to NULL when iterator is exhausted */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13646 | } unicodeiterobject; |
| 13647 | |
| 13648 | static void |
| 13649 | unicodeiter_dealloc(unicodeiterobject *it) |
| 13650 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13651 | _PyObject_GC_UNTRACK(it); |
| 13652 | Py_XDECREF(it->it_seq); |
| 13653 | PyObject_GC_Del(it); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13654 | } |
| 13655 | |
| 13656 | static int |
| 13657 | unicodeiter_traverse(unicodeiterobject *it, visitproc visit, void *arg) |
| 13658 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13659 | Py_VISIT(it->it_seq); |
| 13660 | return 0; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13661 | } |
| 13662 | |
| 13663 | static PyObject * |
| 13664 | unicodeiter_next(unicodeiterobject *it) |
| 13665 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13666 | PyUnicodeObject *seq; |
| 13667 | PyObject *item; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13668 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13669 | assert(it != NULL); |
| 13670 | seq = it->it_seq; |
| 13671 | if (seq == NULL) |
| 13672 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 13673 | assert(_PyUnicode_CHECK(seq)); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13674 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13675 | if (it->it_index < PyUnicode_GET_LENGTH(seq)) { |
| 13676 | int kind = PyUnicode_KIND(seq); |
| 13677 | void *data = PyUnicode_DATA(seq); |
| 13678 | Py_UCS4 chr = PyUnicode_READ(kind, data, it->it_index); |
| 13679 | item = PyUnicode_FromOrdinal(chr); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13680 | if (item != NULL) |
| 13681 | ++it->it_index; |
| 13682 | return item; |
| 13683 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13684 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13685 | Py_DECREF(seq); |
| 13686 | it->it_seq = NULL; |
| 13687 | return NULL; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13688 | } |
| 13689 | |
| 13690 | static PyObject * |
| 13691 | unicodeiter_len(unicodeiterobject *it) |
| 13692 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13693 | Py_ssize_t len = 0; |
| 13694 | if (it->it_seq) |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 13695 | len = PyUnicode_GET_LENGTH(it->it_seq) - it->it_index; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13696 | return PyLong_FromSsize_t(len); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13697 | } |
| 13698 | |
| 13699 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
| 13700 | |
| 13701 | static PyMethodDef unicodeiter_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13702 | {"__length_hint__", (PyCFunction)unicodeiter_len, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13703 | length_hint_doc}, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13704 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13705 | }; |
| 13706 | |
| 13707 | PyTypeObject PyUnicodeIter_Type = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13708 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 13709 | "str_iterator", /* tp_name */ |
| 13710 | sizeof(unicodeiterobject), /* tp_basicsize */ |
| 13711 | 0, /* tp_itemsize */ |
| 13712 | /* methods */ |
| 13713 | (destructor)unicodeiter_dealloc, /* tp_dealloc */ |
| 13714 | 0, /* tp_print */ |
| 13715 | 0, /* tp_getattr */ |
| 13716 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 13717 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13718 | 0, /* tp_repr */ |
| 13719 | 0, /* tp_as_number */ |
| 13720 | 0, /* tp_as_sequence */ |
| 13721 | 0, /* tp_as_mapping */ |
| 13722 | 0, /* tp_hash */ |
| 13723 | 0, /* tp_call */ |
| 13724 | 0, /* tp_str */ |
| 13725 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 13726 | 0, /* tp_setattro */ |
| 13727 | 0, /* tp_as_buffer */ |
| 13728 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 13729 | 0, /* tp_doc */ |
| 13730 | (traverseproc)unicodeiter_traverse, /* tp_traverse */ |
| 13731 | 0, /* tp_clear */ |
| 13732 | 0, /* tp_richcompare */ |
| 13733 | 0, /* tp_weaklistoffset */ |
| 13734 | PyObject_SelfIter, /* tp_iter */ |
| 13735 | (iternextfunc)unicodeiter_next, /* tp_iternext */ |
| 13736 | unicodeiter_methods, /* tp_methods */ |
| 13737 | 0, |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13738 | }; |
| 13739 | |
| 13740 | static PyObject * |
| 13741 | unicode_iter(PyObject *seq) |
| 13742 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13743 | unicodeiterobject *it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13744 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13745 | if (!PyUnicode_Check(seq)) { |
| 13746 | PyErr_BadInternalCall(); |
| 13747 | return NULL; |
| 13748 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13749 | if (PyUnicode_READY(seq) == -1) |
| 13750 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13751 | it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type); |
| 13752 | if (it == NULL) |
| 13753 | return NULL; |
| 13754 | it->it_index = 0; |
| 13755 | Py_INCREF(seq); |
| 13756 | it->it_seq = (PyUnicodeObject *)seq; |
| 13757 | _PyObject_GC_TRACK(it); |
| 13758 | return (PyObject *)it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13759 | } |
| 13760 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13761 | #define UNIOP(x) Py_UNICODE_##x |
| 13762 | #define UNIOP_t Py_UNICODE |
| 13763 | #include "uniops.h" |
| 13764 | #undef UNIOP |
| 13765 | #undef UNIOP_t |
| 13766 | #define UNIOP(x) Py_UCS4_##x |
| 13767 | #define UNIOP_t Py_UCS4 |
| 13768 | #include "uniops.h" |
| 13769 | #undef UNIOP |
| 13770 | #undef UNIOP_t |
Victor Stinner | 331ea92 | 2010-08-10 16:37:20 +0000 | [diff] [blame] | 13771 | |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 13772 | Py_UNICODE* |
Victor Stinner | 4640860 | 2010-09-03 16:18:00 +0000 | [diff] [blame] | 13773 | PyUnicode_AsUnicodeCopy(PyObject *object) |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 13774 | { |
| 13775 | PyUnicodeObject *unicode = (PyUnicodeObject *)object; |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 13776 | Py_UNICODE *u, *copy; |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 13777 | Py_ssize_t size; |
| 13778 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13779 | if (!PyUnicode_Check(unicode)) { |
| 13780 | PyErr_BadArgument(); |
| 13781 | return NULL; |
| 13782 | } |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 13783 | u = PyUnicode_AsUnicode(object); |
| 13784 | if (u == NULL) |
| 13785 | return NULL; |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 13786 | /* Ensure we won't overflow the size. */ |
| 13787 | if (PyUnicode_GET_SIZE(unicode) > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
| 13788 | PyErr_NoMemory(); |
| 13789 | return NULL; |
| 13790 | } |
| 13791 | size = PyUnicode_GET_SIZE(unicode) + 1; /* copy the nul character */ |
| 13792 | size *= sizeof(Py_UNICODE); |
| 13793 | copy = PyMem_Malloc(size); |
| 13794 | if (copy == NULL) { |
| 13795 | PyErr_NoMemory(); |
| 13796 | return NULL; |
| 13797 | } |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 13798 | memcpy(copy, u, size); |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 13799 | return copy; |
| 13800 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 13801 | |
Georg Brandl | 66c221e | 2010-10-14 07:04:07 +0000 | [diff] [blame] | 13802 | /* A _string module, to export formatter_parser and formatter_field_name_split |
| 13803 | to the string.Formatter class implemented in Python. */ |
| 13804 | |
| 13805 | static PyMethodDef _string_methods[] = { |
| 13806 | {"formatter_field_name_split", (PyCFunction) formatter_field_name_split, |
| 13807 | METH_O, PyDoc_STR("split the argument as a field name")}, |
| 13808 | {"formatter_parser", (PyCFunction) formatter_parser, |
| 13809 | METH_O, PyDoc_STR("parse the argument as a format string")}, |
| 13810 | {NULL, NULL} |
| 13811 | }; |
| 13812 | |
| 13813 | static struct PyModuleDef _string_module = { |
| 13814 | PyModuleDef_HEAD_INIT, |
| 13815 | "_string", |
| 13816 | PyDoc_STR("string helper module"), |
| 13817 | 0, |
| 13818 | _string_methods, |
| 13819 | NULL, |
| 13820 | NULL, |
| 13821 | NULL, |
| 13822 | NULL |
| 13823 | }; |
| 13824 | |
| 13825 | PyMODINIT_FUNC |
| 13826 | PyInit__string(void) |
| 13827 | { |
| 13828 | return PyModule_Create(&_string_module); |
| 13829 | } |
| 13830 | |
| 13831 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 13832 | #ifdef __cplusplus |
| 13833 | } |
| 13834 | #endif |