Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1 | /* |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2 | |
| 3 | Unicode implementation based on original code by Fredrik Lundh, |
Benjamin Peterson | 31616ea | 2011-10-01 00:11:09 -0400 | [diff] [blame] | 4 | modified by Marc-Andre Lemburg <mal@lemburg.com>. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6 | Major speed upgrades to the method implementations at the Reykjavik |
| 7 | NeedForSpeed sprint, by Fredrik Lundh and Andrew Dalke. |
| 8 | |
Guido van Rossum | 16b1ad9 | 2000-08-03 16:24:25 +0000 | [diff] [blame] | 9 | Copyright (c) Corporation for National Research Initiatives. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10 | |
Fredrik Lundh | 0fdb90c | 2001-01-19 09:45:02 +0000 | [diff] [blame] | 11 | -------------------------------------------------------------------- |
| 12 | The original string type implementation is: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14 | Copyright (c) 1999 by Secret Labs AB |
| 15 | Copyright (c) 1999 by Fredrik Lundh |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 16 | |
Fredrik Lundh | 0fdb90c | 2001-01-19 09:45:02 +0000 | [diff] [blame] | 17 | By obtaining, using, and/or copying this software and/or its |
| 18 | associated documentation, you agree that you have read, understood, |
| 19 | and will comply with the following terms and conditions: |
| 20 | |
| 21 | Permission to use, copy, modify, and distribute this software and its |
| 22 | associated documentation for any purpose and without fee is hereby |
| 23 | granted, provided that the above copyright notice appears in all |
| 24 | copies, and that both that copyright notice and this permission notice |
| 25 | appear in supporting documentation, and that the name of Secret Labs |
| 26 | AB or the author not be used in advertising or publicity pertaining to |
| 27 | distribution of the software without specific, written prior |
| 28 | permission. |
| 29 | |
| 30 | SECRET LABS AB AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO |
| 31 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 32 | FITNESS. IN NO EVENT SHALL SECRET LABS AB OR THE AUTHOR BE LIABLE FOR |
| 33 | ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 34 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 35 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 36 | OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 37 | -------------------------------------------------------------------- |
| 38 | |
| 39 | */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 40 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 41 | #define PY_SSIZE_T_CLEAN |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 42 | #include "Python.h" |
Marc-André Lemburg | d49e5b4 | 2000-06-30 14:58:20 +0000 | [diff] [blame] | 43 | #include "ucnhash.h" |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 44 | #include "bytes_methods.h" |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 45 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 46 | #ifdef MS_WINDOWS |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 47 | #include <windows.h> |
| 48 | #endif |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 49 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 50 | /* Endianness switches; defaults to little endian */ |
| 51 | |
| 52 | #ifdef WORDS_BIGENDIAN |
| 53 | # define BYTEORDER_IS_BIG_ENDIAN |
| 54 | #else |
| 55 | # define BYTEORDER_IS_LITTLE_ENDIAN |
| 56 | #endif |
| 57 | |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 58 | /* --- Globals ------------------------------------------------------------ |
| 59 | |
| 60 | The globals are initialized by the _PyUnicode_Init() API and should |
| 61 | not be used before calling that API. |
| 62 | |
| 63 | */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 64 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 65 | |
| 66 | #ifdef __cplusplus |
| 67 | extern "C" { |
| 68 | #endif |
| 69 | |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 70 | /* Maximum code point of Unicode 6.0: 0x10ffff (1,114,111) */ |
| 71 | #define MAX_UNICODE 0x10ffff |
| 72 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 73 | #ifdef Py_DEBUG |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 74 | # define _PyUnicode_CHECK(op) _PyUnicode_CheckConsistency(op, 0) |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 75 | #else |
| 76 | # define _PyUnicode_CHECK(op) PyUnicode_Check(op) |
| 77 | #endif |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 78 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 79 | #define _PyUnicode_UTF8(op) \ |
| 80 | (((PyCompactUnicodeObject*)(op))->utf8) |
| 81 | #define PyUnicode_UTF8(op) \ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 82 | (assert(_PyUnicode_CHECK(op)), \ |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 83 | assert(PyUnicode_IS_READY(op)), \ |
| 84 | PyUnicode_IS_COMPACT_ASCII(op) ? \ |
| 85 | ((char*)((PyASCIIObject*)(op) + 1)) : \ |
| 86 | _PyUnicode_UTF8(op)) |
Victor Stinner | bc8b81b | 2011-09-29 19:31:34 +0200 | [diff] [blame] | 87 | #define _PyUnicode_UTF8_LENGTH(op) \ |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 88 | (((PyCompactUnicodeObject*)(op))->utf8_length) |
| 89 | #define PyUnicode_UTF8_LENGTH(op) \ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 90 | (assert(_PyUnicode_CHECK(op)), \ |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 91 | assert(PyUnicode_IS_READY(op)), \ |
| 92 | PyUnicode_IS_COMPACT_ASCII(op) ? \ |
| 93 | ((PyASCIIObject*)(op))->length : \ |
| 94 | _PyUnicode_UTF8_LENGTH(op)) |
Victor Stinner | a5f9163 | 2011-10-04 01:07:11 +0200 | [diff] [blame] | 95 | #define _PyUnicode_WSTR(op) \ |
| 96 | (((PyASCIIObject*)(op))->wstr) |
| 97 | #define _PyUnicode_WSTR_LENGTH(op) \ |
| 98 | (((PyCompactUnicodeObject*)(op))->wstr_length) |
| 99 | #define _PyUnicode_LENGTH(op) \ |
| 100 | (((PyASCIIObject *)(op))->length) |
| 101 | #define _PyUnicode_STATE(op) \ |
| 102 | (((PyASCIIObject *)(op))->state) |
| 103 | #define _PyUnicode_HASH(op) \ |
| 104 | (((PyASCIIObject *)(op))->hash) |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 105 | #define _PyUnicode_KIND(op) \ |
| 106 | (assert(_PyUnicode_CHECK(op)), \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 107 | ((PyASCIIObject *)(op))->state.kind) |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 108 | #define _PyUnicode_GET_LENGTH(op) \ |
| 109 | (assert(_PyUnicode_CHECK(op)), \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 110 | ((PyASCIIObject *)(op))->length) |
Victor Stinner | a5f9163 | 2011-10-04 01:07:11 +0200 | [diff] [blame] | 111 | #define _PyUnicode_DATA_ANY(op) \ |
| 112 | (((PyUnicodeObject*)(op))->data.any) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 113 | |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 114 | /* Optimized version of Py_MAX() to compute the maximum character: |
| 115 | use it when your are computing the second argument of PyUnicode_New() */ |
| 116 | #define MAX_MAXCHAR(maxchar1, maxchar2) \ |
| 117 | ((maxchar1) | (maxchar2)) |
| 118 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 119 | #undef PyUnicode_READY |
| 120 | #define PyUnicode_READY(op) \ |
| 121 | (assert(_PyUnicode_CHECK(op)), \ |
| 122 | (PyUnicode_IS_READY(op) ? \ |
Victor Stinner | a5f9163 | 2011-10-04 01:07:11 +0200 | [diff] [blame] | 123 | 0 : \ |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 124 | _PyUnicode_Ready(op))) |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 125 | |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 126 | #define _PyUnicode_SHARE_UTF8(op) \ |
| 127 | (assert(_PyUnicode_CHECK(op)), \ |
| 128 | assert(!PyUnicode_IS_COMPACT_ASCII(op)), \ |
| 129 | (_PyUnicode_UTF8(op) == PyUnicode_DATA(op))) |
| 130 | #define _PyUnicode_SHARE_WSTR(op) \ |
| 131 | (assert(_PyUnicode_CHECK(op)), \ |
| 132 | (_PyUnicode_WSTR(unicode) == PyUnicode_DATA(op))) |
| 133 | |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 134 | /* true if the Unicode object has an allocated UTF-8 memory block |
| 135 | (not shared with other data) */ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 136 | #define _PyUnicode_HAS_UTF8_MEMORY(op) \ |
| 137 | (assert(_PyUnicode_CHECK(op)), \ |
| 138 | (!PyUnicode_IS_COMPACT_ASCII(op) \ |
| 139 | && _PyUnicode_UTF8(op) \ |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 140 | && _PyUnicode_UTF8(op) != PyUnicode_DATA(op))) |
| 141 | |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 142 | /* true if the Unicode object has an allocated wstr memory block |
| 143 | (not shared with other data) */ |
| 144 | #define _PyUnicode_HAS_WSTR_MEMORY(op) \ |
| 145 | (assert(_PyUnicode_CHECK(op)), \ |
| 146 | (_PyUnicode_WSTR(op) && \ |
| 147 | (!PyUnicode_IS_READY(op) || \ |
| 148 | _PyUnicode_WSTR(op) != PyUnicode_DATA(op)))) |
| 149 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 150 | /* Generic helper macro to convert characters of different types. |
| 151 | from_type and to_type have to be valid type names, begin and end |
| 152 | are pointers to the source characters which should be of type |
| 153 | "from_type *". to is a pointer of type "to_type *" and points to the |
| 154 | buffer where the result characters are written to. */ |
| 155 | #define _PyUnicode_CONVERT_BYTES(from_type, to_type, begin, end, to) \ |
| 156 | do { \ |
Antoine Pitrou | e459a08 | 2011-10-11 20:58:41 +0200 | [diff] [blame] | 157 | to_type *_to = (to_type *) to; \ |
| 158 | const from_type *_iter = (begin); \ |
| 159 | const from_type *_end = (end); \ |
| 160 | Py_ssize_t n = (_end) - (_iter); \ |
| 161 | const from_type *_unrolled_end = \ |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 162 | _iter + _Py_SIZE_ROUND_DOWN(n, 4); \ |
Antoine Pitrou | e459a08 | 2011-10-11 20:58:41 +0200 | [diff] [blame] | 163 | while (_iter < (_unrolled_end)) { \ |
| 164 | _to[0] = (to_type) _iter[0]; \ |
| 165 | _to[1] = (to_type) _iter[1]; \ |
| 166 | _to[2] = (to_type) _iter[2]; \ |
| 167 | _to[3] = (to_type) _iter[3]; \ |
| 168 | _iter += 4; _to += 4; \ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 169 | } \ |
Antoine Pitrou | e459a08 | 2011-10-11 20:58:41 +0200 | [diff] [blame] | 170 | while (_iter < (_end)) \ |
| 171 | *_to++ = (to_type) *_iter++; \ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 172 | } while (0) |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 173 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 174 | /* This dictionary holds all interned unicode strings. Note that references |
| 175 | to strings in this dictionary are *not* counted in the string's ob_refcnt. |
| 176 | When the interned string reaches a refcnt of 0 the string deallocation |
| 177 | function will delete the reference from this dictionary. |
| 178 | |
| 179 | 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] | 180 | 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] | 181 | */ |
| 182 | static PyObject *interned; |
| 183 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 184 | /* The empty Unicode object is shared to improve performance. */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 185 | static PyObject *unicode_empty; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 186 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 187 | /* List of static strings. */ |
| 188 | static _Py_Identifier *static_strings; |
| 189 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 190 | /* Single character Unicode strings in the Latin-1 range are being |
| 191 | shared as well. */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 192 | static PyObject *unicode_latin1[256]; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 193 | |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 194 | /* Fast detection of the most frequent whitespace characters */ |
| 195 | const unsigned char _Py_ascii_whitespace[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 196 | 0, 0, 0, 0, 0, 0, 0, 0, |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 197 | /* case 0x0009: * CHARACTER TABULATION */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 198 | /* case 0x000A: * LINE FEED */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 199 | /* case 0x000B: * LINE TABULATION */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 200 | /* case 0x000C: * FORM FEED */ |
| 201 | /* case 0x000D: * CARRIAGE RETURN */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 202 | 0, 1, 1, 1, 1, 1, 0, 0, |
| 203 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 204 | /* case 0x001C: * FILE SEPARATOR */ |
| 205 | /* case 0x001D: * GROUP SEPARATOR */ |
| 206 | /* case 0x001E: * RECORD SEPARATOR */ |
| 207 | /* case 0x001F: * UNIT SEPARATOR */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 208 | 0, 0, 0, 0, 1, 1, 1, 1, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 209 | /* case 0x0020: * SPACE */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 210 | 1, 0, 0, 0, 0, 0, 0, 0, |
| 211 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 212 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 213 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 214 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 215 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 216 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 217 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 218 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 219 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 220 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 221 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 222 | 0, 0, 0, 0, 0, 0, 0, 0 |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 223 | }; |
| 224 | |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 225 | /* forward */ |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 226 | static PyUnicodeObject *_PyUnicode_New(Py_ssize_t length); |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 227 | static PyObject* get_latin1_char(unsigned char ch); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 228 | static int unicode_modifiable(PyObject *unicode); |
| 229 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 230 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 231 | static PyObject * |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 232 | _PyUnicode_FromUCS1(const unsigned char *s, Py_ssize_t size); |
| 233 | static PyObject * |
| 234 | _PyUnicode_FromUCS2(const Py_UCS2 *s, Py_ssize_t size); |
| 235 | static PyObject * |
| 236 | _PyUnicode_FromUCS4(const Py_UCS4 *s, Py_ssize_t size); |
| 237 | |
| 238 | static PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 239 | unicode_encode_call_errorhandler(const char *errors, |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 240 | PyObject **errorHandler,const char *encoding, const char *reason, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 241 | PyObject *unicode, PyObject **exceptionObject, |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 242 | Py_ssize_t startpos, Py_ssize_t endpos, Py_ssize_t *newpos); |
| 243 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 244 | static void |
| 245 | raise_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 246 | const char *encoding, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 247 | PyObject *unicode, |
| 248 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 249 | const char *reason); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 250 | |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 251 | /* Same for linebreaks */ |
| 252 | static unsigned char ascii_linebreak[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 253 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 254 | /* 0x000A, * LINE FEED */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 255 | /* 0x000B, * LINE TABULATION */ |
| 256 | /* 0x000C, * FORM FEED */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 257 | /* 0x000D, * CARRIAGE RETURN */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 258 | 0, 0, 1, 1, 1, 1, 0, 0, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 259 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 260 | /* 0x001C, * FILE SEPARATOR */ |
| 261 | /* 0x001D, * GROUP SEPARATOR */ |
| 262 | /* 0x001E, * RECORD SEPARATOR */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 263 | 0, 0, 0, 0, 1, 1, 1, 0, |
| 264 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 265 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 266 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 267 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 268 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 269 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 270 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 271 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 272 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 273 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 274 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 275 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 276 | 0, 0, 0, 0, 0, 0, 0, 0 |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 277 | }; |
| 278 | |
Ezio Melotti | 48a2f8f | 2011-09-29 00:18:19 +0300 | [diff] [blame] | 279 | /* The max unicode value is always 0x10FFFF while using the PEP-393 API. |
| 280 | 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] | 281 | Py_UNICODE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 282 | PyUnicode_GetMax(void) |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 283 | { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 284 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 285 | return 0x10FFFF; |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 286 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 287 | /* This is actually an illegal character, so it should |
| 288 | not be passed to unichr. */ |
| 289 | return 0xFFFF; |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 290 | #endif |
| 291 | } |
| 292 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 293 | #ifdef Py_DEBUG |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 294 | int |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 295 | _PyUnicode_CheckConsistency(PyObject *op, int check_content) |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 296 | { |
| 297 | PyASCIIObject *ascii; |
| 298 | unsigned int kind; |
| 299 | |
| 300 | assert(PyUnicode_Check(op)); |
| 301 | |
| 302 | ascii = (PyASCIIObject *)op; |
| 303 | kind = ascii->state.kind; |
| 304 | |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 305 | if (ascii->state.ascii == 1 && ascii->state.compact == 1) { |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 306 | assert(kind == PyUnicode_1BYTE_KIND); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 307 | assert(ascii->state.ready == 1); |
| 308 | } |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 309 | else { |
Victor Stinner | 85041a5 | 2011-10-03 14:42:39 +0200 | [diff] [blame] | 310 | PyCompactUnicodeObject *compact = (PyCompactUnicodeObject *)op; |
Victor Stinner | 7f11ad4 | 2011-10-04 00:00:20 +0200 | [diff] [blame] | 311 | void *data; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 312 | |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 313 | if (ascii->state.compact == 1) { |
| 314 | data = compact + 1; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 315 | assert(kind == PyUnicode_1BYTE_KIND |
| 316 | || kind == PyUnicode_2BYTE_KIND |
| 317 | || kind == PyUnicode_4BYTE_KIND); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 318 | assert(ascii->state.ascii == 0); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 319 | assert(ascii->state.ready == 1); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 320 | assert (compact->utf8 != data); |
Victor Stinner | e30c0a1 | 2011-11-04 20:54:05 +0100 | [diff] [blame] | 321 | } |
| 322 | else { |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 323 | PyUnicodeObject *unicode = (PyUnicodeObject *)op; |
| 324 | |
| 325 | data = unicode->data.any; |
| 326 | if (kind == PyUnicode_WCHAR_KIND) { |
Victor Stinner | e30c0a1 | 2011-11-04 20:54:05 +0100 | [diff] [blame] | 327 | assert(ascii->length == 0); |
| 328 | assert(ascii->hash == -1); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 329 | assert(ascii->state.compact == 0); |
| 330 | assert(ascii->state.ascii == 0); |
| 331 | assert(ascii->state.ready == 0); |
Victor Stinner | e30c0a1 | 2011-11-04 20:54:05 +0100 | [diff] [blame] | 332 | assert(ascii->state.interned == SSTATE_NOT_INTERNED); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 333 | assert(ascii->wstr != NULL); |
| 334 | assert(data == NULL); |
| 335 | assert(compact->utf8 == NULL); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 336 | } |
| 337 | else { |
| 338 | assert(kind == PyUnicode_1BYTE_KIND |
| 339 | || kind == PyUnicode_2BYTE_KIND |
| 340 | || kind == PyUnicode_4BYTE_KIND); |
| 341 | assert(ascii->state.compact == 0); |
| 342 | assert(ascii->state.ready == 1); |
| 343 | assert(data != NULL); |
| 344 | if (ascii->state.ascii) { |
| 345 | assert (compact->utf8 == data); |
| 346 | assert (compact->utf8_length == ascii->length); |
| 347 | } |
| 348 | else |
| 349 | assert (compact->utf8 != data); |
| 350 | } |
| 351 | } |
| 352 | if (kind != PyUnicode_WCHAR_KIND) { |
Victor Stinner | 7f11ad4 | 2011-10-04 00:00:20 +0200 | [diff] [blame] | 353 | if ( |
| 354 | #if SIZEOF_WCHAR_T == 2 |
| 355 | kind == PyUnicode_2BYTE_KIND |
| 356 | #else |
| 357 | kind == PyUnicode_4BYTE_KIND |
| 358 | #endif |
| 359 | ) |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 360 | { |
| 361 | assert(ascii->wstr == data); |
| 362 | assert(compact->wstr_length == ascii->length); |
| 363 | } else |
| 364 | assert(ascii->wstr != data); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 365 | } |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 366 | |
| 367 | if (compact->utf8 == NULL) |
| 368 | assert(compact->utf8_length == 0); |
| 369 | if (ascii->wstr == NULL) |
| 370 | assert(compact->wstr_length == 0); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 371 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 372 | /* check that the best kind is used */ |
| 373 | if (check_content && kind != PyUnicode_WCHAR_KIND) |
| 374 | { |
| 375 | Py_ssize_t i; |
| 376 | Py_UCS4 maxchar = 0; |
Victor Stinner | 718fbf0 | 2012-04-26 00:39:37 +0200 | [diff] [blame] | 377 | void *data; |
| 378 | Py_UCS4 ch; |
| 379 | |
| 380 | data = PyUnicode_DATA(ascii); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 381 | for (i=0; i < ascii->length; i++) |
| 382 | { |
Victor Stinner | 718fbf0 | 2012-04-26 00:39:37 +0200 | [diff] [blame] | 383 | ch = PyUnicode_READ(kind, data, i); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 384 | if (ch > maxchar) |
| 385 | maxchar = ch; |
| 386 | } |
| 387 | if (kind == PyUnicode_1BYTE_KIND) { |
Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 388 | if (ascii->state.ascii == 0) { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 389 | assert(maxchar >= 128); |
Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 390 | assert(maxchar <= 255); |
| 391 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 392 | else |
| 393 | assert(maxchar < 128); |
| 394 | } |
Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 395 | else if (kind == PyUnicode_2BYTE_KIND) { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 396 | assert(maxchar >= 0x100); |
Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 397 | assert(maxchar <= 0xFFFF); |
| 398 | } |
| 399 | else { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 400 | assert(maxchar >= 0x10000); |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 401 | assert(maxchar <= MAX_UNICODE); |
Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 402 | } |
Victor Stinner | 718fbf0 | 2012-04-26 00:39:37 +0200 | [diff] [blame] | 403 | assert(PyUnicode_READ(kind, data, ascii->length) == 0); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 404 | } |
Benjamin Peterson | ccc51c1 | 2011-10-03 19:34:12 -0400 | [diff] [blame] | 405 | return 1; |
| 406 | } |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 407 | #endif |
| 408 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 409 | static PyObject* |
| 410 | unicode_result_wchar(PyObject *unicode) |
| 411 | { |
| 412 | #ifndef Py_DEBUG |
| 413 | Py_ssize_t len; |
| 414 | |
| 415 | assert(Py_REFCNT(unicode) == 1); |
| 416 | |
| 417 | len = _PyUnicode_WSTR_LENGTH(unicode); |
| 418 | if (len == 0) { |
| 419 | Py_INCREF(unicode_empty); |
| 420 | Py_DECREF(unicode); |
| 421 | return unicode_empty; |
| 422 | } |
| 423 | |
| 424 | if (len == 1) { |
| 425 | wchar_t ch = _PyUnicode_WSTR(unicode)[0]; |
| 426 | if (ch < 256) { |
| 427 | PyObject *latin1_char = get_latin1_char((unsigned char)ch); |
| 428 | Py_DECREF(unicode); |
| 429 | return latin1_char; |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | if (_PyUnicode_Ready(unicode) < 0) { |
| 434 | Py_XDECREF(unicode); |
| 435 | return NULL; |
| 436 | } |
| 437 | #else |
| 438 | /* don't make the result ready in debug mode to ensure that the caller |
| 439 | makes the string ready before using it */ |
| 440 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 441 | #endif |
| 442 | return unicode; |
| 443 | } |
| 444 | |
| 445 | static PyObject* |
| 446 | unicode_result_ready(PyObject *unicode) |
| 447 | { |
| 448 | Py_ssize_t length; |
| 449 | |
| 450 | length = PyUnicode_GET_LENGTH(unicode); |
| 451 | if (length == 0) { |
| 452 | if (unicode != unicode_empty) { |
| 453 | Py_INCREF(unicode_empty); |
| 454 | Py_DECREF(unicode); |
| 455 | } |
| 456 | return unicode_empty; |
| 457 | } |
| 458 | |
| 459 | if (length == 1) { |
| 460 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, 0); |
| 461 | if (ch < 256) { |
| 462 | PyObject *latin1_char = unicode_latin1[ch]; |
| 463 | if (latin1_char != NULL) { |
| 464 | if (unicode != latin1_char) { |
| 465 | Py_INCREF(latin1_char); |
| 466 | Py_DECREF(unicode); |
| 467 | } |
| 468 | return latin1_char; |
| 469 | } |
| 470 | else { |
| 471 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 472 | Py_INCREF(unicode); |
| 473 | unicode_latin1[ch] = unicode; |
| 474 | return unicode; |
| 475 | } |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 480 | return unicode; |
| 481 | } |
| 482 | |
| 483 | static PyObject* |
| 484 | unicode_result(PyObject *unicode) |
| 485 | { |
| 486 | assert(_PyUnicode_CHECK(unicode)); |
| 487 | if (PyUnicode_IS_READY(unicode)) |
| 488 | return unicode_result_ready(unicode); |
| 489 | else |
| 490 | return unicode_result_wchar(unicode); |
| 491 | } |
| 492 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 493 | static PyObject* |
| 494 | unicode_result_unchanged(PyObject *unicode) |
| 495 | { |
| 496 | if (PyUnicode_CheckExact(unicode)) { |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 497 | if (PyUnicode_READY(unicode) == -1) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 498 | return NULL; |
| 499 | Py_INCREF(unicode); |
| 500 | return unicode; |
| 501 | } |
| 502 | else |
| 503 | /* Subtype -- return genuine unicode string with the same value. */ |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 504 | return _PyUnicode_Copy(unicode); |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 505 | } |
| 506 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 507 | #ifdef HAVE_MBCS |
| 508 | static OSVERSIONINFOEX winver; |
| 509 | #endif |
| 510 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 511 | /* --- Bloom Filters ----------------------------------------------------- */ |
| 512 | |
| 513 | /* stuff to implement simple "bloom filters" for Unicode characters. |
| 514 | to keep things simple, we use a single bitmask, using the least 5 |
| 515 | bits from each unicode characters as the bit index. */ |
| 516 | |
| 517 | /* the linebreak mask is set up by Unicode_Init below */ |
| 518 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 519 | #if LONG_BIT >= 128 |
| 520 | #define BLOOM_WIDTH 128 |
| 521 | #elif LONG_BIT >= 64 |
| 522 | #define BLOOM_WIDTH 64 |
| 523 | #elif LONG_BIT >= 32 |
| 524 | #define BLOOM_WIDTH 32 |
| 525 | #else |
| 526 | #error "LONG_BIT is smaller than 32" |
| 527 | #endif |
| 528 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 529 | #define BLOOM_MASK unsigned long |
| 530 | |
| 531 | static BLOOM_MASK bloom_linebreak; |
| 532 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 533 | #define BLOOM_ADD(mask, ch) ((mask |= (1UL << ((ch) & (BLOOM_WIDTH - 1))))) |
| 534 | #define BLOOM(mask, ch) ((mask & (1UL << ((ch) & (BLOOM_WIDTH - 1))))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 535 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 536 | #define BLOOM_LINEBREAK(ch) \ |
| 537 | ((ch) < 128U ? ascii_linebreak[(ch)] : \ |
| 538 | (BLOOM(bloom_linebreak, (ch)) && Py_UNICODE_ISLINEBREAK(ch))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 539 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 540 | Py_LOCAL_INLINE(BLOOM_MASK) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 541 | make_bloom_mask(int kind, void* ptr, Py_ssize_t len) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 542 | { |
| 543 | /* calculate simple bloom-style bitmask for a given unicode string */ |
| 544 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 545 | BLOOM_MASK mask; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 546 | Py_ssize_t i; |
| 547 | |
| 548 | mask = 0; |
| 549 | for (i = 0; i < len; i++) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 550 | BLOOM_ADD(mask, PyUnicode_READ(kind, ptr, i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 551 | |
| 552 | return mask; |
| 553 | } |
| 554 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 555 | #define BLOOM_MEMBER(mask, chr, str) \ |
| 556 | (BLOOM(mask, chr) \ |
| 557 | && (PyUnicode_FindChar(str, chr, 0, PyUnicode_GET_LENGTH(str), 1) >= 0)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 558 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 559 | /* Compilation of templated routines */ |
| 560 | |
| 561 | #include "stringlib/asciilib.h" |
| 562 | #include "stringlib/fastsearch.h" |
| 563 | #include "stringlib/partition.h" |
| 564 | #include "stringlib/split.h" |
| 565 | #include "stringlib/count.h" |
| 566 | #include "stringlib/find.h" |
| 567 | #include "stringlib/find_max_char.h" |
| 568 | #include "stringlib/localeutil.h" |
| 569 | #include "stringlib/undef.h" |
| 570 | |
| 571 | #include "stringlib/ucs1lib.h" |
| 572 | #include "stringlib/fastsearch.h" |
| 573 | #include "stringlib/partition.h" |
| 574 | #include "stringlib/split.h" |
| 575 | #include "stringlib/count.h" |
| 576 | #include "stringlib/find.h" |
| 577 | #include "stringlib/find_max_char.h" |
| 578 | #include "stringlib/localeutil.h" |
| 579 | #include "stringlib/undef.h" |
| 580 | |
| 581 | #include "stringlib/ucs2lib.h" |
| 582 | #include "stringlib/fastsearch.h" |
| 583 | #include "stringlib/partition.h" |
| 584 | #include "stringlib/split.h" |
| 585 | #include "stringlib/count.h" |
| 586 | #include "stringlib/find.h" |
| 587 | #include "stringlib/find_max_char.h" |
| 588 | #include "stringlib/localeutil.h" |
| 589 | #include "stringlib/undef.h" |
| 590 | |
| 591 | #include "stringlib/ucs4lib.h" |
| 592 | #include "stringlib/fastsearch.h" |
| 593 | #include "stringlib/partition.h" |
| 594 | #include "stringlib/split.h" |
| 595 | #include "stringlib/count.h" |
| 596 | #include "stringlib/find.h" |
| 597 | #include "stringlib/find_max_char.h" |
| 598 | #include "stringlib/localeutil.h" |
| 599 | #include "stringlib/undef.h" |
| 600 | |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 601 | #include "stringlib/unicodedefs.h" |
| 602 | #include "stringlib/fastsearch.h" |
| 603 | #include "stringlib/count.h" |
| 604 | #include "stringlib/find.h" |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 605 | #include "stringlib/undef.h" |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 606 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 607 | /* --- Unicode Object ----------------------------------------------------- */ |
| 608 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 609 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 610 | fixup(PyObject *self, Py_UCS4 (*fixfct)(PyObject *s)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 611 | |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 612 | Py_LOCAL_INLINE(Py_ssize_t) findchar(void *s, int kind, |
| 613 | Py_ssize_t size, Py_UCS4 ch, |
| 614 | int direction) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 615 | { |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 616 | int mode = (direction == 1) ? FAST_SEARCH : FAST_RSEARCH; |
| 617 | |
| 618 | switch (kind) { |
| 619 | case PyUnicode_1BYTE_KIND: |
| 620 | { |
| 621 | Py_UCS1 ch1 = (Py_UCS1) ch; |
| 622 | if (ch1 == ch) |
| 623 | return ucs1lib_fastsearch((Py_UCS1 *) s, size, &ch1, 1, 0, mode); |
| 624 | else |
| 625 | return -1; |
| 626 | } |
| 627 | case PyUnicode_2BYTE_KIND: |
| 628 | { |
| 629 | Py_UCS2 ch2 = (Py_UCS2) ch; |
| 630 | if (ch2 == ch) |
| 631 | return ucs2lib_fastsearch((Py_UCS2 *) s, size, &ch2, 1, 0, mode); |
| 632 | else |
| 633 | return -1; |
| 634 | } |
| 635 | case PyUnicode_4BYTE_KIND: |
| 636 | return ucs4lib_fastsearch((Py_UCS4 *) s, size, &ch, 1, 0, mode); |
| 637 | default: |
| 638 | assert(0); |
| 639 | return -1; |
Victor Stinner | 9e7a1bc | 2011-10-13 00:18:12 +0200 | [diff] [blame] | 640 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 641 | } |
| 642 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 643 | static PyObject* |
| 644 | resize_compact(PyObject *unicode, Py_ssize_t length) |
| 645 | { |
| 646 | Py_ssize_t char_size; |
| 647 | Py_ssize_t struct_size; |
| 648 | Py_ssize_t new_size; |
| 649 | int share_wstr; |
Victor Stinner | 84def37 | 2011-12-11 20:04:56 +0100 | [diff] [blame] | 650 | PyObject *new_unicode; |
Victor Stinner | 7989157 | 2012-05-03 13:43:07 +0200 | [diff] [blame] | 651 | assert(unicode_modifiable(unicode)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 652 | assert(PyUnicode_IS_READY(unicode)); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 653 | assert(PyUnicode_IS_COMPACT(unicode)); |
| 654 | |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 655 | char_size = PyUnicode_KIND(unicode); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 656 | if (PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 657 | struct_size = sizeof(PyASCIIObject); |
| 658 | else |
| 659 | struct_size = sizeof(PyCompactUnicodeObject); |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 660 | share_wstr = _PyUnicode_SHARE_WSTR(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 661 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 662 | if (length > ((PY_SSIZE_T_MAX - struct_size) / char_size - 1)) { |
| 663 | PyErr_NoMemory(); |
| 664 | return NULL; |
| 665 | } |
| 666 | new_size = (struct_size + (length + 1) * char_size); |
| 667 | |
Victor Stinner | 84def37 | 2011-12-11 20:04:56 +0100 | [diff] [blame] | 668 | _Py_DEC_REFTOTAL; |
| 669 | _Py_ForgetReference(unicode); |
| 670 | |
| 671 | new_unicode = (PyObject *)PyObject_REALLOC((char *)unicode, new_size); |
| 672 | if (new_unicode == NULL) { |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 673 | _Py_NewReference(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 674 | PyErr_NoMemory(); |
| 675 | return NULL; |
| 676 | } |
Victor Stinner | 84def37 | 2011-12-11 20:04:56 +0100 | [diff] [blame] | 677 | unicode = new_unicode; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 678 | _Py_NewReference(unicode); |
Victor Stinner | 84def37 | 2011-12-11 20:04:56 +0100 | [diff] [blame] | 679 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 680 | _PyUnicode_LENGTH(unicode) = length; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 681 | if (share_wstr) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 682 | _PyUnicode_WSTR(unicode) = PyUnicode_DATA(unicode); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 683 | if (!PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 684 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 685 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 686 | PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), |
| 687 | length, 0); |
Victor Stinner | 7989157 | 2012-05-03 13:43:07 +0200 | [diff] [blame] | 688 | assert(_PyUnicode_CheckConsistency(unicode, 0)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 689 | return unicode; |
| 690 | } |
| 691 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 692 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 693 | resize_inplace(PyObject *unicode, Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 694 | { |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 695 | wchar_t *wstr; |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 696 | Py_ssize_t new_size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 697 | assert(!PyUnicode_IS_COMPACT(unicode)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 698 | assert(Py_REFCNT(unicode) == 1); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 699 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 700 | if (PyUnicode_IS_READY(unicode)) { |
| 701 | Py_ssize_t char_size; |
Victor Stinner | 1c8d0c7 | 2011-10-03 12:11:00 +0200 | [diff] [blame] | 702 | int share_wstr, share_utf8; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 703 | void *data; |
| 704 | |
| 705 | data = _PyUnicode_DATA_ANY(unicode); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 706 | char_size = PyUnicode_KIND(unicode); |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 707 | share_wstr = _PyUnicode_SHARE_WSTR(unicode); |
| 708 | share_utf8 = _PyUnicode_SHARE_UTF8(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 709 | |
| 710 | if (length > (PY_SSIZE_T_MAX / char_size - 1)) { |
| 711 | PyErr_NoMemory(); |
| 712 | return -1; |
| 713 | } |
| 714 | new_size = (length + 1) * char_size; |
| 715 | |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 716 | if (!share_utf8 && _PyUnicode_HAS_UTF8_MEMORY(unicode)) |
| 717 | { |
| 718 | PyObject_DEL(_PyUnicode_UTF8(unicode)); |
| 719 | _PyUnicode_UTF8(unicode) = NULL; |
| 720 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
| 721 | } |
| 722 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 723 | data = (PyObject *)PyObject_REALLOC(data, new_size); |
| 724 | if (data == NULL) { |
| 725 | PyErr_NoMemory(); |
| 726 | return -1; |
| 727 | } |
| 728 | _PyUnicode_DATA_ANY(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 729 | if (share_wstr) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 730 | _PyUnicode_WSTR(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 731 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 732 | } |
| 733 | if (share_utf8) { |
Victor Stinner | 1c8d0c7 | 2011-10-03 12:11:00 +0200 | [diff] [blame] | 734 | _PyUnicode_UTF8(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 735 | _PyUnicode_UTF8_LENGTH(unicode) = length; |
| 736 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 737 | _PyUnicode_LENGTH(unicode) = length; |
| 738 | PyUnicode_WRITE(PyUnicode_KIND(unicode), data, length, 0); |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 739 | if (share_wstr || _PyUnicode_WSTR(unicode) == NULL) { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 740 | assert(_PyUnicode_CheckConsistency(unicode, 0)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 741 | return 0; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 742 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 743 | } |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 744 | assert(_PyUnicode_WSTR(unicode) != NULL); |
| 745 | |
| 746 | /* check for integer overflow */ |
| 747 | if (length > PY_SSIZE_T_MAX / sizeof(wchar_t) - 1) { |
| 748 | PyErr_NoMemory(); |
| 749 | return -1; |
| 750 | } |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 751 | new_size = sizeof(wchar_t) * (length + 1); |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 752 | wstr = _PyUnicode_WSTR(unicode); |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 753 | wstr = PyObject_REALLOC(wstr, new_size); |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 754 | if (!wstr) { |
| 755 | PyErr_NoMemory(); |
| 756 | return -1; |
| 757 | } |
| 758 | _PyUnicode_WSTR(unicode) = wstr; |
| 759 | _PyUnicode_WSTR(unicode)[length] = 0; |
| 760 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 761 | assert(_PyUnicode_CheckConsistency(unicode, 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 762 | return 0; |
| 763 | } |
| 764 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 765 | static PyObject* |
| 766 | resize_copy(PyObject *unicode, Py_ssize_t length) |
| 767 | { |
| 768 | Py_ssize_t copy_length; |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 769 | if (_PyUnicode_KIND(unicode) != PyUnicode_WCHAR_KIND) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 770 | PyObject *copy; |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 771 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 772 | if (PyUnicode_READY(unicode) == -1) |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 773 | return NULL; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 774 | |
| 775 | copy = PyUnicode_New(length, PyUnicode_MAX_CHAR_VALUE(unicode)); |
| 776 | if (copy == NULL) |
| 777 | return NULL; |
| 778 | |
| 779 | copy_length = Py_MIN(length, PyUnicode_GET_LENGTH(unicode)); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 780 | _PyUnicode_FastCopyCharacters(copy, 0, unicode, 0, copy_length); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 781 | return copy; |
Victor Stinner | 8cfcbed | 2011-10-03 23:19:21 +0200 | [diff] [blame] | 782 | } |
| 783 | else { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 784 | PyObject *w; |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 785 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 786 | w = (PyObject*)_PyUnicode_New(length); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 787 | if (w == NULL) |
| 788 | return NULL; |
| 789 | copy_length = _PyUnicode_WSTR_LENGTH(unicode); |
| 790 | copy_length = Py_MIN(copy_length, length); |
| 791 | Py_UNICODE_COPY(_PyUnicode_WSTR(w), _PyUnicode_WSTR(unicode), |
| 792 | copy_length); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 793 | return w; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 794 | } |
| 795 | } |
| 796 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 797 | /* 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] | 798 | Ux0000 terminated; some code (e.g. new_identifier) |
| 799 | relies on that. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 800 | |
| 801 | XXX This allocator could further be enhanced by assuring that the |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 802 | free list never reduces its size below 1. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 803 | |
| 804 | */ |
| 805 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 806 | static PyUnicodeObject * |
| 807 | _PyUnicode_New(Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 808 | { |
| 809 | register PyUnicodeObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 810 | size_t new_size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 811 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 812 | /* Optimization for empty strings */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 813 | if (length == 0 && unicode_empty != NULL) { |
| 814 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 815 | return (PyUnicodeObject*)unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 816 | } |
| 817 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 818 | /* Ensure we won't overflow the size. */ |
| 819 | if (length > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
| 820 | return (PyUnicodeObject *)PyErr_NoMemory(); |
| 821 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 822 | if (length < 0) { |
| 823 | PyErr_SetString(PyExc_SystemError, |
| 824 | "Negative size passed to _PyUnicode_New"); |
| 825 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 826 | } |
| 827 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 828 | unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type); |
| 829 | if (unicode == NULL) |
| 830 | return NULL; |
| 831 | new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
| 832 | _PyUnicode_WSTR(unicode) = (Py_UNICODE*) PyObject_MALLOC(new_size); |
| 833 | if (!_PyUnicode_WSTR(unicode)) { |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 834 | Py_DECREF(unicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 835 | PyErr_NoMemory(); |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 836 | return NULL; |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 837 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 838 | |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 839 | /* Initialize the first element to guard against cases where |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 840 | * the caller fails before initializing str -- unicode_resize() |
| 841 | * reads str[0], and the Keep-Alive optimization can keep memory |
| 842 | * allocated for str alive across a call to unicode_dealloc(unicode). |
| 843 | * We don't want unicode_resize to read uninitialized memory in |
| 844 | * that case. |
| 845 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 846 | _PyUnicode_WSTR(unicode)[0] = 0; |
| 847 | _PyUnicode_WSTR(unicode)[length] = 0; |
| 848 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 849 | _PyUnicode_HASH(unicode) = -1; |
| 850 | _PyUnicode_STATE(unicode).interned = 0; |
| 851 | _PyUnicode_STATE(unicode).kind = 0; |
| 852 | _PyUnicode_STATE(unicode).compact = 0; |
| 853 | _PyUnicode_STATE(unicode).ready = 0; |
| 854 | _PyUnicode_STATE(unicode).ascii = 0; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 855 | _PyUnicode_DATA_ANY(unicode) = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 856 | _PyUnicode_LENGTH(unicode) = 0; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 857 | _PyUnicode_UTF8(unicode) = NULL; |
| 858 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 859 | assert(_PyUnicode_CheckConsistency((PyObject *)unicode, 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 860 | return unicode; |
| 861 | } |
| 862 | |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 863 | static const char* |
| 864 | unicode_kind_name(PyObject *unicode) |
| 865 | { |
Victor Stinner | 42dfd71 | 2011-10-03 14:41:45 +0200 | [diff] [blame] | 866 | /* don't check consistency: unicode_kind_name() is called from |
| 867 | _PyUnicode_Dump() */ |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 868 | if (!PyUnicode_IS_COMPACT(unicode)) |
| 869 | { |
| 870 | if (!PyUnicode_IS_READY(unicode)) |
| 871 | return "wstr"; |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 872 | switch (PyUnicode_KIND(unicode)) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 873 | { |
| 874 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 875 | if (PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 876 | return "legacy ascii"; |
| 877 | else |
| 878 | return "legacy latin1"; |
| 879 | case PyUnicode_2BYTE_KIND: |
| 880 | return "legacy UCS2"; |
| 881 | case PyUnicode_4BYTE_KIND: |
| 882 | return "legacy UCS4"; |
| 883 | default: |
| 884 | return "<legacy invalid kind>"; |
| 885 | } |
| 886 | } |
| 887 | assert(PyUnicode_IS_READY(unicode)); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 888 | switch (PyUnicode_KIND(unicode)) { |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 889 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 890 | if (PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 891 | return "ascii"; |
| 892 | else |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 893 | return "latin1"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 894 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 895 | return "UCS2"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 896 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 897 | return "UCS4"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 898 | default: |
| 899 | return "<invalid compact kind>"; |
| 900 | } |
| 901 | } |
| 902 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 903 | #ifdef Py_DEBUG |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 904 | /* Functions wrapping macros for use in debugger */ |
| 905 | char *_PyUnicode_utf8(void *unicode){ |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 906 | return PyUnicode_UTF8(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 907 | } |
| 908 | |
| 909 | void *_PyUnicode_compact_data(void *unicode) { |
| 910 | return _PyUnicode_COMPACT_DATA(unicode); |
| 911 | } |
| 912 | void *_PyUnicode_data(void *unicode){ |
| 913 | printf("obj %p\n", unicode); |
| 914 | printf("compact %d\n", PyUnicode_IS_COMPACT(unicode)); |
| 915 | printf("compact ascii %d\n", PyUnicode_IS_COMPACT_ASCII(unicode)); |
| 916 | printf("ascii op %p\n", ((void*)((PyASCIIObject*)(unicode) + 1))); |
| 917 | printf("compact op %p\n", ((void*)((PyCompactUnicodeObject*)(unicode) + 1))); |
| 918 | printf("compact data %p\n", _PyUnicode_COMPACT_DATA(unicode)); |
| 919 | return PyUnicode_DATA(unicode); |
| 920 | } |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 921 | |
| 922 | void |
| 923 | _PyUnicode_Dump(PyObject *op) |
| 924 | { |
| 925 | PyASCIIObject *ascii = (PyASCIIObject *)op; |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 926 | PyCompactUnicodeObject *compact = (PyCompactUnicodeObject *)op; |
| 927 | PyUnicodeObject *unicode = (PyUnicodeObject *)op; |
| 928 | void *data; |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 929 | |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 930 | if (ascii->state.compact) |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 931 | { |
| 932 | if (ascii->state.ascii) |
| 933 | data = (ascii + 1); |
| 934 | else |
| 935 | data = (compact + 1); |
| 936 | } |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 937 | else |
| 938 | data = unicode->data.any; |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 939 | printf("%s: len=%zu, ",unicode_kind_name(op), ascii->length); |
| 940 | |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 941 | if (ascii->wstr == data) |
| 942 | printf("shared "); |
| 943 | printf("wstr=%p", ascii->wstr); |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 944 | |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 945 | if (!(ascii->state.ascii == 1 && ascii->state.compact == 1)) { |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 946 | printf(" (%zu), ", compact->wstr_length); |
| 947 | if (!ascii->state.compact && compact->utf8 == unicode->data.any) |
| 948 | printf("shared "); |
| 949 | printf("utf8=%p (%zu)", compact->utf8, compact->utf8_length); |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 950 | } |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 951 | printf(", data=%p\n", data); |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 952 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 953 | #endif |
| 954 | |
| 955 | PyObject * |
| 956 | PyUnicode_New(Py_ssize_t size, Py_UCS4 maxchar) |
| 957 | { |
| 958 | PyObject *obj; |
| 959 | PyCompactUnicodeObject *unicode; |
| 960 | void *data; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 961 | enum PyUnicode_Kind kind; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 962 | int is_sharing, is_ascii; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 963 | Py_ssize_t char_size; |
| 964 | Py_ssize_t struct_size; |
| 965 | |
| 966 | /* Optimization for empty strings */ |
| 967 | if (size == 0 && unicode_empty != NULL) { |
| 968 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 969 | return unicode_empty; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 970 | } |
| 971 | |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 972 | is_ascii = 0; |
| 973 | is_sharing = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 974 | struct_size = sizeof(PyCompactUnicodeObject); |
| 975 | if (maxchar < 128) { |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 976 | kind = PyUnicode_1BYTE_KIND; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 977 | char_size = 1; |
| 978 | is_ascii = 1; |
| 979 | struct_size = sizeof(PyASCIIObject); |
| 980 | } |
| 981 | else if (maxchar < 256) { |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 982 | kind = PyUnicode_1BYTE_KIND; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 983 | char_size = 1; |
| 984 | } |
| 985 | else if (maxchar < 65536) { |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 986 | kind = PyUnicode_2BYTE_KIND; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 987 | char_size = 2; |
| 988 | if (sizeof(wchar_t) == 2) |
| 989 | is_sharing = 1; |
| 990 | } |
| 991 | else { |
Victor Stinner | c9590ad | 2012-03-04 01:34:37 +0100 | [diff] [blame] | 992 | if (maxchar > MAX_UNICODE) { |
| 993 | PyErr_SetString(PyExc_SystemError, |
| 994 | "invalid maximum character passed to PyUnicode_New"); |
| 995 | return NULL; |
| 996 | } |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 997 | kind = PyUnicode_4BYTE_KIND; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 998 | char_size = 4; |
| 999 | if (sizeof(wchar_t) == 4) |
| 1000 | is_sharing = 1; |
| 1001 | } |
| 1002 | |
| 1003 | /* Ensure we won't overflow the size. */ |
| 1004 | if (size < 0) { |
| 1005 | PyErr_SetString(PyExc_SystemError, |
| 1006 | "Negative size passed to PyUnicode_New"); |
| 1007 | return NULL; |
| 1008 | } |
| 1009 | if (size > ((PY_SSIZE_T_MAX - struct_size) / char_size - 1)) |
| 1010 | return PyErr_NoMemory(); |
| 1011 | |
| 1012 | /* Duplicated allocation code from _PyObject_New() instead of a call to |
| 1013 | * PyObject_New() so we are able to allocate space for the object and |
| 1014 | * it's data buffer. |
| 1015 | */ |
| 1016 | obj = (PyObject *) PyObject_MALLOC(struct_size + (size + 1) * char_size); |
| 1017 | if (obj == NULL) |
| 1018 | return PyErr_NoMemory(); |
| 1019 | obj = PyObject_INIT(obj, &PyUnicode_Type); |
| 1020 | if (obj == NULL) |
| 1021 | return NULL; |
| 1022 | |
| 1023 | unicode = (PyCompactUnicodeObject *)obj; |
| 1024 | if (is_ascii) |
| 1025 | data = ((PyASCIIObject*)obj) + 1; |
| 1026 | else |
| 1027 | data = unicode + 1; |
| 1028 | _PyUnicode_LENGTH(unicode) = size; |
| 1029 | _PyUnicode_HASH(unicode) = -1; |
| 1030 | _PyUnicode_STATE(unicode).interned = 0; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1031 | _PyUnicode_STATE(unicode).kind = kind; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1032 | _PyUnicode_STATE(unicode).compact = 1; |
| 1033 | _PyUnicode_STATE(unicode).ready = 1; |
| 1034 | _PyUnicode_STATE(unicode).ascii = is_ascii; |
| 1035 | if (is_ascii) { |
| 1036 | ((char*)data)[size] = 0; |
| 1037 | _PyUnicode_WSTR(unicode) = NULL; |
| 1038 | } |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1039 | else if (kind == PyUnicode_1BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1040 | ((char*)data)[size] = 0; |
| 1041 | _PyUnicode_WSTR(unicode) = NULL; |
| 1042 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1043 | unicode->utf8 = NULL; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 1044 | unicode->utf8_length = 0; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1045 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1046 | else { |
| 1047 | unicode->utf8 = NULL; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 1048 | unicode->utf8_length = 0; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1049 | if (kind == PyUnicode_2BYTE_KIND) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1050 | ((Py_UCS2*)data)[size] = 0; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1051 | else /* kind == PyUnicode_4BYTE_KIND */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1052 | ((Py_UCS4*)data)[size] = 0; |
| 1053 | if (is_sharing) { |
| 1054 | _PyUnicode_WSTR_LENGTH(unicode) = size; |
| 1055 | _PyUnicode_WSTR(unicode) = (wchar_t *)data; |
| 1056 | } |
| 1057 | else { |
| 1058 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1059 | _PyUnicode_WSTR(unicode) = NULL; |
| 1060 | } |
| 1061 | } |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1062 | #ifdef Py_DEBUG |
| 1063 | /* Fill the data with invalid characters to detect bugs earlier. |
| 1064 | _PyUnicode_CheckConsistency(str, 1) detects invalid characters, |
| 1065 | at least for ASCII and UCS-4 strings. U+00FF is invalid in ASCII |
| 1066 | and U+FFFFFFFF is an invalid character in Unicode 6.0. */ |
| 1067 | memset(data, 0xff, size * kind); |
| 1068 | #endif |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 1069 | assert(_PyUnicode_CheckConsistency((PyObject*)unicode, 0)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1070 | return obj; |
| 1071 | } |
| 1072 | |
| 1073 | #if SIZEOF_WCHAR_T == 2 |
| 1074 | /* Helper function to convert a 16-bits wchar_t representation to UCS4, this |
| 1075 | will decode surrogate pairs, the other conversions are implemented as macros |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 1076 | for efficiency. |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1077 | |
| 1078 | This function assumes that unicode can hold one more code point than wstr |
| 1079 | characters for a terminating null character. */ |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1080 | static void |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1081 | unicode_convert_wchar_to_ucs4(const wchar_t *begin, const wchar_t *end, |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1082 | PyObject *unicode) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1083 | { |
| 1084 | const wchar_t *iter; |
| 1085 | Py_UCS4 *ucs4_out; |
| 1086 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1087 | assert(unicode != NULL); |
| 1088 | assert(_PyUnicode_CHECK(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1089 | assert(_PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND); |
| 1090 | ucs4_out = PyUnicode_4BYTE_DATA(unicode); |
| 1091 | |
| 1092 | for (iter = begin; iter < end; ) { |
| 1093 | assert(ucs4_out < (PyUnicode_4BYTE_DATA(unicode) + |
| 1094 | _PyUnicode_GET_LENGTH(unicode))); |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 1095 | if (Py_UNICODE_IS_HIGH_SURROGATE(iter[0]) |
| 1096 | && (iter+1) < end |
| 1097 | && Py_UNICODE_IS_LOW_SURROGATE(iter[1])) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1098 | { |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 1099 | *ucs4_out++ = Py_UNICODE_JOIN_SURROGATES(iter[0], iter[1]); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1100 | iter += 2; |
| 1101 | } |
| 1102 | else { |
| 1103 | *ucs4_out++ = *iter; |
| 1104 | iter++; |
| 1105 | } |
| 1106 | } |
| 1107 | assert(ucs4_out == (PyUnicode_4BYTE_DATA(unicode) + |
| 1108 | _PyUnicode_GET_LENGTH(unicode))); |
| 1109 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1110 | } |
| 1111 | #endif |
| 1112 | |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1113 | static int |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1114 | unicode_check_modifiable(PyObject *unicode) |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1115 | { |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1116 | if (!unicode_modifiable(unicode)) { |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 1117 | PyErr_SetString(PyExc_SystemError, |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1118 | "Cannot modify a string currently used"); |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1119 | return -1; |
| 1120 | } |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1121 | return 0; |
| 1122 | } |
| 1123 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1124 | static int |
| 1125 | _copy_characters(PyObject *to, Py_ssize_t to_start, |
| 1126 | PyObject *from, Py_ssize_t from_start, |
| 1127 | Py_ssize_t how_many, int check_maxchar) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1128 | { |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1129 | unsigned int from_kind, to_kind; |
| 1130 | void *from_data, *to_data; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1131 | |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 1132 | assert(0 <= how_many); |
| 1133 | assert(0 <= from_start); |
| 1134 | assert(0 <= to_start); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1135 | assert(PyUnicode_Check(from)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1136 | assert(PyUnicode_IS_READY(from)); |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 1137 | assert(from_start + how_many <= PyUnicode_GET_LENGTH(from)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1138 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1139 | assert(PyUnicode_Check(to)); |
| 1140 | assert(PyUnicode_IS_READY(to)); |
| 1141 | assert(to_start + how_many <= PyUnicode_GET_LENGTH(to)); |
| 1142 | |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1143 | if (how_many == 0) |
| 1144 | return 0; |
| 1145 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1146 | from_kind = PyUnicode_KIND(from); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1147 | from_data = PyUnicode_DATA(from); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1148 | to_kind = PyUnicode_KIND(to); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1149 | to_data = PyUnicode_DATA(to); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1150 | |
Victor Stinner | f185226 | 2012-06-16 16:38:26 +0200 | [diff] [blame] | 1151 | #ifdef Py_DEBUG |
| 1152 | if (!check_maxchar |
| 1153 | && PyUnicode_MAX_CHAR_VALUE(from) > PyUnicode_MAX_CHAR_VALUE(to)) |
| 1154 | { |
| 1155 | const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to); |
| 1156 | Py_UCS4 ch; |
| 1157 | Py_ssize_t i; |
| 1158 | for (i=0; i < how_many; i++) { |
| 1159 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
| 1160 | assert(ch <= to_maxchar); |
| 1161 | } |
| 1162 | } |
| 1163 | #endif |
| 1164 | |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1165 | if (from_kind == to_kind) { |
Victor Stinner | f185226 | 2012-06-16 16:38:26 +0200 | [diff] [blame] | 1166 | if (check_maxchar |
| 1167 | && !PyUnicode_IS_ASCII(from) && PyUnicode_IS_ASCII(to)) |
| 1168 | { |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1169 | /* Writing Latin-1 characters into an ASCII string requires to |
| 1170 | check that all written characters are pure ASCII */ |
Victor Stinner | f185226 | 2012-06-16 16:38:26 +0200 | [diff] [blame] | 1171 | Py_UCS4 max_char; |
| 1172 | max_char = ucs1lib_find_max_char(from_data, |
| 1173 | (Py_UCS1*)from_data + how_many); |
| 1174 | if (max_char >= 128) |
| 1175 | return -1; |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1176 | } |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 1177 | Py_MEMCPY((char*)to_data + to_kind * to_start, |
| 1178 | (char*)from_data + from_kind * from_start, |
| 1179 | to_kind * how_many); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1180 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1181 | else if (from_kind == PyUnicode_1BYTE_KIND |
| 1182 | && to_kind == PyUnicode_2BYTE_KIND) |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1183 | { |
| 1184 | _PyUnicode_CONVERT_BYTES( |
| 1185 | Py_UCS1, Py_UCS2, |
| 1186 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 1187 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 1188 | PyUnicode_2BYTE_DATA(to) + to_start |
| 1189 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1190 | } |
Victor Stinner | 157f83f | 2011-09-28 21:41:31 +0200 | [diff] [blame] | 1191 | else if (from_kind == PyUnicode_1BYTE_KIND |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1192 | && to_kind == PyUnicode_4BYTE_KIND) |
| 1193 | { |
| 1194 | _PyUnicode_CONVERT_BYTES( |
| 1195 | Py_UCS1, Py_UCS4, |
| 1196 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 1197 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 1198 | PyUnicode_4BYTE_DATA(to) + to_start |
| 1199 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1200 | } |
| 1201 | else if (from_kind == PyUnicode_2BYTE_KIND |
| 1202 | && to_kind == PyUnicode_4BYTE_KIND) |
| 1203 | { |
| 1204 | _PyUnicode_CONVERT_BYTES( |
| 1205 | Py_UCS2, Py_UCS4, |
| 1206 | PyUnicode_2BYTE_DATA(from) + from_start, |
| 1207 | PyUnicode_2BYTE_DATA(from) + from_start + how_many, |
| 1208 | PyUnicode_4BYTE_DATA(to) + to_start |
| 1209 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1210 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1211 | else { |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1212 | assert (PyUnicode_MAX_CHAR_VALUE(from) > PyUnicode_MAX_CHAR_VALUE(to)); |
| 1213 | |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1214 | if (!check_maxchar) { |
| 1215 | if (from_kind == PyUnicode_2BYTE_KIND |
| 1216 | && to_kind == PyUnicode_1BYTE_KIND) |
| 1217 | { |
| 1218 | _PyUnicode_CONVERT_BYTES( |
| 1219 | Py_UCS2, Py_UCS1, |
| 1220 | PyUnicode_2BYTE_DATA(from) + from_start, |
| 1221 | PyUnicode_2BYTE_DATA(from) + from_start + how_many, |
| 1222 | PyUnicode_1BYTE_DATA(to) + to_start |
| 1223 | ); |
| 1224 | } |
| 1225 | else if (from_kind == PyUnicode_4BYTE_KIND |
| 1226 | && to_kind == PyUnicode_1BYTE_KIND) |
| 1227 | { |
| 1228 | _PyUnicode_CONVERT_BYTES( |
| 1229 | Py_UCS4, Py_UCS1, |
| 1230 | PyUnicode_4BYTE_DATA(from) + from_start, |
| 1231 | PyUnicode_4BYTE_DATA(from) + from_start + how_many, |
| 1232 | PyUnicode_1BYTE_DATA(to) + to_start |
| 1233 | ); |
| 1234 | } |
| 1235 | else if (from_kind == PyUnicode_4BYTE_KIND |
| 1236 | && to_kind == PyUnicode_2BYTE_KIND) |
| 1237 | { |
| 1238 | _PyUnicode_CONVERT_BYTES( |
| 1239 | Py_UCS4, Py_UCS2, |
| 1240 | PyUnicode_4BYTE_DATA(from) + from_start, |
| 1241 | PyUnicode_4BYTE_DATA(from) + from_start + how_many, |
| 1242 | PyUnicode_2BYTE_DATA(to) + to_start |
| 1243 | ); |
| 1244 | } |
| 1245 | else { |
| 1246 | assert(0); |
| 1247 | return -1; |
| 1248 | } |
| 1249 | } |
Victor Stinner | f185226 | 2012-06-16 16:38:26 +0200 | [diff] [blame] | 1250 | else { |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1251 | const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1252 | Py_UCS4 ch; |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1253 | Py_ssize_t i; |
| 1254 | |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1255 | for (i=0; i < how_many; i++) { |
| 1256 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1257 | if (ch > to_maxchar) |
| 1258 | return -1; |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1259 | PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); |
| 1260 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1261 | } |
| 1262 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1263 | return 0; |
| 1264 | } |
| 1265 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1266 | void |
| 1267 | _PyUnicode_FastCopyCharacters( |
| 1268 | PyObject *to, Py_ssize_t to_start, |
| 1269 | PyObject *from, Py_ssize_t from_start, Py_ssize_t how_many) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1270 | { |
| 1271 | (void)_copy_characters(to, to_start, from, from_start, how_many, 0); |
| 1272 | } |
| 1273 | |
| 1274 | Py_ssize_t |
| 1275 | PyUnicode_CopyCharacters(PyObject *to, Py_ssize_t to_start, |
| 1276 | PyObject *from, Py_ssize_t from_start, |
| 1277 | Py_ssize_t how_many) |
| 1278 | { |
| 1279 | int err; |
| 1280 | |
| 1281 | if (!PyUnicode_Check(from) || !PyUnicode_Check(to)) { |
| 1282 | PyErr_BadInternalCall(); |
| 1283 | return -1; |
| 1284 | } |
| 1285 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 1286 | if (PyUnicode_READY(from) == -1) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1287 | return -1; |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 1288 | if (PyUnicode_READY(to) == -1) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1289 | return -1; |
| 1290 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1291 | if (from_start < 0) { |
| 1292 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 1293 | return -1; |
| 1294 | } |
| 1295 | if (to_start < 0) { |
| 1296 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 1297 | return -1; |
| 1298 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1299 | how_many = Py_MIN(PyUnicode_GET_LENGTH(from), how_many); |
| 1300 | if (to_start + how_many > PyUnicode_GET_LENGTH(to)) { |
| 1301 | PyErr_Format(PyExc_SystemError, |
| 1302 | "Cannot write %zi characters at %zi " |
| 1303 | "in a string of %zi characters", |
| 1304 | how_many, to_start, PyUnicode_GET_LENGTH(to)); |
| 1305 | return -1; |
| 1306 | } |
| 1307 | |
| 1308 | if (how_many == 0) |
| 1309 | return 0; |
| 1310 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1311 | if (unicode_check_modifiable(to)) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1312 | return -1; |
| 1313 | |
| 1314 | err = _copy_characters(to, to_start, from, from_start, how_many, 1); |
| 1315 | if (err) { |
| 1316 | PyErr_Format(PyExc_SystemError, |
| 1317 | "Cannot copy %s characters " |
| 1318 | "into a string of %s characters", |
| 1319 | unicode_kind_name(from), |
| 1320 | unicode_kind_name(to)); |
| 1321 | return -1; |
| 1322 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1323 | return how_many; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1324 | } |
| 1325 | |
Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 1326 | /* Find the maximum code point and count the number of surrogate pairs so a |
| 1327 | correct string length can be computed before converting a string to UCS4. |
| 1328 | This function counts single surrogates as a character and not as a pair. |
| 1329 | |
| 1330 | Return 0 on success, or -1 on error. */ |
| 1331 | static int |
| 1332 | find_maxchar_surrogates(const wchar_t *begin, const wchar_t *end, |
| 1333 | Py_UCS4 *maxchar, Py_ssize_t *num_surrogates) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1334 | { |
| 1335 | const wchar_t *iter; |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1336 | Py_UCS4 ch; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1337 | |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1338 | assert(num_surrogates != NULL && maxchar != NULL); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1339 | *num_surrogates = 0; |
| 1340 | *maxchar = 0; |
| 1341 | |
| 1342 | for (iter = begin; iter < end; ) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1343 | #if SIZEOF_WCHAR_T == 2 |
Victor Stinner | ca4f207 | 2011-11-22 03:38:40 +0100 | [diff] [blame] | 1344 | if (Py_UNICODE_IS_HIGH_SURROGATE(iter[0]) |
| 1345 | && (iter+1) < end |
| 1346 | && Py_UNICODE_IS_LOW_SURROGATE(iter[1])) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1347 | { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1348 | ch = Py_UNICODE_JOIN_SURROGATES(iter[0], iter[1]); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1349 | ++(*num_surrogates); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1350 | iter += 2; |
| 1351 | } |
| 1352 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1353 | #endif |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1354 | { |
| 1355 | ch = *iter; |
| 1356 | iter++; |
| 1357 | } |
| 1358 | if (ch > *maxchar) { |
| 1359 | *maxchar = ch; |
| 1360 | if (*maxchar > MAX_UNICODE) { |
| 1361 | PyErr_Format(PyExc_ValueError, |
| 1362 | "character U+%x is not in range [U+0000; U+10ffff]", |
| 1363 | ch); |
| 1364 | return -1; |
| 1365 | } |
| 1366 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1367 | } |
| 1368 | return 0; |
| 1369 | } |
| 1370 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1371 | int |
| 1372 | _PyUnicode_Ready(PyObject *unicode) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1373 | { |
| 1374 | wchar_t *end; |
| 1375 | Py_UCS4 maxchar = 0; |
| 1376 | Py_ssize_t num_surrogates; |
| 1377 | #if SIZEOF_WCHAR_T == 2 |
| 1378 | Py_ssize_t length_wo_surrogates; |
| 1379 | #endif |
| 1380 | |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 1381 | /* _PyUnicode_Ready() is only intended for old-style API usage where |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1382 | strings were created using _PyObject_New() and where no canonical |
| 1383 | representation (the str field) has been set yet aka strings |
| 1384 | which are not yet ready. */ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1385 | assert(_PyUnicode_CHECK(unicode)); |
| 1386 | assert(_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1387 | assert(_PyUnicode_WSTR(unicode) != NULL); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1388 | assert(_PyUnicode_DATA_ANY(unicode) == NULL); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1389 | assert(_PyUnicode_UTF8(unicode) == NULL); |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1390 | /* Actually, it should neither be interned nor be anything else: */ |
| 1391 | assert(_PyUnicode_STATE(unicode).interned == SSTATE_NOT_INTERNED); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1392 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1393 | end = _PyUnicode_WSTR(unicode) + _PyUnicode_WSTR_LENGTH(unicode); |
Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 1394 | if (find_maxchar_surrogates(_PyUnicode_WSTR(unicode), end, |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1395 | &maxchar, &num_surrogates) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1396 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1397 | |
| 1398 | if (maxchar < 256) { |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1399 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(_PyUnicode_WSTR_LENGTH(unicode) + 1); |
| 1400 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1401 | PyErr_NoMemory(); |
| 1402 | return -1; |
| 1403 | } |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1404 | _PyUnicode_CONVERT_BYTES(wchar_t, unsigned char, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1405 | _PyUnicode_WSTR(unicode), end, |
| 1406 | PyUnicode_1BYTE_DATA(unicode)); |
| 1407 | PyUnicode_1BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1408 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1409 | _PyUnicode_STATE(unicode).kind = PyUnicode_1BYTE_KIND; |
| 1410 | if (maxchar < 128) { |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1411 | _PyUnicode_STATE(unicode).ascii = 1; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1412 | _PyUnicode_UTF8(unicode) = _PyUnicode_DATA_ANY(unicode); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1413 | _PyUnicode_UTF8_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1414 | } |
| 1415 | else { |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1416 | _PyUnicode_STATE(unicode).ascii = 0; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1417 | _PyUnicode_UTF8(unicode) = NULL; |
| 1418 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1419 | } |
| 1420 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1421 | _PyUnicode_WSTR(unicode) = NULL; |
| 1422 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1423 | } |
| 1424 | /* In this case we might have to convert down from 4-byte native |
| 1425 | wchar_t to 2-byte unicode. */ |
| 1426 | else if (maxchar < 65536) { |
| 1427 | assert(num_surrogates == 0 && |
| 1428 | "FindMaxCharAndNumSurrogatePairs() messed up"); |
| 1429 | |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1430 | #if SIZEOF_WCHAR_T == 2 |
| 1431 | /* We can share representations and are done. */ |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1432 | _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1433 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1434 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1435 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1436 | _PyUnicode_UTF8(unicode) = NULL; |
| 1437 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1438 | #else |
| 1439 | /* sizeof(wchar_t) == 4 */ |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1440 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC( |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1441 | 2 * (_PyUnicode_WSTR_LENGTH(unicode) + 1)); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1442 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1443 | PyErr_NoMemory(); |
| 1444 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1445 | } |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1446 | _PyUnicode_CONVERT_BYTES(wchar_t, Py_UCS2, |
| 1447 | _PyUnicode_WSTR(unicode), end, |
| 1448 | PyUnicode_2BYTE_DATA(unicode)); |
| 1449 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1450 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1451 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1452 | _PyUnicode_UTF8(unicode) = NULL; |
| 1453 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1454 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1455 | _PyUnicode_WSTR(unicode) = NULL; |
| 1456 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1457 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1458 | } |
| 1459 | /* maxchar exeeds 16 bit, wee need 4 bytes for unicode characters */ |
| 1460 | else { |
| 1461 | #if SIZEOF_WCHAR_T == 2 |
| 1462 | /* in case the native representation is 2-bytes, we need to allocate a |
| 1463 | new normalized 4-byte version. */ |
| 1464 | length_wo_surrogates = _PyUnicode_WSTR_LENGTH(unicode) - num_surrogates; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1465 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(4 * (length_wo_surrogates + 1)); |
| 1466 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1467 | PyErr_NoMemory(); |
| 1468 | return -1; |
| 1469 | } |
| 1470 | _PyUnicode_LENGTH(unicode) = length_wo_surrogates; |
| 1471 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1472 | _PyUnicode_UTF8(unicode) = NULL; |
| 1473 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 126c559 | 2011-10-03 04:17:10 +0200 | [diff] [blame] | 1474 | /* unicode_convert_wchar_to_ucs4() requires a ready string */ |
| 1475 | _PyUnicode_STATE(unicode).ready = 1; |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1476 | unicode_convert_wchar_to_ucs4(_PyUnicode_WSTR(unicode), end, unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1477 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1478 | _PyUnicode_WSTR(unicode) = NULL; |
| 1479 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1480 | #else |
| 1481 | assert(num_surrogates == 0); |
| 1482 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1483 | _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1484 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1485 | _PyUnicode_UTF8(unicode) = NULL; |
| 1486 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1487 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; |
| 1488 | #endif |
| 1489 | PyUnicode_4BYTE_DATA(unicode)[_PyUnicode_LENGTH(unicode)] = '\0'; |
| 1490 | } |
| 1491 | _PyUnicode_STATE(unicode).ready = 1; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1492 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1493 | return 0; |
| 1494 | } |
| 1495 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1496 | static void |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1497 | unicode_dealloc(register PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1498 | { |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1499 | switch (PyUnicode_CHECK_INTERNED(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1500 | case SSTATE_NOT_INTERNED: |
| 1501 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1502 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1503 | case SSTATE_INTERNED_MORTAL: |
| 1504 | /* revive dead object temporarily for DelItem */ |
| 1505 | Py_REFCNT(unicode) = 3; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 1506 | if (PyDict_DelItem(interned, unicode) != 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1507 | Py_FatalError( |
| 1508 | "deletion of interned string failed"); |
| 1509 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1510 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1511 | case SSTATE_INTERNED_IMMORTAL: |
| 1512 | Py_FatalError("Immortal interned string died."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1513 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1514 | default: |
| 1515 | Py_FatalError("Inconsistent interned string state."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1516 | } |
| 1517 | |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 1518 | if (_PyUnicode_HAS_WSTR_MEMORY(unicode)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1519 | PyObject_DEL(_PyUnicode_WSTR(unicode)); |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 1520 | if (_PyUnicode_HAS_UTF8_MEMORY(unicode)) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1521 | PyObject_DEL(_PyUnicode_UTF8(unicode)); |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1522 | if (!PyUnicode_IS_COMPACT(unicode) && _PyUnicode_DATA_ANY(unicode)) |
| 1523 | PyObject_DEL(_PyUnicode_DATA_ANY(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1524 | |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1525 | Py_TYPE(unicode)->tp_free(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1526 | } |
| 1527 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1528 | #ifdef Py_DEBUG |
| 1529 | static int |
| 1530 | unicode_is_singleton(PyObject *unicode) |
| 1531 | { |
| 1532 | PyASCIIObject *ascii = (PyASCIIObject *)unicode; |
| 1533 | if (unicode == unicode_empty) |
| 1534 | return 1; |
| 1535 | if (ascii->state.kind != PyUnicode_WCHAR_KIND && ascii->length == 1) |
| 1536 | { |
| 1537 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, 0); |
| 1538 | if (ch < 256 && unicode_latin1[ch] == unicode) |
| 1539 | return 1; |
| 1540 | } |
| 1541 | return 0; |
| 1542 | } |
| 1543 | #endif |
| 1544 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1545 | static int |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1546 | unicode_modifiable(PyObject *unicode) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1547 | { |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1548 | assert(_PyUnicode_CHECK(unicode)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1549 | if (Py_REFCNT(unicode) != 1) |
| 1550 | return 0; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1551 | if (_PyUnicode_HASH(unicode) != -1) |
| 1552 | return 0; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1553 | if (PyUnicode_CHECK_INTERNED(unicode)) |
| 1554 | return 0; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1555 | if (!PyUnicode_CheckExact(unicode)) |
| 1556 | return 0; |
Victor Stinner | 77bb47b | 2011-10-03 20:06:05 +0200 | [diff] [blame] | 1557 | #ifdef Py_DEBUG |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1558 | /* singleton refcount is greater than 1 */ |
| 1559 | assert(!unicode_is_singleton(unicode)); |
Victor Stinner | 77bb47b | 2011-10-03 20:06:05 +0200 | [diff] [blame] | 1560 | #endif |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1561 | return 1; |
| 1562 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1563 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1564 | static int |
| 1565 | unicode_resize(PyObject **p_unicode, Py_ssize_t length) |
| 1566 | { |
| 1567 | PyObject *unicode; |
| 1568 | Py_ssize_t old_length; |
| 1569 | |
| 1570 | assert(p_unicode != NULL); |
| 1571 | unicode = *p_unicode; |
| 1572 | |
| 1573 | assert(unicode != NULL); |
| 1574 | assert(PyUnicode_Check(unicode)); |
| 1575 | assert(0 <= length); |
| 1576 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1577 | if (_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1578 | old_length = PyUnicode_WSTR_LENGTH(unicode); |
| 1579 | else |
| 1580 | old_length = PyUnicode_GET_LENGTH(unicode); |
| 1581 | if (old_length == length) |
| 1582 | return 0; |
| 1583 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1584 | if (length == 0) { |
| 1585 | Py_DECREF(*p_unicode); |
| 1586 | *p_unicode = unicode_empty; |
| 1587 | Py_INCREF(*p_unicode); |
| 1588 | return 0; |
| 1589 | } |
| 1590 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1591 | if (!unicode_modifiable(unicode)) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1592 | PyObject *copy = resize_copy(unicode, length); |
| 1593 | if (copy == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1594 | return -1; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1595 | Py_DECREF(*p_unicode); |
| 1596 | *p_unicode = copy; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1597 | return 0; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1598 | } |
| 1599 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1600 | if (PyUnicode_IS_COMPACT(unicode)) { |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1601 | PyObject *new_unicode = resize_compact(unicode, length); |
| 1602 | if (new_unicode == NULL) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1603 | return -1; |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1604 | *p_unicode = new_unicode; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1605 | return 0; |
Benjamin Peterson | 4bfce8f | 2011-10-03 19:35:07 -0400 | [diff] [blame] | 1606 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1607 | return resize_inplace(unicode, length); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1608 | } |
| 1609 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1610 | int |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1611 | PyUnicode_Resize(PyObject **p_unicode, Py_ssize_t length) |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1612 | { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1613 | PyObject *unicode; |
| 1614 | if (p_unicode == NULL) { |
| 1615 | PyErr_BadInternalCall(); |
| 1616 | return -1; |
| 1617 | } |
| 1618 | unicode = *p_unicode; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1619 | if (unicode == NULL || !PyUnicode_Check(unicode) || length < 0) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1620 | { |
| 1621 | PyErr_BadInternalCall(); |
| 1622 | return -1; |
| 1623 | } |
| 1624 | return unicode_resize(p_unicode, length); |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1625 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1626 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1627 | static int |
Victor Stinner | 1b487b4 | 2012-05-03 12:29:04 +0200 | [diff] [blame] | 1628 | unicode_widen(PyObject **p_unicode, Py_ssize_t length, |
| 1629 | unsigned int maxchar) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1630 | { |
| 1631 | PyObject *result; |
| 1632 | assert(PyUnicode_IS_READY(*p_unicode)); |
Victor Stinner | 1b487b4 | 2012-05-03 12:29:04 +0200 | [diff] [blame] | 1633 | assert(length <= PyUnicode_GET_LENGTH(*p_unicode)); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1634 | if (maxchar <= PyUnicode_MAX_CHAR_VALUE(*p_unicode)) |
| 1635 | return 0; |
| 1636 | result = PyUnicode_New(PyUnicode_GET_LENGTH(*p_unicode), |
| 1637 | maxchar); |
| 1638 | if (result == NULL) |
| 1639 | return -1; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1640 | _PyUnicode_FastCopyCharacters(result, 0, *p_unicode, 0, length); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1641 | Py_DECREF(*p_unicode); |
| 1642 | *p_unicode = result; |
| 1643 | return 0; |
| 1644 | } |
| 1645 | |
| 1646 | static int |
| 1647 | unicode_putchar(PyObject **p_unicode, Py_ssize_t *pos, |
| 1648 | Py_UCS4 ch) |
| 1649 | { |
Victor Stinner | 15e9ed2 | 2012-02-22 13:36:20 +0100 | [diff] [blame] | 1650 | assert(ch <= MAX_UNICODE); |
Victor Stinner | 1b487b4 | 2012-05-03 12:29:04 +0200 | [diff] [blame] | 1651 | if (unicode_widen(p_unicode, *pos, ch) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1652 | return -1; |
| 1653 | PyUnicode_WRITE(PyUnicode_KIND(*p_unicode), |
| 1654 | PyUnicode_DATA(*p_unicode), |
| 1655 | (*pos)++, ch); |
| 1656 | return 0; |
| 1657 | } |
| 1658 | |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1659 | /* Copy a ASCII or latin1 char* string into a Python Unicode string. |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1660 | |
Victor Stinner | b429d3b | 2012-02-22 21:22:20 +0100 | [diff] [blame] | 1661 | WARNING: The function doesn't copy the terminating null character and |
| 1662 | doesn't check the maximum character (may write a latin1 character in an |
| 1663 | ASCII string). */ |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1664 | static void |
| 1665 | unicode_write_cstr(PyObject *unicode, Py_ssize_t index, |
| 1666 | const char *str, Py_ssize_t len) |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1667 | { |
| 1668 | enum PyUnicode_Kind kind = PyUnicode_KIND(unicode); |
| 1669 | void *data = PyUnicode_DATA(unicode); |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1670 | const char *end = str + len; |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1671 | |
| 1672 | switch (kind) { |
| 1673 | case PyUnicode_1BYTE_KIND: { |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1674 | assert(index + len <= PyUnicode_GET_LENGTH(unicode)); |
Antoine Pitrou | ba6bafc | 2012-02-22 16:41:50 +0100 | [diff] [blame] | 1675 | memcpy((char *) data + index, str, len); |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1676 | break; |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1677 | } |
| 1678 | case PyUnicode_2BYTE_KIND: { |
| 1679 | Py_UCS2 *start = (Py_UCS2 *)data + index; |
| 1680 | Py_UCS2 *ucs2 = start; |
| 1681 | assert(index <= PyUnicode_GET_LENGTH(unicode)); |
| 1682 | |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1683 | for (; str < end; ++ucs2, ++str) |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1684 | *ucs2 = (Py_UCS2)*str; |
| 1685 | |
| 1686 | assert((ucs2 - start) <= PyUnicode_GET_LENGTH(unicode)); |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1687 | break; |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1688 | } |
| 1689 | default: { |
| 1690 | Py_UCS4 *start = (Py_UCS4 *)data + index; |
| 1691 | Py_UCS4 *ucs4 = start; |
| 1692 | assert(kind == PyUnicode_4BYTE_KIND); |
| 1693 | assert(index <= PyUnicode_GET_LENGTH(unicode)); |
| 1694 | |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1695 | for (; str < end; ++ucs4, ++str) |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1696 | *ucs4 = (Py_UCS4)*str; |
| 1697 | |
| 1698 | assert((ucs4 - start) <= PyUnicode_GET_LENGTH(unicode)); |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1699 | } |
| 1700 | } |
| 1701 | } |
| 1702 | |
| 1703 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1704 | static PyObject* |
| 1705 | get_latin1_char(unsigned char ch) |
| 1706 | { |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1707 | PyObject *unicode = unicode_latin1[ch]; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1708 | if (!unicode) { |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1709 | unicode = PyUnicode_New(1, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1710 | if (!unicode) |
| 1711 | return NULL; |
| 1712 | PyUnicode_1BYTE_DATA(unicode)[0] = ch; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1713 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1714 | unicode_latin1[ch] = unicode; |
| 1715 | } |
| 1716 | Py_INCREF(unicode); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1717 | return unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1718 | } |
| 1719 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1720 | PyObject * |
| 1721 | PyUnicode_FromUnicode(const Py_UNICODE *u, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1722 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1723 | PyObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1724 | Py_UCS4 maxchar = 0; |
| 1725 | Py_ssize_t num_surrogates; |
| 1726 | |
| 1727 | if (u == NULL) |
| 1728 | return (PyObject*)_PyUnicode_New(size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1729 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1730 | /* If the Unicode data is known at construction time, we can apply |
| 1731 | some optimizations which share commonly used objects. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1732 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1733 | /* Optimization for empty strings */ |
| 1734 | if (size == 0 && unicode_empty != NULL) { |
| 1735 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1736 | return unicode_empty; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1737 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1738 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1739 | /* Single character Unicode objects in the Latin-1 range are |
| 1740 | shared when using this constructor */ |
| 1741 | if (size == 1 && *u < 256) |
| 1742 | return get_latin1_char((unsigned char)*u); |
| 1743 | |
| 1744 | /* If not empty and not single character, copy the Unicode data |
| 1745 | into the new object */ |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1746 | if (find_maxchar_surrogates(u, u + size, |
| 1747 | &maxchar, &num_surrogates) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1748 | return NULL; |
| 1749 | |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1750 | unicode = PyUnicode_New(size - num_surrogates, maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1751 | if (!unicode) |
| 1752 | return NULL; |
| 1753 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1754 | switch (PyUnicode_KIND(unicode)) { |
| 1755 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1756 | _PyUnicode_CONVERT_BYTES(Py_UNICODE, unsigned char, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1757 | u, u + size, PyUnicode_1BYTE_DATA(unicode)); |
| 1758 | break; |
| 1759 | case PyUnicode_2BYTE_KIND: |
| 1760 | #if Py_UNICODE_SIZE == 2 |
| 1761 | Py_MEMCPY(PyUnicode_2BYTE_DATA(unicode), u, size * 2); |
| 1762 | #else |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1763 | _PyUnicode_CONVERT_BYTES(Py_UNICODE, Py_UCS2, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1764 | u, u + size, PyUnicode_2BYTE_DATA(unicode)); |
| 1765 | #endif |
| 1766 | break; |
| 1767 | case PyUnicode_4BYTE_KIND: |
| 1768 | #if SIZEOF_WCHAR_T == 2 |
| 1769 | /* This is the only case which has to process surrogates, thus |
| 1770 | a simple copy loop is not enough and we need a function. */ |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1771 | unicode_convert_wchar_to_ucs4(u, u + size, unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1772 | #else |
| 1773 | assert(num_surrogates == 0); |
| 1774 | Py_MEMCPY(PyUnicode_4BYTE_DATA(unicode), u, size * 4); |
| 1775 | #endif |
| 1776 | break; |
| 1777 | default: |
| 1778 | assert(0 && "Impossible state"); |
| 1779 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1780 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1781 | return unicode_result(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1782 | } |
| 1783 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1784 | PyObject * |
| 1785 | PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size) |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1786 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1787 | if (size < 0) { |
| 1788 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1789 | "Negative size passed to PyUnicode_FromStringAndSize"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1790 | return NULL; |
| 1791 | } |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 1792 | if (u != NULL) |
| 1793 | return PyUnicode_DecodeUTF8Stateful(u, size, NULL, NULL); |
| 1794 | else |
| 1795 | return (PyObject *)_PyUnicode_New(size); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1796 | } |
| 1797 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1798 | PyObject * |
| 1799 | PyUnicode_FromString(const char *u) |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1800 | { |
| 1801 | size_t size = strlen(u); |
| 1802 | if (size > PY_SSIZE_T_MAX) { |
| 1803 | PyErr_SetString(PyExc_OverflowError, "input too long"); |
| 1804 | return NULL; |
| 1805 | } |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 1806 | return PyUnicode_DecodeUTF8Stateful(u, (Py_ssize_t)size, NULL, NULL); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1807 | } |
| 1808 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1809 | PyObject * |
| 1810 | _PyUnicode_FromId(_Py_Identifier *id) |
| 1811 | { |
| 1812 | if (!id->object) { |
Victor Stinner | d1cd99b | 2012-02-07 23:05:55 +0100 | [diff] [blame] | 1813 | id->object = PyUnicode_DecodeUTF8Stateful(id->string, |
| 1814 | strlen(id->string), |
| 1815 | NULL, NULL); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1816 | if (!id->object) |
| 1817 | return NULL; |
| 1818 | PyUnicode_InternInPlace(&id->object); |
| 1819 | assert(!id->next); |
| 1820 | id->next = static_strings; |
| 1821 | static_strings = id; |
| 1822 | } |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1823 | return id->object; |
| 1824 | } |
| 1825 | |
| 1826 | void |
| 1827 | _PyUnicode_ClearStaticStrings() |
| 1828 | { |
| 1829 | _Py_Identifier *i; |
| 1830 | for (i = static_strings; i; i = i->next) { |
| 1831 | Py_DECREF(i->object); |
| 1832 | i->object = NULL; |
| 1833 | i->next = NULL; |
| 1834 | } |
| 1835 | } |
| 1836 | |
Benjamin Peterson | 0df5429 | 2012-03-26 14:50:32 -0400 | [diff] [blame] | 1837 | /* Internal function, doesn't check maximum character */ |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1838 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1839 | PyObject* |
| 1840 | _PyUnicode_FromASCII(const char *buffer, Py_ssize_t size) |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1841 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1842 | const unsigned char *s = (const unsigned char *)buffer; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 1843 | PyObject *unicode; |
Victor Stinner | e6b2d44 | 2011-12-11 21:54:30 +0100 | [diff] [blame] | 1844 | if (size == 1) { |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1845 | #ifdef Py_DEBUG |
Victor Stinner | e6b2d44 | 2011-12-11 21:54:30 +0100 | [diff] [blame] | 1846 | assert(s[0] < 128); |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1847 | #endif |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1848 | return get_latin1_char(s[0]); |
Victor Stinner | e6b2d44 | 2011-12-11 21:54:30 +0100 | [diff] [blame] | 1849 | } |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 1850 | unicode = PyUnicode_New(size, 127); |
| 1851 | if (!unicode) |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1852 | return NULL; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 1853 | memcpy(PyUnicode_1BYTE_DATA(unicode), s, size); |
| 1854 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 1855 | return unicode; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1856 | } |
| 1857 | |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 1858 | static Py_UCS4 |
| 1859 | kind_maxchar_limit(unsigned int kind) |
| 1860 | { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 1861 | switch (kind) { |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 1862 | case PyUnicode_1BYTE_KIND: |
| 1863 | return 0x80; |
| 1864 | case PyUnicode_2BYTE_KIND: |
| 1865 | return 0x100; |
| 1866 | case PyUnicode_4BYTE_KIND: |
| 1867 | return 0x10000; |
| 1868 | default: |
| 1869 | assert(0 && "invalid kind"); |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1870 | return MAX_UNICODE; |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 1871 | } |
| 1872 | } |
| 1873 | |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 1874 | Py_LOCAL_INLINE(Py_UCS4) |
| 1875 | align_maxchar(Py_UCS4 maxchar) |
| 1876 | { |
| 1877 | if (maxchar <= 127) |
| 1878 | return 127; |
| 1879 | else if (maxchar <= 255) |
| 1880 | return 255; |
| 1881 | else if (maxchar <= 65535) |
| 1882 | return 65535; |
| 1883 | else |
| 1884 | return MAX_UNICODE; |
| 1885 | } |
| 1886 | |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1887 | static PyObject* |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1888 | _PyUnicode_FromUCS1(const unsigned char* u, Py_ssize_t size) |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1889 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1890 | PyObject *res; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1891 | unsigned char max_char; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1892 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1893 | if (size == 0) { |
| 1894 | Py_INCREF(unicode_empty); |
| 1895 | return unicode_empty; |
| 1896 | } |
| 1897 | assert(size > 0); |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1898 | if (size == 1) |
| 1899 | return get_latin1_char(u[0]); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1900 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1901 | max_char = ucs1lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1902 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1903 | if (!res) |
| 1904 | return NULL; |
| 1905 | memcpy(PyUnicode_1BYTE_DATA(res), u, size); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1906 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1907 | return res; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1908 | } |
| 1909 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1910 | static PyObject* |
| 1911 | _PyUnicode_FromUCS2(const Py_UCS2 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1912 | { |
| 1913 | PyObject *res; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1914 | Py_UCS2 max_char; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1915 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1916 | if (size == 0) { |
| 1917 | Py_INCREF(unicode_empty); |
| 1918 | return unicode_empty; |
| 1919 | } |
| 1920 | assert(size > 0); |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 1921 | if (size == 1) { |
| 1922 | Py_UCS4 ch = u[0]; |
| 1923 | if (ch < 256) |
| 1924 | return get_latin1_char((unsigned char)ch); |
| 1925 | |
| 1926 | res = PyUnicode_New(1, ch); |
| 1927 | if (res == NULL) |
| 1928 | return NULL; |
| 1929 | PyUnicode_WRITE(PyUnicode_KIND(res), PyUnicode_DATA(res), 0, ch); |
| 1930 | assert(_PyUnicode_CheckConsistency(res, 1)); |
| 1931 | return res; |
| 1932 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1933 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1934 | max_char = ucs2lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1935 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1936 | if (!res) |
| 1937 | return NULL; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1938 | if (max_char >= 256) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1939 | memcpy(PyUnicode_2BYTE_DATA(res), u, sizeof(Py_UCS2)*size); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1940 | else { |
| 1941 | _PyUnicode_CONVERT_BYTES( |
| 1942 | Py_UCS2, Py_UCS1, u, u + size, PyUnicode_1BYTE_DATA(res)); |
| 1943 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1944 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1945 | return res; |
| 1946 | } |
| 1947 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1948 | static PyObject* |
| 1949 | _PyUnicode_FromUCS4(const Py_UCS4 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1950 | { |
| 1951 | PyObject *res; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1952 | Py_UCS4 max_char; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1953 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1954 | if (size == 0) { |
| 1955 | Py_INCREF(unicode_empty); |
| 1956 | return unicode_empty; |
| 1957 | } |
| 1958 | assert(size > 0); |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 1959 | if (size == 1) { |
| 1960 | Py_UCS4 ch = u[0]; |
| 1961 | if (ch < 256) |
| 1962 | return get_latin1_char((unsigned char)ch); |
| 1963 | |
| 1964 | res = PyUnicode_New(1, ch); |
| 1965 | if (res == NULL) |
| 1966 | return NULL; |
| 1967 | PyUnicode_WRITE(PyUnicode_KIND(res), PyUnicode_DATA(res), 0, ch); |
| 1968 | assert(_PyUnicode_CheckConsistency(res, 1)); |
| 1969 | return res; |
| 1970 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1971 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1972 | max_char = ucs4lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1973 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1974 | if (!res) |
| 1975 | return NULL; |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 1976 | if (max_char < 256) |
| 1977 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS1, u, u + size, |
| 1978 | PyUnicode_1BYTE_DATA(res)); |
| 1979 | else if (max_char < 0x10000) |
| 1980 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS2, u, u + size, |
| 1981 | PyUnicode_2BYTE_DATA(res)); |
| 1982 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1983 | memcpy(PyUnicode_4BYTE_DATA(res), u, sizeof(Py_UCS4)*size); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1984 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1985 | return res; |
| 1986 | } |
| 1987 | |
| 1988 | PyObject* |
| 1989 | PyUnicode_FromKindAndData(int kind, const void *buffer, Py_ssize_t size) |
| 1990 | { |
Victor Stinner | cfed46e | 2011-11-22 01:29:14 +0100 | [diff] [blame] | 1991 | if (size < 0) { |
| 1992 | PyErr_SetString(PyExc_ValueError, "size must be positive"); |
| 1993 | return NULL; |
| 1994 | } |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 1995 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1996 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1997 | return _PyUnicode_FromUCS1(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1998 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1999 | return _PyUnicode_FromUCS2(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2000 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 2001 | return _PyUnicode_FromUCS4(buffer, size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 2002 | default: |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 2003 | PyErr_SetString(PyExc_SystemError, "invalid kind"); |
| 2004 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2005 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2006 | } |
| 2007 | |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2008 | Py_UCS4 |
| 2009 | _PyUnicode_FindMaxChar(PyObject *unicode, Py_ssize_t start, Py_ssize_t end) |
| 2010 | { |
| 2011 | enum PyUnicode_Kind kind; |
| 2012 | void *startptr, *endptr; |
| 2013 | |
| 2014 | assert(PyUnicode_IS_READY(unicode)); |
| 2015 | assert(0 <= start); |
| 2016 | assert(end <= PyUnicode_GET_LENGTH(unicode)); |
| 2017 | assert(start <= end); |
| 2018 | |
| 2019 | if (start == 0 && end == PyUnicode_GET_LENGTH(unicode)) |
| 2020 | return PyUnicode_MAX_CHAR_VALUE(unicode); |
| 2021 | |
| 2022 | if (start == end) |
| 2023 | return 127; |
| 2024 | |
Victor Stinner | 94d558b | 2012-04-27 22:26:58 +0200 | [diff] [blame] | 2025 | if (PyUnicode_IS_ASCII(unicode)) |
| 2026 | return 127; |
| 2027 | |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2028 | kind = PyUnicode_KIND(unicode); |
Benjamin Peterson | f3b7d86 | 2012-04-23 18:07:01 -0400 | [diff] [blame] | 2029 | startptr = PyUnicode_DATA(unicode); |
Benjamin Peterson | b9f4c9d | 2012-04-23 21:45:40 -0400 | [diff] [blame] | 2030 | endptr = (char *)startptr + end * kind; |
| 2031 | startptr = (char *)startptr + start * kind; |
Benjamin Peterson | 2844a7a | 2012-04-23 18:00:25 -0400 | [diff] [blame] | 2032 | switch(kind) { |
| 2033 | case PyUnicode_1BYTE_KIND: |
| 2034 | return ucs1lib_find_max_char(startptr, endptr); |
| 2035 | case PyUnicode_2BYTE_KIND: |
| 2036 | return ucs2lib_find_max_char(startptr, endptr); |
| 2037 | case PyUnicode_4BYTE_KIND: |
| 2038 | return ucs4lib_find_max_char(startptr, endptr); |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2039 | default: |
Benjamin Peterson | 2844a7a | 2012-04-23 18:00:25 -0400 | [diff] [blame] | 2040 | assert(0); |
| 2041 | return 0; |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2042 | } |
| 2043 | } |
| 2044 | |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2045 | /* Ensure that a string uses the most efficient storage, if it is not the |
| 2046 | case: create a new string with of the right kind. Write NULL into *p_unicode |
| 2047 | on error. */ |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 2048 | static void |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2049 | unicode_adjust_maxchar(PyObject **p_unicode) |
| 2050 | { |
| 2051 | PyObject *unicode, *copy; |
| 2052 | Py_UCS4 max_char; |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2053 | Py_ssize_t len; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2054 | unsigned int kind; |
| 2055 | |
| 2056 | assert(p_unicode != NULL); |
| 2057 | unicode = *p_unicode; |
| 2058 | assert(PyUnicode_IS_READY(unicode)); |
| 2059 | if (PyUnicode_IS_ASCII(unicode)) |
| 2060 | return; |
| 2061 | |
| 2062 | len = PyUnicode_GET_LENGTH(unicode); |
| 2063 | kind = PyUnicode_KIND(unicode); |
| 2064 | if (kind == PyUnicode_1BYTE_KIND) { |
| 2065 | const Py_UCS1 *u = PyUnicode_1BYTE_DATA(unicode); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2066 | max_char = ucs1lib_find_max_char(u, u + len); |
| 2067 | if (max_char >= 128) |
| 2068 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2069 | } |
| 2070 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 2071 | const Py_UCS2 *u = PyUnicode_2BYTE_DATA(unicode); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2072 | max_char = ucs2lib_find_max_char(u, u + len); |
| 2073 | if (max_char >= 256) |
| 2074 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2075 | } |
| 2076 | else { |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2077 | const Py_UCS4 *u = PyUnicode_4BYTE_DATA(unicode); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2078 | assert(kind == PyUnicode_4BYTE_KIND); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2079 | max_char = ucs4lib_find_max_char(u, u + len); |
| 2080 | if (max_char >= 0x10000) |
| 2081 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2082 | } |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2083 | copy = PyUnicode_New(len, max_char); |
Victor Stinner | ca439ee | 2012-06-16 03:17:34 +0200 | [diff] [blame] | 2084 | if (copy != NULL) |
| 2085 | _PyUnicode_FastCopyCharacters(copy, 0, unicode, 0, len); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2086 | Py_DECREF(unicode); |
| 2087 | *p_unicode = copy; |
| 2088 | } |
| 2089 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2090 | PyObject* |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 2091 | _PyUnicode_Copy(PyObject *unicode) |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2092 | { |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 2093 | Py_ssize_t length; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2094 | PyObject *copy; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2095 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2096 | if (!PyUnicode_Check(unicode)) { |
| 2097 | PyErr_BadInternalCall(); |
| 2098 | return NULL; |
| 2099 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 2100 | if (PyUnicode_READY(unicode) == -1) |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2101 | return NULL; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2102 | |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 2103 | length = PyUnicode_GET_LENGTH(unicode); |
| 2104 | copy = PyUnicode_New(length, PyUnicode_MAX_CHAR_VALUE(unicode)); |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2105 | if (!copy) |
| 2106 | return NULL; |
| 2107 | assert(PyUnicode_KIND(copy) == PyUnicode_KIND(unicode)); |
| 2108 | |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 2109 | Py_MEMCPY(PyUnicode_DATA(copy), PyUnicode_DATA(unicode), |
| 2110 | length * PyUnicode_KIND(unicode)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2111 | assert(_PyUnicode_CheckConsistency(copy, 1)); |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2112 | return copy; |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2113 | } |
| 2114 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2115 | |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2116 | /* Widen Unicode objects to larger buffers. Don't write terminating null |
| 2117 | character. Return NULL on error. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2118 | |
| 2119 | void* |
| 2120 | _PyUnicode_AsKind(PyObject *s, unsigned int kind) |
| 2121 | { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2122 | Py_ssize_t len; |
| 2123 | void *result; |
| 2124 | unsigned int skind; |
| 2125 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 2126 | if (PyUnicode_READY(s) == -1) |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2127 | return NULL; |
| 2128 | |
| 2129 | len = PyUnicode_GET_LENGTH(s); |
| 2130 | skind = PyUnicode_KIND(s); |
| 2131 | if (skind >= kind) { |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 2132 | PyErr_SetString(PyExc_SystemError, "invalid widening attempt"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2133 | return NULL; |
| 2134 | } |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 2135 | switch (kind) { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2136 | case PyUnicode_2BYTE_KIND: |
| 2137 | result = PyMem_Malloc(len * sizeof(Py_UCS2)); |
| 2138 | if (!result) |
| 2139 | return PyErr_NoMemory(); |
| 2140 | assert(skind == PyUnicode_1BYTE_KIND); |
| 2141 | _PyUnicode_CONVERT_BYTES( |
| 2142 | Py_UCS1, Py_UCS2, |
| 2143 | PyUnicode_1BYTE_DATA(s), |
| 2144 | PyUnicode_1BYTE_DATA(s) + len, |
| 2145 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2146 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2147 | case PyUnicode_4BYTE_KIND: |
| 2148 | result = PyMem_Malloc(len * sizeof(Py_UCS4)); |
| 2149 | if (!result) |
| 2150 | return PyErr_NoMemory(); |
| 2151 | if (skind == PyUnicode_2BYTE_KIND) { |
| 2152 | _PyUnicode_CONVERT_BYTES( |
| 2153 | Py_UCS2, Py_UCS4, |
| 2154 | PyUnicode_2BYTE_DATA(s), |
| 2155 | PyUnicode_2BYTE_DATA(s) + len, |
| 2156 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2157 | } |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2158 | else { |
| 2159 | assert(skind == PyUnicode_1BYTE_KIND); |
| 2160 | _PyUnicode_CONVERT_BYTES( |
| 2161 | Py_UCS1, Py_UCS4, |
| 2162 | PyUnicode_1BYTE_DATA(s), |
| 2163 | PyUnicode_1BYTE_DATA(s) + len, |
| 2164 | result); |
| 2165 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2166 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2167 | default: |
| 2168 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2169 | } |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 2170 | PyErr_SetString(PyExc_SystemError, "invalid kind"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2171 | return NULL; |
| 2172 | } |
| 2173 | |
| 2174 | static Py_UCS4* |
| 2175 | as_ucs4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 2176 | int copy_null) |
| 2177 | { |
| 2178 | int kind; |
| 2179 | void *data; |
| 2180 | Py_ssize_t len, targetlen; |
| 2181 | if (PyUnicode_READY(string) == -1) |
| 2182 | return NULL; |
| 2183 | kind = PyUnicode_KIND(string); |
| 2184 | data = PyUnicode_DATA(string); |
| 2185 | len = PyUnicode_GET_LENGTH(string); |
| 2186 | targetlen = len; |
| 2187 | if (copy_null) |
| 2188 | targetlen++; |
| 2189 | if (!target) { |
| 2190 | if (PY_SSIZE_T_MAX / sizeof(Py_UCS4) < targetlen) { |
| 2191 | PyErr_NoMemory(); |
| 2192 | return NULL; |
| 2193 | } |
| 2194 | target = PyMem_Malloc(targetlen * sizeof(Py_UCS4)); |
| 2195 | if (!target) { |
| 2196 | PyErr_NoMemory(); |
| 2197 | return NULL; |
| 2198 | } |
| 2199 | } |
| 2200 | else { |
| 2201 | if (targetsize < targetlen) { |
| 2202 | PyErr_Format(PyExc_SystemError, |
| 2203 | "string is longer than the buffer"); |
| 2204 | if (copy_null && 0 < targetsize) |
| 2205 | target[0] = 0; |
| 2206 | return NULL; |
| 2207 | } |
| 2208 | } |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2209 | if (kind == PyUnicode_1BYTE_KIND) { |
| 2210 | Py_UCS1 *start = (Py_UCS1 *) data; |
| 2211 | _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] | 2212 | } |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2213 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 2214 | Py_UCS2 *start = (Py_UCS2 *) data; |
| 2215 | _PyUnicode_CONVERT_BYTES(Py_UCS2, Py_UCS4, start, start + len, target); |
| 2216 | } |
| 2217 | else { |
| 2218 | assert(kind == PyUnicode_4BYTE_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2219 | Py_MEMCPY(target, data, len * sizeof(Py_UCS4)); |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2220 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2221 | if (copy_null) |
| 2222 | target[len] = 0; |
| 2223 | return target; |
| 2224 | } |
| 2225 | |
| 2226 | Py_UCS4* |
| 2227 | PyUnicode_AsUCS4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 2228 | int copy_null) |
| 2229 | { |
Antoine Pitrou | de20b0b | 2011-11-10 21:47:38 +0100 | [diff] [blame] | 2230 | if (target == NULL || targetsize < 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2231 | PyErr_BadInternalCall(); |
| 2232 | return NULL; |
| 2233 | } |
| 2234 | return as_ucs4(string, target, targetsize, copy_null); |
| 2235 | } |
| 2236 | |
| 2237 | Py_UCS4* |
| 2238 | PyUnicode_AsUCS4Copy(PyObject *string) |
| 2239 | { |
| 2240 | return as_ucs4(string, NULL, 0, 1); |
| 2241 | } |
| 2242 | |
| 2243 | #ifdef HAVE_WCHAR_H |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 2244 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2245 | PyObject * |
| 2246 | PyUnicode_FromWideChar(register const wchar_t *w, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2247 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2248 | if (w == NULL) { |
Victor Stinner | 382955f | 2011-12-11 21:44:00 +0100 | [diff] [blame] | 2249 | if (size == 0) { |
| 2250 | Py_INCREF(unicode_empty); |
| 2251 | return unicode_empty; |
| 2252 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2253 | PyErr_BadInternalCall(); |
| 2254 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2255 | } |
| 2256 | |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 2257 | if (size == -1) { |
| 2258 | size = wcslen(w); |
| 2259 | } |
| 2260 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2261 | return PyUnicode_FromUnicode(w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2262 | } |
| 2263 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2264 | #endif /* HAVE_WCHAR_H */ |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 2265 | |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2266 | static void |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2267 | makefmt(char *fmt, int longflag, int longlongflag, int size_tflag, |
| 2268 | int zeropad, int width, int precision, char c) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2269 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2270 | *fmt++ = '%'; |
| 2271 | if (width) { |
| 2272 | if (zeropad) |
| 2273 | *fmt++ = '0'; |
| 2274 | fmt += sprintf(fmt, "%d", width); |
| 2275 | } |
| 2276 | if (precision) |
| 2277 | fmt += sprintf(fmt, ".%d", precision); |
| 2278 | if (longflag) |
| 2279 | *fmt++ = 'l'; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2280 | else if (longlongflag) { |
| 2281 | /* longlongflag should only ever be nonzero on machines with |
| 2282 | HAVE_LONG_LONG defined */ |
| 2283 | #ifdef HAVE_LONG_LONG |
| 2284 | char *f = PY_FORMAT_LONG_LONG; |
| 2285 | while (*f) |
| 2286 | *fmt++ = *f++; |
| 2287 | #else |
| 2288 | /* we shouldn't ever get here */ |
| 2289 | assert(0); |
| 2290 | *fmt++ = 'l'; |
| 2291 | #endif |
| 2292 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2293 | else if (size_tflag) { |
| 2294 | char *f = PY_FORMAT_SIZE_T; |
| 2295 | while (*f) |
| 2296 | *fmt++ = *f++; |
| 2297 | } |
| 2298 | *fmt++ = c; |
| 2299 | *fmt = '\0'; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2300 | } |
| 2301 | |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2302 | /* helper for PyUnicode_FromFormatV() */ |
| 2303 | |
| 2304 | static const char* |
| 2305 | parse_format_flags(const char *f, |
| 2306 | int *p_width, int *p_precision, |
| 2307 | int *p_longflag, int *p_longlongflag, int *p_size_tflag) |
| 2308 | { |
| 2309 | int width, precision, longflag, longlongflag, size_tflag; |
| 2310 | |
| 2311 | /* parse the width.precision part, e.g. "%2.5s" => width=2, precision=5 */ |
| 2312 | f++; |
| 2313 | width = 0; |
| 2314 | while (Py_ISDIGIT((unsigned)*f)) |
| 2315 | width = (width*10) + *f++ - '0'; |
| 2316 | precision = 0; |
| 2317 | if (*f == '.') { |
| 2318 | f++; |
| 2319 | while (Py_ISDIGIT((unsigned)*f)) |
| 2320 | precision = (precision*10) + *f++ - '0'; |
| 2321 | if (*f == '%') { |
| 2322 | /* "%.3%s" => f points to "3" */ |
| 2323 | f--; |
| 2324 | } |
| 2325 | } |
| 2326 | if (*f == '\0') { |
| 2327 | /* bogus format "%.1" => go backward, f points to "1" */ |
| 2328 | f--; |
| 2329 | } |
| 2330 | if (p_width != NULL) |
| 2331 | *p_width = width; |
| 2332 | if (p_precision != NULL) |
| 2333 | *p_precision = precision; |
| 2334 | |
| 2335 | /* Handle %ld, %lu, %lld and %llu. */ |
| 2336 | longflag = 0; |
| 2337 | longlongflag = 0; |
Victor Stinner | e7faec1 | 2011-03-02 00:01:53 +0000 | [diff] [blame] | 2338 | size_tflag = 0; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2339 | |
| 2340 | if (*f == 'l') { |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2341 | if (f[1] == 'd' || f[1] == 'u' || f[1] == 'i') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2342 | longflag = 1; |
| 2343 | ++f; |
| 2344 | } |
| 2345 | #ifdef HAVE_LONG_LONG |
| 2346 | else if (f[1] == 'l' && |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2347 | (f[2] == 'd' || f[2] == 'u' || f[2] == 'i')) { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2348 | longlongflag = 1; |
| 2349 | f += 2; |
| 2350 | } |
| 2351 | #endif |
| 2352 | } |
| 2353 | /* handle the size_t flag. */ |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2354 | 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] | 2355 | size_tflag = 1; |
| 2356 | ++f; |
| 2357 | } |
| 2358 | if (p_longflag != NULL) |
| 2359 | *p_longflag = longflag; |
| 2360 | if (p_longlongflag != NULL) |
| 2361 | *p_longlongflag = longlongflag; |
| 2362 | if (p_size_tflag != NULL) |
| 2363 | *p_size_tflag = size_tflag; |
| 2364 | return f; |
| 2365 | } |
| 2366 | |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2367 | /* maximum number of characters required for output of %ld. 21 characters |
| 2368 | allows for 64-bit integers (in decimal) and an optional sign. */ |
| 2369 | #define MAX_LONG_CHARS 21 |
| 2370 | /* maximum number of characters required for output of %lld. |
| 2371 | We need at most ceil(log10(256)*SIZEOF_LONG_LONG) digits, |
| 2372 | plus 1 for the sign. 53/22 is an upper bound for log10(256). */ |
| 2373 | #define MAX_LONG_LONG_CHARS (2 + (SIZEOF_LONG_LONG*53-1) / 22) |
| 2374 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2375 | PyObject * |
| 2376 | PyUnicode_FromFormatV(const char *format, va_list vargs) |
| 2377 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2378 | va_list count; |
| 2379 | Py_ssize_t callcount = 0; |
| 2380 | PyObject **callresults = NULL; |
| 2381 | PyObject **callresult = NULL; |
| 2382 | Py_ssize_t n = 0; |
| 2383 | int width = 0; |
| 2384 | int precision = 0; |
| 2385 | int zeropad; |
| 2386 | const char* f; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2387 | PyObject *string; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2388 | /* used by sprintf */ |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2389 | char fmt[61]; /* should be enough for %0width.precisionlld */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2390 | Py_UCS4 maxchar = 127; /* result is ASCII by default */ |
| 2391 | Py_UCS4 argmaxchar; |
| 2392 | Py_ssize_t numbersize = 0; |
| 2393 | char *numberresults = NULL; |
| 2394 | char *numberresult = NULL; |
| 2395 | Py_ssize_t i; |
| 2396 | int kind; |
| 2397 | void *data; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2398 | |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 2399 | Py_VA_COPY(count, vargs); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2400 | /* step 1: count the number of %S/%R/%A/%s format specifications |
| 2401 | * (we call PyObject_Str()/PyObject_Repr()/PyObject_ASCII()/ |
| 2402 | * 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] | 2403 | * result in an array) |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 2404 | * also estimate a upper bound for all the number formats in the string, |
| 2405 | * 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] | 2406 | * buffer before putting everything together. */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2407 | for (f = format; *f; f++) { |
| 2408 | if (*f == '%') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2409 | int longlongflag; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2410 | /* skip width or width.precision (eg. "1.2" of "%1.2f") */ |
| 2411 | f = parse_format_flags(f, &width, NULL, NULL, &longlongflag, NULL); |
| 2412 | if (*f == 's' || *f=='S' || *f=='R' || *f=='A' || *f=='V') |
| 2413 | ++callcount; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2414 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2415 | else if (*f == 'd' || *f=='u' || *f=='i' || *f=='x' || *f=='p') { |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2416 | #ifdef HAVE_LONG_LONG |
| 2417 | if (longlongflag) { |
| 2418 | if (width < MAX_LONG_LONG_CHARS) |
| 2419 | width = MAX_LONG_LONG_CHARS; |
| 2420 | } |
| 2421 | else |
| 2422 | #endif |
| 2423 | /* MAX_LONG_CHARS is enough to hold a 64-bit integer, |
| 2424 | including sign. Decimal takes the most space. This |
| 2425 | isn't enough for octal. If a width is specified we |
| 2426 | need more (which we allocate later). */ |
| 2427 | if (width < MAX_LONG_CHARS) |
| 2428 | width = MAX_LONG_CHARS; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2429 | |
| 2430 | /* account for the size + '\0' to separate numbers |
| 2431 | inside of the numberresults buffer */ |
| 2432 | numbersize += (width + 1); |
| 2433 | } |
| 2434 | } |
| 2435 | else if ((unsigned char)*f > 127) { |
| 2436 | PyErr_Format(PyExc_ValueError, |
| 2437 | "PyUnicode_FromFormatV() expects an ASCII-encoded format " |
| 2438 | "string, got a non-ASCII byte: 0x%02x", |
| 2439 | (unsigned char)*f); |
| 2440 | return NULL; |
| 2441 | } |
| 2442 | } |
| 2443 | /* step 2: allocate memory for the results of |
| 2444 | * PyObject_Str()/PyObject_Repr()/PyUnicode_DecodeUTF8() calls */ |
| 2445 | if (callcount) { |
| 2446 | callresults = PyObject_Malloc(sizeof(PyObject *) * callcount); |
| 2447 | if (!callresults) { |
| 2448 | PyErr_NoMemory(); |
| 2449 | return NULL; |
| 2450 | } |
| 2451 | callresult = callresults; |
| 2452 | } |
| 2453 | /* step 2.5: allocate memory for the results of formating numbers */ |
| 2454 | if (numbersize) { |
| 2455 | numberresults = PyObject_Malloc(numbersize); |
| 2456 | if (!numberresults) { |
| 2457 | PyErr_NoMemory(); |
| 2458 | goto fail; |
| 2459 | } |
| 2460 | numberresult = numberresults; |
| 2461 | } |
| 2462 | |
| 2463 | /* step 3: format numbers and figure out how large a buffer we need */ |
| 2464 | for (f = format; *f; f++) { |
| 2465 | if (*f == '%') { |
| 2466 | const char* p; |
| 2467 | int longflag; |
| 2468 | int longlongflag; |
| 2469 | int size_tflag; |
| 2470 | int numprinted; |
| 2471 | |
| 2472 | p = f; |
| 2473 | zeropad = (f[1] == '0'); |
| 2474 | f = parse_format_flags(f, &width, &precision, |
| 2475 | &longflag, &longlongflag, &size_tflag); |
| 2476 | switch (*f) { |
| 2477 | case 'c': |
| 2478 | { |
| 2479 | Py_UCS4 ordinal = va_arg(count, int); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 2480 | maxchar = MAX_MAXCHAR(maxchar, ordinal); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2481 | n++; |
| 2482 | break; |
| 2483 | } |
| 2484 | case '%': |
| 2485 | n++; |
| 2486 | break; |
| 2487 | case 'i': |
| 2488 | case 'd': |
| 2489 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, |
| 2490 | width, precision, *f); |
| 2491 | if (longflag) |
| 2492 | numprinted = sprintf(numberresult, fmt, |
| 2493 | va_arg(count, long)); |
| 2494 | #ifdef HAVE_LONG_LONG |
| 2495 | else if (longlongflag) |
| 2496 | numprinted = sprintf(numberresult, fmt, |
| 2497 | va_arg(count, PY_LONG_LONG)); |
| 2498 | #endif |
| 2499 | else if (size_tflag) |
| 2500 | numprinted = sprintf(numberresult, fmt, |
| 2501 | va_arg(count, Py_ssize_t)); |
| 2502 | else |
| 2503 | numprinted = sprintf(numberresult, fmt, |
| 2504 | va_arg(count, int)); |
| 2505 | n += numprinted; |
| 2506 | /* advance by +1 to skip over the '\0' */ |
| 2507 | numberresult += (numprinted + 1); |
| 2508 | assert(*(numberresult - 1) == '\0'); |
| 2509 | assert(*(numberresult - 2) != '\0'); |
| 2510 | assert(numprinted >= 0); |
| 2511 | assert(numberresult <= numberresults + numbersize); |
| 2512 | break; |
| 2513 | case 'u': |
| 2514 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, |
| 2515 | width, precision, 'u'); |
| 2516 | if (longflag) |
| 2517 | numprinted = sprintf(numberresult, fmt, |
| 2518 | va_arg(count, unsigned long)); |
| 2519 | #ifdef HAVE_LONG_LONG |
| 2520 | else if (longlongflag) |
| 2521 | numprinted = sprintf(numberresult, fmt, |
| 2522 | va_arg(count, unsigned PY_LONG_LONG)); |
| 2523 | #endif |
| 2524 | else if (size_tflag) |
| 2525 | numprinted = sprintf(numberresult, fmt, |
| 2526 | va_arg(count, size_t)); |
| 2527 | else |
| 2528 | numprinted = sprintf(numberresult, fmt, |
| 2529 | va_arg(count, unsigned int)); |
| 2530 | n += numprinted; |
| 2531 | numberresult += (numprinted + 1); |
| 2532 | assert(*(numberresult - 1) == '\0'); |
| 2533 | assert(*(numberresult - 2) != '\0'); |
| 2534 | assert(numprinted >= 0); |
| 2535 | assert(numberresult <= numberresults + numbersize); |
| 2536 | break; |
| 2537 | case 'x': |
| 2538 | makefmt(fmt, 0, 0, 0, zeropad, width, precision, 'x'); |
| 2539 | numprinted = sprintf(numberresult, fmt, va_arg(count, int)); |
| 2540 | n += numprinted; |
| 2541 | numberresult += (numprinted + 1); |
| 2542 | assert(*(numberresult - 1) == '\0'); |
| 2543 | assert(*(numberresult - 2) != '\0'); |
| 2544 | assert(numprinted >= 0); |
| 2545 | assert(numberresult <= numberresults + numbersize); |
| 2546 | break; |
| 2547 | case 'p': |
| 2548 | numprinted = sprintf(numberresult, "%p", va_arg(count, void*)); |
| 2549 | /* %p is ill-defined: ensure leading 0x. */ |
| 2550 | if (numberresult[1] == 'X') |
| 2551 | numberresult[1] = 'x'; |
| 2552 | else if (numberresult[1] != 'x') { |
| 2553 | memmove(numberresult + 2, numberresult, |
| 2554 | strlen(numberresult) + 1); |
| 2555 | numberresult[0] = '0'; |
| 2556 | numberresult[1] = 'x'; |
| 2557 | numprinted += 2; |
| 2558 | } |
| 2559 | n += numprinted; |
| 2560 | numberresult += (numprinted + 1); |
| 2561 | assert(*(numberresult - 1) == '\0'); |
| 2562 | assert(*(numberresult - 2) != '\0'); |
| 2563 | assert(numprinted >= 0); |
| 2564 | assert(numberresult <= numberresults + numbersize); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2565 | break; |
| 2566 | case 's': |
| 2567 | { |
| 2568 | /* UTF-8 */ |
Georg Brandl | 780b2a6 | 2009-05-05 09:19:59 +0000 | [diff] [blame] | 2569 | const char *s = va_arg(count, const char*); |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 2570 | PyObject *str = PyUnicode_DecodeUTF8Stateful(s, strlen(s), "replace", NULL); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2571 | if (!str) |
| 2572 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2573 | /* since PyUnicode_DecodeUTF8 returns already flexible |
| 2574 | unicode objects, there is no need to call ready on them */ |
| 2575 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 2576 | maxchar = MAX_MAXCHAR(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2577 | n += PyUnicode_GET_LENGTH(str); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2578 | /* Remember the str and switch to the next slot */ |
| 2579 | *callresult++ = str; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2580 | break; |
| 2581 | } |
| 2582 | case 'U': |
| 2583 | { |
| 2584 | PyObject *obj = va_arg(count, PyObject *); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 2585 | assert(obj && _PyUnicode_CHECK(obj)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2586 | if (PyUnicode_READY(obj) == -1) |
| 2587 | goto fail; |
| 2588 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(obj); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 2589 | maxchar = MAX_MAXCHAR(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2590 | n += PyUnicode_GET_LENGTH(obj); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2591 | break; |
| 2592 | } |
| 2593 | case 'V': |
| 2594 | { |
| 2595 | PyObject *obj = va_arg(count, PyObject *); |
| 2596 | const char *str = va_arg(count, const char *); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2597 | PyObject *str_obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2598 | assert(obj || str); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 2599 | assert(!obj || _PyUnicode_CHECK(obj)); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2600 | if (obj) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2601 | if (PyUnicode_READY(obj) == -1) |
| 2602 | goto fail; |
| 2603 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(obj); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 2604 | maxchar = MAX_MAXCHAR(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2605 | n += PyUnicode_GET_LENGTH(obj); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2606 | *callresult++ = NULL; |
| 2607 | } |
| 2608 | else { |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 2609 | str_obj = PyUnicode_DecodeUTF8Stateful(str, strlen(str), "replace", NULL); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2610 | if (!str_obj) |
| 2611 | goto fail; |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 2612 | if (PyUnicode_READY(str_obj) == -1) { |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 2613 | Py_DECREF(str_obj); |
| 2614 | goto fail; |
| 2615 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2616 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str_obj); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 2617 | maxchar = MAX_MAXCHAR(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2618 | n += PyUnicode_GET_LENGTH(str_obj); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2619 | *callresult++ = str_obj; |
| 2620 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2621 | break; |
| 2622 | } |
| 2623 | case 'S': |
| 2624 | { |
| 2625 | PyObject *obj = va_arg(count, PyObject *); |
| 2626 | PyObject *str; |
| 2627 | assert(obj); |
| 2628 | str = PyObject_Str(obj); |
Benjamin Peterson | c8d8b88 | 2012-01-14 13:37:31 -0500 | [diff] [blame] | 2629 | if (!str) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2630 | goto fail; |
Benjamin Peterson | c8d8b88 | 2012-01-14 13:37:31 -0500 | [diff] [blame] | 2631 | if (PyUnicode_READY(str) == -1) { |
| 2632 | Py_DECREF(str); |
| 2633 | goto fail; |
| 2634 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2635 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 2636 | maxchar = MAX_MAXCHAR(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2637 | n += PyUnicode_GET_LENGTH(str); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2638 | /* Remember the str and switch to the next slot */ |
| 2639 | *callresult++ = str; |
| 2640 | break; |
| 2641 | } |
| 2642 | case 'R': |
| 2643 | { |
| 2644 | PyObject *obj = va_arg(count, PyObject *); |
| 2645 | PyObject *repr; |
| 2646 | assert(obj); |
| 2647 | repr = PyObject_Repr(obj); |
Benjamin Peterson | c8d8b88 | 2012-01-14 13:37:31 -0500 | [diff] [blame] | 2648 | if (!repr) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2649 | goto fail; |
Benjamin Peterson | c8d8b88 | 2012-01-14 13:37:31 -0500 | [diff] [blame] | 2650 | if (PyUnicode_READY(repr) == -1) { |
| 2651 | Py_DECREF(repr); |
| 2652 | goto fail; |
| 2653 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2654 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(repr); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 2655 | maxchar = MAX_MAXCHAR(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2656 | n += PyUnicode_GET_LENGTH(repr); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2657 | /* Remember the repr and switch to the next slot */ |
| 2658 | *callresult++ = repr; |
| 2659 | break; |
| 2660 | } |
| 2661 | case 'A': |
| 2662 | { |
| 2663 | PyObject *obj = va_arg(count, PyObject *); |
| 2664 | PyObject *ascii; |
| 2665 | assert(obj); |
| 2666 | ascii = PyObject_ASCII(obj); |
Benjamin Peterson | c8d8b88 | 2012-01-14 13:37:31 -0500 | [diff] [blame] | 2667 | if (!ascii) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2668 | goto fail; |
Benjamin Peterson | c8d8b88 | 2012-01-14 13:37:31 -0500 | [diff] [blame] | 2669 | if (PyUnicode_READY(ascii) == -1) { |
| 2670 | Py_DECREF(ascii); |
| 2671 | goto fail; |
| 2672 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2673 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(ascii); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 2674 | maxchar = MAX_MAXCHAR(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2675 | n += PyUnicode_GET_LENGTH(ascii); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2676 | /* Remember the repr and switch to the next slot */ |
| 2677 | *callresult++ = ascii; |
| 2678 | break; |
| 2679 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2680 | default: |
| 2681 | /* if we stumble upon an unknown |
| 2682 | formatting code, copy the rest of |
| 2683 | the format string to the output |
| 2684 | string. (we cannot just skip the |
| 2685 | code, since there's no way to know |
| 2686 | what's in the argument list) */ |
| 2687 | n += strlen(p); |
| 2688 | goto expand; |
| 2689 | } |
| 2690 | } else |
| 2691 | n++; |
| 2692 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2693 | expand: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2694 | /* step 4: fill the buffer */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2695 | /* Since we've analyzed how much space we need, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2696 | we don't have to resize the string. |
| 2697 | There can be no errors beyond this point. */ |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2698 | string = PyUnicode_New(n, maxchar); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2699 | if (!string) |
| 2700 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2701 | kind = PyUnicode_KIND(string); |
| 2702 | data = PyUnicode_DATA(string); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2703 | callresult = callresults; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2704 | numberresult = numberresults; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2705 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2706 | for (i = 0, f = format; *f; f++) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2707 | if (*f == '%') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2708 | const char* p; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2709 | |
| 2710 | p = f; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2711 | f = parse_format_flags(f, NULL, NULL, NULL, NULL, NULL); |
| 2712 | /* checking for == because the last argument could be a empty |
| 2713 | string, which causes i to point to end, the assert at the end of |
| 2714 | the loop */ |
| 2715 | assert(i <= PyUnicode_GET_LENGTH(string)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2716 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2717 | switch (*f) { |
| 2718 | case 'c': |
Victor Stinner | 5ed8b2c | 2011-02-21 21:13:44 +0000 | [diff] [blame] | 2719 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2720 | const int ordinal = va_arg(vargs, int); |
| 2721 | PyUnicode_WRITE(kind, data, i++, ordinal); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2722 | break; |
Victor Stinner | 5ed8b2c | 2011-02-21 21:13:44 +0000 | [diff] [blame] | 2723 | } |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2724 | case 'i': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2725 | case 'd': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2726 | case 'u': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2727 | case 'x': |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2728 | case 'p': |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 2729 | { |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 2730 | Py_ssize_t len; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2731 | /* unused, since we already have the result */ |
| 2732 | if (*f == 'p') |
| 2733 | (void) va_arg(vargs, void *); |
| 2734 | else |
| 2735 | (void) va_arg(vargs, int); |
| 2736 | /* extract the result from numberresults and append. */ |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 2737 | len = strlen(numberresult); |
| 2738 | unicode_write_cstr(string, i, numberresult, len); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2739 | /* skip over the separating '\0' */ |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 2740 | i += len; |
| 2741 | numberresult += len; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2742 | assert(*numberresult == '\0'); |
| 2743 | numberresult++; |
| 2744 | assert(numberresult <= numberresults + numbersize); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2745 | break; |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 2746 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2747 | case 's': |
| 2748 | { |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2749 | /* unused, since we already have the result */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2750 | Py_ssize_t size; |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2751 | (void) va_arg(vargs, char *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2752 | size = PyUnicode_GET_LENGTH(*callresult); |
| 2753 | assert(PyUnicode_KIND(*callresult) <= PyUnicode_KIND(string)); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 2754 | _PyUnicode_FastCopyCharacters(string, i, *callresult, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2755 | i += size; |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2756 | /* We're done with the unicode()/repr() => forget it */ |
| 2757 | Py_DECREF(*callresult); |
| 2758 | /* switch to next unicode()/repr() result */ |
| 2759 | ++callresult; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2760 | break; |
| 2761 | } |
| 2762 | case 'U': |
| 2763 | { |
| 2764 | PyObject *obj = va_arg(vargs, PyObject *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2765 | Py_ssize_t size; |
| 2766 | assert(PyUnicode_KIND(obj) <= PyUnicode_KIND(string)); |
| 2767 | size = PyUnicode_GET_LENGTH(obj); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 2768 | _PyUnicode_FastCopyCharacters(string, i, obj, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2769 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2770 | break; |
| 2771 | } |
| 2772 | case 'V': |
| 2773 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2774 | Py_ssize_t size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2775 | PyObject *obj = va_arg(vargs, PyObject *); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2776 | va_arg(vargs, const char *); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2777 | if (obj) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2778 | size = PyUnicode_GET_LENGTH(obj); |
| 2779 | assert(PyUnicode_KIND(obj) <= PyUnicode_KIND(string)); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 2780 | _PyUnicode_FastCopyCharacters(string, i, obj, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2781 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2782 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2783 | size = PyUnicode_GET_LENGTH(*callresult); |
| 2784 | assert(PyUnicode_KIND(*callresult) <= |
| 2785 | PyUnicode_KIND(string)); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 2786 | _PyUnicode_FastCopyCharacters(string, i, *callresult, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2787 | i += size; |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2788 | Py_DECREF(*callresult); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2789 | } |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2790 | ++callresult; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2791 | break; |
| 2792 | } |
| 2793 | case 'S': |
| 2794 | case 'R': |
Victor Stinner | 9a90900 | 2010-10-18 20:59:24 +0000 | [diff] [blame] | 2795 | case 'A': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2796 | { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2797 | Py_ssize_t size = PyUnicode_GET_LENGTH(*callresult); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2798 | /* unused, since we already have the result */ |
| 2799 | (void) va_arg(vargs, PyObject *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2800 | assert(PyUnicode_KIND(*callresult) <= PyUnicode_KIND(string)); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 2801 | _PyUnicode_FastCopyCharacters(string, i, *callresult, 0, size); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2802 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2803 | /* We're done with the unicode()/repr() => forget it */ |
| 2804 | Py_DECREF(*callresult); |
| 2805 | /* switch to next unicode()/repr() result */ |
| 2806 | ++callresult; |
| 2807 | break; |
| 2808 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2809 | case '%': |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2810 | PyUnicode_WRITE(kind, data, i++, '%'); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2811 | break; |
| 2812 | default: |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 2813 | { |
| 2814 | Py_ssize_t len = strlen(p); |
| 2815 | unicode_write_cstr(string, i, p, len); |
| 2816 | i += len; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2817 | assert(i == PyUnicode_GET_LENGTH(string)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2818 | goto end; |
| 2819 | } |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 2820 | } |
Victor Stinner | 1205f27 | 2010-09-11 00:54:47 +0000 | [diff] [blame] | 2821 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2822 | else { |
| 2823 | assert(i < PyUnicode_GET_LENGTH(string)); |
| 2824 | PyUnicode_WRITE(kind, data, i++, *f); |
| 2825 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2826 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2827 | assert(i == PyUnicode_GET_LENGTH(string)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2828 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2829 | end: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2830 | if (callresults) |
| 2831 | PyObject_Free(callresults); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2832 | if (numberresults) |
| 2833 | PyObject_Free(numberresults); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2834 | return unicode_result(string); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2835 | fail: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2836 | if (callresults) { |
| 2837 | PyObject **callresult2 = callresults; |
| 2838 | while (callresult2 < callresult) { |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2839 | Py_XDECREF(*callresult2); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2840 | ++callresult2; |
| 2841 | } |
| 2842 | PyObject_Free(callresults); |
| 2843 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2844 | if (numberresults) |
| 2845 | PyObject_Free(numberresults); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2846 | return NULL; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2847 | } |
| 2848 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2849 | PyObject * |
| 2850 | PyUnicode_FromFormat(const char *format, ...) |
| 2851 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2852 | PyObject* ret; |
| 2853 | va_list vargs; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2854 | |
| 2855 | #ifdef HAVE_STDARG_PROTOTYPES |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2856 | va_start(vargs, format); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2857 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2858 | va_start(vargs); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2859 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2860 | ret = PyUnicode_FromFormatV(format, vargs); |
| 2861 | va_end(vargs); |
| 2862 | return ret; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2863 | } |
| 2864 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2865 | #ifdef HAVE_WCHAR_H |
| 2866 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2867 | /* Helper function for PyUnicode_AsWideChar() and PyUnicode_AsWideCharString(): |
| 2868 | convert a Unicode object to a wide character string. |
| 2869 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2870 | - 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] | 2871 | character) required to convert the unicode object. Ignore size argument. |
| 2872 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2873 | - Otherwise: return the number of wide characters (excluding the null |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2874 | character) written into w. Write at most size wide characters (including |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2875 | the null character). */ |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2876 | static Py_ssize_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2877 | unicode_aswidechar(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2878 | wchar_t *w, |
| 2879 | Py_ssize_t size) |
| 2880 | { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2881 | Py_ssize_t res; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2882 | const wchar_t *wstr; |
| 2883 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2884 | wstr = PyUnicode_AsUnicodeAndSize(unicode, &res); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2885 | if (wstr == NULL) |
| 2886 | return -1; |
| 2887 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2888 | if (w != NULL) { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2889 | if (size > res) |
| 2890 | size = res + 1; |
| 2891 | else |
| 2892 | res = size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2893 | Py_MEMCPY(w, wstr, size * sizeof(wchar_t)); |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2894 | return res; |
| 2895 | } |
| 2896 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2897 | return res + 1; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2898 | } |
| 2899 | |
| 2900 | Py_ssize_t |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2901 | PyUnicode_AsWideChar(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2902 | wchar_t *w, |
| 2903 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2904 | { |
| 2905 | if (unicode == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2906 | PyErr_BadInternalCall(); |
| 2907 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2908 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2909 | return unicode_aswidechar(unicode, w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2910 | } |
| 2911 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2912 | wchar_t* |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 2913 | PyUnicode_AsWideCharString(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2914 | Py_ssize_t *size) |
| 2915 | { |
| 2916 | wchar_t* buffer; |
| 2917 | Py_ssize_t buflen; |
| 2918 | |
| 2919 | if (unicode == NULL) { |
| 2920 | PyErr_BadInternalCall(); |
| 2921 | return NULL; |
| 2922 | } |
| 2923 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2924 | buflen = unicode_aswidechar(unicode, NULL, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2925 | if (buflen == -1) |
| 2926 | return NULL; |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2927 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < buflen) { |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2928 | PyErr_NoMemory(); |
| 2929 | return NULL; |
| 2930 | } |
| 2931 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2932 | buffer = PyMem_MALLOC(buflen * sizeof(wchar_t)); |
| 2933 | if (buffer == NULL) { |
| 2934 | PyErr_NoMemory(); |
| 2935 | return NULL; |
| 2936 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2937 | buflen = unicode_aswidechar(unicode, buffer, buflen); |
Stefan Krah | 8528c31 | 2012-08-19 21:52:43 +0200 | [diff] [blame] | 2938 | if (buflen == -1) { |
| 2939 | PyMem_FREE(buffer); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2940 | return NULL; |
Stefan Krah | 8528c31 | 2012-08-19 21:52:43 +0200 | [diff] [blame] | 2941 | } |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2942 | if (size != NULL) |
| 2943 | *size = buflen; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2944 | return buffer; |
| 2945 | } |
| 2946 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2947 | #endif /* HAVE_WCHAR_H */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2948 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2949 | PyObject * |
| 2950 | PyUnicode_FromOrdinal(int ordinal) |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2951 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2952 | PyObject *v; |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 2953 | if (ordinal < 0 || ordinal > MAX_UNICODE) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2954 | PyErr_SetString(PyExc_ValueError, |
| 2955 | "chr() arg not in range(0x110000)"); |
| 2956 | return NULL; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2957 | } |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 2958 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2959 | if (ordinal < 256) |
| 2960 | return get_latin1_char(ordinal); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2961 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2962 | v = PyUnicode_New(1, ordinal); |
| 2963 | if (v == NULL) |
| 2964 | return NULL; |
| 2965 | PyUnicode_WRITE(PyUnicode_KIND(v), PyUnicode_DATA(v), 0, ordinal); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2966 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2967 | return v; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2968 | } |
| 2969 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2970 | PyObject * |
| 2971 | PyUnicode_FromObject(register PyObject *obj) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2972 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2973 | /* XXX Perhaps we should make this API an alias of |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2974 | PyObject_Str() instead ?! */ |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2975 | if (PyUnicode_CheckExact(obj)) { |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 2976 | if (PyUnicode_READY(obj) == -1) |
Victor Stinner | d3a83d5 | 2011-10-01 03:09:33 +0200 | [diff] [blame] | 2977 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2978 | Py_INCREF(obj); |
| 2979 | return obj; |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2980 | } |
| 2981 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2982 | /* For a Unicode subtype that's not a Unicode object, |
| 2983 | return a true Unicode object with the same data. */ |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 2984 | return _PyUnicode_Copy(obj); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2985 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2986 | PyErr_Format(PyExc_TypeError, |
| 2987 | "Can't convert '%.100s' object to str implicitly", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2988 | Py_TYPE(obj)->tp_name); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2989 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2990 | } |
| 2991 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2992 | PyObject * |
| 2993 | PyUnicode_FromEncodedObject(register PyObject *obj, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2994 | const char *encoding, |
| 2995 | const char *errors) |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2996 | { |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2997 | Py_buffer buffer; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2998 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2999 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3000 | if (obj == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3001 | PyErr_BadInternalCall(); |
| 3002 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3003 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 3004 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 3005 | /* Decoding bytes objects is the most common case and should be fast */ |
| 3006 | if (PyBytes_Check(obj)) { |
| 3007 | if (PyBytes_GET_SIZE(obj) == 0) { |
| 3008 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 3009 | v = unicode_empty; |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 3010 | } |
| 3011 | else { |
| 3012 | v = PyUnicode_Decode( |
| 3013 | PyBytes_AS_STRING(obj), PyBytes_GET_SIZE(obj), |
| 3014 | encoding, errors); |
| 3015 | } |
| 3016 | return v; |
| 3017 | } |
| 3018 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 3019 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3020 | PyErr_SetString(PyExc_TypeError, |
| 3021 | "decoding str is not supported"); |
| 3022 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3023 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 3024 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 3025 | /* Retrieve a bytes buffer view through the PEP 3118 buffer interface */ |
| 3026 | if (PyObject_GetBuffer(obj, &buffer, PyBUF_SIMPLE) < 0) { |
| 3027 | PyErr_Format(PyExc_TypeError, |
| 3028 | "coercing to str: need bytes, bytearray " |
| 3029 | "or buffer-like object, %.80s found", |
| 3030 | Py_TYPE(obj)->tp_name); |
| 3031 | return NULL; |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 3032 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3033 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 3034 | if (buffer.len == 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3035 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 3036 | v = unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3037 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3038 | else |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 3039 | v = PyUnicode_Decode((char*) buffer.buf, buffer.len, encoding, errors); |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 3040 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 3041 | PyBuffer_Release(&buffer); |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 3042 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3043 | } |
| 3044 | |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3045 | /* Convert encoding to lower case and replace '_' with '-' in order to |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3046 | catch e.g. UTF_8. Return 0 on error (encoding is longer than lower_len-1), |
| 3047 | 1 on success. */ |
| 3048 | static int |
| 3049 | normalize_encoding(const char *encoding, |
| 3050 | char *lower, |
| 3051 | size_t lower_len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3052 | { |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 3053 | const char *e; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3054 | char *l; |
| 3055 | char *l_end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3056 | |
Benjamin Peterson | 7a6debe | 2011-10-15 09:25:28 -0400 | [diff] [blame] | 3057 | if (encoding == NULL) { |
| 3058 | strcpy(lower, "utf-8"); |
| 3059 | return 1; |
| 3060 | } |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 3061 | e = encoding; |
| 3062 | l = lower; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3063 | l_end = &lower[lower_len - 1]; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3064 | while (*e) { |
| 3065 | if (l == l_end) |
| 3066 | return 0; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 3067 | if (Py_ISUPPER(*e)) { |
| 3068 | *l++ = Py_TOLOWER(*e++); |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 3069 | } |
| 3070 | else if (*e == '_') { |
| 3071 | *l++ = '-'; |
| 3072 | e++; |
| 3073 | } |
| 3074 | else { |
| 3075 | *l++ = *e++; |
| 3076 | } |
| 3077 | } |
| 3078 | *l = '\0'; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3079 | return 1; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3080 | } |
| 3081 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3082 | PyObject * |
| 3083 | PyUnicode_Decode(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3084 | Py_ssize_t size, |
| 3085 | const char *encoding, |
| 3086 | const char *errors) |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3087 | { |
| 3088 | PyObject *buffer = NULL, *unicode; |
| 3089 | Py_buffer info; |
| 3090 | char lower[11]; /* Enough for any encoding shortcut */ |
| 3091 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3092 | /* Shortcuts for common default encodings */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3093 | if (normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3094 | if ((strcmp(lower, "utf-8") == 0) || |
| 3095 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 3096 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3097 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3098 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3099 | (strcmp(lower, "iso-8859-1") == 0)) |
| 3100 | return PyUnicode_DecodeLatin1(s, size, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3101 | #ifdef HAVE_MBCS |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3102 | else if (strcmp(lower, "mbcs") == 0) |
| 3103 | return PyUnicode_DecodeMBCS(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 3104 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3105 | else if (strcmp(lower, "ascii") == 0) |
| 3106 | return PyUnicode_DecodeASCII(s, size, errors); |
| 3107 | else if (strcmp(lower, "utf-16") == 0) |
| 3108 | return PyUnicode_DecodeUTF16(s, size, errors, 0); |
| 3109 | else if (strcmp(lower, "utf-32") == 0) |
| 3110 | return PyUnicode_DecodeUTF32(s, size, errors, 0); |
| 3111 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3112 | |
| 3113 | /* Decode via the codec registry */ |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 3114 | buffer = NULL; |
Antoine Pitrou | c3b3924 | 2009-01-03 16:59:18 +0000 | [diff] [blame] | 3115 | 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] | 3116 | goto onError; |
Antoine Pitrou | ee58fa4 | 2008-08-19 18:22:14 +0000 | [diff] [blame] | 3117 | buffer = PyMemoryView_FromBuffer(&info); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3118 | if (buffer == NULL) |
| 3119 | goto onError; |
| 3120 | unicode = PyCodec_Decode(buffer, encoding, errors); |
| 3121 | if (unicode == NULL) |
| 3122 | goto onError; |
| 3123 | if (!PyUnicode_Check(unicode)) { |
| 3124 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3125 | "decoder did not return a str object (type=%.400s)", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 3126 | Py_TYPE(unicode)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3127 | Py_DECREF(unicode); |
| 3128 | goto onError; |
| 3129 | } |
| 3130 | Py_DECREF(buffer); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 3131 | return unicode_result(unicode); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3132 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3133 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3134 | Py_XDECREF(buffer); |
| 3135 | return NULL; |
| 3136 | } |
| 3137 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3138 | PyObject * |
| 3139 | PyUnicode_AsDecodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3140 | const char *encoding, |
| 3141 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3142 | { |
| 3143 | PyObject *v; |
| 3144 | |
| 3145 | if (!PyUnicode_Check(unicode)) { |
| 3146 | PyErr_BadArgument(); |
| 3147 | goto onError; |
| 3148 | } |
| 3149 | |
| 3150 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3151 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3152 | |
| 3153 | /* Decode via the codec registry */ |
| 3154 | v = PyCodec_Decode(unicode, encoding, errors); |
| 3155 | if (v == NULL) |
| 3156 | goto onError; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 3157 | return unicode_result(v); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3158 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3159 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3160 | return NULL; |
| 3161 | } |
| 3162 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3163 | PyObject * |
| 3164 | PyUnicode_AsDecodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3165 | const char *encoding, |
| 3166 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3167 | { |
| 3168 | PyObject *v; |
| 3169 | |
| 3170 | if (!PyUnicode_Check(unicode)) { |
| 3171 | PyErr_BadArgument(); |
| 3172 | goto onError; |
| 3173 | } |
| 3174 | |
| 3175 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3176 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3177 | |
| 3178 | /* Decode via the codec registry */ |
| 3179 | v = PyCodec_Decode(unicode, encoding, errors); |
| 3180 | if (v == NULL) |
| 3181 | goto onError; |
| 3182 | if (!PyUnicode_Check(v)) { |
| 3183 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3184 | "decoder did not return a str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3185 | Py_TYPE(v)->tp_name); |
| 3186 | Py_DECREF(v); |
| 3187 | goto onError; |
| 3188 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 3189 | return unicode_result(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3190 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3191 | onError: |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3192 | return NULL; |
| 3193 | } |
| 3194 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3195 | PyObject * |
| 3196 | PyUnicode_Encode(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3197 | Py_ssize_t size, |
| 3198 | const char *encoding, |
| 3199 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3200 | { |
| 3201 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3202 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3203 | unicode = PyUnicode_FromUnicode(s, size); |
| 3204 | if (unicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3205 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3206 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 3207 | Py_DECREF(unicode); |
| 3208 | return v; |
| 3209 | } |
| 3210 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3211 | PyObject * |
| 3212 | PyUnicode_AsEncodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3213 | const char *encoding, |
| 3214 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3215 | { |
| 3216 | PyObject *v; |
| 3217 | |
| 3218 | if (!PyUnicode_Check(unicode)) { |
| 3219 | PyErr_BadArgument(); |
| 3220 | goto onError; |
| 3221 | } |
| 3222 | |
| 3223 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3224 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3225 | |
| 3226 | /* Encode via the codec registry */ |
| 3227 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3228 | if (v == NULL) |
| 3229 | goto onError; |
| 3230 | return v; |
| 3231 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3232 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3233 | return NULL; |
| 3234 | } |
| 3235 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3236 | static size_t |
| 3237 | wcstombs_errorpos(const wchar_t *wstr) |
| 3238 | { |
| 3239 | size_t len; |
| 3240 | #if SIZEOF_WCHAR_T == 2 |
| 3241 | wchar_t buf[3]; |
| 3242 | #else |
| 3243 | wchar_t buf[2]; |
| 3244 | #endif |
| 3245 | char outbuf[MB_LEN_MAX]; |
| 3246 | const wchar_t *start, *previous; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3247 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3248 | #if SIZEOF_WCHAR_T == 2 |
| 3249 | buf[2] = 0; |
| 3250 | #else |
| 3251 | buf[1] = 0; |
| 3252 | #endif |
| 3253 | start = wstr; |
| 3254 | while (*wstr != L'\0') |
| 3255 | { |
| 3256 | previous = wstr; |
| 3257 | #if SIZEOF_WCHAR_T == 2 |
| 3258 | if (Py_UNICODE_IS_HIGH_SURROGATE(wstr[0]) |
| 3259 | && Py_UNICODE_IS_LOW_SURROGATE(wstr[1])) |
| 3260 | { |
| 3261 | buf[0] = wstr[0]; |
| 3262 | buf[1] = wstr[1]; |
| 3263 | wstr += 2; |
| 3264 | } |
| 3265 | else { |
| 3266 | buf[0] = *wstr; |
| 3267 | buf[1] = 0; |
| 3268 | wstr++; |
| 3269 | } |
| 3270 | #else |
| 3271 | buf[0] = *wstr; |
| 3272 | wstr++; |
| 3273 | #endif |
| 3274 | len = wcstombs(outbuf, buf, sizeof(outbuf)); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3275 | if (len == (size_t)-1) |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3276 | return previous - start; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3277 | } |
| 3278 | |
| 3279 | /* failed to find the unencodable character */ |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3280 | return 0; |
| 3281 | } |
| 3282 | |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3283 | static int |
| 3284 | locale_error_handler(const char *errors, int *surrogateescape) |
| 3285 | { |
| 3286 | if (errors == NULL) { |
| 3287 | *surrogateescape = 0; |
| 3288 | return 0; |
| 3289 | } |
| 3290 | |
| 3291 | if (strcmp(errors, "strict") == 0) { |
| 3292 | *surrogateescape = 0; |
| 3293 | return 0; |
| 3294 | } |
| 3295 | if (strcmp(errors, "surrogateescape") == 0) { |
| 3296 | *surrogateescape = 1; |
| 3297 | return 0; |
| 3298 | } |
| 3299 | PyErr_Format(PyExc_ValueError, |
| 3300 | "only 'strict' and 'surrogateescape' error handlers " |
| 3301 | "are supported, not '%s'", |
| 3302 | errors); |
| 3303 | return -1; |
| 3304 | } |
| 3305 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3306 | PyObject * |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3307 | PyUnicode_EncodeLocale(PyObject *unicode, const char *errors) |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3308 | { |
| 3309 | Py_ssize_t wlen, wlen2; |
| 3310 | wchar_t *wstr; |
| 3311 | PyObject *bytes = NULL; |
| 3312 | char *errmsg; |
Victor Stinner | 1f33f2b | 2011-12-17 04:45:09 +0100 | [diff] [blame] | 3313 | PyObject *reason; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3314 | PyObject *exc; |
| 3315 | size_t error_pos; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3316 | int surrogateescape; |
| 3317 | |
| 3318 | if (locale_error_handler(errors, &surrogateescape) < 0) |
| 3319 | return NULL; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3320 | |
| 3321 | wstr = PyUnicode_AsWideCharString(unicode, &wlen); |
| 3322 | if (wstr == NULL) |
| 3323 | return NULL; |
| 3324 | |
| 3325 | wlen2 = wcslen(wstr); |
| 3326 | if (wlen2 != wlen) { |
| 3327 | PyMem_Free(wstr); |
| 3328 | PyErr_SetString(PyExc_TypeError, "embedded null character"); |
| 3329 | return NULL; |
| 3330 | } |
| 3331 | |
| 3332 | if (surrogateescape) { |
| 3333 | /* locale encoding with surrogateescape */ |
| 3334 | char *str; |
| 3335 | |
| 3336 | str = _Py_wchar2char(wstr, &error_pos); |
| 3337 | if (str == NULL) { |
| 3338 | if (error_pos == (size_t)-1) { |
| 3339 | PyErr_NoMemory(); |
| 3340 | PyMem_Free(wstr); |
| 3341 | return NULL; |
| 3342 | } |
| 3343 | else { |
| 3344 | goto encode_error; |
| 3345 | } |
| 3346 | } |
| 3347 | PyMem_Free(wstr); |
| 3348 | |
| 3349 | bytes = PyBytes_FromString(str); |
| 3350 | PyMem_Free(str); |
| 3351 | } |
| 3352 | else { |
| 3353 | size_t len, len2; |
| 3354 | |
| 3355 | len = wcstombs(NULL, wstr, 0); |
| 3356 | if (len == (size_t)-1) { |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3357 | error_pos = (size_t)-1; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3358 | goto encode_error; |
| 3359 | } |
| 3360 | |
| 3361 | bytes = PyBytes_FromStringAndSize(NULL, len); |
| 3362 | if (bytes == NULL) { |
| 3363 | PyMem_Free(wstr); |
| 3364 | return NULL; |
| 3365 | } |
| 3366 | |
| 3367 | len2 = wcstombs(PyBytes_AS_STRING(bytes), wstr, len+1); |
| 3368 | if (len2 == (size_t)-1 || len2 > len) { |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3369 | error_pos = (size_t)-1; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3370 | goto encode_error; |
| 3371 | } |
| 3372 | PyMem_Free(wstr); |
| 3373 | } |
| 3374 | return bytes; |
| 3375 | |
| 3376 | encode_error: |
| 3377 | errmsg = strerror(errno); |
| 3378 | assert(errmsg != NULL); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3379 | |
| 3380 | if (error_pos == (size_t)-1) |
| 3381 | error_pos = wcstombs_errorpos(wstr); |
| 3382 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3383 | PyMem_Free(wstr); |
| 3384 | Py_XDECREF(bytes); |
| 3385 | |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3386 | if (errmsg != NULL) { |
| 3387 | size_t errlen; |
| 3388 | wstr = _Py_char2wchar(errmsg, &errlen); |
| 3389 | if (wstr != NULL) { |
| 3390 | reason = PyUnicode_FromWideChar(wstr, errlen); |
| 3391 | PyMem_Free(wstr); |
| 3392 | } else |
| 3393 | errmsg = NULL; |
| 3394 | } |
| 3395 | if (errmsg == NULL) |
Victor Stinner | 1f33f2b | 2011-12-17 04:45:09 +0100 | [diff] [blame] | 3396 | reason = PyUnicode_FromString( |
| 3397 | "wcstombs() encountered an unencodable " |
| 3398 | "wide character"); |
| 3399 | if (reason == NULL) |
| 3400 | return NULL; |
| 3401 | |
| 3402 | exc = PyObject_CallFunction(PyExc_UnicodeEncodeError, "sOnnO", |
| 3403 | "locale", unicode, |
| 3404 | (Py_ssize_t)error_pos, |
| 3405 | (Py_ssize_t)(error_pos+1), |
| 3406 | reason); |
| 3407 | Py_DECREF(reason); |
| 3408 | if (exc != NULL) { |
| 3409 | PyCodec_StrictErrors(exc); |
| 3410 | Py_XDECREF(exc); |
| 3411 | } |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3412 | return NULL; |
| 3413 | } |
| 3414 | |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3415 | PyObject * |
| 3416 | PyUnicode_EncodeFSDefault(PyObject *unicode) |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3417 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3418 | #ifdef HAVE_MBCS |
Victor Stinner | ac931b1 | 2011-11-20 18:27:03 +0100 | [diff] [blame] | 3419 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, NULL); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3420 | #elif defined(__APPLE__) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3421 | return _PyUnicode_AsUTF8String(unicode, "surrogateescape"); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3422 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 3423 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 3424 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 3425 | cannot use it to encode and decode filenames before it is loaded. Load |
| 3426 | the Python codec requires to encode at least its own filename. Use the C |
| 3427 | version of the locale codec until the codec registry is initialized and |
| 3428 | the Python codec is loaded. |
| 3429 | |
| 3430 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 3431 | cannot only rely on it: check also interp->fscodec_initialized for |
| 3432 | subinterpreters. */ |
| 3433 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3434 | return PyUnicode_AsEncodedString(unicode, |
| 3435 | Py_FileSystemDefaultEncoding, |
| 3436 | "surrogateescape"); |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 3437 | } |
| 3438 | else { |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3439 | return PyUnicode_EncodeLocale(unicode, "surrogateescape"); |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 3440 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3441 | #endif |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3442 | } |
| 3443 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3444 | PyObject * |
| 3445 | PyUnicode_AsEncodedString(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3446 | const char *encoding, |
| 3447 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3448 | { |
| 3449 | PyObject *v; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3450 | char lower[11]; /* Enough for any encoding shortcut */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3451 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3452 | if (!PyUnicode_Check(unicode)) { |
| 3453 | PyErr_BadArgument(); |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3454 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3455 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3456 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3457 | /* Shortcuts for common default encodings */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3458 | if (normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3459 | if ((strcmp(lower, "utf-8") == 0) || |
| 3460 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3461 | { |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3462 | if (errors == NULL || strcmp(errors, "strict") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3463 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3464 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3465 | return _PyUnicode_AsUTF8String(unicode, errors); |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3466 | } |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3467 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3468 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3469 | (strcmp(lower, "iso-8859-1") == 0)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3470 | return _PyUnicode_AsLatin1String(unicode, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3471 | #ifdef HAVE_MBCS |
Victor Stinner | ac931b1 | 2011-11-20 18:27:03 +0100 | [diff] [blame] | 3472 | else if (strcmp(lower, "mbcs") == 0) |
| 3473 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 3474 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3475 | else if (strcmp(lower, "ascii") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3476 | return _PyUnicode_AsASCIIString(unicode, errors); |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3477 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3478 | |
| 3479 | /* Encode via the codec registry */ |
| 3480 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3481 | if (v == NULL) |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3482 | return NULL; |
| 3483 | |
| 3484 | /* The normal path */ |
| 3485 | if (PyBytes_Check(v)) |
| 3486 | return v; |
| 3487 | |
| 3488 | /* 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] | 3489 | if (PyByteArray_Check(v)) { |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3490 | int error; |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3491 | PyObject *b; |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3492 | |
| 3493 | error = PyErr_WarnFormat(PyExc_RuntimeWarning, 1, |
| 3494 | "encoder %s returned bytearray instead of bytes", |
| 3495 | encoding); |
| 3496 | if (error) { |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3497 | Py_DECREF(v); |
| 3498 | return NULL; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3499 | } |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3500 | |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3501 | b = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(v), Py_SIZE(v)); |
| 3502 | Py_DECREF(v); |
| 3503 | return b; |
| 3504 | } |
| 3505 | |
| 3506 | PyErr_Format(PyExc_TypeError, |
| 3507 | "encoder did not return a bytes object (type=%.400s)", |
| 3508 | Py_TYPE(v)->tp_name); |
| 3509 | Py_DECREF(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3510 | return NULL; |
| 3511 | } |
| 3512 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3513 | PyObject * |
| 3514 | PyUnicode_AsEncodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3515 | const char *encoding, |
| 3516 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3517 | { |
| 3518 | PyObject *v; |
| 3519 | |
| 3520 | if (!PyUnicode_Check(unicode)) { |
| 3521 | PyErr_BadArgument(); |
| 3522 | goto onError; |
| 3523 | } |
| 3524 | |
| 3525 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3526 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3527 | |
| 3528 | /* Encode via the codec registry */ |
| 3529 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3530 | if (v == NULL) |
| 3531 | goto onError; |
| 3532 | if (!PyUnicode_Check(v)) { |
| 3533 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3534 | "encoder did not return an str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3535 | Py_TYPE(v)->tp_name); |
| 3536 | Py_DECREF(v); |
| 3537 | goto onError; |
| 3538 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3539 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3540 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3541 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3542 | return NULL; |
| 3543 | } |
| 3544 | |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3545 | static size_t |
| 3546 | mbstowcs_errorpos(const char *str, size_t len) |
| 3547 | { |
| 3548 | #ifdef HAVE_MBRTOWC |
| 3549 | const char *start = str; |
| 3550 | mbstate_t mbs; |
| 3551 | size_t converted; |
| 3552 | wchar_t ch; |
| 3553 | |
| 3554 | memset(&mbs, 0, sizeof mbs); |
| 3555 | while (len) |
| 3556 | { |
| 3557 | converted = mbrtowc(&ch, (char*)str, len, &mbs); |
| 3558 | if (converted == 0) |
| 3559 | /* Reached end of string */ |
| 3560 | break; |
| 3561 | if (converted == (size_t)-1 || converted == (size_t)-2) { |
| 3562 | /* Conversion error or incomplete character */ |
| 3563 | return str - start; |
| 3564 | } |
| 3565 | else { |
| 3566 | str += converted; |
| 3567 | len -= converted; |
| 3568 | } |
| 3569 | } |
| 3570 | /* failed to find the undecodable byte sequence */ |
| 3571 | return 0; |
| 3572 | #endif |
| 3573 | return 0; |
| 3574 | } |
| 3575 | |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3576 | PyObject* |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3577 | PyUnicode_DecodeLocaleAndSize(const char *str, Py_ssize_t len, |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3578 | const char *errors) |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3579 | { |
| 3580 | wchar_t smallbuf[256]; |
| 3581 | size_t smallbuf_len = Py_ARRAY_LENGTH(smallbuf); |
| 3582 | wchar_t *wstr; |
| 3583 | size_t wlen, wlen2; |
| 3584 | PyObject *unicode; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3585 | int surrogateescape; |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3586 | size_t error_pos; |
| 3587 | char *errmsg; |
| 3588 | PyObject *reason, *exc; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3589 | |
| 3590 | if (locale_error_handler(errors, &surrogateescape) < 0) |
| 3591 | return NULL; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3592 | |
| 3593 | if (str[len] != '\0' || len != strlen(str)) { |
| 3594 | PyErr_SetString(PyExc_TypeError, "embedded null character"); |
| 3595 | return NULL; |
| 3596 | } |
| 3597 | |
| 3598 | if (surrogateescape) |
| 3599 | { |
| 3600 | wstr = _Py_char2wchar(str, &wlen); |
| 3601 | if (wstr == NULL) { |
| 3602 | if (wlen == (size_t)-1) |
| 3603 | PyErr_NoMemory(); |
| 3604 | else |
| 3605 | PyErr_SetFromErrno(PyExc_OSError); |
| 3606 | return NULL; |
| 3607 | } |
| 3608 | |
| 3609 | unicode = PyUnicode_FromWideChar(wstr, wlen); |
| 3610 | PyMem_Free(wstr); |
| 3611 | } |
| 3612 | else { |
| 3613 | #ifndef HAVE_BROKEN_MBSTOWCS |
| 3614 | wlen = mbstowcs(NULL, str, 0); |
| 3615 | #else |
| 3616 | wlen = len; |
| 3617 | #endif |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3618 | if (wlen == (size_t)-1) |
| 3619 | goto decode_error; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3620 | if (wlen+1 <= smallbuf_len) { |
| 3621 | wstr = smallbuf; |
| 3622 | } |
| 3623 | else { |
| 3624 | if (wlen > PY_SSIZE_T_MAX / sizeof(wchar_t) - 1) |
| 3625 | return PyErr_NoMemory(); |
| 3626 | |
| 3627 | wstr = PyMem_Malloc((wlen+1) * sizeof(wchar_t)); |
| 3628 | if (!wstr) |
| 3629 | return PyErr_NoMemory(); |
| 3630 | } |
| 3631 | |
| 3632 | /* This shouldn't fail now */ |
| 3633 | wlen2 = mbstowcs(wstr, str, wlen+1); |
| 3634 | if (wlen2 == (size_t)-1) { |
| 3635 | if (wstr != smallbuf) |
| 3636 | PyMem_Free(wstr); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3637 | goto decode_error; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3638 | } |
| 3639 | #ifdef HAVE_BROKEN_MBSTOWCS |
| 3640 | assert(wlen2 == wlen); |
| 3641 | #endif |
| 3642 | unicode = PyUnicode_FromWideChar(wstr, wlen2); |
| 3643 | if (wstr != smallbuf) |
| 3644 | PyMem_Free(wstr); |
| 3645 | } |
| 3646 | return unicode; |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3647 | |
| 3648 | decode_error: |
| 3649 | errmsg = strerror(errno); |
| 3650 | assert(errmsg != NULL); |
| 3651 | |
| 3652 | error_pos = mbstowcs_errorpos(str, len); |
| 3653 | if (errmsg != NULL) { |
| 3654 | size_t errlen; |
| 3655 | wstr = _Py_char2wchar(errmsg, &errlen); |
| 3656 | if (wstr != NULL) { |
| 3657 | reason = PyUnicode_FromWideChar(wstr, errlen); |
| 3658 | PyMem_Free(wstr); |
| 3659 | } else |
| 3660 | errmsg = NULL; |
| 3661 | } |
| 3662 | if (errmsg == NULL) |
| 3663 | reason = PyUnicode_FromString( |
| 3664 | "mbstowcs() encountered an invalid multibyte sequence"); |
| 3665 | if (reason == NULL) |
| 3666 | return NULL; |
| 3667 | |
| 3668 | exc = PyObject_CallFunction(PyExc_UnicodeDecodeError, "sy#nnO", |
| 3669 | "locale", str, len, |
| 3670 | (Py_ssize_t)error_pos, |
| 3671 | (Py_ssize_t)(error_pos+1), |
| 3672 | reason); |
| 3673 | Py_DECREF(reason); |
| 3674 | if (exc != NULL) { |
| 3675 | PyCodec_StrictErrors(exc); |
| 3676 | Py_XDECREF(exc); |
| 3677 | } |
| 3678 | return NULL; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3679 | } |
| 3680 | |
| 3681 | PyObject* |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3682 | PyUnicode_DecodeLocale(const char *str, const char *errors) |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3683 | { |
| 3684 | Py_ssize_t size = (Py_ssize_t)strlen(str); |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3685 | return PyUnicode_DecodeLocaleAndSize(str, size, errors); |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3686 | } |
| 3687 | |
| 3688 | |
| 3689 | PyObject* |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3690 | PyUnicode_DecodeFSDefault(const char *s) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3691 | Py_ssize_t size = (Py_ssize_t)strlen(s); |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3692 | return PyUnicode_DecodeFSDefaultAndSize(s, size); |
| 3693 | } |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3694 | |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3695 | PyObject* |
| 3696 | PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size) |
| 3697 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3698 | #ifdef HAVE_MBCS |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3699 | return PyUnicode_DecodeMBCS(s, size, NULL); |
| 3700 | #elif defined(__APPLE__) |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 3701 | return PyUnicode_DecodeUTF8Stateful(s, size, "surrogateescape", NULL); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3702 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 3703 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 3704 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 3705 | cannot use it to encode and decode filenames before it is loaded. Load |
| 3706 | the Python codec requires to encode at least its own filename. Use the C |
| 3707 | version of the locale codec until the codec registry is initialized and |
| 3708 | the Python codec is loaded. |
| 3709 | |
| 3710 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 3711 | cannot only rely on it: check also interp->fscodec_initialized for |
| 3712 | subinterpreters. */ |
| 3713 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3714 | return PyUnicode_Decode(s, size, |
| 3715 | Py_FileSystemDefaultEncoding, |
Victor Stinner | b9a20ad | 2010-04-30 16:37:52 +0000 | [diff] [blame] | 3716 | "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3717 | } |
| 3718 | else { |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3719 | return PyUnicode_DecodeLocaleAndSize(s, size, "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3720 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3721 | #endif |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3722 | } |
| 3723 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3724 | |
| 3725 | int |
Antoine Pitrou | 1334884 | 2012-01-29 18:36:34 +0100 | [diff] [blame] | 3726 | _PyUnicode_HasNULChars(PyObject* s) |
| 3727 | { |
| 3728 | static PyObject *nul = NULL; |
| 3729 | |
| 3730 | if (nul == NULL) |
| 3731 | nul = PyUnicode_FromStringAndSize("\0", 1); |
| 3732 | if (nul == NULL) |
| 3733 | return -1; |
| 3734 | return PyUnicode_Contains(s, nul); |
| 3735 | } |
| 3736 | |
| 3737 | |
| 3738 | int |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3739 | PyUnicode_FSConverter(PyObject* arg, void* addr) |
| 3740 | { |
| 3741 | PyObject *output = NULL; |
| 3742 | Py_ssize_t size; |
| 3743 | void *data; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3744 | if (arg == NULL) { |
| 3745 | Py_DECREF(*(PyObject**)addr); |
| 3746 | return 1; |
| 3747 | } |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 3748 | if (PyBytes_Check(arg)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3749 | output = arg; |
| 3750 | Py_INCREF(output); |
| 3751 | } |
| 3752 | else { |
| 3753 | arg = PyUnicode_FromObject(arg); |
| 3754 | if (!arg) |
| 3755 | return 0; |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3756 | output = PyUnicode_EncodeFSDefault(arg); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3757 | Py_DECREF(arg); |
| 3758 | if (!output) |
| 3759 | return 0; |
| 3760 | if (!PyBytes_Check(output)) { |
| 3761 | Py_DECREF(output); |
| 3762 | PyErr_SetString(PyExc_TypeError, "encoder failed to return bytes"); |
| 3763 | return 0; |
| 3764 | } |
| 3765 | } |
Victor Stinner | 0ea2a46 | 2010-04-30 00:22:08 +0000 | [diff] [blame] | 3766 | size = PyBytes_GET_SIZE(output); |
| 3767 | data = PyBytes_AS_STRING(output); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3768 | if (size != strlen(data)) { |
Benjamin Peterson | 7a6b44a | 2011-08-18 13:51:47 -0500 | [diff] [blame] | 3769 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3770 | Py_DECREF(output); |
| 3771 | return 0; |
| 3772 | } |
| 3773 | *(PyObject**)addr = output; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3774 | return Py_CLEANUP_SUPPORTED; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3775 | } |
| 3776 | |
| 3777 | |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3778 | int |
| 3779 | PyUnicode_FSDecoder(PyObject* arg, void* addr) |
| 3780 | { |
| 3781 | PyObject *output = NULL; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3782 | if (arg == NULL) { |
| 3783 | Py_DECREF(*(PyObject**)addr); |
| 3784 | return 1; |
| 3785 | } |
| 3786 | if (PyUnicode_Check(arg)) { |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 3787 | if (PyUnicode_READY(arg) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3788 | return 0; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3789 | output = arg; |
| 3790 | Py_INCREF(output); |
| 3791 | } |
| 3792 | else { |
| 3793 | arg = PyBytes_FromObject(arg); |
| 3794 | if (!arg) |
| 3795 | return 0; |
| 3796 | output = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AS_STRING(arg), |
| 3797 | PyBytes_GET_SIZE(arg)); |
| 3798 | Py_DECREF(arg); |
| 3799 | if (!output) |
| 3800 | return 0; |
| 3801 | if (!PyUnicode_Check(output)) { |
| 3802 | Py_DECREF(output); |
| 3803 | PyErr_SetString(PyExc_TypeError, "decoder failed to return unicode"); |
| 3804 | return 0; |
| 3805 | } |
| 3806 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 3807 | if (PyUnicode_READY(output) == -1) { |
Victor Stinner | 065836e | 2011-10-27 01:56:33 +0200 | [diff] [blame] | 3808 | Py_DECREF(output); |
| 3809 | return 0; |
| 3810 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3811 | if (findchar(PyUnicode_DATA(output), PyUnicode_KIND(output), |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 3812 | PyUnicode_GET_LENGTH(output), 0, 1) >= 0) { |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3813 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 3814 | Py_DECREF(output); |
| 3815 | return 0; |
| 3816 | } |
| 3817 | *(PyObject**)addr = output; |
| 3818 | return Py_CLEANUP_SUPPORTED; |
| 3819 | } |
| 3820 | |
| 3821 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3822 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3823 | PyUnicode_AsUTF8AndSize(PyObject *unicode, Py_ssize_t *psize) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3824 | { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 3825 | PyObject *bytes; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3826 | |
Neal Norwitz | e0a0a6e | 2007-08-25 01:04:21 +0000 | [diff] [blame] | 3827 | if (!PyUnicode_Check(unicode)) { |
| 3828 | PyErr_BadArgument(); |
| 3829 | return NULL; |
| 3830 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3831 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3832 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3833 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3834 | if (PyUnicode_UTF8(unicode) == NULL) { |
| 3835 | assert(!PyUnicode_IS_COMPACT_ASCII(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3836 | bytes = _PyUnicode_AsUTF8String(unicode, "strict"); |
| 3837 | if (bytes == NULL) |
| 3838 | return NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3839 | _PyUnicode_UTF8(unicode) = PyObject_MALLOC(PyBytes_GET_SIZE(bytes) + 1); |
| 3840 | if (_PyUnicode_UTF8(unicode) == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3841 | Py_DECREF(bytes); |
| 3842 | return NULL; |
| 3843 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3844 | _PyUnicode_UTF8_LENGTH(unicode) = PyBytes_GET_SIZE(bytes); |
| 3845 | Py_MEMCPY(_PyUnicode_UTF8(unicode), |
| 3846 | PyBytes_AS_STRING(bytes), |
| 3847 | _PyUnicode_UTF8_LENGTH(unicode) + 1); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3848 | Py_DECREF(bytes); |
| 3849 | } |
| 3850 | |
| 3851 | if (psize) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3852 | *psize = PyUnicode_UTF8_LENGTH(unicode); |
| 3853 | return PyUnicode_UTF8(unicode); |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3854 | } |
| 3855 | |
| 3856 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3857 | PyUnicode_AsUTF8(PyObject *unicode) |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3858 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3859 | return PyUnicode_AsUTF8AndSize(unicode, NULL); |
| 3860 | } |
| 3861 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3862 | Py_UNICODE * |
| 3863 | PyUnicode_AsUnicodeAndSize(PyObject *unicode, Py_ssize_t *size) |
| 3864 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3865 | const unsigned char *one_byte; |
| 3866 | #if SIZEOF_WCHAR_T == 4 |
| 3867 | const Py_UCS2 *two_bytes; |
| 3868 | #else |
| 3869 | const Py_UCS4 *four_bytes; |
| 3870 | const Py_UCS4 *ucs4_end; |
| 3871 | Py_ssize_t num_surrogates; |
| 3872 | #endif |
| 3873 | wchar_t *w; |
| 3874 | wchar_t *wchar_end; |
| 3875 | |
| 3876 | if (!PyUnicode_Check(unicode)) { |
| 3877 | PyErr_BadArgument(); |
| 3878 | return NULL; |
| 3879 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3880 | if (_PyUnicode_WSTR(unicode) == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3881 | /* Non-ASCII compact unicode object */ |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3882 | assert(_PyUnicode_KIND(unicode) != 0); |
| 3883 | assert(PyUnicode_IS_READY(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3884 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3885 | if (PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3886 | #if SIZEOF_WCHAR_T == 2 |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3887 | four_bytes = PyUnicode_4BYTE_DATA(unicode); |
| 3888 | ucs4_end = four_bytes + _PyUnicode_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3889 | num_surrogates = 0; |
| 3890 | |
| 3891 | for (; four_bytes < ucs4_end; ++four_bytes) { |
| 3892 | if (*four_bytes > 0xFFFF) |
| 3893 | ++num_surrogates; |
| 3894 | } |
| 3895 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3896 | _PyUnicode_WSTR(unicode) = (wchar_t *) PyObject_MALLOC( |
| 3897 | sizeof(wchar_t) * (_PyUnicode_LENGTH(unicode) + 1 + num_surrogates)); |
| 3898 | if (!_PyUnicode_WSTR(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3899 | PyErr_NoMemory(); |
| 3900 | return NULL; |
| 3901 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3902 | _PyUnicode_WSTR_LENGTH(unicode) = _PyUnicode_LENGTH(unicode) + num_surrogates; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3903 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3904 | w = _PyUnicode_WSTR(unicode); |
| 3905 | wchar_end = w + _PyUnicode_WSTR_LENGTH(unicode); |
| 3906 | four_bytes = PyUnicode_4BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3907 | for (; four_bytes < ucs4_end; ++four_bytes, ++w) { |
| 3908 | if (*four_bytes > 0xFFFF) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 3909 | assert(*four_bytes <= MAX_UNICODE); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3910 | /* encode surrogate pair in this case */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 3911 | *w++ = Py_UNICODE_HIGH_SURROGATE(*four_bytes); |
| 3912 | *w = Py_UNICODE_LOW_SURROGATE(*four_bytes); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3913 | } |
| 3914 | else |
| 3915 | *w = *four_bytes; |
| 3916 | |
| 3917 | if (w > wchar_end) { |
| 3918 | assert(0 && "Miscalculated string end"); |
| 3919 | } |
| 3920 | } |
| 3921 | *w = 0; |
| 3922 | #else |
| 3923 | /* sizeof(wchar_t) == 4 */ |
| 3924 | Py_FatalError("Impossible unicode object state, wstr and str " |
| 3925 | "should share memory already."); |
| 3926 | return NULL; |
| 3927 | #endif |
| 3928 | } |
| 3929 | else { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3930 | _PyUnicode_WSTR(unicode) = (wchar_t *) PyObject_MALLOC(sizeof(wchar_t) * |
| 3931 | (_PyUnicode_LENGTH(unicode) + 1)); |
| 3932 | if (!_PyUnicode_WSTR(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3933 | PyErr_NoMemory(); |
| 3934 | return NULL; |
| 3935 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3936 | if (!PyUnicode_IS_COMPACT_ASCII(unicode)) |
| 3937 | _PyUnicode_WSTR_LENGTH(unicode) = _PyUnicode_LENGTH(unicode); |
| 3938 | w = _PyUnicode_WSTR(unicode); |
| 3939 | wchar_end = w + _PyUnicode_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3940 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3941 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) { |
| 3942 | one_byte = PyUnicode_1BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3943 | for (; w < wchar_end; ++one_byte, ++w) |
| 3944 | *w = *one_byte; |
| 3945 | /* null-terminate the wstr */ |
| 3946 | *w = 0; |
| 3947 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3948 | else if (PyUnicode_KIND(unicode) == PyUnicode_2BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3949 | #if SIZEOF_WCHAR_T == 4 |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3950 | two_bytes = PyUnicode_2BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3951 | for (; w < wchar_end; ++two_bytes, ++w) |
| 3952 | *w = *two_bytes; |
| 3953 | /* null-terminate the wstr */ |
| 3954 | *w = 0; |
| 3955 | #else |
| 3956 | /* sizeof(wchar_t) == 2 */ |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3957 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 3958 | _PyUnicode_WSTR(unicode) = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3959 | Py_FatalError("Impossible unicode object state, wstr " |
| 3960 | "and str should share memory already."); |
| 3961 | return NULL; |
| 3962 | #endif |
| 3963 | } |
| 3964 | else { |
| 3965 | assert(0 && "This should never happen."); |
| 3966 | } |
| 3967 | } |
| 3968 | } |
| 3969 | if (size != NULL) |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3970 | *size = PyUnicode_WSTR_LENGTH(unicode); |
| 3971 | return _PyUnicode_WSTR(unicode); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3972 | } |
| 3973 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3974 | Py_UNICODE * |
| 3975 | PyUnicode_AsUnicode(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3976 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3977 | return PyUnicode_AsUnicodeAndSize(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3978 | } |
| 3979 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3980 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3981 | Py_ssize_t |
| 3982 | PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3983 | { |
| 3984 | if (!PyUnicode_Check(unicode)) { |
| 3985 | PyErr_BadArgument(); |
| 3986 | goto onError; |
| 3987 | } |
| 3988 | return PyUnicode_GET_SIZE(unicode); |
| 3989 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3990 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3991 | return -1; |
| 3992 | } |
| 3993 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3994 | Py_ssize_t |
| 3995 | PyUnicode_GetLength(PyObject *unicode) |
| 3996 | { |
Victor Stinner | 0762133 | 2012-06-16 04:53:46 +0200 | [diff] [blame] | 3997 | if (!PyUnicode_Check(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3998 | PyErr_BadArgument(); |
| 3999 | return -1; |
| 4000 | } |
Victor Stinner | 0762133 | 2012-06-16 04:53:46 +0200 | [diff] [blame] | 4001 | if (PyUnicode_READY(unicode) == -1) |
| 4002 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4003 | return PyUnicode_GET_LENGTH(unicode); |
| 4004 | } |
| 4005 | |
| 4006 | Py_UCS4 |
| 4007 | PyUnicode_ReadChar(PyObject *unicode, Py_ssize_t index) |
| 4008 | { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 4009 | if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) { |
| 4010 | PyErr_BadArgument(); |
| 4011 | return (Py_UCS4)-1; |
| 4012 | } |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 4013 | if (index < 0 || index >= PyUnicode_GET_LENGTH(unicode)) { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 4014 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4015 | return (Py_UCS4)-1; |
| 4016 | } |
| 4017 | return PyUnicode_READ_CHAR(unicode, index); |
| 4018 | } |
| 4019 | |
| 4020 | int |
| 4021 | PyUnicode_WriteChar(PyObject *unicode, Py_ssize_t index, Py_UCS4 ch) |
| 4022 | { |
| 4023 | if (!PyUnicode_Check(unicode) || !PyUnicode_IS_COMPACT(unicode)) { |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 4024 | PyErr_BadArgument(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4025 | return -1; |
| 4026 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 4027 | assert(PyUnicode_IS_READY(unicode)); |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 4028 | if (index < 0 || index >= PyUnicode_GET_LENGTH(unicode)) { |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 4029 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 4030 | return -1; |
| 4031 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 4032 | if (unicode_check_modifiable(unicode)) |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 4033 | return -1; |
Victor Stinner | c9590ad | 2012-03-04 01:34:37 +0100 | [diff] [blame] | 4034 | if (ch > PyUnicode_MAX_CHAR_VALUE(unicode)) { |
| 4035 | PyErr_SetString(PyExc_ValueError, "character out of range"); |
| 4036 | return -1; |
| 4037 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4038 | PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), |
| 4039 | index, ch); |
| 4040 | return 0; |
| 4041 | } |
| 4042 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4043 | const char * |
| 4044 | PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 4045 | { |
Victor Stinner | 42cb462 | 2010-09-01 19:39:01 +0000 | [diff] [blame] | 4046 | return "utf-8"; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 4047 | } |
| 4048 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4049 | /* create or adjust a UnicodeDecodeError */ |
| 4050 | static void |
| 4051 | make_decode_exception(PyObject **exceptionObject, |
| 4052 | const char *encoding, |
| 4053 | const char *input, Py_ssize_t length, |
| 4054 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 4055 | const char *reason) |
| 4056 | { |
| 4057 | if (*exceptionObject == NULL) { |
| 4058 | *exceptionObject = PyUnicodeDecodeError_Create( |
| 4059 | encoding, input, length, startpos, endpos, reason); |
| 4060 | } |
| 4061 | else { |
| 4062 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, startpos)) |
| 4063 | goto onError; |
| 4064 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, endpos)) |
| 4065 | goto onError; |
| 4066 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 4067 | goto onError; |
| 4068 | } |
| 4069 | return; |
| 4070 | |
| 4071 | onError: |
| 4072 | Py_DECREF(*exceptionObject); |
| 4073 | *exceptionObject = NULL; |
| 4074 | } |
| 4075 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4076 | /* error handling callback helper: |
| 4077 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 4078 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4079 | and adjust various state variables. |
| 4080 | return 0 on success, -1 on error |
| 4081 | */ |
| 4082 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4083 | static int |
| 4084 | unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4085 | const char *encoding, const char *reason, |
| 4086 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 4087 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4088 | PyObject **output, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4089 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 4090 | 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] | 4091 | |
| 4092 | PyObject *restuple = NULL; |
| 4093 | PyObject *repunicode = NULL; |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 4094 | Py_ssize_t outsize; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4095 | Py_ssize_t insize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4096 | Py_ssize_t requiredsize; |
| 4097 | Py_ssize_t newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4098 | PyObject *inputobj = NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4099 | int res = -1; |
| 4100 | |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 4101 | if (_PyUnicode_KIND(*output) != PyUnicode_WCHAR_KIND) |
| 4102 | outsize = PyUnicode_GET_LENGTH(*output); |
| 4103 | else |
| 4104 | outsize = _PyUnicode_WSTR_LENGTH(*output); |
| 4105 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4106 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4107 | *errorHandler = PyCodec_LookupError(errors); |
| 4108 | if (*errorHandler == NULL) |
| 4109 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4110 | } |
| 4111 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4112 | make_decode_exception(exceptionObject, |
| 4113 | encoding, |
| 4114 | *input, *inend - *input, |
| 4115 | *startinpos, *endinpos, |
| 4116 | reason); |
| 4117 | if (*exceptionObject == NULL) |
| 4118 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4119 | |
| 4120 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 4121 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4122 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4123 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 4124 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4125 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4126 | } |
| 4127 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4128 | goto onError; |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 4129 | if (PyUnicode_READY(repunicode) == -1) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4130 | goto onError; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4131 | |
| 4132 | /* Copy back the bytes variables, which might have been modified by the |
| 4133 | callback */ |
| 4134 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 4135 | if (!inputobj) |
| 4136 | goto onError; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4137 | if (!PyBytes_Check(inputobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4138 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4139 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4140 | *input = PyBytes_AS_STRING(inputobj); |
| 4141 | insize = PyBytes_GET_SIZE(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4142 | *inend = *input + insize; |
Walter Dörwald | 36f938f | 2007-08-10 10:11:43 +0000 | [diff] [blame] | 4143 | /* we can DECREF safely, as the exception has another reference, |
| 4144 | so the object won't go away. */ |
| 4145 | Py_DECREF(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4146 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4147 | if (newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4148 | newpos = insize+newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4149 | if (newpos<0 || newpos>insize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4150 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos); |
| 4151 | goto onError; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4152 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4153 | |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 4154 | if (_PyUnicode_KIND(*output) != PyUnicode_WCHAR_KIND) { |
| 4155 | /* need more space? (at least enough for what we |
| 4156 | have+the replacement+the rest of the string (starting |
| 4157 | at the new input position), so we won't have to check space |
| 4158 | when there are no errors in the rest of the string) */ |
| 4159 | Py_ssize_t replen = PyUnicode_GET_LENGTH(repunicode); |
| 4160 | requiredsize = *outpos + replen + insize-newpos; |
| 4161 | if (requiredsize > outsize) { |
| 4162 | if (requiredsize<2*outsize) |
| 4163 | requiredsize = 2*outsize; |
| 4164 | if (unicode_resize(output, requiredsize) < 0) |
| 4165 | goto onError; |
| 4166 | } |
Victor Stinner | 1b487b4 | 2012-05-03 12:29:04 +0200 | [diff] [blame] | 4167 | if (unicode_widen(output, *outpos, |
| 4168 | PyUnicode_MAX_CHAR_VALUE(repunicode)) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4169 | goto onError; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 4170 | _PyUnicode_FastCopyCharacters(*output, *outpos, repunicode, 0, replen); |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 4171 | *outpos += replen; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4172 | } |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 4173 | else { |
| 4174 | wchar_t *repwstr; |
| 4175 | Py_ssize_t repwlen; |
| 4176 | repwstr = PyUnicode_AsUnicodeAndSize(repunicode, &repwlen); |
| 4177 | if (repwstr == NULL) |
| 4178 | goto onError; |
| 4179 | /* need more space? (at least enough for what we |
| 4180 | have+the replacement+the rest of the string (starting |
| 4181 | at the new input position), so we won't have to check space |
| 4182 | when there are no errors in the rest of the string) */ |
| 4183 | requiredsize = *outpos + repwlen + insize-newpos; |
| 4184 | if (requiredsize > outsize) { |
| 4185 | if (requiredsize < 2*outsize) |
| 4186 | requiredsize = 2*outsize; |
| 4187 | if (unicode_resize(output, requiredsize) < 0) |
| 4188 | goto onError; |
| 4189 | } |
| 4190 | wcsncpy(_PyUnicode_WSTR(*output) + *outpos, repwstr, repwlen); |
| 4191 | *outpos += repwlen; |
| 4192 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4193 | *endinpos = newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4194 | *inptr = *input + newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4195 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4196 | /* we made it! */ |
| 4197 | res = 0; |
| 4198 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4199 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4200 | Py_XDECREF(restuple); |
| 4201 | return res; |
| 4202 | } |
| 4203 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4204 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 4205 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4206 | /* See RFC2152 for details. We encode conservatively and decode liberally. */ |
| 4207 | |
| 4208 | /* Three simple macros defining base-64. */ |
| 4209 | |
| 4210 | /* Is c a base-64 character? */ |
| 4211 | |
| 4212 | #define IS_BASE64(c) \ |
| 4213 | (((c) >= 'A' && (c) <= 'Z') || \ |
| 4214 | ((c) >= 'a' && (c) <= 'z') || \ |
| 4215 | ((c) >= '0' && (c) <= '9') || \ |
| 4216 | (c) == '+' || (c) == '/') |
| 4217 | |
| 4218 | /* given that c is a base-64 character, what is its base-64 value? */ |
| 4219 | |
| 4220 | #define FROM_BASE64(c) \ |
| 4221 | (((c) >= 'A' && (c) <= 'Z') ? (c) - 'A' : \ |
| 4222 | ((c) >= 'a' && (c) <= 'z') ? (c) - 'a' + 26 : \ |
| 4223 | ((c) >= '0' && (c) <= '9') ? (c) - '0' + 52 : \ |
| 4224 | (c) == '+' ? 62 : 63) |
| 4225 | |
| 4226 | /* What is the base-64 character of the bottom 6 bits of n? */ |
| 4227 | |
| 4228 | #define TO_BASE64(n) \ |
| 4229 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 4230 | |
| 4231 | /* DECODE_DIRECT: this byte encountered in a UTF-7 string should be |
| 4232 | * decoded as itself. We are permissive on decoding; the only ASCII |
| 4233 | * byte not decoding to itself is the + which begins a base64 |
| 4234 | * string. */ |
| 4235 | |
| 4236 | #define DECODE_DIRECT(c) \ |
| 4237 | ((c) <= 127 && (c) != '+') |
| 4238 | |
| 4239 | /* The UTF-7 encoder treats ASCII characters differently according to |
| 4240 | * whether they are Set D, Set O, Whitespace, or special (i.e. none of |
| 4241 | * the above). See RFC2152. This array identifies these different |
| 4242 | * sets: |
| 4243 | * 0 : "Set D" |
| 4244 | * alphanumeric and '(),-./:? |
| 4245 | * 1 : "Set O" |
| 4246 | * !"#$%&*;<=>@[]^_`{|} |
| 4247 | * 2 : "whitespace" |
| 4248 | * ht nl cr sp |
| 4249 | * 3 : special (must be base64 encoded) |
| 4250 | * everything else (i.e. +\~ and non-printing codes 0-8 11-12 14-31 127) |
| 4251 | */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4252 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4253 | static |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4254 | char utf7_category[128] = { |
| 4255 | /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */ |
| 4256 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 3, 3, 2, 3, 3, |
| 4257 | /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */ |
| 4258 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 4259 | /* sp ! " # $ % & ' ( ) * + , - . / */ |
| 4260 | 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 3, 0, 0, 0, 0, |
| 4261 | /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */ |
| 4262 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, |
| 4263 | /* @ A B C D E F G H I J K L M N O */ |
| 4264 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4265 | /* P Q R S T U V W X Y Z [ \ ] ^ _ */ |
| 4266 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 1, 1, 1, |
| 4267 | /* ` a b c d e f g h i j k l m n o */ |
| 4268 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4269 | /* p q r s t u v w x y z { | } ~ del */ |
| 4270 | 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] | 4271 | }; |
| 4272 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4273 | /* ENCODE_DIRECT: this character should be encoded as itself. The |
| 4274 | * answer depends on whether we are encoding set O as itself, and also |
| 4275 | * on whether we are encoding whitespace as itself. RFC2152 makes it |
| 4276 | * clear that the answers to these questions vary between |
| 4277 | * applications, so this code needs to be flexible. */ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 4278 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4279 | #define ENCODE_DIRECT(c, directO, directWS) \ |
| 4280 | ((c) < 128 && (c) > 0 && \ |
| 4281 | ((utf7_category[(c)] == 0) || \ |
| 4282 | (directWS && (utf7_category[(c)] == 2)) || \ |
| 4283 | (directO && (utf7_category[(c)] == 1)))) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4284 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4285 | PyObject * |
| 4286 | PyUnicode_DecodeUTF7(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4287 | Py_ssize_t size, |
| 4288 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4289 | { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4290 | return PyUnicode_DecodeUTF7Stateful(s, size, errors, NULL); |
| 4291 | } |
| 4292 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4293 | /* The decoder. The only state we preserve is our read position, |
| 4294 | * i.e. how many characters we have consumed. So if we end in the |
| 4295 | * middle of a shift sequence we have to back off the read position |
| 4296 | * and the output to the beginning of the sequence, otherwise we lose |
| 4297 | * all the shift state (seen bits, number of bits seen, high |
| 4298 | * surrogate). */ |
| 4299 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4300 | PyObject * |
| 4301 | PyUnicode_DecodeUTF7Stateful(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4302 | Py_ssize_t size, |
| 4303 | const char *errors, |
| 4304 | Py_ssize_t *consumed) |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4305 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4306 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4307 | Py_ssize_t startinpos; |
| 4308 | Py_ssize_t endinpos; |
| 4309 | Py_ssize_t outpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4310 | const char *e; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4311 | PyObject *unicode; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4312 | const char *errmsg = ""; |
| 4313 | int inShift = 0; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4314 | Py_ssize_t shiftOutStart; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4315 | unsigned int base64bits = 0; |
| 4316 | unsigned long base64buffer = 0; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 4317 | Py_UCS4 surrogate = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4318 | PyObject *errorHandler = NULL; |
| 4319 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4320 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4321 | /* Start off assuming it's all ASCII. Widen later as necessary. */ |
| 4322 | unicode = PyUnicode_New(size, 127); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4323 | if (!unicode) |
| 4324 | return NULL; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4325 | if (size == 0) { |
| 4326 | if (consumed) |
| 4327 | *consumed = 0; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4328 | return unicode; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4329 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4330 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4331 | shiftOutStart = outpos = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4332 | e = s + size; |
| 4333 | |
| 4334 | while (s < e) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4335 | Py_UCS4 ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4336 | restart: |
Antoine Pitrou | 5ffd9e9 | 2008-07-25 18:05:24 +0000 | [diff] [blame] | 4337 | ch = (unsigned char) *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4338 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4339 | if (inShift) { /* in a base-64 section */ |
| 4340 | if (IS_BASE64(ch)) { /* consume a base-64 character */ |
| 4341 | base64buffer = (base64buffer << 6) | FROM_BASE64(ch); |
| 4342 | base64bits += 6; |
| 4343 | s++; |
| 4344 | if (base64bits >= 16) { |
| 4345 | /* we have enough bits for a UTF-16 value */ |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 4346 | Py_UCS4 outCh = (Py_UCS4)(base64buffer >> (base64bits-16)); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4347 | base64bits -= 16; |
| 4348 | base64buffer &= (1 << base64bits) - 1; /* clear high bits */ |
| 4349 | if (surrogate) { |
| 4350 | /* expecting a second surrogate */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4351 | if (Py_UNICODE_IS_LOW_SURROGATE(outCh)) { |
| 4352 | Py_UCS4 ch2 = Py_UNICODE_JOIN_SURROGATES(surrogate, outCh); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4353 | if (unicode_putchar(&unicode, &outpos, ch2) < 0) |
| 4354 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4355 | surrogate = 0; |
Antoine Pitrou | 5418ee0 | 2011-11-15 01:42:21 +0100 | [diff] [blame] | 4356 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4357 | } |
| 4358 | else { |
Antoine Pitrou | 78edf75 | 2011-11-15 01:44:16 +0100 | [diff] [blame] | 4359 | if (unicode_putchar(&unicode, &outpos, surrogate) < 0) |
| 4360 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4361 | surrogate = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4362 | } |
| 4363 | } |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4364 | if (Py_UNICODE_IS_HIGH_SURROGATE(outCh)) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4365 | /* first surrogate */ |
| 4366 | surrogate = outCh; |
| 4367 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4368 | else { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4369 | if (unicode_putchar(&unicode, &outpos, outCh) < 0) |
| 4370 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4371 | } |
| 4372 | } |
| 4373 | } |
| 4374 | else { /* now leaving a base-64 section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4375 | inShift = 0; |
| 4376 | s++; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4377 | if (surrogate) { |
Antoine Pitrou | 78edf75 | 2011-11-15 01:44:16 +0100 | [diff] [blame] | 4378 | if (unicode_putchar(&unicode, &outpos, surrogate) < 0) |
| 4379 | goto onError; |
Antoine Pitrou | 5418ee0 | 2011-11-15 01:42:21 +0100 | [diff] [blame] | 4380 | surrogate = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4381 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4382 | if (base64bits > 0) { /* left-over bits */ |
| 4383 | if (base64bits >= 6) { |
| 4384 | /* We've seen at least one base-64 character */ |
| 4385 | errmsg = "partial character in shift sequence"; |
| 4386 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4387 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4388 | else { |
| 4389 | /* Some bits remain; they should be zero */ |
| 4390 | if (base64buffer != 0) { |
| 4391 | errmsg = "non-zero padding bits in shift sequence"; |
| 4392 | goto utf7Error; |
| 4393 | } |
| 4394 | } |
| 4395 | } |
| 4396 | if (ch != '-') { |
| 4397 | /* '-' is absorbed; other terminating |
| 4398 | characters are preserved */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4399 | if (unicode_putchar(&unicode, &outpos, ch) < 0) |
| 4400 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4401 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4402 | } |
| 4403 | } |
| 4404 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4405 | startinpos = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4406 | s++; /* consume '+' */ |
| 4407 | if (s < e && *s == '-') { /* '+-' encodes '+' */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4408 | s++; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4409 | if (unicode_putchar(&unicode, &outpos, '+') < 0) |
| 4410 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4411 | } |
| 4412 | else { /* begin base64-encoded section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4413 | inShift = 1; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4414 | shiftOutStart = outpos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4415 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4416 | } |
| 4417 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4418 | else if (DECODE_DIRECT(ch)) { /* character decodes as itself */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4419 | if (unicode_putchar(&unicode, &outpos, ch) < 0) |
| 4420 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4421 | s++; |
| 4422 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4423 | else { |
| 4424 | startinpos = s-starts; |
| 4425 | s++; |
| 4426 | errmsg = "unexpected special character"; |
| 4427 | goto utf7Error; |
| 4428 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4429 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4430 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4431 | endinpos = s-starts; |
| 4432 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4433 | errors, &errorHandler, |
| 4434 | "utf7", errmsg, |
| 4435 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4436 | &unicode, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4437 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4438 | } |
| 4439 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4440 | /* end of string */ |
| 4441 | |
| 4442 | if (inShift && !consumed) { /* in shift sequence, no more to follow */ |
| 4443 | /* if we're in an inconsistent state, that's an error */ |
| 4444 | if (surrogate || |
| 4445 | (base64bits >= 6) || |
| 4446 | (base64bits > 0 && base64buffer != 0)) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4447 | endinpos = size; |
| 4448 | if (unicode_decode_call_errorhandler( |
| 4449 | errors, &errorHandler, |
| 4450 | "utf7", "unterminated shift sequence", |
| 4451 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4452 | &unicode, &outpos)) |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4453 | goto onError; |
| 4454 | if (s < e) |
| 4455 | goto restart; |
| 4456 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4457 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4458 | |
| 4459 | /* return state */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4460 | if (consumed) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4461 | if (inShift) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4462 | outpos = shiftOutStart; /* back off output */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4463 | *consumed = startinpos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4464 | } |
| 4465 | else { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4466 | *consumed = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4467 | } |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4468 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4469 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4470 | if (unicode_resize(&unicode, outpos) < 0) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4471 | goto onError; |
| 4472 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4473 | Py_XDECREF(errorHandler); |
| 4474 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 4475 | return unicode_result(unicode); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4476 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4477 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4478 | Py_XDECREF(errorHandler); |
| 4479 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4480 | Py_DECREF(unicode); |
| 4481 | return NULL; |
| 4482 | } |
| 4483 | |
| 4484 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4485 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4486 | _PyUnicode_EncodeUTF7(PyObject *str, |
| 4487 | int base64SetO, |
| 4488 | int base64WhiteSpace, |
| 4489 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4490 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4491 | int kind; |
| 4492 | void *data; |
| 4493 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4494 | PyObject *v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4495 | int inShift = 0; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4496 | Py_ssize_t i; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4497 | unsigned int base64bits = 0; |
| 4498 | unsigned long base64buffer = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4499 | char * out; |
| 4500 | char * start; |
| 4501 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 4502 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4503 | return NULL; |
| 4504 | kind = PyUnicode_KIND(str); |
| 4505 | data = PyUnicode_DATA(str); |
| 4506 | len = PyUnicode_GET_LENGTH(str); |
| 4507 | |
| 4508 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4509 | return PyBytes_FromStringAndSize(NULL, 0); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4510 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4511 | /* It might be possible to tighten this worst case */ |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 4512 | if (len > PY_SSIZE_T_MAX / 8) |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4513 | return PyErr_NoMemory(); |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 4514 | v = PyBytes_FromStringAndSize(NULL, len * 8); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4515 | if (v == NULL) |
| 4516 | return NULL; |
| 4517 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4518 | start = out = PyBytes_AS_STRING(v); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4519 | for (i = 0; i < len; ++i) { |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 4520 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4521 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4522 | if (inShift) { |
| 4523 | if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 4524 | /* shifting out */ |
| 4525 | if (base64bits) { /* output remaining bits */ |
| 4526 | *out++ = TO_BASE64(base64buffer << (6-base64bits)); |
| 4527 | base64buffer = 0; |
| 4528 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4529 | } |
| 4530 | inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4531 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 4532 | so no '-' is required, except if the character is itself a '-' */ |
| 4533 | if (IS_BASE64(ch) || ch == '-') { |
| 4534 | *out++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4535 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4536 | *out++ = (char) ch; |
| 4537 | } |
| 4538 | else { |
| 4539 | goto encode_char; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4540 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4541 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4542 | else { /* not in a shift sequence */ |
| 4543 | if (ch == '+') { |
| 4544 | *out++ = '+'; |
| 4545 | *out++ = '-'; |
| 4546 | } |
| 4547 | else if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 4548 | *out++ = (char) ch; |
| 4549 | } |
| 4550 | else { |
| 4551 | *out++ = '+'; |
| 4552 | inShift = 1; |
| 4553 | goto encode_char; |
| 4554 | } |
| 4555 | } |
| 4556 | continue; |
| 4557 | encode_char: |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4558 | if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 4559 | assert(ch <= MAX_UNICODE); |
Victor Stinner | 0d3721d | 2011-11-22 03:27:53 +0100 | [diff] [blame] | 4560 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4561 | /* code first surrogate */ |
| 4562 | base64bits += 16; |
| 4563 | base64buffer = (base64buffer << 16) | 0xd800 | ((ch-0x10000) >> 10); |
| 4564 | while (base64bits >= 6) { |
| 4565 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 4566 | base64bits -= 6; |
| 4567 | } |
| 4568 | /* prepare second surrogate */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4569 | ch = Py_UNICODE_LOW_SURROGATE(ch); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4570 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4571 | base64bits += 16; |
| 4572 | base64buffer = (base64buffer << 16) | ch; |
| 4573 | while (base64bits >= 6) { |
| 4574 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 4575 | base64bits -= 6; |
| 4576 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 4577 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4578 | if (base64bits) |
| 4579 | *out++= TO_BASE64(base64buffer << (6-base64bits) ); |
| 4580 | if (inShift) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4581 | *out++ = '-'; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4582 | if (_PyBytes_Resize(&v, out - start) < 0) |
| 4583 | return NULL; |
| 4584 | return v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4585 | } |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4586 | PyObject * |
| 4587 | PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
| 4588 | Py_ssize_t size, |
| 4589 | int base64SetO, |
| 4590 | int base64WhiteSpace, |
| 4591 | const char *errors) |
| 4592 | { |
| 4593 | PyObject *result; |
| 4594 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 4595 | if (tmp == NULL) |
| 4596 | return NULL; |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 4597 | result = _PyUnicode_EncodeUTF7(tmp, base64SetO, |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4598 | base64WhiteSpace, errors); |
| 4599 | Py_DECREF(tmp); |
| 4600 | return result; |
| 4601 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4602 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4603 | #undef IS_BASE64 |
| 4604 | #undef FROM_BASE64 |
| 4605 | #undef TO_BASE64 |
| 4606 | #undef DECODE_DIRECT |
| 4607 | #undef ENCODE_DIRECT |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4608 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4609 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 4610 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4611 | PyObject * |
| 4612 | PyUnicode_DecodeUTF8(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4613 | Py_ssize_t size, |
| 4614 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4615 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4616 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 4617 | } |
| 4618 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4619 | #include "stringlib/asciilib.h" |
| 4620 | #include "stringlib/codecs.h" |
| 4621 | #include "stringlib/undef.h" |
| 4622 | |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4623 | #include "stringlib/ucs1lib.h" |
| 4624 | #include "stringlib/codecs.h" |
| 4625 | #include "stringlib/undef.h" |
| 4626 | |
| 4627 | #include "stringlib/ucs2lib.h" |
| 4628 | #include "stringlib/codecs.h" |
| 4629 | #include "stringlib/undef.h" |
| 4630 | |
| 4631 | #include "stringlib/ucs4lib.h" |
| 4632 | #include "stringlib/codecs.h" |
| 4633 | #include "stringlib/undef.h" |
| 4634 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4635 | /* Mask to quickly check whether a C 'long' contains a |
| 4636 | non-ASCII, UTF8-encoded char. */ |
| 4637 | #if (SIZEOF_LONG == 8) |
Mark Dickinson | 01ac8b6 | 2012-07-07 14:08:48 +0200 | [diff] [blame] | 4638 | # define ASCII_CHAR_MASK 0x8080808080808080UL |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4639 | #elif (SIZEOF_LONG == 4) |
Mark Dickinson | 01ac8b6 | 2012-07-07 14:08:48 +0200 | [diff] [blame] | 4640 | # define ASCII_CHAR_MASK 0x80808080UL |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4641 | #else |
| 4642 | # error C 'long' size should be either 4 or 8! |
| 4643 | #endif |
| 4644 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4645 | static Py_ssize_t |
| 4646 | ascii_decode(const char *start, const char *end, Py_UCS1 *dest) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4647 | { |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4648 | const char *p = start; |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 4649 | const char *aligned_end = (const char *) _Py_ALIGN_DOWN(end, SIZEOF_LONG); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4650 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4651 | #if SIZEOF_LONG <= SIZEOF_VOID_P |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 4652 | assert(_Py_IS_ALIGNED(dest, SIZEOF_LONG)); |
| 4653 | if (_Py_IS_ALIGNED(p, SIZEOF_LONG)) { |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4654 | /* Fast path, see in STRINGLIB(utf8_decode) for |
| 4655 | an explanation. */ |
| 4656 | /* Help register allocation */ |
| 4657 | register const char *_p = p; |
| 4658 | register Py_UCS1 * q = dest; |
| 4659 | while (_p < aligned_end) { |
| 4660 | unsigned long value = *(const unsigned long *) _p; |
| 4661 | if (value & ASCII_CHAR_MASK) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4662 | break; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4663 | *((unsigned long *)q) = value; |
| 4664 | _p += SIZEOF_LONG; |
| 4665 | q += SIZEOF_LONG; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4666 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4667 | p = _p; |
| 4668 | while (p < end) { |
| 4669 | if ((unsigned char)*p & 0x80) |
| 4670 | break; |
| 4671 | *q++ = *p++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4672 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4673 | return p - start; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4674 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4675 | #endif |
| 4676 | while (p < end) { |
| 4677 | /* Fast path, see in STRINGLIB(utf8_decode) in stringlib/codecs.h |
| 4678 | for an explanation. */ |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 4679 | if (_Py_IS_ALIGNED(p, SIZEOF_LONG)) { |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4680 | /* Help register allocation */ |
| 4681 | register const char *_p = p; |
| 4682 | while (_p < aligned_end) { |
| 4683 | unsigned long value = *(unsigned long *) _p; |
| 4684 | if (value & ASCII_CHAR_MASK) |
| 4685 | break; |
| 4686 | _p += SIZEOF_LONG; |
| 4687 | } |
| 4688 | p = _p; |
| 4689 | if (_p == end) |
| 4690 | break; |
| 4691 | } |
| 4692 | if ((unsigned char)*p & 0x80) |
| 4693 | break; |
| 4694 | ++p; |
| 4695 | } |
| 4696 | memcpy(dest, start, p - start); |
| 4697 | return p - start; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4698 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4699 | |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4700 | PyObject * |
| 4701 | PyUnicode_DecodeUTF8Stateful(const char *s, |
| 4702 | Py_ssize_t size, |
| 4703 | const char *errors, |
| 4704 | Py_ssize_t *consumed) |
| 4705 | { |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4706 | PyObject *unicode; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4707 | const char *starts = s; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4708 | const char *end = s + size; |
| 4709 | Py_ssize_t outpos; |
| 4710 | |
| 4711 | Py_ssize_t startinpos; |
| 4712 | Py_ssize_t endinpos; |
| 4713 | const char *errmsg = ""; |
| 4714 | PyObject *errorHandler = NULL; |
| 4715 | PyObject *exc = NULL; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4716 | |
| 4717 | if (size == 0) { |
| 4718 | if (consumed) |
| 4719 | *consumed = 0; |
Victor Stinner | 382955f | 2011-12-11 21:44:00 +0100 | [diff] [blame] | 4720 | Py_INCREF(unicode_empty); |
| 4721 | return unicode_empty; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4722 | } |
| 4723 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4724 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
| 4725 | if (size == 1 && (unsigned char)s[0] < 128) { |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4726 | if (consumed) |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4727 | *consumed = 1; |
| 4728 | return get_latin1_char((unsigned char)s[0]); |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4729 | } |
| 4730 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4731 | unicode = PyUnicode_New(size, 127); |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4732 | if (!unicode) |
| 4733 | return NULL; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4734 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4735 | outpos = ascii_decode(s, end, PyUnicode_1BYTE_DATA(unicode)); |
| 4736 | s += outpos; |
| 4737 | while (s < end) { |
| 4738 | Py_UCS4 ch; |
| 4739 | int kind = PyUnicode_KIND(unicode); |
| 4740 | if (kind == PyUnicode_1BYTE_KIND) { |
| 4741 | if (PyUnicode_IS_ASCII(unicode)) |
| 4742 | ch = asciilib_utf8_decode(&s, end, |
| 4743 | PyUnicode_1BYTE_DATA(unicode), &outpos); |
| 4744 | else |
| 4745 | ch = ucs1lib_utf8_decode(&s, end, |
| 4746 | PyUnicode_1BYTE_DATA(unicode), &outpos); |
| 4747 | } else if (kind == PyUnicode_2BYTE_KIND) { |
| 4748 | ch = ucs2lib_utf8_decode(&s, end, |
| 4749 | PyUnicode_2BYTE_DATA(unicode), &outpos); |
| 4750 | } else { |
| 4751 | assert(kind == PyUnicode_4BYTE_KIND); |
| 4752 | ch = ucs4lib_utf8_decode(&s, end, |
| 4753 | PyUnicode_4BYTE_DATA(unicode), &outpos); |
| 4754 | } |
| 4755 | |
| 4756 | switch (ch) { |
| 4757 | case 0: |
| 4758 | if (s == end || consumed) |
| 4759 | goto End; |
| 4760 | errmsg = "unexpected end of data"; |
| 4761 | startinpos = s - starts; |
| 4762 | endinpos = startinpos + 1; |
| 4763 | while (endinpos < size && (starts[endinpos] & 0xC0) == 0x80) |
| 4764 | endinpos++; |
| 4765 | break; |
| 4766 | case 1: |
| 4767 | errmsg = "invalid start byte"; |
| 4768 | startinpos = s - starts; |
| 4769 | endinpos = startinpos + 1; |
| 4770 | break; |
| 4771 | case 2: |
| 4772 | errmsg = "invalid continuation byte"; |
| 4773 | startinpos = s - starts; |
| 4774 | endinpos = startinpos + 1; |
| 4775 | while (endinpos < size && (starts[endinpos] & 0xC0) == 0x80) |
| 4776 | endinpos++; |
| 4777 | break; |
| 4778 | default: |
| 4779 | if (unicode_putchar(&unicode, &outpos, ch) < 0) |
| 4780 | goto onError; |
| 4781 | continue; |
| 4782 | } |
| 4783 | |
| 4784 | if (unicode_decode_call_errorhandler( |
| 4785 | errors, &errorHandler, |
| 4786 | "utf-8", errmsg, |
| 4787 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 4788 | &unicode, &outpos)) |
| 4789 | goto onError; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4790 | } |
| 4791 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4792 | End: |
| 4793 | if (unicode_resize(&unicode, outpos) < 0) |
| 4794 | goto onError; |
| 4795 | |
| 4796 | if (consumed) |
| 4797 | *consumed = s - starts; |
| 4798 | |
| 4799 | Py_XDECREF(errorHandler); |
| 4800 | Py_XDECREF(exc); |
| 4801 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 4802 | return unicode; |
| 4803 | |
| 4804 | onError: |
| 4805 | Py_XDECREF(errorHandler); |
| 4806 | Py_XDECREF(exc); |
| 4807 | Py_XDECREF(unicode); |
| 4808 | return NULL; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4809 | } |
| 4810 | |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4811 | #ifdef __APPLE__ |
| 4812 | |
| 4813 | /* Simplified UTF-8 decoder using surrogateescape error handler, |
| 4814 | used to decode the command line arguments on Mac OS X. */ |
| 4815 | |
| 4816 | wchar_t* |
| 4817 | _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size) |
| 4818 | { |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4819 | const char *e; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4820 | wchar_t *unicode; |
| 4821 | Py_ssize_t outpos; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4822 | |
| 4823 | /* Note: size will always be longer than the resulting Unicode |
| 4824 | character count */ |
| 4825 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < (size + 1)) { |
| 4826 | PyErr_NoMemory(); |
| 4827 | return NULL; |
| 4828 | } |
| 4829 | unicode = PyMem_Malloc((size + 1) * sizeof(wchar_t)); |
| 4830 | if (!unicode) |
| 4831 | return NULL; |
| 4832 | |
| 4833 | /* Unpack UTF-8 encoded data */ |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4834 | e = s + size; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4835 | outpos = 0; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4836 | while (s < e) { |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4837 | Py_UCS4 ch; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4838 | #if SIZEOF_WCHAR_T == 4 |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4839 | ch = ucs4lib_utf8_decode(&s, e, (Py_UCS4 *)unicode, &outpos); |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4840 | #else |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4841 | ch = ucs2lib_utf8_decode(&s, e, (Py_UCS2 *)unicode, &outpos); |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4842 | #endif |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4843 | if (ch > 0xFF) { |
| 4844 | #if SIZEOF_WCHAR_T == 4 |
| 4845 | assert(0); |
| 4846 | #else |
| 4847 | assert(Py_UNICODE_IS_SURROGATE(ch)); |
| 4848 | /* compute and append the two surrogates: */ |
| 4849 | unicode[outpos++] = (wchar_t)Py_UNICODE_HIGH_SURROGATE(ch); |
| 4850 | unicode[outpos++] = (wchar_t)Py_UNICODE_LOW_SURROGATE(ch); |
| 4851 | #endif |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4852 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4853 | else { |
| 4854 | if (!ch && s == e) |
| 4855 | break; |
| 4856 | /* surrogateescape */ |
| 4857 | unicode[outpos++] = 0xDC00 + (unsigned char)*s++; |
| 4858 | } |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4859 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4860 | unicode[outpos] = L'\0'; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4861 | return unicode; |
| 4862 | } |
| 4863 | |
| 4864 | #endif /* __APPLE__ */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4865 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4866 | /* Primary internal function which creates utf8 encoded bytes objects. |
| 4867 | |
| 4868 | Allocation strategy: if the string is short, convert into a stack buffer |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4869 | and allocate exactly as much space needed at the end. Else allocate the |
| 4870 | maximum possible needed (4 result bytes per Unicode character), and return |
| 4871 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4872 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 4873 | PyObject * |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4874 | _PyUnicode_AsUTF8String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4875 | { |
Victor Stinner | 6099a03 | 2011-12-18 14:22:26 +0100 | [diff] [blame] | 4876 | enum PyUnicode_Kind kind; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4877 | void *data; |
| 4878 | Py_ssize_t size; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 4879 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4880 | if (!PyUnicode_Check(unicode)) { |
| 4881 | PyErr_BadArgument(); |
| 4882 | return NULL; |
| 4883 | } |
| 4884 | |
| 4885 | if (PyUnicode_READY(unicode) == -1) |
| 4886 | return NULL; |
| 4887 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 4888 | if (PyUnicode_UTF8(unicode)) |
| 4889 | return PyBytes_FromStringAndSize(PyUnicode_UTF8(unicode), |
| 4890 | PyUnicode_UTF8_LENGTH(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4891 | |
| 4892 | kind = PyUnicode_KIND(unicode); |
| 4893 | data = PyUnicode_DATA(unicode); |
| 4894 | size = PyUnicode_GET_LENGTH(unicode); |
| 4895 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 4896 | switch (kind) { |
Victor Stinner | 6099a03 | 2011-12-18 14:22:26 +0100 | [diff] [blame] | 4897 | default: |
| 4898 | assert(0); |
| 4899 | case PyUnicode_1BYTE_KIND: |
| 4900 | /* the string cannot be ASCII, or PyUnicode_UTF8() would be set */ |
| 4901 | assert(!PyUnicode_IS_ASCII(unicode)); |
| 4902 | return ucs1lib_utf8_encoder(unicode, data, size, errors); |
| 4903 | case PyUnicode_2BYTE_KIND: |
| 4904 | return ucs2lib_utf8_encoder(unicode, data, size, errors); |
| 4905 | case PyUnicode_4BYTE_KIND: |
| 4906 | return ucs4lib_utf8_encoder(unicode, data, size, errors); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4907 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4908 | } |
| 4909 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4910 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4911 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
| 4912 | Py_ssize_t size, |
| 4913 | const char *errors) |
| 4914 | { |
| 4915 | PyObject *v, *unicode; |
| 4916 | |
| 4917 | unicode = PyUnicode_FromUnicode(s, size); |
| 4918 | if (unicode == NULL) |
| 4919 | return NULL; |
| 4920 | v = _PyUnicode_AsUTF8String(unicode, errors); |
| 4921 | Py_DECREF(unicode); |
| 4922 | return v; |
| 4923 | } |
| 4924 | |
| 4925 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4926 | PyUnicode_AsUTF8String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4927 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4928 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4929 | } |
| 4930 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4931 | /* --- UTF-32 Codec ------------------------------------------------------- */ |
| 4932 | |
| 4933 | PyObject * |
| 4934 | PyUnicode_DecodeUTF32(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4935 | Py_ssize_t size, |
| 4936 | const char *errors, |
| 4937 | int *byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4938 | { |
| 4939 | return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); |
| 4940 | } |
| 4941 | |
| 4942 | PyObject * |
| 4943 | PyUnicode_DecodeUTF32Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4944 | Py_ssize_t size, |
| 4945 | const char *errors, |
| 4946 | int *byteorder, |
| 4947 | Py_ssize_t *consumed) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4948 | { |
| 4949 | const char *starts = s; |
| 4950 | Py_ssize_t startinpos; |
| 4951 | Py_ssize_t endinpos; |
| 4952 | Py_ssize_t outpos; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4953 | PyObject *unicode; |
Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 4954 | const unsigned char *q, *e; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4955 | int bo = 0; /* assume native ordering by default */ |
| 4956 | const char *errmsg = ""; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4957 | /* Offsets from q for retrieving bytes in the right order. */ |
| 4958 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 4959 | int iorder[] = {0, 1, 2, 3}; |
| 4960 | #else |
| 4961 | int iorder[] = {3, 2, 1, 0}; |
| 4962 | #endif |
| 4963 | PyObject *errorHandler = NULL; |
| 4964 | PyObject *exc = NULL; |
Victor Stinner | 313a120 | 2010-06-11 23:56:51 +0000 | [diff] [blame] | 4965 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4966 | q = (unsigned char *)s; |
| 4967 | e = q + size; |
| 4968 | |
| 4969 | if (byteorder) |
| 4970 | bo = *byteorder; |
| 4971 | |
| 4972 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 4973 | byte order setting accordingly. In native mode, the leading BOM |
| 4974 | mark is skipped, in all other modes, it is copied to the output |
| 4975 | stream as-is (giving a ZWNBSP character). */ |
| 4976 | if (bo == 0) { |
| 4977 | if (size >= 4) { |
| 4978 | const Py_UCS4 bom = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4979 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4980 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4981 | if (bom == 0x0000FEFF) { |
| 4982 | q += 4; |
| 4983 | bo = -1; |
| 4984 | } |
| 4985 | else if (bom == 0xFFFE0000) { |
| 4986 | q += 4; |
| 4987 | bo = 1; |
| 4988 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4989 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4990 | if (bom == 0x0000FEFF) { |
| 4991 | q += 4; |
| 4992 | bo = 1; |
| 4993 | } |
| 4994 | else if (bom == 0xFFFE0000) { |
| 4995 | q += 4; |
| 4996 | bo = -1; |
| 4997 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4998 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4999 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5000 | } |
| 5001 | |
| 5002 | if (bo == -1) { |
| 5003 | /* force LE */ |
| 5004 | iorder[0] = 0; |
| 5005 | iorder[1] = 1; |
| 5006 | iorder[2] = 2; |
| 5007 | iorder[3] = 3; |
| 5008 | } |
| 5009 | else if (bo == 1) { |
| 5010 | /* force BE */ |
| 5011 | iorder[0] = 3; |
| 5012 | iorder[1] = 2; |
| 5013 | iorder[2] = 1; |
| 5014 | iorder[3] = 0; |
| 5015 | } |
| 5016 | |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 5017 | /* This might be one to much, because of a BOM */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5018 | unicode = PyUnicode_New((size+3)/4, 127); |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 5019 | if (!unicode) |
| 5020 | return NULL; |
| 5021 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5022 | return unicode; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5023 | outpos = 0; |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 5024 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5025 | while (q < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5026 | Py_UCS4 ch; |
| 5027 | /* remaining bytes at the end? (size should be divisible by 4) */ |
| 5028 | if (e-q<4) { |
| 5029 | if (consumed) |
| 5030 | break; |
| 5031 | errmsg = "truncated data"; |
| 5032 | startinpos = ((const char *)q)-starts; |
| 5033 | endinpos = ((const char *)e)-starts; |
| 5034 | goto utf32Error; |
| 5035 | /* The remaining input chars are ignored if the callback |
| 5036 | chooses to skip the input */ |
| 5037 | } |
| 5038 | ch = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
| 5039 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5040 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5041 | if (ch >= 0x110000) |
| 5042 | { |
| 5043 | errmsg = "codepoint not in range(0x110000)"; |
| 5044 | startinpos = ((const char *)q)-starts; |
| 5045 | endinpos = startinpos+4; |
| 5046 | goto utf32Error; |
| 5047 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5048 | if (unicode_putchar(&unicode, &outpos, ch) < 0) |
| 5049 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5050 | q += 4; |
| 5051 | continue; |
| 5052 | utf32Error: |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5053 | if (unicode_decode_call_errorhandler( |
| 5054 | errors, &errorHandler, |
| 5055 | "utf32", errmsg, |
| 5056 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5057 | &unicode, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5058 | goto onError; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5059 | } |
| 5060 | |
| 5061 | if (byteorder) |
| 5062 | *byteorder = bo; |
| 5063 | |
| 5064 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5065 | *consumed = (const char *)q-starts; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5066 | |
| 5067 | /* Adjust length */ |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 5068 | if (unicode_resize(&unicode, outpos) < 0) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5069 | goto onError; |
| 5070 | |
| 5071 | Py_XDECREF(errorHandler); |
| 5072 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 5073 | return unicode_result(unicode); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5074 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5075 | onError: |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5076 | Py_DECREF(unicode); |
| 5077 | Py_XDECREF(errorHandler); |
| 5078 | Py_XDECREF(exc); |
| 5079 | return NULL; |
| 5080 | } |
| 5081 | |
| 5082 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5083 | _PyUnicode_EncodeUTF32(PyObject *str, |
| 5084 | const char *errors, |
| 5085 | int byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5086 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5087 | int kind; |
| 5088 | void *data; |
| 5089 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5090 | PyObject *v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5091 | unsigned char *p; |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 5092 | Py_ssize_t nsize, i; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5093 | /* Offsets from p for storing byte pairs in the right order. */ |
| 5094 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5095 | int iorder[] = {0, 1, 2, 3}; |
| 5096 | #else |
| 5097 | int iorder[] = {3, 2, 1, 0}; |
| 5098 | #endif |
| 5099 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5100 | #define STORECHAR(CH) \ |
| 5101 | do { \ |
| 5102 | p[iorder[3]] = ((CH) >> 24) & 0xff; \ |
| 5103 | p[iorder[2]] = ((CH) >> 16) & 0xff; \ |
| 5104 | p[iorder[1]] = ((CH) >> 8) & 0xff; \ |
| 5105 | p[iorder[0]] = (CH) & 0xff; \ |
| 5106 | p += 4; \ |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5107 | } while(0) |
| 5108 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5109 | if (!PyUnicode_Check(str)) { |
| 5110 | PyErr_BadArgument(); |
| 5111 | return NULL; |
| 5112 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 5113 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5114 | return NULL; |
| 5115 | kind = PyUnicode_KIND(str); |
| 5116 | data = PyUnicode_DATA(str); |
| 5117 | len = PyUnicode_GET_LENGTH(str); |
| 5118 | |
| 5119 | nsize = len + (byteorder == 0); |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 5120 | if (nsize > PY_SSIZE_T_MAX / 4) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5121 | return PyErr_NoMemory(); |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 5122 | v = PyBytes_FromStringAndSize(NULL, nsize * 4); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5123 | if (v == NULL) |
| 5124 | return NULL; |
| 5125 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5126 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5127 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5128 | STORECHAR(0xFEFF); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5129 | if (len == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5130 | goto done; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5131 | |
| 5132 | if (byteorder == -1) { |
| 5133 | /* force LE */ |
| 5134 | iorder[0] = 0; |
| 5135 | iorder[1] = 1; |
| 5136 | iorder[2] = 2; |
| 5137 | iorder[3] = 3; |
| 5138 | } |
| 5139 | else if (byteorder == 1) { |
| 5140 | /* force BE */ |
| 5141 | iorder[0] = 3; |
| 5142 | iorder[1] = 2; |
| 5143 | iorder[2] = 1; |
| 5144 | iorder[3] = 0; |
| 5145 | } |
| 5146 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5147 | for (i = 0; i < len; i++) |
| 5148 | STORECHAR(PyUnicode_READ(kind, data, i)); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5149 | |
| 5150 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5151 | return v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5152 | #undef STORECHAR |
| 5153 | } |
| 5154 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5155 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5156 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
| 5157 | Py_ssize_t size, |
| 5158 | const char *errors, |
| 5159 | int byteorder) |
| 5160 | { |
| 5161 | PyObject *result; |
| 5162 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5163 | if (tmp == NULL) |
| 5164 | return NULL; |
| 5165 | result = _PyUnicode_EncodeUTF32(tmp, errors, byteorder); |
| 5166 | Py_DECREF(tmp); |
| 5167 | return result; |
| 5168 | } |
| 5169 | |
| 5170 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5171 | PyUnicode_AsUTF32String(PyObject *unicode) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5172 | { |
Victor Stinner | b960b34 | 2011-11-20 19:12:52 +0100 | [diff] [blame] | 5173 | return _PyUnicode_EncodeUTF32(unicode, NULL, 0); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5174 | } |
| 5175 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5176 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 5177 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5178 | PyObject * |
| 5179 | PyUnicode_DecodeUTF16(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5180 | Py_ssize_t size, |
| 5181 | const char *errors, |
| 5182 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5183 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5184 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 5185 | } |
| 5186 | |
| 5187 | PyObject * |
| 5188 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5189 | Py_ssize_t size, |
| 5190 | const char *errors, |
| 5191 | int *byteorder, |
| 5192 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5193 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5194 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5195 | Py_ssize_t startinpos; |
| 5196 | Py_ssize_t endinpos; |
| 5197 | Py_ssize_t outpos; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5198 | PyObject *unicode; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5199 | const unsigned char *q, *e; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5200 | int bo = 0; /* assume native ordering by default */ |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5201 | int native_ordering; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 5202 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5203 | PyObject *errorHandler = NULL; |
| 5204 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5205 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5206 | q = (unsigned char *)s; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5207 | e = q + size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5208 | |
| 5209 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5210 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5211 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5212 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 5213 | byte order setting accordingly. In native mode, the leading BOM |
| 5214 | mark is skipped, in all other modes, it is copied to the output |
| 5215 | stream as-is (giving a ZWNBSP character). */ |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5216 | if (bo == 0 && size >= 2) { |
| 5217 | const Py_UCS4 bom = (q[1] << 8) | q[0]; |
| 5218 | if (bom == 0xFEFF) { |
| 5219 | q += 2; |
| 5220 | bo = -1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5221 | } |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5222 | else if (bom == 0xFFFE) { |
| 5223 | q += 2; |
| 5224 | bo = 1; |
| 5225 | } |
| 5226 | if (byteorder) |
| 5227 | *byteorder = bo; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5228 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5229 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5230 | if (q == e) { |
| 5231 | if (consumed) |
| 5232 | *consumed = size; |
| 5233 | Py_INCREF(unicode_empty); |
| 5234 | return unicode_empty; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5235 | } |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5236 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5237 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5238 | native_ordering = bo <= 0; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5239 | #else |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5240 | native_ordering = bo >= 0; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5241 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5242 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5243 | /* Note: size will always be longer than the resulting Unicode |
| 5244 | character count */ |
| 5245 | unicode = PyUnicode_New((e - q + 1) / 2, 127); |
| 5246 | if (!unicode) |
| 5247 | return NULL; |
| 5248 | |
| 5249 | outpos = 0; |
| 5250 | while (1) { |
| 5251 | Py_UCS4 ch = 0; |
| 5252 | if (e - q >= 2) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5253 | int kind = PyUnicode_KIND(unicode); |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5254 | if (kind == PyUnicode_1BYTE_KIND) { |
| 5255 | if (PyUnicode_IS_ASCII(unicode)) |
| 5256 | ch = asciilib_utf16_decode(&q, e, |
| 5257 | PyUnicode_1BYTE_DATA(unicode), &outpos, |
| 5258 | native_ordering); |
| 5259 | else |
| 5260 | ch = ucs1lib_utf16_decode(&q, e, |
| 5261 | PyUnicode_1BYTE_DATA(unicode), &outpos, |
| 5262 | native_ordering); |
| 5263 | } else if (kind == PyUnicode_2BYTE_KIND) { |
| 5264 | ch = ucs2lib_utf16_decode(&q, e, |
| 5265 | PyUnicode_2BYTE_DATA(unicode), &outpos, |
| 5266 | native_ordering); |
| 5267 | } else { |
| 5268 | assert(kind == PyUnicode_4BYTE_KIND); |
| 5269 | ch = ucs4lib_utf16_decode(&q, e, |
| 5270 | PyUnicode_4BYTE_DATA(unicode), &outpos, |
| 5271 | native_ordering); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5272 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5273 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5274 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5275 | switch (ch) |
| 5276 | { |
| 5277 | case 0: |
| 5278 | /* remaining byte at the end? (size should be even) */ |
| 5279 | if (q == e || consumed) |
| 5280 | goto End; |
| 5281 | errmsg = "truncated data"; |
| 5282 | startinpos = ((const char *)q) - starts; |
| 5283 | endinpos = ((const char *)e) - starts; |
| 5284 | break; |
| 5285 | /* The remaining input chars are ignored if the callback |
| 5286 | chooses to skip the input */ |
| 5287 | case 1: |
| 5288 | errmsg = "unexpected end of data"; |
| 5289 | startinpos = ((const char *)q) - 2 - starts; |
| 5290 | endinpos = ((const char *)e) - starts; |
| 5291 | break; |
| 5292 | case 2: |
| 5293 | errmsg = "illegal encoding"; |
| 5294 | startinpos = ((const char *)q) - 2 - starts; |
| 5295 | endinpos = startinpos + 2; |
| 5296 | break; |
| 5297 | case 3: |
| 5298 | errmsg = "illegal UTF-16 surrogate"; |
| 5299 | startinpos = ((const char *)q) - 4 - starts; |
| 5300 | endinpos = startinpos + 2; |
| 5301 | break; |
| 5302 | default: |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5303 | if (unicode_putchar(&unicode, &outpos, ch) < 0) |
| 5304 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5305 | continue; |
| 5306 | } |
| 5307 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5308 | if (unicode_decode_call_errorhandler( |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5309 | errors, |
| 5310 | &errorHandler, |
| 5311 | "utf16", errmsg, |
| 5312 | &starts, |
| 5313 | (const char **)&e, |
| 5314 | &startinpos, |
| 5315 | &endinpos, |
| 5316 | &exc, |
| 5317 | (const char **)&q, |
| 5318 | &unicode, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5319 | &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5320 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5321 | } |
| 5322 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5323 | End: |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5324 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5325 | *consumed = (const char *)q-starts; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5326 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5327 | /* Adjust length */ |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 5328 | if (unicode_resize(&unicode, outpos) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5329 | goto onError; |
| 5330 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5331 | Py_XDECREF(errorHandler); |
| 5332 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 5333 | return unicode_result(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5334 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5335 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5336 | Py_DECREF(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5337 | Py_XDECREF(errorHandler); |
| 5338 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5339 | return NULL; |
| 5340 | } |
| 5341 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5342 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5343 | _PyUnicode_EncodeUTF16(PyObject *str, |
| 5344 | const char *errors, |
| 5345 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5346 | { |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5347 | enum PyUnicode_Kind kind; |
| 5348 | const void *data; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5349 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5350 | PyObject *v; |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5351 | unsigned short *out; |
| 5352 | Py_ssize_t bytesize; |
| 5353 | Py_ssize_t pairs; |
| 5354 | #ifdef WORDS_BIGENDIAN |
| 5355 | int native_ordering = byteorder >= 0; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5356 | #else |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5357 | int native_ordering = byteorder <= 0; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5358 | #endif |
| 5359 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5360 | if (!PyUnicode_Check(str)) { |
| 5361 | PyErr_BadArgument(); |
| 5362 | return NULL; |
| 5363 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 5364 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5365 | return NULL; |
| 5366 | kind = PyUnicode_KIND(str); |
| 5367 | data = PyUnicode_DATA(str); |
| 5368 | len = PyUnicode_GET_LENGTH(str); |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 5369 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5370 | pairs = 0; |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5371 | if (kind == PyUnicode_4BYTE_KIND) { |
| 5372 | const Py_UCS4 *in = (const Py_UCS4 *)data; |
| 5373 | const Py_UCS4 *end = in + len; |
| 5374 | while (in < end) |
| 5375 | if (*in++ >= 0x10000) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5376 | pairs++; |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5377 | } |
| 5378 | if (len > PY_SSIZE_T_MAX / 2 - pairs - (byteorder == 0)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5379 | return PyErr_NoMemory(); |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5380 | bytesize = (len + pairs + (byteorder == 0)) * 2; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5381 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5382 | if (v == NULL) |
| 5383 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5384 | |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5385 | /* output buffer is 2-bytes aligned */ |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 5386 | assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(v), 2)); |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5387 | out = (unsigned short *)PyBytes_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5388 | if (byteorder == 0) |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5389 | *out++ = 0xFEFF; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5390 | if (len == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5391 | goto done; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5392 | |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5393 | switch (kind) { |
| 5394 | case PyUnicode_1BYTE_KIND: { |
| 5395 | ucs1lib_utf16_encode(out, (const Py_UCS1 *)data, len, native_ordering); |
| 5396 | break; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5397 | } |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5398 | case PyUnicode_2BYTE_KIND: { |
| 5399 | ucs2lib_utf16_encode(out, (const Py_UCS2 *)data, len, native_ordering); |
| 5400 | break; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5401 | } |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5402 | case PyUnicode_4BYTE_KIND: { |
| 5403 | ucs4lib_utf16_encode(out, (const Py_UCS4 *)data, len, native_ordering); |
| 5404 | break; |
| 5405 | } |
| 5406 | default: |
| 5407 | assert(0); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5408 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5409 | |
| 5410 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5411 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5412 | } |
| 5413 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5414 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5415 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
| 5416 | Py_ssize_t size, |
| 5417 | const char *errors, |
| 5418 | int byteorder) |
| 5419 | { |
| 5420 | PyObject *result; |
| 5421 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5422 | if (tmp == NULL) |
| 5423 | return NULL; |
| 5424 | result = _PyUnicode_EncodeUTF16(tmp, errors, byteorder); |
| 5425 | Py_DECREF(tmp); |
| 5426 | return result; |
| 5427 | } |
| 5428 | |
| 5429 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5430 | PyUnicode_AsUTF16String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5431 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5432 | return _PyUnicode_EncodeUTF16(unicode, NULL, 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5433 | } |
| 5434 | |
| 5435 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 5436 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5437 | /* Helper function for PyUnicode_DecodeUnicodeEscape, determines |
| 5438 | if all the escapes in the string make it still a valid ASCII string. |
| 5439 | Returns -1 if any escapes were found which cause the string to |
| 5440 | pop out of ASCII range. Otherwise returns the length of the |
| 5441 | required buffer to hold the string. |
| 5442 | */ |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 5443 | static Py_ssize_t |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5444 | length_of_escaped_ascii_string(const char *s, Py_ssize_t size) |
| 5445 | { |
| 5446 | const unsigned char *p = (const unsigned char *)s; |
| 5447 | const unsigned char *end = p + size; |
| 5448 | Py_ssize_t length = 0; |
| 5449 | |
| 5450 | if (size < 0) |
| 5451 | return -1; |
| 5452 | |
| 5453 | for (; p < end; ++p) { |
| 5454 | if (*p > 127) { |
| 5455 | /* Non-ASCII */ |
| 5456 | return -1; |
| 5457 | } |
| 5458 | else if (*p != '\\') { |
| 5459 | /* Normal character */ |
| 5460 | ++length; |
| 5461 | } |
| 5462 | else { |
| 5463 | /* Backslash-escape, check next char */ |
| 5464 | ++p; |
| 5465 | /* Escape sequence reaches till end of string or |
| 5466 | non-ASCII follow-up. */ |
| 5467 | if (p >= end || *p > 127) |
| 5468 | return -1; |
| 5469 | switch (*p) { |
| 5470 | case '\n': |
| 5471 | /* backslash + \n result in zero characters */ |
| 5472 | break; |
| 5473 | case '\\': case '\'': case '\"': |
| 5474 | case 'b': case 'f': case 't': |
| 5475 | case 'n': case 'r': case 'v': case 'a': |
| 5476 | ++length; |
| 5477 | break; |
| 5478 | case '0': case '1': case '2': case '3': |
| 5479 | case '4': case '5': case '6': case '7': |
| 5480 | case 'x': case 'u': case 'U': case 'N': |
| 5481 | /* these do not guarantee ASCII characters */ |
| 5482 | return -1; |
| 5483 | default: |
| 5484 | /* count the backslash + the other character */ |
| 5485 | length += 2; |
| 5486 | } |
| 5487 | } |
| 5488 | } |
| 5489 | return length; |
| 5490 | } |
| 5491 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5492 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 5493 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5494 | PyObject * |
| 5495 | PyUnicode_DecodeUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5496 | Py_ssize_t size, |
Victor Stinner | c17f540 | 2011-09-29 00:16:58 +0200 | [diff] [blame] | 5497 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5498 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5499 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5500 | Py_ssize_t startinpos; |
| 5501 | Py_ssize_t endinpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5502 | int j; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5503 | PyObject *v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5504 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5505 | char* message; |
| 5506 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5507 | PyObject *errorHandler = NULL; |
| 5508 | PyObject *exc = NULL; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5509 | Py_ssize_t len; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5510 | Py_ssize_t i; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5511 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5512 | len = length_of_escaped_ascii_string(s, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5513 | |
| 5514 | /* After length_of_escaped_ascii_string() there are two alternatives, |
| 5515 | either the string is pure ASCII with named escapes like \n, etc. |
| 5516 | and we determined it's exact size (common case) |
| 5517 | or it contains \x, \u, ... escape sequences. then we create a |
| 5518 | legacy wchar string and resize it at the end of this function. */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5519 | if (len >= 0) { |
| 5520 | v = PyUnicode_New(len, 127); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5521 | if (!v) |
| 5522 | goto onError; |
| 5523 | assert(PyUnicode_KIND(v) == PyUnicode_1BYTE_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5524 | } |
| 5525 | else { |
| 5526 | /* Escaped strings will always be longer than the resulting |
| 5527 | Unicode string, so we start with size here and then reduce the |
| 5528 | length after conversion to the true value. |
| 5529 | (but if the error callback returns a long replacement string |
| 5530 | we'll have to allocate more space) */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5531 | v = PyUnicode_New(size, 127); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5532 | if (!v) |
| 5533 | goto onError; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5534 | len = size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5535 | } |
| 5536 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5537 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5538 | return v; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5539 | i = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5540 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5541 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5542 | while (s < end) { |
| 5543 | unsigned char c; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 5544 | Py_UCS4 x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5545 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5546 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5547 | /* The only case in which i == ascii_length is a backslash |
| 5548 | followed by a newline. */ |
| 5549 | assert(i <= len); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5550 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5551 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 5552 | if (*s != '\\') { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5553 | if (unicode_putchar(&v, &i, (unsigned char) *s++) < 0) |
| 5554 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5555 | continue; |
| 5556 | } |
| 5557 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5558 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5559 | /* \ - Escapes */ |
| 5560 | s++; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5561 | c = *s++; |
| 5562 | if (s > end) |
| 5563 | c = '\0'; /* Invalid after \ */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5564 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5565 | /* The only case in which i == ascii_length is a backslash |
| 5566 | followed by a newline. */ |
| 5567 | assert(i < len || (i == len && c == '\n')); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5568 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5569 | switch (c) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5570 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5571 | /* \x escapes */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5572 | #define WRITECHAR(ch) \ |
| 5573 | do { \ |
| 5574 | if (unicode_putchar(&v, &i, ch) < 0) \ |
| 5575 | goto onError; \ |
| 5576 | }while(0) |
| 5577 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5578 | case '\n': break; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5579 | case '\\': WRITECHAR('\\'); break; |
| 5580 | case '\'': WRITECHAR('\''); break; |
| 5581 | case '\"': WRITECHAR('\"'); break; |
| 5582 | case 'b': WRITECHAR('\b'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5583 | /* FF */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5584 | case 'f': WRITECHAR('\014'); break; |
| 5585 | case 't': WRITECHAR('\t'); break; |
| 5586 | case 'n': WRITECHAR('\n'); break; |
| 5587 | case 'r': WRITECHAR('\r'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5588 | /* VT */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5589 | case 'v': WRITECHAR('\013'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5590 | /* BEL, not classic C */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5591 | case 'a': WRITECHAR('\007'); break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5592 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5593 | /* \OOO (octal) escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5594 | case '0': case '1': case '2': case '3': |
| 5595 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5596 | x = s[-1] - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5597 | if (s < end && '0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5598 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5599 | if (s < end && '0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5600 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5601 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5602 | WRITECHAR(x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5603 | break; |
| 5604 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5605 | /* hex escapes */ |
| 5606 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5607 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5608 | digits = 2; |
| 5609 | message = "truncated \\xXX escape"; |
| 5610 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5611 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5612 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5613 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5614 | digits = 4; |
| 5615 | message = "truncated \\uXXXX escape"; |
| 5616 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5617 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5618 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5619 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5620 | digits = 8; |
| 5621 | message = "truncated \\UXXXXXXXX escape"; |
| 5622 | hexescape: |
| 5623 | chr = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5624 | if (s+digits>end) { |
| 5625 | endinpos = size; |
| 5626 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5627 | errors, &errorHandler, |
| 5628 | "unicodeescape", "end of string in escape sequence", |
| 5629 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5630 | &v, &i)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5631 | goto onError; |
| 5632 | goto nextByte; |
| 5633 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5634 | for (j = 0; j < digits; ++j) { |
| 5635 | c = (unsigned char) s[j]; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 5636 | if (!Py_ISXDIGIT(c)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5637 | endinpos = (s+j+1)-starts; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5638 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5639 | errors, &errorHandler, |
| 5640 | "unicodeescape", message, |
| 5641 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5642 | &v, &i)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5643 | goto onError; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5644 | len = PyUnicode_GET_LENGTH(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5645 | goto nextByte; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5646 | } |
| 5647 | chr = (chr<<4) & ~0xF; |
| 5648 | if (c >= '0' && c <= '9') |
| 5649 | chr += c - '0'; |
| 5650 | else if (c >= 'a' && c <= 'f') |
| 5651 | chr += 10 + c - 'a'; |
| 5652 | else |
| 5653 | chr += 10 + c - 'A'; |
| 5654 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5655 | s += j; |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 5656 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5657 | /* _decoding_error will have already written into the |
| 5658 | target buffer. */ |
| 5659 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5660 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5661 | /* when we get here, chr is a 32-bit unicode character */ |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 5662 | if (chr <= MAX_UNICODE) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5663 | WRITECHAR(chr); |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5664 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5665 | endinpos = s-starts; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5666 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5667 | errors, &errorHandler, |
| 5668 | "unicodeescape", "illegal Unicode character", |
| 5669 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5670 | &v, &i)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5671 | goto onError; |
| 5672 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5673 | break; |
| 5674 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5675 | /* \N{name} */ |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5676 | case 'N': |
| 5677 | message = "malformed \\N character escape"; |
| 5678 | if (ucnhash_CAPI == NULL) { |
| 5679 | /* load the unicode data module */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5680 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCapsule_Import( |
| 5681 | PyUnicodeData_CAPSULE_NAME, 1); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5682 | if (ucnhash_CAPI == NULL) |
| 5683 | goto ucnhashError; |
| 5684 | } |
| 5685 | if (*s == '{') { |
| 5686 | const char *start = s+1; |
| 5687 | /* look for the closing brace */ |
| 5688 | while (*s != '}' && s < end) |
| 5689 | s++; |
| 5690 | if (s > start && s < end && *s == '}') { |
| 5691 | /* found a name. look it up in the unicode database */ |
| 5692 | message = "unknown Unicode character name"; |
| 5693 | s++; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5694 | if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), |
Ezio Melotti | 931b8aa | 2011-10-21 21:57:36 +0300 | [diff] [blame] | 5695 | &chr, 0)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5696 | goto store; |
| 5697 | } |
| 5698 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5699 | endinpos = s-starts; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5700 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5701 | errors, &errorHandler, |
| 5702 | "unicodeescape", message, |
| 5703 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5704 | &v, &i)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5705 | goto onError; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5706 | break; |
| 5707 | |
| 5708 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5709 | if (s > end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5710 | message = "\\ at end of string"; |
| 5711 | s--; |
| 5712 | endinpos = s-starts; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5713 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5714 | errors, &errorHandler, |
| 5715 | "unicodeescape", message, |
| 5716 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5717 | &v, &i)) |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5718 | goto onError; |
| 5719 | } |
| 5720 | else { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5721 | WRITECHAR('\\'); |
| 5722 | WRITECHAR(s[-1]); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5723 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5724 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5725 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5726 | nextByte: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5727 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5728 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5729 | #undef WRITECHAR |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5730 | |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 5731 | if (unicode_resize(&v, i) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5732 | goto onError; |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 5733 | Py_XDECREF(errorHandler); |
| 5734 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 5735 | return unicode_result(v); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5736 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5737 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5738 | PyErr_SetString( |
| 5739 | PyExc_UnicodeError, |
| 5740 | "\\N escapes not supported (can't load unicodedata module)" |
| 5741 | ); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 5742 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5743 | Py_XDECREF(errorHandler); |
| 5744 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 5745 | return NULL; |
| 5746 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5747 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5748 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5749 | Py_XDECREF(errorHandler); |
| 5750 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5751 | return NULL; |
| 5752 | } |
| 5753 | |
| 5754 | /* Return a Unicode-Escape string version of the Unicode object. |
| 5755 | |
| 5756 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 5757 | appropriate. |
| 5758 | |
| 5759 | */ |
| 5760 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5761 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5762 | PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5763 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5764 | Py_ssize_t i, len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5765 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5766 | char *p; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5767 | int kind; |
| 5768 | void *data; |
| 5769 | Py_ssize_t expandsize = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5770 | |
Ezio Melotti | e7f9037 | 2012-10-05 03:33:31 +0300 | [diff] [blame] | 5771 | /* Initial allocation is based on the longest-possible character |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5772 | escape. |
| 5773 | |
Ezio Melotti | e7f9037 | 2012-10-05 03:33:31 +0300 | [diff] [blame] | 5774 | For UCS1 strings it's '\xxx', 4 bytes per source character. |
| 5775 | For UCS2 strings it's '\uxxxx', 6 bytes per source character. |
| 5776 | For UCS4 strings it's '\U00xxxxxx', 10 bytes per source character. |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5777 | */ |
| 5778 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5779 | if (!PyUnicode_Check(unicode)) { |
| 5780 | PyErr_BadArgument(); |
| 5781 | return NULL; |
| 5782 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 5783 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5784 | return NULL; |
| 5785 | len = PyUnicode_GET_LENGTH(unicode); |
| 5786 | kind = PyUnicode_KIND(unicode); |
| 5787 | data = PyUnicode_DATA(unicode); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 5788 | switch (kind) { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5789 | case PyUnicode_1BYTE_KIND: expandsize = 4; break; |
| 5790 | case PyUnicode_2BYTE_KIND: expandsize = 6; break; |
| 5791 | case PyUnicode_4BYTE_KIND: expandsize = 10; break; |
| 5792 | } |
| 5793 | |
| 5794 | if (len == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5795 | return PyBytes_FromStringAndSize(NULL, 0); |
| 5796 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5797 | if (len > (PY_SSIZE_T_MAX - 2 - 1) / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5798 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5799 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5800 | repr = PyBytes_FromStringAndSize(NULL, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5801 | 2 |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5802 | + expandsize*len |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5803 | + 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5804 | if (repr == NULL) |
| 5805 | return NULL; |
| 5806 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5807 | p = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5808 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5809 | for (i = 0; i < len; i++) { |
Victor Stinner | 3326cb6 | 2011-11-10 20:15:25 +0100 | [diff] [blame] | 5810 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5811 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5812 | /* Escape backslashes */ |
| 5813 | if (ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5814 | *p++ = '\\'; |
| 5815 | *p++ = (char) ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5816 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5817 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5818 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5819 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 5820 | else if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 5821 | assert(ch <= MAX_UNICODE); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5822 | *p++ = '\\'; |
| 5823 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 5824 | *p++ = Py_hexdigits[(ch >> 28) & 0x0000000F]; |
| 5825 | *p++ = Py_hexdigits[(ch >> 24) & 0x0000000F]; |
| 5826 | *p++ = Py_hexdigits[(ch >> 20) & 0x0000000F]; |
| 5827 | *p++ = Py_hexdigits[(ch >> 16) & 0x0000000F]; |
| 5828 | *p++ = Py_hexdigits[(ch >> 12) & 0x0000000F]; |
| 5829 | *p++ = Py_hexdigits[(ch >> 8) & 0x0000000F]; |
| 5830 | *p++ = Py_hexdigits[(ch >> 4) & 0x0000000F]; |
| 5831 | *p++ = Py_hexdigits[ch & 0x0000000F]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5832 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5833 | } |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5834 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5835 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5836 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5837 | *p++ = '\\'; |
| 5838 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 5839 | *p++ = Py_hexdigits[(ch >> 12) & 0x000F]; |
| 5840 | *p++ = Py_hexdigits[(ch >> 8) & 0x000F]; |
| 5841 | *p++ = Py_hexdigits[(ch >> 4) & 0x000F]; |
| 5842 | *p++ = Py_hexdigits[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5843 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5844 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5845 | /* Map special whitespace to '\t', \n', '\r' */ |
| 5846 | else if (ch == '\t') { |
| 5847 | *p++ = '\\'; |
| 5848 | *p++ = 't'; |
| 5849 | } |
| 5850 | else if (ch == '\n') { |
| 5851 | *p++ = '\\'; |
| 5852 | *p++ = 'n'; |
| 5853 | } |
| 5854 | else if (ch == '\r') { |
| 5855 | *p++ = '\\'; |
| 5856 | *p++ = 'r'; |
| 5857 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5858 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5859 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 5860 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5861 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5862 | *p++ = 'x'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 5863 | *p++ = Py_hexdigits[(ch >> 4) & 0x000F]; |
| 5864 | *p++ = Py_hexdigits[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5865 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5866 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5867 | /* Copy everything else as-is */ |
| 5868 | else |
| 5869 | *p++ = (char) ch; |
| 5870 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5871 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5872 | assert(p - PyBytes_AS_STRING(repr) > 0); |
| 5873 | if (_PyBytes_Resize(&repr, p - PyBytes_AS_STRING(repr)) < 0) |
| 5874 | return NULL; |
| 5875 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5876 | } |
| 5877 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5878 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5879 | PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
| 5880 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5881 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5882 | PyObject *result; |
| 5883 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5884 | if (tmp == NULL) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5885 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5886 | result = PyUnicode_AsUnicodeEscapeString(tmp); |
| 5887 | Py_DECREF(tmp); |
| 5888 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5889 | } |
| 5890 | |
| 5891 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 5892 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5893 | PyObject * |
| 5894 | PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5895 | Py_ssize_t size, |
| 5896 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5897 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5898 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5899 | Py_ssize_t startinpos; |
| 5900 | Py_ssize_t endinpos; |
| 5901 | Py_ssize_t outpos; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5902 | PyObject *v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5903 | const char *end; |
| 5904 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5905 | PyObject *errorHandler = NULL; |
| 5906 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5907 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5908 | /* Escaped strings will always be longer than the resulting |
| 5909 | 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] | 5910 | length after conversion to the true value. (But decoding error |
| 5911 | handler might have to resize the string) */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5912 | v = PyUnicode_New(size, 127); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5913 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5914 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5915 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5916 | return v; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5917 | outpos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5918 | end = s + size; |
| 5919 | while (s < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5920 | unsigned char c; |
| 5921 | Py_UCS4 x; |
| 5922 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5923 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5924 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5925 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 5926 | if (*s != '\\') { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5927 | if (unicode_putchar(&v, &outpos, (unsigned char)*s++) < 0) |
| 5928 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5929 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5930 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5931 | startinpos = s-starts; |
| 5932 | |
| 5933 | /* \u-escapes are only interpreted iff the number of leading |
| 5934 | backslashes if odd */ |
| 5935 | bs = s; |
| 5936 | for (;s < end;) { |
| 5937 | if (*s != '\\') |
| 5938 | break; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5939 | if (unicode_putchar(&v, &outpos, (unsigned char)*s++) < 0) |
| 5940 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5941 | } |
| 5942 | if (((s - bs) & 1) == 0 || |
| 5943 | s >= end || |
| 5944 | (*s != 'u' && *s != 'U')) { |
| 5945 | continue; |
| 5946 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5947 | outpos--; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5948 | count = *s=='u' ? 4 : 8; |
| 5949 | s++; |
| 5950 | |
| 5951 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5952 | for (x = 0, i = 0; i < count; ++i, ++s) { |
| 5953 | c = (unsigned char)*s; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 5954 | if (!Py_ISXDIGIT(c)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5955 | endinpos = s-starts; |
| 5956 | if (unicode_decode_call_errorhandler( |
| 5957 | errors, &errorHandler, |
| 5958 | "rawunicodeescape", "truncated \\uXXXX", |
| 5959 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5960 | &v, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5961 | goto onError; |
| 5962 | goto nextByte; |
| 5963 | } |
| 5964 | x = (x<<4) & ~0xF; |
| 5965 | if (c >= '0' && c <= '9') |
| 5966 | x += c - '0'; |
| 5967 | else if (c >= 'a' && c <= 'f') |
| 5968 | x += 10 + c - 'a'; |
| 5969 | else |
| 5970 | x += 10 + c - 'A'; |
| 5971 | } |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 5972 | if (x <= MAX_UNICODE) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5973 | if (unicode_putchar(&v, &outpos, x) < 0) |
| 5974 | goto onError; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 5975 | } else { |
| 5976 | endinpos = s-starts; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5977 | if (unicode_decode_call_errorhandler( |
| 5978 | errors, &errorHandler, |
| 5979 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5980 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5981 | &v, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5982 | goto onError; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5983 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5984 | nextByte: |
| 5985 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5986 | } |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 5987 | if (unicode_resize(&v, outpos) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5988 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5989 | Py_XDECREF(errorHandler); |
| 5990 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 5991 | return unicode_result(v); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5992 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5993 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5994 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5995 | Py_XDECREF(errorHandler); |
| 5996 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5997 | return NULL; |
| 5998 | } |
| 5999 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6000 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6001 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6002 | PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6003 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6004 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6005 | char *p; |
| 6006 | char *q; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6007 | Py_ssize_t expandsize, pos; |
| 6008 | int kind; |
| 6009 | void *data; |
| 6010 | Py_ssize_t len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6011 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6012 | if (!PyUnicode_Check(unicode)) { |
| 6013 | PyErr_BadArgument(); |
| 6014 | return NULL; |
| 6015 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6016 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6017 | return NULL; |
| 6018 | kind = PyUnicode_KIND(unicode); |
| 6019 | data = PyUnicode_DATA(unicode); |
| 6020 | len = PyUnicode_GET_LENGTH(unicode); |
Benjamin Peterson | 1518e87 | 2011-11-23 10:44:52 -0600 | [diff] [blame] | 6021 | /* 4 byte characters can take up 10 bytes, 2 byte characters can take up 6 |
| 6022 | bytes, and 1 byte characters 4. */ |
| 6023 | expandsize = kind * 2 + 2; |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 6024 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6025 | if (len > PY_SSIZE_T_MAX / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6026 | return PyErr_NoMemory(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6027 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6028 | repr = PyBytes_FromStringAndSize(NULL, expandsize * len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6029 | if (repr == NULL) |
| 6030 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6031 | if (len == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6032 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6033 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6034 | p = q = PyBytes_AS_STRING(repr); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6035 | for (pos = 0; pos < len; pos++) { |
| 6036 | Py_UCS4 ch = PyUnicode_READ(kind, data, pos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6037 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 6038 | if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 6039 | assert(ch <= MAX_UNICODE); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6040 | *p++ = '\\'; |
| 6041 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6042 | *p++ = Py_hexdigits[(ch >> 28) & 0xf]; |
| 6043 | *p++ = Py_hexdigits[(ch >> 24) & 0xf]; |
| 6044 | *p++ = Py_hexdigits[(ch >> 20) & 0xf]; |
| 6045 | *p++ = Py_hexdigits[(ch >> 16) & 0xf]; |
| 6046 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 6047 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 6048 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 6049 | *p++ = Py_hexdigits[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6050 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6051 | /* Map 16-bit characters to '\uxxxx' */ |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6052 | else if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6053 | *p++ = '\\'; |
| 6054 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6055 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 6056 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 6057 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 6058 | *p++ = Py_hexdigits[ch & 15]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6059 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6060 | /* Copy everything else as-is */ |
| 6061 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6062 | *p++ = (char) ch; |
| 6063 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6064 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6065 | assert(p > q); |
| 6066 | if (_PyBytes_Resize(&repr, p - q) < 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6067 | return NULL; |
| 6068 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6069 | } |
| 6070 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6071 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6072 | PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
| 6073 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6074 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6075 | PyObject *result; |
| 6076 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 6077 | if (tmp == NULL) |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 6078 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6079 | result = PyUnicode_AsRawUnicodeEscapeString(tmp); |
| 6080 | Py_DECREF(tmp); |
| 6081 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6082 | } |
| 6083 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6084 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 6085 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6086 | PyObject * |
| 6087 | _PyUnicode_DecodeUnicodeInternal(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6088 | Py_ssize_t size, |
| 6089 | const char *errors) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6090 | { |
| 6091 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6092 | Py_ssize_t startinpos; |
| 6093 | Py_ssize_t endinpos; |
| 6094 | Py_ssize_t outpos; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6095 | PyObject *v; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6096 | const char *end; |
| 6097 | const char *reason; |
| 6098 | PyObject *errorHandler = NULL; |
| 6099 | PyObject *exc = NULL; |
| 6100 | |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6101 | if (PyErr_WarnEx(PyExc_DeprecationWarning, |
Ezio Melotti | 11060a4 | 2011-11-16 09:39:10 +0200 | [diff] [blame] | 6102 | "unicode_internal codec has been deprecated", |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6103 | 1)) |
| 6104 | return NULL; |
| 6105 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6106 | /* XXX overflow detection missing */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6107 | v = PyUnicode_New((size+Py_UNICODE_SIZE-1)/ Py_UNICODE_SIZE, 127); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6108 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6109 | goto onError; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6110 | if (PyUnicode_GET_LENGTH(v) == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6111 | return v; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6112 | outpos = 0; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6113 | end = s + size; |
| 6114 | |
| 6115 | while (s < end) { |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6116 | Py_UNICODE uch; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6117 | Py_UCS4 ch; |
| 6118 | /* We copy the raw representation one byte at a time because the |
| 6119 | pointer may be unaligned (see test_codeccallbacks). */ |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6120 | ((char *) &uch)[0] = s[0]; |
| 6121 | ((char *) &uch)[1] = s[1]; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6122 | #ifdef Py_UNICODE_WIDE |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6123 | ((char *) &uch)[2] = s[2]; |
| 6124 | ((char *) &uch)[3] = s[3]; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6125 | #endif |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6126 | ch = uch; |
| 6127 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6128 | /* We have to sanity check the raw data, otherwise doom looms for |
| 6129 | some malformed UCS-4 data. */ |
| 6130 | if ( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6131 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6132 | ch > 0x10ffff || |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6133 | #endif |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6134 | end-s < Py_UNICODE_SIZE |
| 6135 | ) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6136 | { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6137 | startinpos = s - starts; |
| 6138 | if (end-s < Py_UNICODE_SIZE) { |
| 6139 | endinpos = end-starts; |
| 6140 | reason = "truncated input"; |
| 6141 | } |
| 6142 | else { |
| 6143 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 6144 | reason = "illegal code point (> 0x10FFFF)"; |
| 6145 | } |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6146 | if (unicode_decode_call_errorhandler( |
| 6147 | errors, &errorHandler, |
| 6148 | "unicode_internal", reason, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 6149 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6150 | &v, &outpos)) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6151 | goto onError; |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6152 | continue; |
| 6153 | } |
| 6154 | |
| 6155 | s += Py_UNICODE_SIZE; |
| 6156 | #ifndef Py_UNICODE_WIDE |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 6157 | if (Py_UNICODE_IS_HIGH_SURROGATE(ch) && s < end) |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6158 | { |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6159 | Py_UNICODE uch2; |
| 6160 | ((char *) &uch2)[0] = s[0]; |
| 6161 | ((char *) &uch2)[1] = s[1]; |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 6162 | if (Py_UNICODE_IS_LOW_SURROGATE(uch2)) |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6163 | { |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 6164 | ch = Py_UNICODE_JOIN_SURROGATES(uch, uch2); |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6165 | s += Py_UNICODE_SIZE; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6166 | } |
| 6167 | } |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6168 | #endif |
| 6169 | |
| 6170 | if (unicode_putchar(&v, &outpos, ch) < 0) |
| 6171 | goto onError; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6172 | } |
| 6173 | |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 6174 | if (unicode_resize(&v, outpos) < 0) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6175 | goto onError; |
| 6176 | Py_XDECREF(errorHandler); |
| 6177 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 6178 | return unicode_result(v); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6179 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6180 | onError: |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6181 | Py_XDECREF(v); |
| 6182 | Py_XDECREF(errorHandler); |
| 6183 | Py_XDECREF(exc); |
| 6184 | return NULL; |
| 6185 | } |
| 6186 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6187 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 6188 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6189 | PyObject * |
| 6190 | PyUnicode_DecodeLatin1(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6191 | Py_ssize_t size, |
| 6192 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6193 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6194 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 6195 | return _PyUnicode_FromUCS1((unsigned char*)s, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6196 | } |
| 6197 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6198 | /* create or adjust a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6199 | static void |
| 6200 | make_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6201 | const char *encoding, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6202 | PyObject *unicode, |
| 6203 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6204 | const char *reason) |
| 6205 | { |
| 6206 | if (*exceptionObject == NULL) { |
| 6207 | *exceptionObject = PyObject_CallFunction( |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6208 | PyExc_UnicodeEncodeError, "sOnns", |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6209 | encoding, unicode, startpos, endpos, reason); |
| 6210 | } |
| 6211 | else { |
| 6212 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 6213 | goto onError; |
| 6214 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 6215 | goto onError; |
| 6216 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 6217 | goto onError; |
| 6218 | return; |
| 6219 | onError: |
| 6220 | Py_DECREF(*exceptionObject); |
| 6221 | *exceptionObject = NULL; |
| 6222 | } |
| 6223 | } |
| 6224 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6225 | /* raises a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6226 | static void |
| 6227 | raise_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6228 | const char *encoding, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6229 | PyObject *unicode, |
| 6230 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6231 | const char *reason) |
| 6232 | { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6233 | make_encode_exception(exceptionObject, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6234 | encoding, unicode, startpos, endpos, reason); |
| 6235 | if (*exceptionObject != NULL) |
| 6236 | PyCodec_StrictErrors(*exceptionObject); |
| 6237 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6238 | |
| 6239 | /* error handling callback helper: |
| 6240 | build arguments, call the callback and check the arguments, |
| 6241 | put the result into newpos and return the replacement string, which |
| 6242 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6243 | static PyObject * |
| 6244 | unicode_encode_call_errorhandler(const char *errors, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6245 | PyObject **errorHandler, |
| 6246 | const char *encoding, const char *reason, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6247 | PyObject *unicode, PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6248 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6249 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6250 | { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6251 | static char *argparse = "On;encoding error handler must return (str/bytes, int) tuple"; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6252 | Py_ssize_t len; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6253 | PyObject *restuple; |
| 6254 | PyObject *resunicode; |
| 6255 | |
| 6256 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6257 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6258 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6259 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6260 | } |
| 6261 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6262 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6263 | return NULL; |
| 6264 | len = PyUnicode_GET_LENGTH(unicode); |
| 6265 | |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6266 | make_encode_exception(exceptionObject, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6267 | encoding, unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6268 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6269 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6270 | |
| 6271 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6272 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6273 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6274 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6275 | if (!PyTuple_Check(restuple)) { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6276 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6277 | Py_DECREF(restuple); |
| 6278 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6279 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6280 | if (!PyArg_ParseTuple(restuple, argparse, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6281 | &resunicode, newpos)) { |
| 6282 | Py_DECREF(restuple); |
| 6283 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6284 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6285 | if (!PyUnicode_Check(resunicode) && !PyBytes_Check(resunicode)) { |
| 6286 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
| 6287 | Py_DECREF(restuple); |
| 6288 | return NULL; |
| 6289 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6290 | if (*newpos<0) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6291 | *newpos = len + *newpos; |
| 6292 | if (*newpos<0 || *newpos>len) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6293 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 6294 | Py_DECREF(restuple); |
| 6295 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 6296 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6297 | Py_INCREF(resunicode); |
| 6298 | Py_DECREF(restuple); |
| 6299 | return resunicode; |
| 6300 | } |
| 6301 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6302 | static PyObject * |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6303 | unicode_encode_ucs1(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6304 | const char *errors, |
Victor Stinner | fcd9653 | 2011-11-04 00:28:50 +0100 | [diff] [blame] | 6305 | unsigned int limit) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6306 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6307 | /* input state */ |
| 6308 | Py_ssize_t pos=0, size; |
| 6309 | int kind; |
| 6310 | void *data; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6311 | /* output object */ |
| 6312 | PyObject *res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6313 | /* pointer into the output */ |
| 6314 | char *str; |
| 6315 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6316 | Py_ssize_t ressize; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6317 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 6318 | 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] | 6319 | PyObject *errorHandler = NULL; |
| 6320 | PyObject *exc = NULL; |
| 6321 | /* the following variable is used for caching string comparisons |
| 6322 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 6323 | int known_errorHandler = -1; |
| 6324 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6325 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6326 | return NULL; |
| 6327 | size = PyUnicode_GET_LENGTH(unicode); |
| 6328 | kind = PyUnicode_KIND(unicode); |
| 6329 | data = PyUnicode_DATA(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6330 | /* allocate enough for a simple encoding without |
| 6331 | replacements, if we need more, we'll resize */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6332 | if (size == 0) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6333 | return PyBytes_FromStringAndSize(NULL, 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6334 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6335 | if (res == NULL) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6336 | return NULL; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6337 | str = PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6338 | ressize = size; |
| 6339 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6340 | while (pos < size) { |
| 6341 | Py_UCS4 c = PyUnicode_READ(kind, data, pos); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6342 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6343 | /* can we encode this? */ |
| 6344 | if (c<limit) { |
| 6345 | /* no overflow check, because we know that the space is enough */ |
| 6346 | *str++ = (char)c; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6347 | ++pos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6348 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6349 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6350 | Py_ssize_t requiredsize; |
| 6351 | PyObject *repunicode; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6352 | Py_ssize_t repsize, newpos, respos, i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6353 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6354 | Py_ssize_t collstart = pos; |
| 6355 | Py_ssize_t collend = pos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6356 | /* find all unecodable characters */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6357 | while ((collend < size) && (PyUnicode_READ(kind, data, collend)>=limit)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6358 | ++collend; |
| 6359 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 6360 | if (known_errorHandler==-1) { |
| 6361 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 6362 | known_errorHandler = 1; |
| 6363 | else if (!strcmp(errors, "replace")) |
| 6364 | known_errorHandler = 2; |
| 6365 | else if (!strcmp(errors, "ignore")) |
| 6366 | known_errorHandler = 3; |
| 6367 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 6368 | known_errorHandler = 4; |
| 6369 | else |
| 6370 | known_errorHandler = 0; |
| 6371 | } |
| 6372 | switch (known_errorHandler) { |
| 6373 | case 1: /* strict */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6374 | raise_encode_exception(&exc, encoding, unicode, collstart, collend, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6375 | goto onError; |
| 6376 | case 2: /* replace */ |
| 6377 | while (collstart++<collend) |
| 6378 | *str++ = '?'; /* fall through */ |
| 6379 | case 3: /* ignore */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6380 | pos = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6381 | break; |
| 6382 | case 4: /* xmlcharrefreplace */ |
| 6383 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6384 | /* determine replacement size */ |
| 6385 | for (i = collstart, repsize = 0; i < collend; ++i) { |
| 6386 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 6387 | if (ch < 10) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6388 | repsize += 2+1+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6389 | else if (ch < 100) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6390 | repsize += 2+2+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6391 | else if (ch < 1000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6392 | repsize += 2+3+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6393 | else if (ch < 10000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6394 | repsize += 2+4+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6395 | else if (ch < 100000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6396 | repsize += 2+5+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6397 | else if (ch < 1000000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6398 | repsize += 2+6+1; |
Victor Stinner | 0d3721d | 2011-11-22 03:27:53 +0100 | [diff] [blame] | 6399 | else { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 6400 | assert(ch <= MAX_UNICODE); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6401 | repsize += 2+7+1; |
Victor Stinner | 0d3721d | 2011-11-22 03:27:53 +0100 | [diff] [blame] | 6402 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6403 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6404 | requiredsize = respos+repsize+(size-collend); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6405 | if (requiredsize > ressize) { |
| 6406 | if (requiredsize<2*ressize) |
| 6407 | requiredsize = 2*ressize; |
| 6408 | if (_PyBytes_Resize(&res, requiredsize)) |
| 6409 | goto onError; |
| 6410 | str = PyBytes_AS_STRING(res) + respos; |
| 6411 | ressize = requiredsize; |
| 6412 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6413 | /* generate replacement */ |
| 6414 | for (i = collstart; i < collend; ++i) { |
| 6415 | str += sprintf(str, "&#%d;", PyUnicode_READ(kind, data, i)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6416 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6417 | pos = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6418 | break; |
| 6419 | default: |
| 6420 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6421 | encoding, reason, unicode, &exc, |
| 6422 | collstart, collend, &newpos); |
| 6423 | if (repunicode == NULL || (PyUnicode_Check(repunicode) && |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6424 | PyUnicode_READY(repunicode) == -1)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6425 | goto onError; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6426 | if (PyBytes_Check(repunicode)) { |
| 6427 | /* Directly copy bytes result to output. */ |
| 6428 | repsize = PyBytes_Size(repunicode); |
| 6429 | if (repsize > 1) { |
| 6430 | /* Make room for all additional bytes. */ |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6431 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6432 | if (_PyBytes_Resize(&res, ressize+repsize-1)) { |
| 6433 | Py_DECREF(repunicode); |
| 6434 | goto onError; |
| 6435 | } |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6436 | str = PyBytes_AS_STRING(res) + respos; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6437 | ressize += repsize-1; |
| 6438 | } |
| 6439 | memcpy(str, PyBytes_AsString(repunicode), repsize); |
| 6440 | str += repsize; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6441 | pos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6442 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6443 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6444 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6445 | /* need more space? (at least enough for what we |
| 6446 | have+the replacement+the rest of the string, so |
| 6447 | we won't have to check space for encodable characters) */ |
| 6448 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6449 | repsize = PyUnicode_GET_LENGTH(repunicode); |
| 6450 | requiredsize = respos+repsize+(size-collend); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6451 | if (requiredsize > ressize) { |
| 6452 | if (requiredsize<2*ressize) |
| 6453 | requiredsize = 2*ressize; |
| 6454 | if (_PyBytes_Resize(&res, requiredsize)) { |
| 6455 | Py_DECREF(repunicode); |
| 6456 | goto onError; |
| 6457 | } |
| 6458 | str = PyBytes_AS_STRING(res) + respos; |
| 6459 | ressize = requiredsize; |
| 6460 | } |
| 6461 | /* check if there is anything unencodable in the replacement |
| 6462 | and copy it to the output */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6463 | for (i = 0; repsize-->0; ++i, ++str) { |
| 6464 | c = PyUnicode_READ_CHAR(repunicode, i); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6465 | if (c >= limit) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6466 | raise_encode_exception(&exc, encoding, unicode, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6467 | pos, pos+1, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6468 | Py_DECREF(repunicode); |
| 6469 | goto onError; |
| 6470 | } |
| 6471 | *str = (char)c; |
| 6472 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6473 | pos = newpos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6474 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6475 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6476 | } |
| 6477 | } |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6478 | /* Resize if we allocated to much */ |
| 6479 | size = str - PyBytes_AS_STRING(res); |
| 6480 | if (size < ressize) { /* If this falls res will be NULL */ |
Alexandre Vassalotti | bad1b92 | 2008-12-27 09:49:09 +0000 | [diff] [blame] | 6481 | assert(size >= 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6482 | if (_PyBytes_Resize(&res, size) < 0) |
| 6483 | goto onError; |
| 6484 | } |
| 6485 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6486 | Py_XDECREF(errorHandler); |
| 6487 | Py_XDECREF(exc); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6488 | return res; |
| 6489 | |
| 6490 | onError: |
| 6491 | Py_XDECREF(res); |
| 6492 | Py_XDECREF(errorHandler); |
| 6493 | Py_XDECREF(exc); |
| 6494 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6495 | } |
| 6496 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6497 | /* Deprecated */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6498 | PyObject * |
| 6499 | PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6500 | Py_ssize_t size, |
| 6501 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6502 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6503 | PyObject *result; |
| 6504 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 6505 | if (unicode == NULL) |
| 6506 | return NULL; |
| 6507 | result = unicode_encode_ucs1(unicode, errors, 256); |
| 6508 | Py_DECREF(unicode); |
| 6509 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6510 | } |
| 6511 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6512 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6513 | _PyUnicode_AsLatin1String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6514 | { |
| 6515 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6516 | PyErr_BadArgument(); |
| 6517 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6518 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6519 | if (PyUnicode_READY(unicode) == -1) |
| 6520 | return NULL; |
| 6521 | /* Fast path: if it is a one-byte string, construct |
| 6522 | bytes object directly. */ |
| 6523 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) |
| 6524 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6525 | PyUnicode_GET_LENGTH(unicode)); |
| 6526 | /* Non-Latin-1 characters present. Defer to above function to |
| 6527 | raise the exception. */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6528 | return unicode_encode_ucs1(unicode, errors, 256); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6529 | } |
| 6530 | |
| 6531 | PyObject* |
| 6532 | PyUnicode_AsLatin1String(PyObject *unicode) |
| 6533 | { |
| 6534 | return _PyUnicode_AsLatin1String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6535 | } |
| 6536 | |
| 6537 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 6538 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6539 | PyObject * |
| 6540 | PyUnicode_DecodeASCII(const char *s, |
| 6541 | Py_ssize_t size, |
| 6542 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6543 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6544 | const char *starts = s; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6545 | PyObject *unicode; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6546 | int kind; |
| 6547 | void *data; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6548 | Py_ssize_t startinpos; |
| 6549 | Py_ssize_t endinpos; |
| 6550 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6551 | const char *e; |
| 6552 | PyObject *errorHandler = NULL; |
| 6553 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6554 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 6555 | if (size == 0) { |
| 6556 | Py_INCREF(unicode_empty); |
| 6557 | return unicode_empty; |
| 6558 | } |
| 6559 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6560 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6561 | if (size == 1 && (unsigned char)s[0] < 128) |
| 6562 | return get_latin1_char((unsigned char)s[0]); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6563 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6564 | unicode = PyUnicode_New(size, 127); |
| 6565 | if (unicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6566 | goto onError; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6567 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6568 | e = s + size; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6569 | data = PyUnicode_1BYTE_DATA(unicode); |
| 6570 | outpos = ascii_decode(s, e, (Py_UCS1 *)data); |
| 6571 | if (outpos == size) |
| 6572 | return unicode; |
| 6573 | |
| 6574 | s += outpos; |
| 6575 | kind = PyUnicode_1BYTE_KIND; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6576 | while (s < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6577 | register unsigned char c = (unsigned char)*s; |
| 6578 | if (c < 128) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6579 | PyUnicode_WRITE(kind, data, outpos++, c); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6580 | ++s; |
| 6581 | } |
| 6582 | else { |
| 6583 | startinpos = s-starts; |
| 6584 | endinpos = startinpos + 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6585 | if (unicode_decode_call_errorhandler( |
| 6586 | errors, &errorHandler, |
| 6587 | "ascii", "ordinal not in range(128)", |
| 6588 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6589 | &unicode, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6590 | goto onError; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6591 | kind = PyUnicode_KIND(unicode); |
| 6592 | data = PyUnicode_DATA(unicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6593 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6594 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6595 | if (unicode_resize(&unicode, outpos) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6596 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6597 | Py_XDECREF(errorHandler); |
| 6598 | Py_XDECREF(exc); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6599 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 6600 | return unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6601 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6602 | onError: |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6603 | Py_XDECREF(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6604 | Py_XDECREF(errorHandler); |
| 6605 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6606 | return NULL; |
| 6607 | } |
| 6608 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6609 | /* Deprecated */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6610 | PyObject * |
| 6611 | PyUnicode_EncodeASCII(const Py_UNICODE *p, |
| 6612 | Py_ssize_t size, |
| 6613 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6614 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6615 | PyObject *result; |
| 6616 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 6617 | if (unicode == NULL) |
| 6618 | return NULL; |
| 6619 | result = unicode_encode_ucs1(unicode, errors, 128); |
| 6620 | Py_DECREF(unicode); |
| 6621 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6622 | } |
| 6623 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6624 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6625 | _PyUnicode_AsASCIIString(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6626 | { |
| 6627 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6628 | PyErr_BadArgument(); |
| 6629 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6630 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6631 | if (PyUnicode_READY(unicode) == -1) |
| 6632 | return NULL; |
| 6633 | /* Fast path: if it is an ASCII-only string, construct bytes object |
| 6634 | directly. Else defer to above function to raise the exception. */ |
| 6635 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) |
| 6636 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6637 | PyUnicode_GET_LENGTH(unicode)); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6638 | return unicode_encode_ucs1(unicode, errors, 128); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6639 | } |
| 6640 | |
| 6641 | PyObject * |
| 6642 | PyUnicode_AsASCIIString(PyObject *unicode) |
| 6643 | { |
| 6644 | return _PyUnicode_AsASCIIString(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6645 | } |
| 6646 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 6647 | #ifdef HAVE_MBCS |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6648 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6649 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6650 | |
Hirokazu Yamamoto | 3530246 | 2009-03-21 13:23:27 +0000 | [diff] [blame] | 6651 | #if SIZEOF_INT < SIZEOF_SIZE_T |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6652 | #define NEED_RETRY |
| 6653 | #endif |
| 6654 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6655 | #ifndef WC_ERR_INVALID_CHARS |
| 6656 | # define WC_ERR_INVALID_CHARS 0x0080 |
| 6657 | #endif |
| 6658 | |
| 6659 | static char* |
| 6660 | code_page_name(UINT code_page, PyObject **obj) |
| 6661 | { |
| 6662 | *obj = NULL; |
| 6663 | if (code_page == CP_ACP) |
| 6664 | return "mbcs"; |
| 6665 | if (code_page == CP_UTF7) |
| 6666 | return "CP_UTF7"; |
| 6667 | if (code_page == CP_UTF8) |
| 6668 | return "CP_UTF8"; |
| 6669 | |
| 6670 | *obj = PyBytes_FromFormat("cp%u", code_page); |
| 6671 | if (*obj == NULL) |
| 6672 | return NULL; |
| 6673 | return PyBytes_AS_STRING(*obj); |
| 6674 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6675 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6676 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6677 | is_dbcs_lead_byte(UINT code_page, const char *s, int offset) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6678 | { |
| 6679 | const char *curr = s + offset; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6680 | const char *prev; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6681 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6682 | if (!IsDBCSLeadByteEx(code_page, *curr)) |
| 6683 | return 0; |
| 6684 | |
| 6685 | prev = CharPrevExA(code_page, s, curr, 0); |
| 6686 | if (prev == curr) |
| 6687 | return 1; |
| 6688 | /* FIXME: This code is limited to "true" double-byte encodings, |
| 6689 | as it assumes an incomplete character consists of a single |
| 6690 | byte. */ |
| 6691 | if (curr - prev == 2) |
| 6692 | return 1; |
| 6693 | if (!IsDBCSLeadByteEx(code_page, *prev)) |
| 6694 | return 1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6695 | return 0; |
| 6696 | } |
| 6697 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6698 | static DWORD |
| 6699 | decode_code_page_flags(UINT code_page) |
| 6700 | { |
| 6701 | if (code_page == CP_UTF7) { |
| 6702 | /* The CP_UTF7 decoder only supports flags=0 */ |
| 6703 | return 0; |
| 6704 | } |
| 6705 | else |
| 6706 | return MB_ERR_INVALID_CHARS; |
| 6707 | } |
| 6708 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6709 | /* |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6710 | * Decode a byte string from a Windows code page into unicode object in strict |
| 6711 | * mode. |
| 6712 | * |
| 6713 | * Returns consumed size if succeed, returns -2 on decode error, or raise a |
| 6714 | * WindowsError and returns -1 on other error. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6715 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6716 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6717 | decode_code_page_strict(UINT code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6718 | PyObject **v, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6719 | const char *in, |
| 6720 | int insize) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6721 | { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6722 | const DWORD flags = decode_code_page_flags(code_page); |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 6723 | wchar_t *out; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6724 | DWORD outsize; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6725 | |
| 6726 | /* First get the size of the result */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6727 | assert(insize > 0); |
| 6728 | outsize = MultiByteToWideChar(code_page, flags, in, insize, NULL, 0); |
| 6729 | if (outsize <= 0) |
| 6730 | goto error; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6731 | |
| 6732 | if (*v == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6733 | /* Create unicode object */ |
Victor Stinner | ab59594 | 2011-12-17 04:59:06 +0100 | [diff] [blame] | 6734 | /* FIXME: don't use _PyUnicode_New(), but allocate a wchar_t* buffer */ |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6735 | *v = (PyObject*)_PyUnicode_New(outsize); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6736 | if (*v == NULL) |
| 6737 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6738 | out = PyUnicode_AS_UNICODE(*v); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6739 | } |
| 6740 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6741 | /* Extend unicode object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6742 | Py_ssize_t n = PyUnicode_GET_SIZE(*v); |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 6743 | if (unicode_resize(v, n + outsize) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6744 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6745 | out = PyUnicode_AS_UNICODE(*v) + n; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6746 | } |
| 6747 | |
| 6748 | /* Do the conversion */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6749 | outsize = MultiByteToWideChar(code_page, flags, in, insize, out, outsize); |
| 6750 | if (outsize <= 0) |
| 6751 | goto error; |
| 6752 | return insize; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6753 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6754 | error: |
| 6755 | if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION) |
| 6756 | return -2; |
| 6757 | PyErr_SetFromWindowsErr(0); |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6758 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6759 | } |
| 6760 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6761 | /* |
| 6762 | * Decode a byte string from a code page into unicode object with an error |
| 6763 | * handler. |
| 6764 | * |
| 6765 | * Returns consumed size if succeed, or raise a WindowsError or |
| 6766 | * UnicodeDecodeError exception and returns -1 on error. |
| 6767 | */ |
| 6768 | static int |
| 6769 | decode_code_page_errors(UINT code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6770 | PyObject **v, |
| 6771 | const char *in, const int size, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6772 | const char *errors) |
| 6773 | { |
| 6774 | const char *startin = in; |
| 6775 | const char *endin = in + size; |
| 6776 | const DWORD flags = decode_code_page_flags(code_page); |
| 6777 | /* Ideally, we should get reason from FormatMessage. This is the Windows |
| 6778 | 2000 English version of the message. */ |
| 6779 | const char *reason = "No mapping for the Unicode character exists " |
| 6780 | "in the target code page."; |
| 6781 | /* each step cannot decode more than 1 character, but a character can be |
| 6782 | represented as a surrogate pair */ |
| 6783 | wchar_t buffer[2], *startout, *out; |
| 6784 | int insize, outsize; |
| 6785 | PyObject *errorHandler = NULL; |
| 6786 | PyObject *exc = NULL; |
| 6787 | PyObject *encoding_obj = NULL; |
| 6788 | char *encoding; |
| 6789 | DWORD err; |
| 6790 | int ret = -1; |
| 6791 | |
| 6792 | assert(size > 0); |
| 6793 | |
| 6794 | encoding = code_page_name(code_page, &encoding_obj); |
| 6795 | if (encoding == NULL) |
| 6796 | return -1; |
| 6797 | |
| 6798 | if (errors == NULL || strcmp(errors, "strict") == 0) { |
| 6799 | /* The last error was ERROR_NO_UNICODE_TRANSLATION, then we raise a |
| 6800 | UnicodeDecodeError. */ |
| 6801 | make_decode_exception(&exc, encoding, in, size, 0, 0, reason); |
| 6802 | if (exc != NULL) { |
| 6803 | PyCodec_StrictErrors(exc); |
| 6804 | Py_CLEAR(exc); |
| 6805 | } |
| 6806 | goto error; |
| 6807 | } |
| 6808 | |
| 6809 | if (*v == NULL) { |
| 6810 | /* Create unicode object */ |
| 6811 | if (size > PY_SSIZE_T_MAX / (Py_ssize_t)Py_ARRAY_LENGTH(buffer)) { |
| 6812 | PyErr_NoMemory(); |
| 6813 | goto error; |
| 6814 | } |
Victor Stinner | ab59594 | 2011-12-17 04:59:06 +0100 | [diff] [blame] | 6815 | /* FIXME: don't use _PyUnicode_New(), but allocate a wchar_t* buffer */ |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6816 | *v = (PyObject*)_PyUnicode_New(size * Py_ARRAY_LENGTH(buffer)); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6817 | if (*v == NULL) |
| 6818 | goto error; |
| 6819 | startout = PyUnicode_AS_UNICODE(*v); |
| 6820 | } |
| 6821 | else { |
| 6822 | /* Extend unicode object */ |
| 6823 | Py_ssize_t n = PyUnicode_GET_SIZE(*v); |
| 6824 | if (size > (PY_SSIZE_T_MAX - n) / (Py_ssize_t)Py_ARRAY_LENGTH(buffer)) { |
| 6825 | PyErr_NoMemory(); |
| 6826 | goto error; |
| 6827 | } |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 6828 | if (unicode_resize(v, n + size * Py_ARRAY_LENGTH(buffer)) < 0) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6829 | goto error; |
| 6830 | startout = PyUnicode_AS_UNICODE(*v) + n; |
| 6831 | } |
| 6832 | |
| 6833 | /* Decode the byte string character per character */ |
| 6834 | out = startout; |
| 6835 | while (in < endin) |
| 6836 | { |
| 6837 | /* Decode a character */ |
| 6838 | insize = 1; |
| 6839 | do |
| 6840 | { |
| 6841 | outsize = MultiByteToWideChar(code_page, flags, |
| 6842 | in, insize, |
| 6843 | buffer, Py_ARRAY_LENGTH(buffer)); |
| 6844 | if (outsize > 0) |
| 6845 | break; |
| 6846 | err = GetLastError(); |
| 6847 | if (err != ERROR_NO_UNICODE_TRANSLATION |
| 6848 | && err != ERROR_INSUFFICIENT_BUFFER) |
| 6849 | { |
| 6850 | PyErr_SetFromWindowsErr(0); |
| 6851 | goto error; |
| 6852 | } |
| 6853 | insize++; |
| 6854 | } |
| 6855 | /* 4=maximum length of a UTF-8 sequence */ |
| 6856 | while (insize <= 4 && (in + insize) <= endin); |
| 6857 | |
| 6858 | if (outsize <= 0) { |
| 6859 | Py_ssize_t startinpos, endinpos, outpos; |
| 6860 | |
| 6861 | startinpos = in - startin; |
| 6862 | endinpos = startinpos + 1; |
| 6863 | outpos = out - PyUnicode_AS_UNICODE(*v); |
| 6864 | if (unicode_decode_call_errorhandler( |
| 6865 | errors, &errorHandler, |
| 6866 | encoding, reason, |
| 6867 | &startin, &endin, &startinpos, &endinpos, &exc, &in, |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 6868 | v, &outpos)) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6869 | { |
| 6870 | goto error; |
| 6871 | } |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 6872 | out = PyUnicode_AS_UNICODE(*v) + outpos; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6873 | } |
| 6874 | else { |
| 6875 | in += insize; |
| 6876 | memcpy(out, buffer, outsize * sizeof(wchar_t)); |
| 6877 | out += outsize; |
| 6878 | } |
| 6879 | } |
| 6880 | |
| 6881 | /* write a NUL character at the end */ |
| 6882 | *out = 0; |
| 6883 | |
| 6884 | /* Extend unicode object */ |
| 6885 | outsize = out - startout; |
| 6886 | assert(outsize <= PyUnicode_WSTR_LENGTH(*v)); |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 6887 | if (unicode_resize(v, outsize) < 0) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6888 | goto error; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6889 | ret = size; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6890 | |
| 6891 | error: |
| 6892 | Py_XDECREF(encoding_obj); |
| 6893 | Py_XDECREF(errorHandler); |
| 6894 | Py_XDECREF(exc); |
| 6895 | return ret; |
| 6896 | } |
| 6897 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6898 | static PyObject * |
| 6899 | decode_code_page_stateful(int code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6900 | const char *s, Py_ssize_t size, |
| 6901 | const char *errors, Py_ssize_t *consumed) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6902 | { |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6903 | PyObject *v = NULL; |
| 6904 | int chunk_size, final, converted, done; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6905 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6906 | if (code_page < 0) { |
| 6907 | PyErr_SetString(PyExc_ValueError, "invalid code page number"); |
| 6908 | return NULL; |
| 6909 | } |
| 6910 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6911 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6912 | *consumed = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6913 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6914 | do |
| 6915 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6916 | #ifdef NEED_RETRY |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6917 | if (size > INT_MAX) { |
| 6918 | chunk_size = INT_MAX; |
| 6919 | final = 0; |
| 6920 | done = 0; |
| 6921 | } |
| 6922 | else |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6923 | #endif |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6924 | { |
| 6925 | chunk_size = (int)size; |
| 6926 | final = (consumed == NULL); |
| 6927 | done = 1; |
| 6928 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6929 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6930 | /* Skip trailing lead-byte unless 'final' is set */ |
| 6931 | if (!final && is_dbcs_lead_byte(code_page, s, chunk_size - 1)) |
| 6932 | --chunk_size; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6933 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6934 | if (chunk_size == 0 && done) { |
| 6935 | if (v != NULL) |
| 6936 | break; |
| 6937 | Py_INCREF(unicode_empty); |
| 6938 | return unicode_empty; |
| 6939 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6940 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6941 | |
| 6942 | converted = decode_code_page_strict(code_page, &v, |
| 6943 | s, chunk_size); |
| 6944 | if (converted == -2) |
| 6945 | converted = decode_code_page_errors(code_page, &v, |
| 6946 | s, chunk_size, |
| 6947 | errors); |
| 6948 | assert(converted != 0); |
| 6949 | |
| 6950 | if (converted < 0) { |
| 6951 | Py_XDECREF(v); |
| 6952 | return NULL; |
| 6953 | } |
| 6954 | |
| 6955 | if (consumed) |
| 6956 | *consumed += converted; |
| 6957 | |
| 6958 | s += converted; |
| 6959 | size -= converted; |
| 6960 | } while (!done); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6961 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 6962 | return unicode_result(v); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6963 | } |
| 6964 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6965 | PyObject * |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6966 | PyUnicode_DecodeCodePageStateful(int code_page, |
| 6967 | const char *s, |
| 6968 | Py_ssize_t size, |
| 6969 | const char *errors, |
| 6970 | Py_ssize_t *consumed) |
| 6971 | { |
| 6972 | return decode_code_page_stateful(code_page, s, size, errors, consumed); |
| 6973 | } |
| 6974 | |
| 6975 | PyObject * |
| 6976 | PyUnicode_DecodeMBCSStateful(const char *s, |
| 6977 | Py_ssize_t size, |
| 6978 | const char *errors, |
| 6979 | Py_ssize_t *consumed) |
| 6980 | { |
| 6981 | return decode_code_page_stateful(CP_ACP, s, size, errors, consumed); |
| 6982 | } |
| 6983 | |
| 6984 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6985 | PyUnicode_DecodeMBCS(const char *s, |
| 6986 | Py_ssize_t size, |
| 6987 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6988 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6989 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 6990 | } |
| 6991 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6992 | static DWORD |
| 6993 | encode_code_page_flags(UINT code_page, const char *errors) |
| 6994 | { |
| 6995 | if (code_page == CP_UTF8) { |
| 6996 | if (winver.dwMajorVersion >= 6) |
| 6997 | /* CP_UTF8 supports WC_ERR_INVALID_CHARS on Windows Vista |
| 6998 | and later */ |
| 6999 | return WC_ERR_INVALID_CHARS; |
| 7000 | else |
| 7001 | /* CP_UTF8 only supports flags=0 on Windows older than Vista */ |
| 7002 | return 0; |
| 7003 | } |
| 7004 | else if (code_page == CP_UTF7) { |
| 7005 | /* CP_UTF7 only supports flags=0 */ |
| 7006 | return 0; |
| 7007 | } |
| 7008 | else { |
| 7009 | if (errors != NULL && strcmp(errors, "replace") == 0) |
| 7010 | return 0; |
| 7011 | else |
| 7012 | return WC_NO_BEST_FIT_CHARS; |
| 7013 | } |
| 7014 | } |
| 7015 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7016 | /* |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7017 | * Encode a Unicode string to a Windows code page into a byte string in strict |
| 7018 | * mode. |
| 7019 | * |
| 7020 | * Returns consumed characters if succeed, returns -2 on encode error, or raise |
| 7021 | * a WindowsError and returns -1 on other error. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7022 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7023 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7024 | encode_code_page_strict(UINT code_page, PyObject **outbytes, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7025 | PyObject *unicode, Py_ssize_t offset, int len, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7026 | const char* errors) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7027 | { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7028 | BOOL usedDefaultChar = FALSE; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7029 | BOOL *pusedDefaultChar = &usedDefaultChar; |
| 7030 | int outsize; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7031 | PyObject *exc = NULL; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 7032 | wchar_t *p; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7033 | Py_ssize_t size; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7034 | const DWORD flags = encode_code_page_flags(code_page, NULL); |
| 7035 | char *out; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7036 | /* Create a substring so that we can get the UTF-16 representation |
| 7037 | of just the slice under consideration. */ |
| 7038 | PyObject *substring; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7039 | |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7040 | assert(len > 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7041 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7042 | if (code_page != CP_UTF8 && code_page != CP_UTF7) |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7043 | pusedDefaultChar = &usedDefaultChar; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7044 | else |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7045 | pusedDefaultChar = NULL; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7046 | |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7047 | substring = PyUnicode_Substring(unicode, offset, offset+len); |
| 7048 | if (substring == NULL) |
| 7049 | return -1; |
| 7050 | p = PyUnicode_AsUnicodeAndSize(substring, &size); |
| 7051 | if (p == NULL) { |
| 7052 | Py_DECREF(substring); |
| 7053 | return -1; |
| 7054 | } |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7055 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7056 | /* First get the size of the result */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7057 | outsize = WideCharToMultiByte(code_page, flags, |
| 7058 | p, size, |
| 7059 | NULL, 0, |
| 7060 | NULL, pusedDefaultChar); |
| 7061 | if (outsize <= 0) |
| 7062 | goto error; |
| 7063 | /* If we used a default char, then we failed! */ |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7064 | if (pusedDefaultChar && *pusedDefaultChar) { |
| 7065 | Py_DECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7066 | return -2; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7067 | } |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7068 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7069 | if (*outbytes == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7070 | /* Create string object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7071 | *outbytes = PyBytes_FromStringAndSize(NULL, outsize); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7072 | if (*outbytes == NULL) { |
| 7073 | Py_DECREF(substring); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7074 | return -1; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7075 | } |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7076 | out = PyBytes_AS_STRING(*outbytes); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7077 | } |
| 7078 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7079 | /* Extend string object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7080 | const Py_ssize_t n = PyBytes_Size(*outbytes); |
| 7081 | if (outsize > PY_SSIZE_T_MAX - n) { |
| 7082 | PyErr_NoMemory(); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7083 | Py_DECREF(substring); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7084 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7085 | } |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7086 | if (_PyBytes_Resize(outbytes, n + outsize) < 0) { |
| 7087 | Py_DECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7088 | return -1; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7089 | } |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7090 | out = PyBytes_AS_STRING(*outbytes) + n; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7091 | } |
| 7092 | |
| 7093 | /* Do the conversion */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7094 | outsize = WideCharToMultiByte(code_page, flags, |
| 7095 | p, size, |
| 7096 | out, outsize, |
| 7097 | NULL, pusedDefaultChar); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7098 | Py_CLEAR(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7099 | if (outsize <= 0) |
| 7100 | goto error; |
| 7101 | if (pusedDefaultChar && *pusedDefaultChar) |
| 7102 | return -2; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7103 | return 0; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7104 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7105 | error: |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7106 | Py_XDECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7107 | if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION) |
| 7108 | return -2; |
| 7109 | PyErr_SetFromWindowsErr(0); |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7110 | return -1; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7111 | } |
| 7112 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7113 | /* |
| 7114 | * Encode a Unicode string to a Windows code page into a byte string using a |
| 7115 | * error handler. |
| 7116 | * |
| 7117 | * Returns consumed characters if succeed, or raise a WindowsError and returns |
| 7118 | * -1 on other error. |
| 7119 | */ |
| 7120 | static int |
| 7121 | encode_code_page_errors(UINT code_page, PyObject **outbytes, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7122 | PyObject *unicode, Py_ssize_t unicode_offset, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7123 | Py_ssize_t insize, const char* errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7124 | { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7125 | const DWORD flags = encode_code_page_flags(code_page, errors); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7126 | Py_ssize_t pos = unicode_offset; |
| 7127 | Py_ssize_t endin = unicode_offset + insize; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7128 | /* Ideally, we should get reason from FormatMessage. This is the Windows |
| 7129 | 2000 English version of the message. */ |
| 7130 | const char *reason = "invalid character"; |
| 7131 | /* 4=maximum length of a UTF-8 sequence */ |
| 7132 | char buffer[4]; |
| 7133 | BOOL usedDefaultChar = FALSE, *pusedDefaultChar; |
| 7134 | Py_ssize_t outsize; |
| 7135 | char *out; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7136 | PyObject *errorHandler = NULL; |
| 7137 | PyObject *exc = NULL; |
| 7138 | PyObject *encoding_obj = NULL; |
| 7139 | char *encoding; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7140 | Py_ssize_t newpos, newoutsize; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7141 | PyObject *rep; |
| 7142 | int ret = -1; |
| 7143 | |
| 7144 | assert(insize > 0); |
| 7145 | |
| 7146 | encoding = code_page_name(code_page, &encoding_obj); |
| 7147 | if (encoding == NULL) |
| 7148 | return -1; |
| 7149 | |
| 7150 | if (errors == NULL || strcmp(errors, "strict") == 0) { |
| 7151 | /* The last error was ERROR_NO_UNICODE_TRANSLATION, |
| 7152 | then we raise a UnicodeEncodeError. */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7153 | make_encode_exception(&exc, encoding, unicode, 0, 0, reason); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7154 | if (exc != NULL) { |
| 7155 | PyCodec_StrictErrors(exc); |
| 7156 | Py_DECREF(exc); |
| 7157 | } |
| 7158 | Py_XDECREF(encoding_obj); |
| 7159 | return -1; |
| 7160 | } |
| 7161 | |
| 7162 | if (code_page != CP_UTF8 && code_page != CP_UTF7) |
| 7163 | pusedDefaultChar = &usedDefaultChar; |
| 7164 | else |
| 7165 | pusedDefaultChar = NULL; |
| 7166 | |
| 7167 | if (Py_ARRAY_LENGTH(buffer) > PY_SSIZE_T_MAX / insize) { |
| 7168 | PyErr_NoMemory(); |
| 7169 | goto error; |
| 7170 | } |
| 7171 | outsize = insize * Py_ARRAY_LENGTH(buffer); |
| 7172 | |
| 7173 | if (*outbytes == NULL) { |
| 7174 | /* Create string object */ |
| 7175 | *outbytes = PyBytes_FromStringAndSize(NULL, outsize); |
| 7176 | if (*outbytes == NULL) |
| 7177 | goto error; |
| 7178 | out = PyBytes_AS_STRING(*outbytes); |
| 7179 | } |
| 7180 | else { |
| 7181 | /* Extend string object */ |
| 7182 | Py_ssize_t n = PyBytes_Size(*outbytes); |
| 7183 | if (n > PY_SSIZE_T_MAX - outsize) { |
| 7184 | PyErr_NoMemory(); |
| 7185 | goto error; |
| 7186 | } |
| 7187 | if (_PyBytes_Resize(outbytes, n + outsize) < 0) |
| 7188 | goto error; |
| 7189 | out = PyBytes_AS_STRING(*outbytes) + n; |
| 7190 | } |
| 7191 | |
| 7192 | /* Encode the string character per character */ |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7193 | while (pos < endin) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7194 | { |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7195 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, pos); |
| 7196 | wchar_t chars[2]; |
| 7197 | int charsize; |
| 7198 | if (ch < 0x10000) { |
| 7199 | chars[0] = (wchar_t)ch; |
| 7200 | charsize = 1; |
| 7201 | } |
| 7202 | else { |
| 7203 | ch -= 0x10000; |
| 7204 | chars[0] = 0xd800 + (ch >> 10); |
| 7205 | chars[1] = 0xdc00 + (ch & 0x3ff); |
| 7206 | charsize = 2; |
| 7207 | } |
| 7208 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7209 | outsize = WideCharToMultiByte(code_page, flags, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7210 | chars, charsize, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7211 | buffer, Py_ARRAY_LENGTH(buffer), |
| 7212 | NULL, pusedDefaultChar); |
| 7213 | if (outsize > 0) { |
| 7214 | if (pusedDefaultChar == NULL || !(*pusedDefaultChar)) |
| 7215 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7216 | pos++; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7217 | memcpy(out, buffer, outsize); |
| 7218 | out += outsize; |
| 7219 | continue; |
| 7220 | } |
| 7221 | } |
| 7222 | else if (GetLastError() != ERROR_NO_UNICODE_TRANSLATION) { |
| 7223 | PyErr_SetFromWindowsErr(0); |
| 7224 | goto error; |
| 7225 | } |
| 7226 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7227 | rep = unicode_encode_call_errorhandler( |
| 7228 | errors, &errorHandler, encoding, reason, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7229 | unicode, &exc, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7230 | pos, pos + 1, &newpos); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7231 | if (rep == NULL) |
| 7232 | goto error; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7233 | pos = newpos; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7234 | |
| 7235 | if (PyBytes_Check(rep)) { |
| 7236 | outsize = PyBytes_GET_SIZE(rep); |
| 7237 | if (outsize != 1) { |
| 7238 | Py_ssize_t offset = out - PyBytes_AS_STRING(*outbytes); |
| 7239 | newoutsize = PyBytes_GET_SIZE(*outbytes) + (outsize - 1); |
| 7240 | if (_PyBytes_Resize(outbytes, newoutsize) < 0) { |
| 7241 | Py_DECREF(rep); |
| 7242 | goto error; |
| 7243 | } |
| 7244 | out = PyBytes_AS_STRING(*outbytes) + offset; |
| 7245 | } |
| 7246 | memcpy(out, PyBytes_AS_STRING(rep), outsize); |
| 7247 | out += outsize; |
| 7248 | } |
| 7249 | else { |
| 7250 | Py_ssize_t i; |
| 7251 | enum PyUnicode_Kind kind; |
| 7252 | void *data; |
| 7253 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7254 | if (PyUnicode_READY(rep) == -1) { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7255 | Py_DECREF(rep); |
| 7256 | goto error; |
| 7257 | } |
| 7258 | |
| 7259 | outsize = PyUnicode_GET_LENGTH(rep); |
| 7260 | if (outsize != 1) { |
| 7261 | Py_ssize_t offset = out - PyBytes_AS_STRING(*outbytes); |
| 7262 | newoutsize = PyBytes_GET_SIZE(*outbytes) + (outsize - 1); |
| 7263 | if (_PyBytes_Resize(outbytes, newoutsize) < 0) { |
| 7264 | Py_DECREF(rep); |
| 7265 | goto error; |
| 7266 | } |
| 7267 | out = PyBytes_AS_STRING(*outbytes) + offset; |
| 7268 | } |
| 7269 | kind = PyUnicode_KIND(rep); |
| 7270 | data = PyUnicode_DATA(rep); |
| 7271 | for (i=0; i < outsize; i++) { |
| 7272 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 7273 | if (ch > 127) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7274 | raise_encode_exception(&exc, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7275 | encoding, unicode, |
| 7276 | pos, pos + 1, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7277 | "unable to encode error handler result to ASCII"); |
| 7278 | Py_DECREF(rep); |
| 7279 | goto error; |
| 7280 | } |
| 7281 | *out = (unsigned char)ch; |
| 7282 | out++; |
| 7283 | } |
| 7284 | } |
| 7285 | Py_DECREF(rep); |
| 7286 | } |
| 7287 | /* write a NUL byte */ |
| 7288 | *out = 0; |
| 7289 | outsize = out - PyBytes_AS_STRING(*outbytes); |
| 7290 | assert(outsize <= PyBytes_GET_SIZE(*outbytes)); |
| 7291 | if (_PyBytes_Resize(outbytes, outsize) < 0) |
| 7292 | goto error; |
| 7293 | ret = 0; |
| 7294 | |
| 7295 | error: |
| 7296 | Py_XDECREF(encoding_obj); |
| 7297 | Py_XDECREF(errorHandler); |
| 7298 | Py_XDECREF(exc); |
| 7299 | return ret; |
| 7300 | } |
| 7301 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7302 | static PyObject * |
| 7303 | encode_code_page(int code_page, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7304 | PyObject *unicode, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7305 | const char *errors) |
| 7306 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7307 | Py_ssize_t len; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7308 | PyObject *outbytes = NULL; |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7309 | Py_ssize_t offset; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7310 | int chunk_len, ret, done; |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7311 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7312 | if (PyUnicode_READY(unicode) == -1) |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7313 | return NULL; |
| 7314 | len = PyUnicode_GET_LENGTH(unicode); |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 7315 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7316 | if (code_page < 0) { |
| 7317 | PyErr_SetString(PyExc_ValueError, "invalid code page number"); |
| 7318 | return NULL; |
| 7319 | } |
| 7320 | |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7321 | if (len == 0) |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7322 | return PyBytes_FromStringAndSize(NULL, 0); |
| 7323 | |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7324 | offset = 0; |
| 7325 | do |
| 7326 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7327 | #ifdef NEED_RETRY |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7328 | /* UTF-16 encoding may double the size, so use only INT_MAX/2 |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7329 | chunks. */ |
| 7330 | if (len > INT_MAX/2) { |
| 7331 | chunk_len = INT_MAX/2; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7332 | done = 0; |
| 7333 | } |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7334 | else |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7335 | #endif |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7336 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7337 | chunk_len = (int)len; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7338 | done = 1; |
| 7339 | } |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7340 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7341 | ret = encode_code_page_strict(code_page, &outbytes, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7342 | unicode, offset, chunk_len, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7343 | errors); |
| 7344 | if (ret == -2) |
| 7345 | ret = encode_code_page_errors(code_page, &outbytes, |
| 7346 | unicode, offset, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7347 | chunk_len, errors); |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7348 | if (ret < 0) { |
| 7349 | Py_XDECREF(outbytes); |
| 7350 | return NULL; |
| 7351 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7352 | |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7353 | offset += chunk_len; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7354 | len -= chunk_len; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7355 | } while (!done); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7356 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7357 | return outbytes; |
| 7358 | } |
| 7359 | |
| 7360 | PyObject * |
| 7361 | PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
| 7362 | Py_ssize_t size, |
| 7363 | const char *errors) |
| 7364 | { |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7365 | PyObject *unicode, *res; |
| 7366 | unicode = PyUnicode_FromUnicode(p, size); |
| 7367 | if (unicode == NULL) |
| 7368 | return NULL; |
| 7369 | res = encode_code_page(CP_ACP, unicode, errors); |
| 7370 | Py_DECREF(unicode); |
| 7371 | return res; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7372 | } |
| 7373 | |
| 7374 | PyObject * |
| 7375 | PyUnicode_EncodeCodePage(int code_page, |
| 7376 | PyObject *unicode, |
| 7377 | const char *errors) |
| 7378 | { |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7379 | return encode_code_page(code_page, unicode, errors); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7380 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 7381 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7382 | PyObject * |
| 7383 | PyUnicode_AsMBCSString(PyObject *unicode) |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7384 | { |
| 7385 | if (!PyUnicode_Check(unicode)) { |
| 7386 | PyErr_BadArgument(); |
| 7387 | return NULL; |
| 7388 | } |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7389 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, NULL); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7390 | } |
| 7391 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7392 | #undef NEED_RETRY |
| 7393 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 7394 | #endif /* HAVE_MBCS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7395 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7396 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 7397 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7398 | PyObject * |
| 7399 | PyUnicode_DecodeCharmap(const char *s, |
| 7400 | Py_ssize_t size, |
| 7401 | PyObject *mapping, |
| 7402 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7403 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7404 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7405 | Py_ssize_t startinpos; |
| 7406 | Py_ssize_t endinpos; |
| 7407 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7408 | const char *e; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 7409 | PyObject *v; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7410 | Py_ssize_t extrachars = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7411 | PyObject *errorHandler = NULL; |
| 7412 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7413 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7414 | /* Default to Latin-1 */ |
| 7415 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7416 | return PyUnicode_DecodeLatin1(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7417 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7418 | v = PyUnicode_New(size, 127); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7419 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7420 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7421 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 7422 | return v; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7423 | outpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7424 | e = s + size; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7425 | if (PyUnicode_CheckExact(mapping)) { |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7426 | Py_ssize_t maplen; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7427 | enum PyUnicode_Kind mapkind; |
| 7428 | void *mapdata; |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7429 | Py_UCS4 x; |
| 7430 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7431 | if (PyUnicode_READY(mapping) == -1) |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7432 | return NULL; |
| 7433 | |
| 7434 | maplen = PyUnicode_GET_LENGTH(mapping); |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7435 | mapdata = PyUnicode_DATA(mapping); |
| 7436 | mapkind = PyUnicode_KIND(mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7437 | while (s < e) { |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7438 | unsigned char ch; |
| 7439 | if (mapkind == PyUnicode_2BYTE_KIND && maplen >= 256) { |
| 7440 | enum PyUnicode_Kind outkind = PyUnicode_KIND(v); |
| 7441 | if (outkind == PyUnicode_1BYTE_KIND) { |
| 7442 | void *outdata = PyUnicode_DATA(v); |
| 7443 | Py_UCS4 maxchar = PyUnicode_MAX_CHAR_VALUE(v); |
| 7444 | while (s < e) { |
| 7445 | unsigned char ch = *s; |
| 7446 | x = PyUnicode_READ(PyUnicode_2BYTE_KIND, mapdata, ch); |
| 7447 | if (x > maxchar) |
| 7448 | goto Error; |
| 7449 | PyUnicode_WRITE(PyUnicode_1BYTE_KIND, outdata, outpos++, x); |
| 7450 | ++s; |
| 7451 | } |
| 7452 | break; |
| 7453 | } |
| 7454 | else if (outkind == PyUnicode_2BYTE_KIND) { |
| 7455 | void *outdata = PyUnicode_DATA(v); |
| 7456 | while (s < e) { |
| 7457 | unsigned char ch = *s; |
| 7458 | x = PyUnicode_READ(PyUnicode_2BYTE_KIND, mapdata, ch); |
| 7459 | if (x == 0xFFFE) |
| 7460 | goto Error; |
| 7461 | PyUnicode_WRITE(PyUnicode_2BYTE_KIND, outdata, outpos++, x); |
| 7462 | ++s; |
| 7463 | } |
| 7464 | break; |
| 7465 | } |
| 7466 | } |
| 7467 | ch = *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7468 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7469 | if (ch < maplen) |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7470 | x = PyUnicode_READ(mapkind, mapdata, ch); |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7471 | else |
| 7472 | x = 0xfffe; /* invalid value */ |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7473 | Error: |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7474 | if (x == 0xfffe) |
| 7475 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7476 | /* undefined mapping */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7477 | startinpos = s-starts; |
| 7478 | endinpos = startinpos+1; |
| 7479 | if (unicode_decode_call_errorhandler( |
| 7480 | errors, &errorHandler, |
| 7481 | "charmap", "character maps to <undefined>", |
| 7482 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7483 | &v, &outpos)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7484 | goto onError; |
| 7485 | } |
| 7486 | continue; |
| 7487 | } |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7488 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7489 | if (unicode_putchar(&v, &outpos, x) < 0) |
| 7490 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7491 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7492 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7493 | } |
| 7494 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7495 | while (s < e) { |
| 7496 | unsigned char ch = *s; |
| 7497 | PyObject *w, *x; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7498 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7499 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 7500 | w = PyLong_FromLong((long)ch); |
| 7501 | if (w == NULL) |
| 7502 | goto onError; |
| 7503 | x = PyObject_GetItem(mapping, w); |
| 7504 | Py_DECREF(w); |
| 7505 | if (x == NULL) { |
| 7506 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 7507 | /* No mapping found means: mapping is undefined. */ |
| 7508 | PyErr_Clear(); |
| 7509 | x = Py_None; |
| 7510 | Py_INCREF(x); |
| 7511 | } else |
| 7512 | goto onError; |
| 7513 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7514 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7515 | /* Apply mapping */ |
| 7516 | if (PyLong_Check(x)) { |
| 7517 | long value = PyLong_AS_LONG(x); |
Antoine Pitrou | a1f7655 | 2012-09-23 20:00:04 +0200 | [diff] [blame] | 7518 | if (value < 0 || value > MAX_UNICODE) { |
| 7519 | PyErr_Format(PyExc_TypeError, |
| 7520 | "character mapping must be in range(0x%lx)", |
| 7521 | (unsigned long)MAX_UNICODE + 1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7522 | Py_DECREF(x); |
| 7523 | goto onError; |
| 7524 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7525 | if (unicode_putchar(&v, &outpos, value) < 0) |
| 7526 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7527 | } |
| 7528 | else if (x == Py_None) { |
| 7529 | /* undefined mapping */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7530 | startinpos = s-starts; |
| 7531 | endinpos = startinpos+1; |
| 7532 | if (unicode_decode_call_errorhandler( |
| 7533 | errors, &errorHandler, |
| 7534 | "charmap", "character maps to <undefined>", |
| 7535 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7536 | &v, &outpos)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7537 | Py_DECREF(x); |
| 7538 | goto onError; |
| 7539 | } |
| 7540 | Py_DECREF(x); |
| 7541 | continue; |
| 7542 | } |
| 7543 | else if (PyUnicode_Check(x)) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7544 | Py_ssize_t targetsize; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7545 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7546 | if (PyUnicode_READY(x) == -1) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7547 | goto onError; |
| 7548 | targetsize = PyUnicode_GET_LENGTH(x); |
| 7549 | |
| 7550 | if (targetsize == 1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7551 | /* 1-1 mapping */ |
Victor Stinner | 62aa4d0 | 2011-11-09 00:03:45 +0100 | [diff] [blame] | 7552 | if (unicode_putchar(&v, &outpos, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7553 | PyUnicode_READ_CHAR(x, 0)) < 0) |
| 7554 | goto onError; |
| 7555 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7556 | else if (targetsize > 1) { |
| 7557 | /* 1-n mapping */ |
| 7558 | if (targetsize > extrachars) { |
| 7559 | /* resize first */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7560 | Py_ssize_t needed = (targetsize - extrachars) + \ |
| 7561 | (targetsize << 2); |
| 7562 | extrachars += needed; |
| 7563 | /* XXX overflow detection missing */ |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 7564 | if (unicode_resize(&v, |
| 7565 | PyUnicode_GET_LENGTH(v) + needed) < 0) |
| 7566 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7567 | Py_DECREF(x); |
| 7568 | goto onError; |
| 7569 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7570 | } |
Victor Stinner | 1b487b4 | 2012-05-03 12:29:04 +0200 | [diff] [blame] | 7571 | if (unicode_widen(&v, outpos, PyUnicode_MAX_CHAR_VALUE(x)) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7572 | goto onError; |
| 7573 | PyUnicode_CopyCharacters(v, outpos, x, 0, targetsize); |
| 7574 | outpos += targetsize; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7575 | extrachars -= targetsize; |
| 7576 | } |
| 7577 | /* 1-0 mapping: skip the character */ |
| 7578 | } |
| 7579 | else { |
| 7580 | /* wrong return value */ |
| 7581 | PyErr_SetString(PyExc_TypeError, |
| 7582 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7583 | Py_DECREF(x); |
| 7584 | goto onError; |
| 7585 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7586 | Py_DECREF(x); |
| 7587 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7588 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7589 | } |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 7590 | if (unicode_resize(&v, outpos) < 0) |
Antoine Pitrou | a8f63c0 | 2011-11-08 18:37:16 +0100 | [diff] [blame] | 7591 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7592 | Py_XDECREF(errorHandler); |
| 7593 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 7594 | return unicode_result(v); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7595 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7596 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7597 | Py_XDECREF(errorHandler); |
| 7598 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7599 | Py_XDECREF(v); |
| 7600 | return NULL; |
| 7601 | } |
| 7602 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7603 | /* Charmap encoding: the lookup table */ |
| 7604 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7605 | struct encoding_map { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7606 | PyObject_HEAD |
| 7607 | unsigned char level1[32]; |
| 7608 | int count2, count3; |
| 7609 | unsigned char level23[1]; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7610 | }; |
| 7611 | |
| 7612 | static PyObject* |
| 7613 | encoding_map_size(PyObject *obj, PyObject* args) |
| 7614 | { |
| 7615 | struct encoding_map *map = (struct encoding_map*)obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7616 | return PyLong_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7617 | 128*map->count3); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7618 | } |
| 7619 | |
| 7620 | static PyMethodDef encoding_map_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7621 | {"size", encoding_map_size, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7622 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 7623 | { 0 } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7624 | }; |
| 7625 | |
| 7626 | static void |
| 7627 | encoding_map_dealloc(PyObject* o) |
| 7628 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7629 | PyObject_FREE(o); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7630 | } |
| 7631 | |
| 7632 | static PyTypeObject EncodingMapType = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7633 | PyVarObject_HEAD_INIT(NULL, 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7634 | "EncodingMap", /*tp_name*/ |
| 7635 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 7636 | 0, /*tp_itemsize*/ |
| 7637 | /* methods */ |
| 7638 | encoding_map_dealloc, /*tp_dealloc*/ |
| 7639 | 0, /*tp_print*/ |
| 7640 | 0, /*tp_getattr*/ |
| 7641 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 7642 | 0, /*tp_reserved*/ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7643 | 0, /*tp_repr*/ |
| 7644 | 0, /*tp_as_number*/ |
| 7645 | 0, /*tp_as_sequence*/ |
| 7646 | 0, /*tp_as_mapping*/ |
| 7647 | 0, /*tp_hash*/ |
| 7648 | 0, /*tp_call*/ |
| 7649 | 0, /*tp_str*/ |
| 7650 | 0, /*tp_getattro*/ |
| 7651 | 0, /*tp_setattro*/ |
| 7652 | 0, /*tp_as_buffer*/ |
| 7653 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 7654 | 0, /*tp_doc*/ |
| 7655 | 0, /*tp_traverse*/ |
| 7656 | 0, /*tp_clear*/ |
| 7657 | 0, /*tp_richcompare*/ |
| 7658 | 0, /*tp_weaklistoffset*/ |
| 7659 | 0, /*tp_iter*/ |
| 7660 | 0, /*tp_iternext*/ |
| 7661 | encoding_map_methods, /*tp_methods*/ |
| 7662 | 0, /*tp_members*/ |
| 7663 | 0, /*tp_getset*/ |
| 7664 | 0, /*tp_base*/ |
| 7665 | 0, /*tp_dict*/ |
| 7666 | 0, /*tp_descr_get*/ |
| 7667 | 0, /*tp_descr_set*/ |
| 7668 | 0, /*tp_dictoffset*/ |
| 7669 | 0, /*tp_init*/ |
| 7670 | 0, /*tp_alloc*/ |
| 7671 | 0, /*tp_new*/ |
| 7672 | 0, /*tp_free*/ |
| 7673 | 0, /*tp_is_gc*/ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7674 | }; |
| 7675 | |
| 7676 | PyObject* |
| 7677 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 7678 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7679 | PyObject *result; |
| 7680 | struct encoding_map *mresult; |
| 7681 | int i; |
| 7682 | int need_dict = 0; |
| 7683 | unsigned char level1[32]; |
| 7684 | unsigned char level2[512]; |
| 7685 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 7686 | int count2 = 0, count3 = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7687 | int kind; |
| 7688 | void *data; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7689 | Py_ssize_t length; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7690 | Py_UCS4 ch; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7691 | |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7692 | if (!PyUnicode_Check(string) || !PyUnicode_GET_LENGTH(string)) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7693 | PyErr_BadArgument(); |
| 7694 | return NULL; |
| 7695 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7696 | kind = PyUnicode_KIND(string); |
| 7697 | data = PyUnicode_DATA(string); |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7698 | length = PyUnicode_GET_LENGTH(string); |
| 7699 | length = Py_MIN(length, 256); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7700 | memset(level1, 0xFF, sizeof level1); |
| 7701 | memset(level2, 0xFF, sizeof level2); |
| 7702 | |
| 7703 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 7704 | or if there are non-BMP characters, we need to use |
| 7705 | a mapping dictionary. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7706 | if (PyUnicode_READ(kind, data, 0) != 0) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7707 | need_dict = 1; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7708 | for (i = 1; i < length; i++) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7709 | int l1, l2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7710 | ch = PyUnicode_READ(kind, data, i); |
| 7711 | if (ch == 0 || ch > 0xFFFF) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7712 | need_dict = 1; |
| 7713 | break; |
| 7714 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7715 | if (ch == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7716 | /* unmapped character */ |
| 7717 | continue; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7718 | l1 = ch >> 11; |
| 7719 | l2 = ch >> 7; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7720 | if (level1[l1] == 0xFF) |
| 7721 | level1[l1] = count2++; |
| 7722 | if (level2[l2] == 0xFF) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7723 | level2[l2] = count3++; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7724 | } |
| 7725 | |
| 7726 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 7727 | need_dict = 1; |
| 7728 | |
| 7729 | if (need_dict) { |
| 7730 | PyObject *result = PyDict_New(); |
| 7731 | PyObject *key, *value; |
| 7732 | if (!result) |
| 7733 | return NULL; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7734 | for (i = 0; i < length; i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7735 | key = PyLong_FromLong(PyUnicode_READ(kind, data, i)); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7736 | value = PyLong_FromLong(i); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7737 | if (!key || !value) |
| 7738 | goto failed1; |
| 7739 | if (PyDict_SetItem(result, key, value) == -1) |
| 7740 | goto failed1; |
| 7741 | Py_DECREF(key); |
| 7742 | Py_DECREF(value); |
| 7743 | } |
| 7744 | return result; |
| 7745 | failed1: |
| 7746 | Py_XDECREF(key); |
| 7747 | Py_XDECREF(value); |
| 7748 | Py_DECREF(result); |
| 7749 | return NULL; |
| 7750 | } |
| 7751 | |
| 7752 | /* Create a three-level trie */ |
| 7753 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 7754 | 16*count2 + 128*count3 - 1); |
| 7755 | if (!result) |
| 7756 | return PyErr_NoMemory(); |
| 7757 | PyObject_Init(result, &EncodingMapType); |
| 7758 | mresult = (struct encoding_map*)result; |
| 7759 | mresult->count2 = count2; |
| 7760 | mresult->count3 = count3; |
| 7761 | mlevel1 = mresult->level1; |
| 7762 | mlevel2 = mresult->level23; |
| 7763 | mlevel3 = mresult->level23 + 16*count2; |
| 7764 | memcpy(mlevel1, level1, 32); |
| 7765 | memset(mlevel2, 0xFF, 16*count2); |
| 7766 | memset(mlevel3, 0, 128*count3); |
| 7767 | count3 = 0; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7768 | for (i = 1; i < length; i++) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7769 | int o1, o2, o3, i2, i3; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7770 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 7771 | if (ch == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7772 | /* unmapped character */ |
| 7773 | continue; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7774 | o1 = ch>>11; |
| 7775 | o2 = (ch>>7) & 0xF; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7776 | i2 = 16*mlevel1[o1] + o2; |
| 7777 | if (mlevel2[i2] == 0xFF) |
| 7778 | mlevel2[i2] = count3++; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7779 | o3 = ch & 0x7F; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7780 | i3 = 128*mlevel2[i2] + o3; |
| 7781 | mlevel3[i3] = i; |
| 7782 | } |
| 7783 | return result; |
| 7784 | } |
| 7785 | |
| 7786 | static int |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 7787 | encoding_map_lookup(Py_UCS4 c, PyObject *mapping) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7788 | { |
| 7789 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 7790 | int l1 = c>>11; |
| 7791 | int l2 = (c>>7) & 0xF; |
| 7792 | int l3 = c & 0x7F; |
| 7793 | int i; |
| 7794 | |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 7795 | if (c > 0xFFFF) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7796 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7797 | if (c == 0) |
| 7798 | return 0; |
| 7799 | /* level 1*/ |
| 7800 | i = map->level1[l1]; |
| 7801 | if (i == 0xFF) { |
| 7802 | return -1; |
| 7803 | } |
| 7804 | /* level 2*/ |
| 7805 | i = map->level23[16*i+l2]; |
| 7806 | if (i == 0xFF) { |
| 7807 | return -1; |
| 7808 | } |
| 7809 | /* level 3 */ |
| 7810 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 7811 | if (i == 0) { |
| 7812 | return -1; |
| 7813 | } |
| 7814 | return i; |
| 7815 | } |
| 7816 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7817 | /* Lookup the character ch in the mapping. If the character |
| 7818 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 7819 | error occurred). */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7820 | static PyObject * |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 7821 | charmapencode_lookup(Py_UCS4 c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7822 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7823 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7824 | PyObject *x; |
| 7825 | |
| 7826 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7827 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7828 | x = PyObject_GetItem(mapping, w); |
| 7829 | Py_DECREF(w); |
| 7830 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7831 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 7832 | /* No mapping found means: mapping is undefined. */ |
| 7833 | PyErr_Clear(); |
| 7834 | x = Py_None; |
| 7835 | Py_INCREF(x); |
| 7836 | return x; |
| 7837 | } else |
| 7838 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7839 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 7840 | else if (x == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7841 | return x; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7842 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7843 | long value = PyLong_AS_LONG(x); |
| 7844 | if (value < 0 || value > 255) { |
| 7845 | PyErr_SetString(PyExc_TypeError, |
| 7846 | "character mapping must be in range(256)"); |
| 7847 | Py_DECREF(x); |
| 7848 | return NULL; |
| 7849 | } |
| 7850 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7851 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7852 | else if (PyBytes_Check(x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7853 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7854 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7855 | /* wrong return value */ |
| 7856 | PyErr_Format(PyExc_TypeError, |
| 7857 | "character mapping must return integer, bytes or None, not %.400s", |
| 7858 | x->ob_type->tp_name); |
| 7859 | Py_DECREF(x); |
| 7860 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7861 | } |
| 7862 | } |
| 7863 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7864 | static int |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 7865 | charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7866 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7867 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
| 7868 | /* exponentially overallocate to minimize reallocations */ |
| 7869 | if (requiredsize < 2*outsize) |
| 7870 | requiredsize = 2*outsize; |
| 7871 | if (_PyBytes_Resize(outobj, requiredsize)) |
| 7872 | return -1; |
| 7873 | return 0; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7874 | } |
| 7875 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7876 | typedef enum charmapencode_result { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7877 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7878 | } charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7879 | /* 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] | 7880 | various state variables. Resize the output bytes object if not enough |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7881 | space is available. Return a new reference to the object that |
| 7882 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 7883 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 7884 | reallocation error occurred. The caller must decref the result */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7885 | static charmapencode_result |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 7886 | charmapencode_output(Py_UCS4 c, PyObject *mapping, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7887 | PyObject **outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7888 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7889 | PyObject *rep; |
| 7890 | char *outstart; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7891 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7892 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7893 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7894 | int res = encoding_map_lookup(c, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7895 | Py_ssize_t requiredsize = *outpos+1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7896 | if (res == -1) |
| 7897 | return enc_FAILED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7898 | if (outsize<requiredsize) |
| 7899 | if (charmapencode_resize(outobj, outpos, requiredsize)) |
| 7900 | return enc_EXCEPTION; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7901 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7902 | outstart[(*outpos)++] = (char)res; |
| 7903 | return enc_SUCCESS; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7904 | } |
| 7905 | |
| 7906 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7907 | if (rep==NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7908 | return enc_EXCEPTION; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7909 | else if (rep==Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7910 | Py_DECREF(rep); |
| 7911 | return enc_FAILED; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7912 | } else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7913 | if (PyLong_Check(rep)) { |
| 7914 | Py_ssize_t requiredsize = *outpos+1; |
| 7915 | if (outsize<requiredsize) |
| 7916 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 7917 | Py_DECREF(rep); |
| 7918 | return enc_EXCEPTION; |
| 7919 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7920 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7921 | outstart[(*outpos)++] = (char)PyLong_AS_LONG(rep); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7922 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7923 | else { |
| 7924 | const char *repchars = PyBytes_AS_STRING(rep); |
| 7925 | Py_ssize_t repsize = PyBytes_GET_SIZE(rep); |
| 7926 | Py_ssize_t requiredsize = *outpos+repsize; |
| 7927 | if (outsize<requiredsize) |
| 7928 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 7929 | Py_DECREF(rep); |
| 7930 | return enc_EXCEPTION; |
| 7931 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7932 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7933 | memcpy(outstart + *outpos, repchars, repsize); |
| 7934 | *outpos += repsize; |
| 7935 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7936 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7937 | Py_DECREF(rep); |
| 7938 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7939 | } |
| 7940 | |
| 7941 | /* handle an error in PyUnicode_EncodeCharmap |
| 7942 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7943 | static int |
| 7944 | charmap_encoding_error( |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7945 | PyObject *unicode, Py_ssize_t *inpos, PyObject *mapping, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7946 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 7947 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 7948 | PyObject **res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7949 | { |
| 7950 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7951 | Py_ssize_t size, repsize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7952 | Py_ssize_t newpos; |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 7953 | enum PyUnicode_Kind kind; |
| 7954 | void *data; |
| 7955 | Py_ssize_t index; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7956 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7957 | Py_ssize_t collstartpos = *inpos; |
| 7958 | Py_ssize_t collendpos = *inpos+1; |
| 7959 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7960 | char *encoding = "charmap"; |
| 7961 | char *reason = "character maps to <undefined>"; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7962 | charmapencode_result x; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7963 | Py_UCS4 ch; |
Brian Curtin | 2787ea4 | 2011-11-02 15:09:37 -0500 | [diff] [blame] | 7964 | int val; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7965 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7966 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7967 | return -1; |
| 7968 | size = PyUnicode_GET_LENGTH(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7969 | /* find all unencodable characters */ |
| 7970 | while (collendpos < size) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7971 | PyObject *rep; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7972 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7973 | ch = PyUnicode_READ_CHAR(unicode, collendpos); |
Brian Curtin | 2787ea4 | 2011-11-02 15:09:37 -0500 | [diff] [blame] | 7974 | val = encoding_map_lookup(ch, mapping); |
| 7975 | if (val != -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7976 | break; |
| 7977 | ++collendpos; |
| 7978 | continue; |
| 7979 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7980 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7981 | ch = PyUnicode_READ_CHAR(unicode, collendpos); |
| 7982 | rep = charmapencode_lookup(ch, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7983 | if (rep==NULL) |
| 7984 | return -1; |
| 7985 | else if (rep!=Py_None) { |
| 7986 | Py_DECREF(rep); |
| 7987 | break; |
| 7988 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7989 | Py_DECREF(rep); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7990 | ++collendpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7991 | } |
| 7992 | /* cache callback name lookup |
| 7993 | * (if not done yet, i.e. it's the first error) */ |
| 7994 | if (*known_errorHandler==-1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7995 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 7996 | *known_errorHandler = 1; |
| 7997 | else if (!strcmp(errors, "replace")) |
| 7998 | *known_errorHandler = 2; |
| 7999 | else if (!strcmp(errors, "ignore")) |
| 8000 | *known_errorHandler = 3; |
| 8001 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 8002 | *known_errorHandler = 4; |
| 8003 | else |
| 8004 | *known_errorHandler = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8005 | } |
| 8006 | switch (*known_errorHandler) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8007 | case 1: /* strict */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8008 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8009 | return -1; |
| 8010 | case 2: /* replace */ |
| 8011 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8012 | x = charmapencode_output('?', mapping, res, respos); |
| 8013 | if (x==enc_EXCEPTION) { |
| 8014 | return -1; |
| 8015 | } |
| 8016 | else if (x==enc_FAILED) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8017 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8018 | return -1; |
| 8019 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8020 | } |
| 8021 | /* fall through */ |
| 8022 | case 3: /* ignore */ |
| 8023 | *inpos = collendpos; |
| 8024 | break; |
| 8025 | case 4: /* xmlcharrefreplace */ |
| 8026 | /* generate replacement (temporarily (mis)uses p) */ |
| 8027 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8028 | char buffer[2+29+1+1]; |
| 8029 | char *cp; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8030 | sprintf(buffer, "&#%d;", (int)PyUnicode_READ_CHAR(unicode, collpos)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8031 | for (cp = buffer; *cp; ++cp) { |
| 8032 | x = charmapencode_output(*cp, mapping, res, respos); |
| 8033 | if (x==enc_EXCEPTION) |
| 8034 | return -1; |
| 8035 | else if (x==enc_FAILED) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8036 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8037 | return -1; |
| 8038 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8039 | } |
| 8040 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8041 | *inpos = collendpos; |
| 8042 | break; |
| 8043 | default: |
| 8044 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8045 | encoding, reason, unicode, exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8046 | collstartpos, collendpos, &newpos); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8047 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8048 | return -1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8049 | if (PyBytes_Check(repunicode)) { |
| 8050 | /* Directly copy bytes result to output. */ |
| 8051 | Py_ssize_t outsize = PyBytes_Size(*res); |
| 8052 | Py_ssize_t requiredsize; |
| 8053 | repsize = PyBytes_Size(repunicode); |
| 8054 | requiredsize = *respos + repsize; |
| 8055 | if (requiredsize > outsize) |
| 8056 | /* Make room for all additional bytes. */ |
| 8057 | if (charmapencode_resize(res, respos, requiredsize)) { |
| 8058 | Py_DECREF(repunicode); |
| 8059 | return -1; |
| 8060 | } |
| 8061 | memcpy(PyBytes_AsString(*res) + *respos, |
| 8062 | PyBytes_AsString(repunicode), repsize); |
| 8063 | *respos += repsize; |
| 8064 | *inpos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8065 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8066 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8067 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8068 | /* generate replacement */ |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 8069 | if (PyUnicode_READY(repunicode) == -1) { |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8070 | Py_DECREF(repunicode); |
| 8071 | return -1; |
| 8072 | } |
Victor Stinner | 9e30aa5 | 2011-11-21 02:49:52 +0100 | [diff] [blame] | 8073 | repsize = PyUnicode_GET_LENGTH(repunicode); |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8074 | data = PyUnicode_DATA(repunicode); |
| 8075 | kind = PyUnicode_KIND(repunicode); |
| 8076 | for (index = 0; index < repsize; index++) { |
| 8077 | Py_UCS4 repch = PyUnicode_READ(kind, data, index); |
| 8078 | x = charmapencode_output(repch, mapping, res, respos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8079 | if (x==enc_EXCEPTION) { |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8080 | Py_DECREF(repunicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8081 | return -1; |
| 8082 | } |
| 8083 | else if (x==enc_FAILED) { |
| 8084 | Py_DECREF(repunicode); |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8085 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8086 | return -1; |
| 8087 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8088 | } |
| 8089 | *inpos = newpos; |
| 8090 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8091 | } |
| 8092 | return 0; |
| 8093 | } |
| 8094 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8095 | PyObject * |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8096 | _PyUnicode_EncodeCharmap(PyObject *unicode, |
| 8097 | PyObject *mapping, |
| 8098 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8099 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8100 | /* output object */ |
| 8101 | PyObject *res = NULL; |
| 8102 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8103 | Py_ssize_t inpos = 0; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8104 | Py_ssize_t size; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8105 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8106 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8107 | PyObject *errorHandler = NULL; |
| 8108 | PyObject *exc = NULL; |
| 8109 | /* the following variable is used for caching string comparisons |
| 8110 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 8111 | * 3=ignore, 4=xmlcharrefreplace */ |
| 8112 | int known_errorHandler = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8113 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 8114 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8115 | return NULL; |
| 8116 | size = PyUnicode_GET_LENGTH(unicode); |
| 8117 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8118 | /* Default to Latin-1 */ |
| 8119 | if (mapping == NULL) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8120 | return unicode_encode_ucs1(unicode, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8121 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8122 | /* allocate enough for a simple encoding without |
| 8123 | replacements, if we need more, we'll resize */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8124 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8125 | if (res == NULL) |
| 8126 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 8127 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8128 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8129 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8130 | while (inpos<size) { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8131 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, inpos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8132 | /* try to encode it */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8133 | charmapencode_result x = charmapencode_output(ch, mapping, &res, &respos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8134 | if (x==enc_EXCEPTION) /* error */ |
| 8135 | goto onError; |
| 8136 | if (x==enc_FAILED) { /* unencodable character */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8137 | if (charmap_encoding_error(unicode, &inpos, mapping, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8138 | &exc, |
| 8139 | &known_errorHandler, &errorHandler, errors, |
| 8140 | &res, &respos)) { |
| 8141 | goto onError; |
| 8142 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8143 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8144 | else |
| 8145 | /* done with this character => adjust input position */ |
| 8146 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8147 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8148 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8149 | /* Resize if we allocated to much */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8150 | if (respos<PyBytes_GET_SIZE(res)) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 8151 | if (_PyBytes_Resize(&res, respos) < 0) |
| 8152 | goto onError; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8153 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8154 | Py_XDECREF(exc); |
| 8155 | Py_XDECREF(errorHandler); |
| 8156 | return res; |
| 8157 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8158 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8159 | Py_XDECREF(res); |
| 8160 | Py_XDECREF(exc); |
| 8161 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8162 | return NULL; |
| 8163 | } |
| 8164 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8165 | /* Deprecated */ |
| 8166 | PyObject * |
| 8167 | PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
| 8168 | Py_ssize_t size, |
| 8169 | PyObject *mapping, |
| 8170 | const char *errors) |
| 8171 | { |
| 8172 | PyObject *result; |
| 8173 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 8174 | if (unicode == NULL) |
| 8175 | return NULL; |
| 8176 | result = _PyUnicode_EncodeCharmap(unicode, mapping, errors); |
| 8177 | Py_DECREF(unicode); |
Victor Stinner | fc026c9 | 2011-11-04 00:24:51 +0100 | [diff] [blame] | 8178 | return result; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8179 | } |
| 8180 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8181 | PyObject * |
| 8182 | PyUnicode_AsCharmapString(PyObject *unicode, |
| 8183 | PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8184 | { |
| 8185 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8186 | PyErr_BadArgument(); |
| 8187 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8188 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8189 | return _PyUnicode_EncodeCharmap(unicode, mapping, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8190 | } |
| 8191 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8192 | /* create or adjust a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8193 | static void |
| 8194 | make_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8195 | PyObject *unicode, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8196 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8197 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8198 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8199 | if (*exceptionObject == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8200 | *exceptionObject = _PyUnicodeTranslateError_Create( |
| 8201 | unicode, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8202 | } |
| 8203 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8204 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 8205 | goto onError; |
| 8206 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 8207 | goto onError; |
| 8208 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 8209 | goto onError; |
| 8210 | return; |
| 8211 | onError: |
| 8212 | Py_DECREF(*exceptionObject); |
| 8213 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8214 | } |
| 8215 | } |
| 8216 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8217 | /* raises a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8218 | static void |
| 8219 | raise_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8220 | PyObject *unicode, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8221 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8222 | const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8223 | { |
| 8224 | make_translate_exception(exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8225 | unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8226 | if (*exceptionObject != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8227 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8228 | } |
| 8229 | |
| 8230 | /* error handling callback helper: |
| 8231 | build arguments, call the callback and check the arguments, |
| 8232 | put the result into newpos and return the replacement string, which |
| 8233 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8234 | static PyObject * |
| 8235 | unicode_translate_call_errorhandler(const char *errors, |
| 8236 | PyObject **errorHandler, |
| 8237 | const char *reason, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8238 | PyObject *unicode, PyObject **exceptionObject, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8239 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8240 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8241 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8242 | 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] | 8243 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8244 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8245 | PyObject *restuple; |
| 8246 | PyObject *resunicode; |
| 8247 | |
| 8248 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8249 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8250 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8251 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8252 | } |
| 8253 | |
| 8254 | make_translate_exception(exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8255 | unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8256 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8257 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8258 | |
| 8259 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8260 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8261 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8262 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8263 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 8264 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8265 | Py_DECREF(restuple); |
| 8266 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8267 | } |
| 8268 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8269 | &resunicode, &i_newpos)) { |
| 8270 | Py_DECREF(restuple); |
| 8271 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8272 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8273 | if (i_newpos<0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8274 | *newpos = PyUnicode_GET_LENGTH(unicode)+i_newpos; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8275 | else |
| 8276 | *newpos = i_newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8277 | if (*newpos<0 || *newpos>PyUnicode_GET_LENGTH(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8278 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 8279 | Py_DECREF(restuple); |
| 8280 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 8281 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8282 | Py_INCREF(resunicode); |
| 8283 | Py_DECREF(restuple); |
| 8284 | return resunicode; |
| 8285 | } |
| 8286 | |
| 8287 | /* Lookup the character ch in the mapping and put the result in result, |
| 8288 | which must be decrefed by the caller. |
| 8289 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8290 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8291 | charmaptranslate_lookup(Py_UCS4 c, PyObject *mapping, PyObject **result) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8292 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8293 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8294 | PyObject *x; |
| 8295 | |
| 8296 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8297 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8298 | x = PyObject_GetItem(mapping, w); |
| 8299 | Py_DECREF(w); |
| 8300 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8301 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 8302 | /* No mapping found means: use 1:1 mapping. */ |
| 8303 | PyErr_Clear(); |
| 8304 | *result = NULL; |
| 8305 | return 0; |
| 8306 | } else |
| 8307 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8308 | } |
| 8309 | else if (x == Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8310 | *result = x; |
| 8311 | return 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8312 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8313 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8314 | long value = PyLong_AS_LONG(x); |
| 8315 | long max = PyUnicode_GetMax(); |
| 8316 | if (value < 0 || value > max) { |
| 8317 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5a2f7e60 | 2007-10-24 21:13:09 +0000 | [diff] [blame] | 8318 | "character mapping must be in range(0x%x)", max+1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8319 | Py_DECREF(x); |
| 8320 | return -1; |
| 8321 | } |
| 8322 | *result = x; |
| 8323 | return 0; |
| 8324 | } |
| 8325 | else if (PyUnicode_Check(x)) { |
| 8326 | *result = x; |
| 8327 | return 0; |
| 8328 | } |
| 8329 | else { |
| 8330 | /* wrong return value */ |
| 8331 | PyErr_SetString(PyExc_TypeError, |
| 8332 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8333 | Py_DECREF(x); |
| 8334 | return -1; |
| 8335 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8336 | } |
| 8337 | /* ensure that *outobj is at least requiredsize characters long, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8338 | if not reallocate and adjust various state variables. |
| 8339 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8340 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8341 | charmaptranslate_makespace(Py_UCS4 **outobj, Py_ssize_t *psize, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8342 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8343 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8344 | Py_ssize_t oldsize = *psize; |
Kristjan Valur Jonsson | 85634d7 | 2012-05-31 09:37:31 +0000 | [diff] [blame] | 8345 | Py_UCS4 *new_outobj; |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 8346 | if (requiredsize > oldsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8347 | /* exponentially overallocate to minimize reallocations */ |
| 8348 | if (requiredsize < 2 * oldsize) |
| 8349 | requiredsize = 2 * oldsize; |
Kristjan Valur Jonsson | 85634d7 | 2012-05-31 09:37:31 +0000 | [diff] [blame] | 8350 | new_outobj = PyMem_Realloc(*outobj, requiredsize * sizeof(Py_UCS4)); |
| 8351 | if (new_outobj == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8352 | return -1; |
Kristjan Valur Jonsson | 85634d7 | 2012-05-31 09:37:31 +0000 | [diff] [blame] | 8353 | *outobj = new_outobj; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8354 | *psize = requiredsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8355 | } |
| 8356 | return 0; |
| 8357 | } |
| 8358 | /* lookup the character, put the result in the output string and adjust |
| 8359 | various state variables. Return a new reference to the object that |
| 8360 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 8361 | undefined (in which case no character was written). |
| 8362 | The called must decref result. |
| 8363 | Return 0 on success, -1 on error. */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8364 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8365 | charmaptranslate_output(PyObject *input, Py_ssize_t ipos, |
| 8366 | PyObject *mapping, Py_UCS4 **output, |
| 8367 | Py_ssize_t *osize, Py_ssize_t *opos, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8368 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8369 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8370 | Py_UCS4 curinp = PyUnicode_READ_CHAR(input, ipos); |
| 8371 | if (charmaptranslate_lookup(curinp, mapping, res)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8372 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8373 | if (*res==NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8374 | /* not found => default to 1:1 mapping */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8375 | (*output)[(*opos)++] = curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8376 | } |
| 8377 | else if (*res==Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8378 | ; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8379 | else if (PyLong_Check(*res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8380 | /* 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] | 8381 | (*output)[(*opos)++] = (Py_UCS4)PyLong_AS_LONG(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8382 | } |
| 8383 | else if (PyUnicode_Check(*res)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8384 | Py_ssize_t repsize; |
| 8385 | if (PyUnicode_READY(*res) == -1) |
| 8386 | return -1; |
| 8387 | repsize = PyUnicode_GET_LENGTH(*res); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8388 | if (repsize==1) { |
| 8389 | /* 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] | 8390 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8391 | } |
| 8392 | else if (repsize!=0) { |
| 8393 | /* more than one character */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8394 | Py_ssize_t requiredsize = *opos + |
| 8395 | (PyUnicode_GET_LENGTH(input) - ipos) + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8396 | repsize - 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8397 | Py_ssize_t i; |
| 8398 | if (charmaptranslate_makespace(output, osize, requiredsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8399 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8400 | for(i = 0; i < repsize; i++) |
| 8401 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, i); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8402 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8403 | } |
| 8404 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8405 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8406 | return 0; |
| 8407 | } |
| 8408 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8409 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8410 | _PyUnicode_TranslateCharmap(PyObject *input, |
| 8411 | PyObject *mapping, |
| 8412 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8413 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8414 | /* input object */ |
| 8415 | char *idata; |
| 8416 | Py_ssize_t size, i; |
| 8417 | int kind; |
| 8418 | /* output buffer */ |
| 8419 | Py_UCS4 *output = NULL; |
| 8420 | Py_ssize_t osize; |
| 8421 | PyObject *res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8422 | /* current output position */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8423 | Py_ssize_t opos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8424 | char *reason = "character maps to <undefined>"; |
| 8425 | PyObject *errorHandler = NULL; |
| 8426 | PyObject *exc = NULL; |
| 8427 | /* the following variable is used for caching string comparisons |
| 8428 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 8429 | * 3=ignore, 4=xmlcharrefreplace */ |
| 8430 | int known_errorHandler = -1; |
| 8431 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8432 | if (mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8433 | PyErr_BadArgument(); |
| 8434 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8435 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8436 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8437 | if (PyUnicode_READY(input) == -1) |
| 8438 | return NULL; |
| 8439 | idata = (char*)PyUnicode_DATA(input); |
| 8440 | kind = PyUnicode_KIND(input); |
| 8441 | size = PyUnicode_GET_LENGTH(input); |
| 8442 | i = 0; |
| 8443 | |
| 8444 | if (size == 0) { |
| 8445 | Py_INCREF(input); |
| 8446 | return input; |
| 8447 | } |
| 8448 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8449 | /* allocate enough for a simple 1:1 translation without |
| 8450 | replacements, if we need more, we'll resize */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8451 | osize = size; |
| 8452 | output = PyMem_Malloc(osize * sizeof(Py_UCS4)); |
| 8453 | opos = 0; |
| 8454 | if (output == NULL) { |
| 8455 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8456 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8457 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8458 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8459 | while (i<size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8460 | /* try to encode it */ |
| 8461 | PyObject *x = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8462 | if (charmaptranslate_output(input, i, mapping, |
| 8463 | &output, &osize, &opos, &x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8464 | Py_XDECREF(x); |
| 8465 | goto onError; |
| 8466 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8467 | Py_XDECREF(x); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8468 | if (x!=Py_None) /* it worked => adjust input pointer */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8469 | ++i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8470 | else { /* untranslatable character */ |
| 8471 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
| 8472 | Py_ssize_t repsize; |
| 8473 | Py_ssize_t newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8474 | Py_ssize_t uni2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8475 | /* startpos for collecting untranslatable chars */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8476 | Py_ssize_t collstart = i; |
| 8477 | Py_ssize_t collend = i+1; |
| 8478 | Py_ssize_t coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8479 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8480 | /* find all untranslatable characters */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8481 | while (collend < size) { |
| 8482 | if (charmaptranslate_lookup(PyUnicode_READ(kind,idata, collend), mapping, &x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8483 | goto onError; |
| 8484 | Py_XDECREF(x); |
| 8485 | if (x!=Py_None) |
| 8486 | break; |
| 8487 | ++collend; |
| 8488 | } |
| 8489 | /* cache callback name lookup |
| 8490 | * (if not done yet, i.e. it's the first error) */ |
| 8491 | if (known_errorHandler==-1) { |
| 8492 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 8493 | known_errorHandler = 1; |
| 8494 | else if (!strcmp(errors, "replace")) |
| 8495 | known_errorHandler = 2; |
| 8496 | else if (!strcmp(errors, "ignore")) |
| 8497 | known_errorHandler = 3; |
| 8498 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 8499 | known_errorHandler = 4; |
| 8500 | else |
| 8501 | known_errorHandler = 0; |
| 8502 | } |
| 8503 | switch (known_errorHandler) { |
| 8504 | case 1: /* strict */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8505 | raise_translate_exception(&exc, input, collstart, |
| 8506 | collend, reason); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8507 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8508 | case 2: /* replace */ |
| 8509 | /* 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] | 8510 | for (coll = collstart; coll<collend; coll++) |
| 8511 | output[opos++] = '?'; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8512 | /* fall through */ |
| 8513 | case 3: /* ignore */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8514 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8515 | break; |
| 8516 | case 4: /* xmlcharrefreplace */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8517 | /* generate replacement (temporarily (mis)uses i) */ |
| 8518 | for (i = collstart; i < collend; ++i) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8519 | char buffer[2+29+1+1]; |
| 8520 | char *cp; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8521 | sprintf(buffer, "&#%d;", PyUnicode_READ(kind, idata, i)); |
| 8522 | if (charmaptranslate_makespace(&output, &osize, |
| 8523 | opos+strlen(buffer)+(size-collend))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8524 | goto onError; |
| 8525 | for (cp = buffer; *cp; ++cp) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8526 | output[opos++] = *cp; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8527 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8528 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8529 | break; |
| 8530 | default: |
| 8531 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8532 | reason, input, &exc, |
| 8533 | collstart, collend, &newpos); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 8534 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8535 | goto onError; |
Benjamin Peterson | 9ca3ffa | 2012-01-01 16:04:29 -0600 | [diff] [blame] | 8536 | if (PyUnicode_READY(repunicode) == -1) { |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 8537 | Py_DECREF(repunicode); |
| 8538 | goto onError; |
| 8539 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8540 | /* generate replacement */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8541 | repsize = PyUnicode_GET_LENGTH(repunicode); |
| 8542 | if (charmaptranslate_makespace(&output, &osize, |
| 8543 | opos+repsize+(size-collend))) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8544 | Py_DECREF(repunicode); |
| 8545 | goto onError; |
| 8546 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8547 | for (uni2 = 0; repsize-->0; ++uni2) |
| 8548 | output[opos++] = PyUnicode_READ_CHAR(repunicode, uni2); |
| 8549 | i = newpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8550 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8551 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8552 | } |
| 8553 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8554 | res = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, output, opos); |
| 8555 | if (!res) |
| 8556 | goto onError; |
| 8557 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8558 | Py_XDECREF(exc); |
| 8559 | Py_XDECREF(errorHandler); |
| 8560 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8561 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8562 | onError: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8563 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8564 | Py_XDECREF(exc); |
| 8565 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8566 | return NULL; |
| 8567 | } |
| 8568 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8569 | /* Deprecated. Use PyUnicode_Translate instead. */ |
| 8570 | PyObject * |
| 8571 | PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
| 8572 | Py_ssize_t size, |
| 8573 | PyObject *mapping, |
| 8574 | const char *errors) |
| 8575 | { |
Christian Heimes | 5f520f4 | 2012-09-11 14:03:25 +0200 | [diff] [blame] | 8576 | PyObject *result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8577 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 8578 | if (!unicode) |
| 8579 | return NULL; |
Christian Heimes | 5f520f4 | 2012-09-11 14:03:25 +0200 | [diff] [blame] | 8580 | result = _PyUnicode_TranslateCharmap(unicode, mapping, errors); |
| 8581 | Py_DECREF(unicode); |
| 8582 | return result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8583 | } |
| 8584 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8585 | PyObject * |
| 8586 | PyUnicode_Translate(PyObject *str, |
| 8587 | PyObject *mapping, |
| 8588 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8589 | { |
| 8590 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8591 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8592 | str = PyUnicode_FromObject(str); |
| 8593 | if (str == NULL) |
Christian Heimes | 5f520f4 | 2012-09-11 14:03:25 +0200 | [diff] [blame] | 8594 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8595 | result = _PyUnicode_TranslateCharmap(str, mapping, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8596 | Py_DECREF(str); |
| 8597 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8598 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8599 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8600 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8601 | fix_decimal_and_space_to_ascii(PyObject *self) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8602 | { |
| 8603 | /* No need to call PyUnicode_READY(self) because this function is only |
| 8604 | called as a callback from fixup() which does it already. */ |
| 8605 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8606 | const int kind = PyUnicode_KIND(self); |
| 8607 | void *data = PyUnicode_DATA(self); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 8608 | Py_UCS4 maxchar = 127, ch, fixed; |
Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame] | 8609 | int modified = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8610 | Py_ssize_t i; |
| 8611 | |
| 8612 | for (i = 0; i < len; ++i) { |
| 8613 | ch = PyUnicode_READ(kind, data, i); |
| 8614 | fixed = 0; |
| 8615 | if (ch > 127) { |
| 8616 | if (Py_UNICODE_ISSPACE(ch)) |
| 8617 | fixed = ' '; |
| 8618 | else { |
| 8619 | const int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8620 | if (decimal >= 0) |
| 8621 | fixed = '0' + decimal; |
| 8622 | } |
| 8623 | if (fixed != 0) { |
Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame] | 8624 | modified = 1; |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 8625 | maxchar = MAX_MAXCHAR(maxchar, fixed); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8626 | PyUnicode_WRITE(kind, data, i, fixed); |
| 8627 | } |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 8628 | else |
| 8629 | maxchar = MAX_MAXCHAR(maxchar, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8630 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8631 | } |
| 8632 | |
Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame] | 8633 | return (modified) ? maxchar : 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8634 | } |
| 8635 | |
| 8636 | PyObject * |
| 8637 | _PyUnicode_TransformDecimalAndSpaceToASCII(PyObject *unicode) |
| 8638 | { |
| 8639 | if (!PyUnicode_Check(unicode)) { |
| 8640 | PyErr_BadInternalCall(); |
| 8641 | return NULL; |
| 8642 | } |
| 8643 | if (PyUnicode_READY(unicode) == -1) |
| 8644 | return NULL; |
| 8645 | if (PyUnicode_MAX_CHAR_VALUE(unicode) <= 127) { |
| 8646 | /* If the string is already ASCII, just return the same string */ |
| 8647 | Py_INCREF(unicode); |
| 8648 | return unicode; |
| 8649 | } |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8650 | return fixup(unicode, fix_decimal_and_space_to_ascii); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8651 | } |
| 8652 | |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8653 | PyObject * |
| 8654 | PyUnicode_TransformDecimalToASCII(Py_UNICODE *s, |
| 8655 | Py_ssize_t length) |
| 8656 | { |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8657 | PyObject *decimal; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8658 | Py_ssize_t i; |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8659 | Py_UCS4 maxchar; |
| 8660 | enum PyUnicode_Kind kind; |
| 8661 | void *data; |
| 8662 | |
Victor Stinner | 99d7ad0 | 2012-02-22 13:37:39 +0100 | [diff] [blame] | 8663 | maxchar = 127; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8664 | for (i = 0; i < length; i++) { |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8665 | Py_UNICODE ch = s[i]; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8666 | if (ch > 127) { |
| 8667 | int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8668 | if (decimal >= 0) |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8669 | ch = '0' + decimal; |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 8670 | maxchar = MAX_MAXCHAR(maxchar, ch); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8671 | } |
| 8672 | } |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8673 | |
| 8674 | /* Copy to a new string */ |
| 8675 | decimal = PyUnicode_New(length, maxchar); |
| 8676 | if (decimal == NULL) |
| 8677 | return decimal; |
| 8678 | kind = PyUnicode_KIND(decimal); |
| 8679 | data = PyUnicode_DATA(decimal); |
| 8680 | /* Iterate over code points */ |
| 8681 | for (i = 0; i < length; i++) { |
| 8682 | Py_UNICODE ch = s[i]; |
| 8683 | if (ch > 127) { |
| 8684 | int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8685 | if (decimal >= 0) |
| 8686 | ch = '0' + decimal; |
| 8687 | } |
| 8688 | PyUnicode_WRITE(kind, data, i, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8689 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 8690 | return unicode_result(decimal); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8691 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8692 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 8693 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8694 | int |
| 8695 | PyUnicode_EncodeDecimal(Py_UNICODE *s, |
| 8696 | Py_ssize_t length, |
| 8697 | char *output, |
| 8698 | const char *errors) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8699 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8700 | PyObject *unicode; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8701 | Py_ssize_t i; |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8702 | enum PyUnicode_Kind kind; |
| 8703 | void *data; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8704 | |
| 8705 | if (output == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8706 | PyErr_BadArgument(); |
| 8707 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8708 | } |
| 8709 | |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8710 | unicode = PyUnicode_FromUnicode(s, length); |
| 8711 | if (unicode == NULL) |
| 8712 | return -1; |
| 8713 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 8714 | if (PyUnicode_READY(unicode) == -1) { |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8715 | Py_DECREF(unicode); |
| 8716 | return -1; |
| 8717 | } |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8718 | kind = PyUnicode_KIND(unicode); |
| 8719 | data = PyUnicode_DATA(unicode); |
| 8720 | |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8721 | for (i=0; i < length; ) { |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8722 | PyObject *exc; |
| 8723 | Py_UCS4 ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8724 | int decimal; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8725 | Py_ssize_t startpos; |
| 8726 | |
| 8727 | ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8728 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8729 | if (Py_UNICODE_ISSPACE(ch)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8730 | *output++ = ' '; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8731 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8732 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8733 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8734 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 8735 | if (decimal >= 0) { |
| 8736 | *output++ = '0' + decimal; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8737 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8738 | continue; |
| 8739 | } |
| 8740 | if (0 < ch && ch < 256) { |
| 8741 | *output++ = (char)ch; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8742 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8743 | continue; |
| 8744 | } |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8745 | |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8746 | startpos = i; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8747 | exc = NULL; |
| 8748 | raise_encode_exception(&exc, "decimal", unicode, |
| 8749 | startpos, startpos+1, |
| 8750 | "invalid decimal Unicode string"); |
| 8751 | Py_XDECREF(exc); |
| 8752 | Py_DECREF(unicode); |
| 8753 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8754 | } |
| 8755 | /* 0-terminate the output string */ |
| 8756 | *output++ = '\0'; |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8757 | Py_DECREF(unicode); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8758 | return 0; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8759 | } |
| 8760 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8761 | /* --- Helpers ------------------------------------------------------------ */ |
| 8762 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8763 | static Py_ssize_t |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 8764 | any_find_slice(int direction, PyObject* s1, PyObject* s2, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8765 | Py_ssize_t start, |
| 8766 | Py_ssize_t end) |
| 8767 | { |
| 8768 | int kind1, kind2, kind; |
| 8769 | void *buf1, *buf2; |
| 8770 | Py_ssize_t len1, len2, result; |
| 8771 | |
| 8772 | kind1 = PyUnicode_KIND(s1); |
| 8773 | kind2 = PyUnicode_KIND(s2); |
| 8774 | kind = kind1 > kind2 ? kind1 : kind2; |
| 8775 | buf1 = PyUnicode_DATA(s1); |
| 8776 | buf2 = PyUnicode_DATA(s2); |
| 8777 | if (kind1 != kind) |
| 8778 | buf1 = _PyUnicode_AsKind(s1, kind); |
| 8779 | if (!buf1) |
| 8780 | return -2; |
| 8781 | if (kind2 != kind) |
| 8782 | buf2 = _PyUnicode_AsKind(s2, kind); |
| 8783 | if (!buf2) { |
| 8784 | if (kind1 != kind) PyMem_Free(buf1); |
| 8785 | return -2; |
| 8786 | } |
| 8787 | len1 = PyUnicode_GET_LENGTH(s1); |
| 8788 | len2 = PyUnicode_GET_LENGTH(s2); |
| 8789 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 8790 | if (direction > 0) { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 8791 | switch (kind) { |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 8792 | case PyUnicode_1BYTE_KIND: |
| 8793 | if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2)) |
| 8794 | result = asciilib_find_slice(buf1, len1, buf2, len2, start, end); |
| 8795 | else |
| 8796 | result = ucs1lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 8797 | break; |
| 8798 | case PyUnicode_2BYTE_KIND: |
| 8799 | result = ucs2lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 8800 | break; |
| 8801 | case PyUnicode_4BYTE_KIND: |
| 8802 | result = ucs4lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 8803 | break; |
| 8804 | default: |
| 8805 | assert(0); result = -2; |
| 8806 | } |
| 8807 | } |
| 8808 | else { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 8809 | switch (kind) { |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 8810 | case PyUnicode_1BYTE_KIND: |
| 8811 | if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2)) |
| 8812 | result = asciilib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 8813 | else |
| 8814 | result = ucs1lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 8815 | break; |
| 8816 | case PyUnicode_2BYTE_KIND: |
| 8817 | result = ucs2lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 8818 | break; |
| 8819 | case PyUnicode_4BYTE_KIND: |
| 8820 | result = ucs4lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 8821 | break; |
| 8822 | default: |
| 8823 | assert(0); result = -2; |
| 8824 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8825 | } |
| 8826 | |
| 8827 | if (kind1 != kind) |
| 8828 | PyMem_Free(buf1); |
| 8829 | if (kind2 != kind) |
| 8830 | PyMem_Free(buf2); |
| 8831 | |
| 8832 | return result; |
| 8833 | } |
| 8834 | |
| 8835 | Py_ssize_t |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8836 | _PyUnicode_InsertThousandsGrouping( |
| 8837 | PyObject *unicode, Py_ssize_t index, |
| 8838 | Py_ssize_t n_buffer, |
| 8839 | void *digits, Py_ssize_t n_digits, |
| 8840 | Py_ssize_t min_width, |
| 8841 | const char *grouping, PyObject *thousands_sep, |
| 8842 | Py_UCS4 *maxchar) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8843 | { |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8844 | unsigned int kind, thousands_sep_kind; |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8845 | char *data, *thousands_sep_data; |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8846 | Py_ssize_t thousands_sep_len; |
| 8847 | Py_ssize_t len; |
| 8848 | |
| 8849 | if (unicode != NULL) { |
| 8850 | kind = PyUnicode_KIND(unicode); |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8851 | data = (char *) PyUnicode_DATA(unicode) + index * kind; |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8852 | } |
| 8853 | else { |
| 8854 | kind = PyUnicode_1BYTE_KIND; |
| 8855 | data = NULL; |
| 8856 | } |
| 8857 | thousands_sep_kind = PyUnicode_KIND(thousands_sep); |
| 8858 | thousands_sep_data = PyUnicode_DATA(thousands_sep); |
| 8859 | thousands_sep_len = PyUnicode_GET_LENGTH(thousands_sep); |
| 8860 | if (unicode != NULL && thousands_sep_kind != kind) { |
Victor Stinner | 90f50d4 | 2012-02-24 01:44:47 +0100 | [diff] [blame] | 8861 | if (thousands_sep_kind < kind) { |
| 8862 | thousands_sep_data = _PyUnicode_AsKind(thousands_sep, kind); |
| 8863 | if (!thousands_sep_data) |
| 8864 | return -1; |
| 8865 | } |
| 8866 | else { |
| 8867 | data = _PyUnicode_AsKind(unicode, thousands_sep_kind); |
| 8868 | if (!data) |
| 8869 | return -1; |
| 8870 | } |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8871 | } |
| 8872 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 8873 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8874 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8875 | if (unicode != NULL && PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8876 | len = asciilib_InsertThousandsGrouping( |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8877 | (Py_UCS1 *) data, n_buffer, (Py_UCS1 *) digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8878 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8879 | (Py_UCS1 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8880 | else |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8881 | len = ucs1lib_InsertThousandsGrouping( |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8882 | (Py_UCS1*)data, n_buffer, (Py_UCS1*)digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8883 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8884 | (Py_UCS1 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8885 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8886 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8887 | len = ucs2lib_InsertThousandsGrouping( |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8888 | (Py_UCS2 *) data, n_buffer, (Py_UCS2 *) digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8889 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8890 | (Py_UCS2 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8891 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8892 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8893 | len = ucs4lib_InsertThousandsGrouping( |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8894 | (Py_UCS4 *) data, n_buffer, (Py_UCS4 *) digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8895 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8896 | (Py_UCS4 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8897 | break; |
| 8898 | default: |
| 8899 | assert(0); |
| 8900 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8901 | } |
Victor Stinner | 90f50d4 | 2012-02-24 01:44:47 +0100 | [diff] [blame] | 8902 | if (unicode != NULL && thousands_sep_kind != kind) { |
| 8903 | if (thousands_sep_kind < kind) |
| 8904 | PyMem_Free(thousands_sep_data); |
| 8905 | else |
| 8906 | PyMem_Free(data); |
| 8907 | } |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8908 | if (unicode == NULL) { |
| 8909 | *maxchar = 127; |
| 8910 | if (len != n_digits) { |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 8911 | *maxchar = MAX_MAXCHAR(*maxchar, |
| 8912 | PyUnicode_MAX_CHAR_VALUE(thousands_sep)); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8913 | } |
| 8914 | } |
| 8915 | return len; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8916 | } |
| 8917 | |
| 8918 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8919 | /* helper macro to fixup start/end slice values */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 8920 | #define ADJUST_INDICES(start, end, len) \ |
| 8921 | if (end > len) \ |
| 8922 | end = len; \ |
| 8923 | else if (end < 0) { \ |
| 8924 | end += len; \ |
| 8925 | if (end < 0) \ |
| 8926 | end = 0; \ |
| 8927 | } \ |
| 8928 | if (start < 0) { \ |
| 8929 | start += len; \ |
| 8930 | if (start < 0) \ |
| 8931 | start = 0; \ |
| 8932 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8933 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8934 | Py_ssize_t |
| 8935 | PyUnicode_Count(PyObject *str, |
| 8936 | PyObject *substr, |
| 8937 | Py_ssize_t start, |
| 8938 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8939 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8940 | Py_ssize_t result; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 8941 | PyObject* str_obj; |
| 8942 | PyObject* sub_obj; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8943 | int kind1, kind2, kind; |
| 8944 | void *buf1 = NULL, *buf2 = NULL; |
| 8945 | Py_ssize_t len1, len2; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8946 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 8947 | str_obj = PyUnicode_FromObject(str); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 8948 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8949 | return -1; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 8950 | sub_obj = PyUnicode_FromObject(substr); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 8951 | if (!sub_obj) { |
| 8952 | Py_DECREF(str_obj); |
| 8953 | return -1; |
| 8954 | } |
Benjamin Peterson | 4c13a4a | 2012-01-02 09:07:38 -0600 | [diff] [blame] | 8955 | if (PyUnicode_READY(sub_obj) == -1 || PyUnicode_READY(str_obj) == -1) { |
Benjamin Peterson | 5e458f5 | 2012-01-02 10:12:13 -0600 | [diff] [blame] | 8956 | Py_DECREF(sub_obj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8957 | Py_DECREF(str_obj); |
| 8958 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8959 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8960 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8961 | kind1 = PyUnicode_KIND(str_obj); |
| 8962 | kind2 = PyUnicode_KIND(sub_obj); |
Antoine Pitrou | e45c0c5 | 2012-05-12 15:49:07 +0200 | [diff] [blame] | 8963 | kind = kind1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8964 | buf1 = PyUnicode_DATA(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8965 | buf2 = PyUnicode_DATA(sub_obj); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 8966 | if (kind2 != kind) { |
Antoine Pitrou | 758153b | 2012-05-12 15:51:51 +0200 | [diff] [blame] | 8967 | if (kind2 > kind) { |
| 8968 | Py_DECREF(sub_obj); |
| 8969 | Py_DECREF(str_obj); |
Antoine Pitrou | e45c0c5 | 2012-05-12 15:49:07 +0200 | [diff] [blame] | 8970 | return 0; |
Antoine Pitrou | 758153b | 2012-05-12 15:51:51 +0200 | [diff] [blame] | 8971 | } |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 8972 | buf2 = _PyUnicode_AsKind(sub_obj, kind); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 8973 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8974 | if (!buf2) |
| 8975 | goto onError; |
| 8976 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 8977 | len2 = PyUnicode_GET_LENGTH(sub_obj); |
| 8978 | |
| 8979 | ADJUST_INDICES(start, end, len1); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 8980 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8981 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8982 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sub_obj)) |
| 8983 | result = asciilib_count( |
| 8984 | ((Py_UCS1*)buf1) + start, end - start, |
| 8985 | buf2, len2, PY_SSIZE_T_MAX |
| 8986 | ); |
| 8987 | else |
| 8988 | result = ucs1lib_count( |
| 8989 | ((Py_UCS1*)buf1) + start, end - start, |
| 8990 | buf2, len2, PY_SSIZE_T_MAX |
| 8991 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8992 | break; |
| 8993 | case PyUnicode_2BYTE_KIND: |
| 8994 | result = ucs2lib_count( |
| 8995 | ((Py_UCS2*)buf1) + start, end - start, |
| 8996 | buf2, len2, PY_SSIZE_T_MAX |
| 8997 | ); |
| 8998 | break; |
| 8999 | case PyUnicode_4BYTE_KIND: |
| 9000 | result = ucs4lib_count( |
| 9001 | ((Py_UCS4*)buf1) + start, end - start, |
| 9002 | buf2, len2, PY_SSIZE_T_MAX |
| 9003 | ); |
| 9004 | break; |
| 9005 | default: |
| 9006 | assert(0); result = 0; |
| 9007 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9008 | |
| 9009 | Py_DECREF(sub_obj); |
| 9010 | Py_DECREF(str_obj); |
| 9011 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9012 | if (kind2 != kind) |
| 9013 | PyMem_Free(buf2); |
| 9014 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9015 | return result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9016 | onError: |
| 9017 | Py_DECREF(sub_obj); |
| 9018 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9019 | if (kind2 != kind && buf2) |
| 9020 | PyMem_Free(buf2); |
| 9021 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9022 | } |
| 9023 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9024 | Py_ssize_t |
| 9025 | PyUnicode_Find(PyObject *str, |
| 9026 | PyObject *sub, |
| 9027 | Py_ssize_t start, |
| 9028 | Py_ssize_t end, |
| 9029 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9030 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9031 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9032 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9033 | str = PyUnicode_FromObject(str); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9034 | if (!str) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9035 | return -2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9036 | sub = PyUnicode_FromObject(sub); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9037 | if (!sub) { |
| 9038 | Py_DECREF(str); |
| 9039 | return -2; |
| 9040 | } |
| 9041 | if (PyUnicode_READY(sub) == -1 || PyUnicode_READY(str) == -1) { |
| 9042 | Py_DECREF(sub); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9043 | Py_DECREF(str); |
| 9044 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9045 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9046 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9047 | result = any_find_slice(direction, |
| 9048 | str, sub, start, end |
| 9049 | ); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9050 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9051 | Py_DECREF(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9052 | Py_DECREF(sub); |
| 9053 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9054 | return result; |
| 9055 | } |
| 9056 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9057 | Py_ssize_t |
| 9058 | PyUnicode_FindChar(PyObject *str, Py_UCS4 ch, |
| 9059 | Py_ssize_t start, Py_ssize_t end, |
| 9060 | int direction) |
| 9061 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9062 | int kind; |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9063 | Py_ssize_t result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9064 | if (PyUnicode_READY(str) == -1) |
| 9065 | return -2; |
Victor Stinner | 267aa24 | 2011-10-02 01:08:37 +0200 | [diff] [blame] | 9066 | if (start < 0 || end < 0) { |
| 9067 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 9068 | return -2; |
| 9069 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9070 | if (end > PyUnicode_GET_LENGTH(str)) |
| 9071 | end = PyUnicode_GET_LENGTH(str); |
| 9072 | kind = PyUnicode_KIND(str); |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9073 | result = findchar(PyUnicode_1BYTE_DATA(str) + kind*start, |
| 9074 | kind, end-start, ch, direction); |
| 9075 | if (result == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9076 | return -1; |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9077 | else |
| 9078 | return start + result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9079 | } |
| 9080 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9081 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9082 | tailmatch(PyObject *self, |
| 9083 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9084 | Py_ssize_t start, |
| 9085 | Py_ssize_t end, |
| 9086 | int direction) |
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 | int kind_self; |
| 9089 | int kind_sub; |
| 9090 | void *data_self; |
| 9091 | void *data_sub; |
| 9092 | Py_ssize_t offset; |
| 9093 | Py_ssize_t i; |
| 9094 | Py_ssize_t end_sub; |
| 9095 | |
| 9096 | if (PyUnicode_READY(self) == -1 || |
| 9097 | PyUnicode_READY(substring) == -1) |
| 9098 | return 0; |
| 9099 | |
| 9100 | if (PyUnicode_GET_LENGTH(substring) == 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9101 | return 1; |
| 9102 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9103 | ADJUST_INDICES(start, end, PyUnicode_GET_LENGTH(self)); |
| 9104 | end -= PyUnicode_GET_LENGTH(substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9105 | if (end < start) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9106 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9107 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9108 | kind_self = PyUnicode_KIND(self); |
| 9109 | data_self = PyUnicode_DATA(self); |
| 9110 | kind_sub = PyUnicode_KIND(substring); |
| 9111 | data_sub = PyUnicode_DATA(substring); |
| 9112 | end_sub = PyUnicode_GET_LENGTH(substring) - 1; |
| 9113 | |
| 9114 | if (direction > 0) |
| 9115 | offset = end; |
| 9116 | else |
| 9117 | offset = start; |
| 9118 | |
| 9119 | if (PyUnicode_READ(kind_self, data_self, offset) == |
| 9120 | PyUnicode_READ(kind_sub, data_sub, 0) && |
| 9121 | PyUnicode_READ(kind_self, data_self, offset + end_sub) == |
| 9122 | PyUnicode_READ(kind_sub, data_sub, end_sub)) { |
| 9123 | /* If both are of the same kind, memcmp is sufficient */ |
| 9124 | if (kind_self == kind_sub) { |
| 9125 | return ! memcmp((char *)data_self + |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9126 | (offset * PyUnicode_KIND(substring)), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9127 | data_sub, |
| 9128 | PyUnicode_GET_LENGTH(substring) * |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9129 | PyUnicode_KIND(substring)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9130 | } |
| 9131 | /* otherwise we have to compare each character by first accesing it */ |
| 9132 | else { |
| 9133 | /* We do not need to compare 0 and len(substring)-1 because |
| 9134 | the if statement above ensured already that they are equal |
| 9135 | when we end up here. */ |
Antoine Pitrou | 057119b | 2012-09-02 17:56:33 +0200 | [diff] [blame] | 9136 | /* TODO: honor direction and do a forward or backwards search */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9137 | for (i = 1; i < end_sub; ++i) { |
| 9138 | if (PyUnicode_READ(kind_self, data_self, offset + i) != |
| 9139 | PyUnicode_READ(kind_sub, data_sub, i)) |
| 9140 | return 0; |
| 9141 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9142 | return 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9143 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9144 | } |
| 9145 | |
| 9146 | return 0; |
| 9147 | } |
| 9148 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9149 | Py_ssize_t |
| 9150 | PyUnicode_Tailmatch(PyObject *str, |
| 9151 | PyObject *substr, |
| 9152 | Py_ssize_t start, |
| 9153 | Py_ssize_t end, |
| 9154 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9155 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9156 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9157 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9158 | str = PyUnicode_FromObject(str); |
| 9159 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9160 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9161 | substr = PyUnicode_FromObject(substr); |
| 9162 | if (substr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9163 | Py_DECREF(str); |
| 9164 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9165 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9166 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9167 | result = tailmatch(str, substr, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9168 | start, end, direction); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9169 | Py_DECREF(str); |
| 9170 | Py_DECREF(substr); |
| 9171 | return result; |
| 9172 | } |
| 9173 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9174 | /* Apply fixfct filter to the Unicode object self and return a |
| 9175 | reference to the modified object */ |
| 9176 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9177 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9178 | fixup(PyObject *self, |
| 9179 | Py_UCS4 (*fixfct)(PyObject *s)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9180 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9181 | PyObject *u; |
| 9182 | Py_UCS4 maxchar_old, maxchar_new = 0; |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9183 | PyObject *v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9184 | |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 9185 | u = _PyUnicode_Copy(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9186 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9187 | return NULL; |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 9188 | maxchar_old = PyUnicode_MAX_CHAR_VALUE(u); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9189 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9190 | /* fix functions return the new maximum character in a string, |
| 9191 | if the kind of the resulting unicode object does not change, |
| 9192 | everything is fine. Otherwise we need to change the string kind |
| 9193 | and re-run the fix function. */ |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9194 | maxchar_new = fixfct(u); |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9195 | |
| 9196 | if (maxchar_new == 0) { |
| 9197 | /* no changes */; |
| 9198 | if (PyUnicode_CheckExact(self)) { |
| 9199 | Py_DECREF(u); |
| 9200 | Py_INCREF(self); |
| 9201 | return self; |
| 9202 | } |
| 9203 | else |
| 9204 | return u; |
| 9205 | } |
| 9206 | |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9207 | maxchar_new = align_maxchar(maxchar_new); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9208 | |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9209 | if (maxchar_new == maxchar_old) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9210 | return u; |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9211 | |
| 9212 | /* In case the maximum character changed, we need to |
| 9213 | convert the string to the new category. */ |
| 9214 | v = PyUnicode_New(PyUnicode_GET_LENGTH(self), maxchar_new); |
| 9215 | if (v == NULL) { |
| 9216 | Py_DECREF(u); |
| 9217 | return NULL; |
| 9218 | } |
| 9219 | if (maxchar_new > maxchar_old) { |
| 9220 | /* If the maxchar increased so that the kind changed, not all |
| 9221 | characters are representable anymore and we need to fix the |
| 9222 | string again. This only happens in very few cases. */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9223 | _PyUnicode_FastCopyCharacters(v, 0, |
| 9224 | self, 0, PyUnicode_GET_LENGTH(self)); |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9225 | maxchar_old = fixfct(v); |
| 9226 | assert(maxchar_old > 0 && maxchar_old <= maxchar_new); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9227 | } |
| 9228 | else { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9229 | _PyUnicode_FastCopyCharacters(v, 0, |
| 9230 | u, 0, PyUnicode_GET_LENGTH(self)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9231 | } |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9232 | Py_DECREF(u); |
| 9233 | assert(_PyUnicode_CheckConsistency(v, 1)); |
| 9234 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9235 | } |
| 9236 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9237 | static PyObject * |
| 9238 | ascii_upper_or_lower(PyObject *self, int lower) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9239 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9240 | Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9241 | char *resdata, *data = PyUnicode_DATA(self); |
| 9242 | PyObject *res; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9243 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9244 | res = PyUnicode_New(len, 127); |
| 9245 | if (res == NULL) |
| 9246 | return NULL; |
| 9247 | resdata = PyUnicode_DATA(res); |
| 9248 | if (lower) |
| 9249 | _Py_bytes_lower(resdata, data, len); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9250 | else |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9251 | _Py_bytes_upper(resdata, data, len); |
| 9252 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9253 | } |
| 9254 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9255 | static Py_UCS4 |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9256 | handle_capital_sigma(int kind, void *data, Py_ssize_t length, Py_ssize_t i) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9257 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9258 | Py_ssize_t j; |
| 9259 | int final_sigma; |
| 9260 | Py_UCS4 c; |
| 9261 | /* U+03A3 is in the Final_Sigma context when, it is found like this: |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9262 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9263 | \p{cased}\p{case-ignorable}*U+03A3!(\p{case-ignorable}*\p{cased}) |
| 9264 | |
| 9265 | where ! is a negation and \p{xxx} is a character with property xxx. |
| 9266 | */ |
| 9267 | for (j = i - 1; j >= 0; j--) { |
| 9268 | c = PyUnicode_READ(kind, data, j); |
| 9269 | if (!_PyUnicode_IsCaseIgnorable(c)) |
| 9270 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9271 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9272 | final_sigma = j >= 0 && _PyUnicode_IsCased(c); |
| 9273 | if (final_sigma) { |
| 9274 | for (j = i + 1; j < length; j++) { |
| 9275 | c = PyUnicode_READ(kind, data, j); |
| 9276 | if (!_PyUnicode_IsCaseIgnorable(c)) |
| 9277 | break; |
| 9278 | } |
| 9279 | final_sigma = j == length || !_PyUnicode_IsCased(c); |
| 9280 | } |
| 9281 | return (final_sigma) ? 0x3C2 : 0x3C3; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9282 | } |
| 9283 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9284 | static int |
| 9285 | lower_ucs4(int kind, void *data, Py_ssize_t length, Py_ssize_t i, |
| 9286 | Py_UCS4 c, Py_UCS4 *mapped) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9287 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9288 | /* Obscure special case. */ |
| 9289 | if (c == 0x3A3) { |
| 9290 | mapped[0] = handle_capital_sigma(kind, data, length, i); |
| 9291 | return 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9292 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9293 | return _PyUnicode_ToLowerFull(c, mapped); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9294 | } |
| 9295 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9296 | static Py_ssize_t |
| 9297 | do_capitalize(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9298 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9299 | Py_ssize_t i, k = 0; |
| 9300 | int n_res, j; |
| 9301 | Py_UCS4 c, mapped[3]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9302 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9303 | c = PyUnicode_READ(kind, data, 0); |
| 9304 | n_res = _PyUnicode_ToUpperFull(c, mapped); |
| 9305 | for (j = 0; j < n_res; j++) { |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9306 | *maxchar = MAX_MAXCHAR(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9307 | res[k++] = mapped[j]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9308 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9309 | for (i = 1; i < length; i++) { |
| 9310 | c = PyUnicode_READ(kind, data, i); |
| 9311 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9312 | for (j = 0; j < n_res; j++) { |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9313 | *maxchar = MAX_MAXCHAR(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9314 | res[k++] = mapped[j]; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9315 | } |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9316 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9317 | return k; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9318 | } |
| 9319 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9320 | static Py_ssize_t |
| 9321 | do_swapcase(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) { |
| 9322 | Py_ssize_t i, k = 0; |
| 9323 | |
| 9324 | for (i = 0; i < length; i++) { |
| 9325 | Py_UCS4 c = PyUnicode_READ(kind, data, i), mapped[3]; |
| 9326 | int n_res, j; |
| 9327 | if (Py_UNICODE_ISUPPER(c)) { |
| 9328 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9329 | } |
| 9330 | else if (Py_UNICODE_ISLOWER(c)) { |
| 9331 | n_res = _PyUnicode_ToUpperFull(c, mapped); |
| 9332 | } |
| 9333 | else { |
| 9334 | n_res = 1; |
| 9335 | mapped[0] = c; |
| 9336 | } |
| 9337 | for (j = 0; j < n_res; j++) { |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9338 | *maxchar = MAX_MAXCHAR(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9339 | res[k++] = mapped[j]; |
| 9340 | } |
| 9341 | } |
| 9342 | return k; |
| 9343 | } |
| 9344 | |
| 9345 | static Py_ssize_t |
| 9346 | do_upper_or_lower(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, |
| 9347 | Py_UCS4 *maxchar, int lower) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9348 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9349 | Py_ssize_t i, k = 0; |
| 9350 | |
| 9351 | for (i = 0; i < length; i++) { |
| 9352 | Py_UCS4 c = PyUnicode_READ(kind, data, i), mapped[3]; |
| 9353 | int n_res, j; |
| 9354 | if (lower) |
| 9355 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9356 | else |
| 9357 | n_res = _PyUnicode_ToUpperFull(c, mapped); |
| 9358 | for (j = 0; j < n_res; j++) { |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9359 | *maxchar = MAX_MAXCHAR(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9360 | res[k++] = mapped[j]; |
| 9361 | } |
| 9362 | } |
| 9363 | return k; |
| 9364 | } |
| 9365 | |
| 9366 | static Py_ssize_t |
| 9367 | do_upper(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9368 | { |
| 9369 | return do_upper_or_lower(kind, data, length, res, maxchar, 0); |
| 9370 | } |
| 9371 | |
| 9372 | static Py_ssize_t |
| 9373 | do_lower(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9374 | { |
| 9375 | return do_upper_or_lower(kind, data, length, res, maxchar, 1); |
| 9376 | } |
| 9377 | |
Benjamin Peterson | e51757f | 2012-01-12 21:10:29 -0500 | [diff] [blame] | 9378 | static Py_ssize_t |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 9379 | do_casefold(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9380 | { |
| 9381 | Py_ssize_t i, k = 0; |
| 9382 | |
| 9383 | for (i = 0; i < length; i++) { |
| 9384 | Py_UCS4 c = PyUnicode_READ(kind, data, i); |
| 9385 | Py_UCS4 mapped[3]; |
| 9386 | int j, n_res = _PyUnicode_ToFoldedFull(c, mapped); |
| 9387 | for (j = 0; j < n_res; j++) { |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9388 | *maxchar = MAX_MAXCHAR(*maxchar, mapped[j]); |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 9389 | res[k++] = mapped[j]; |
| 9390 | } |
| 9391 | } |
| 9392 | return k; |
| 9393 | } |
| 9394 | |
| 9395 | static Py_ssize_t |
Benjamin Peterson | e51757f | 2012-01-12 21:10:29 -0500 | [diff] [blame] | 9396 | do_title(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9397 | { |
| 9398 | Py_ssize_t i, k = 0; |
| 9399 | int previous_is_cased; |
| 9400 | |
| 9401 | previous_is_cased = 0; |
| 9402 | for (i = 0; i < length; i++) { |
| 9403 | const Py_UCS4 c = PyUnicode_READ(kind, data, i); |
| 9404 | Py_UCS4 mapped[3]; |
| 9405 | int n_res, j; |
| 9406 | |
| 9407 | if (previous_is_cased) |
| 9408 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9409 | else |
| 9410 | n_res = _PyUnicode_ToTitleFull(c, mapped); |
| 9411 | |
| 9412 | for (j = 0; j < n_res; j++) { |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9413 | *maxchar = MAX_MAXCHAR(*maxchar, mapped[j]); |
Benjamin Peterson | e51757f | 2012-01-12 21:10:29 -0500 | [diff] [blame] | 9414 | res[k++] = mapped[j]; |
| 9415 | } |
| 9416 | |
| 9417 | previous_is_cased = _PyUnicode_IsCased(c); |
| 9418 | } |
| 9419 | return k; |
| 9420 | } |
| 9421 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9422 | static PyObject * |
| 9423 | case_operation(PyObject *self, |
| 9424 | Py_ssize_t (*perform)(int, void *, Py_ssize_t, Py_UCS4 *, Py_UCS4 *)) |
| 9425 | { |
| 9426 | PyObject *res = NULL; |
| 9427 | Py_ssize_t length, newlength = 0; |
| 9428 | int kind, outkind; |
| 9429 | void *data, *outdata; |
| 9430 | Py_UCS4 maxchar = 0, *tmp, *tmpend; |
| 9431 | |
Benjamin Peterson | eea4846 | 2012-01-16 14:28:50 -0500 | [diff] [blame] | 9432 | assert(PyUnicode_IS_READY(self)); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9433 | |
| 9434 | kind = PyUnicode_KIND(self); |
| 9435 | data = PyUnicode_DATA(self); |
| 9436 | length = PyUnicode_GET_LENGTH(self); |
| 9437 | tmp = PyMem_MALLOC(sizeof(Py_UCS4) * 3 * length); |
| 9438 | if (tmp == NULL) |
| 9439 | return PyErr_NoMemory(); |
| 9440 | newlength = perform(kind, data, length, tmp, &maxchar); |
| 9441 | res = PyUnicode_New(newlength, maxchar); |
| 9442 | if (res == NULL) |
| 9443 | goto leave; |
| 9444 | tmpend = tmp + newlength; |
| 9445 | outdata = PyUnicode_DATA(res); |
| 9446 | outkind = PyUnicode_KIND(res); |
| 9447 | switch (outkind) { |
| 9448 | case PyUnicode_1BYTE_KIND: |
| 9449 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS1, tmp, tmpend, outdata); |
| 9450 | break; |
| 9451 | case PyUnicode_2BYTE_KIND: |
| 9452 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS2, tmp, tmpend, outdata); |
| 9453 | break; |
| 9454 | case PyUnicode_4BYTE_KIND: |
| 9455 | memcpy(outdata, tmp, sizeof(Py_UCS4) * newlength); |
| 9456 | break; |
| 9457 | default: |
| 9458 | assert(0); |
| 9459 | break; |
| 9460 | } |
| 9461 | leave: |
| 9462 | PyMem_FREE(tmp); |
| 9463 | return res; |
| 9464 | } |
| 9465 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9466 | PyObject * |
| 9467 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9468 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9469 | PyObject *sep = NULL; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9470 | Py_ssize_t seplen; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9471 | PyObject *res = NULL; /* the result */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9472 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9473 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
| 9474 | PyObject **items; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9475 | PyObject *item; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9476 | Py_ssize_t sz, i, res_offset; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9477 | Py_UCS4 maxchar; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9478 | Py_UCS4 item_maxchar; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9479 | int use_memcpy; |
| 9480 | unsigned char *res_data = NULL, *sep_data = NULL; |
| 9481 | PyObject *last_obj; |
| 9482 | unsigned int kind = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9483 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9484 | fseq = PySequence_Fast(seq, ""); |
| 9485 | if (fseq == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9486 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9487 | } |
| 9488 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9489 | /* NOTE: the following code can't call back into Python code, |
| 9490 | * so we are sure that fseq won't be mutated. |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9491 | */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9492 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9493 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 9494 | /* If empty sequence, return u"". */ |
| 9495 | if (seqlen == 0) { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9496 | Py_DECREF(fseq); |
| 9497 | Py_INCREF(unicode_empty); |
| 9498 | res = unicode_empty; |
| 9499 | return res; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9500 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9501 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9502 | /* If singleton sequence with an exact Unicode, return that. */ |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9503 | last_obj = NULL; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9504 | items = PySequence_Fast_ITEMS(fseq); |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9505 | if (seqlen == 1) { |
| 9506 | if (PyUnicode_CheckExact(items[0])) { |
| 9507 | res = items[0]; |
| 9508 | Py_INCREF(res); |
| 9509 | Py_DECREF(fseq); |
| 9510 | return res; |
| 9511 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9512 | seplen = 0; |
Victor Stinner | c6f0df7 | 2011-10-06 15:58:54 +0200 | [diff] [blame] | 9513 | maxchar = 0; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9514 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9515 | else { |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9516 | /* Set up sep and seplen */ |
| 9517 | if (separator == NULL) { |
| 9518 | /* fall back to a blank space separator */ |
| 9519 | sep = PyUnicode_FromOrdinal(' '); |
| 9520 | if (!sep) |
| 9521 | goto onError; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9522 | seplen = 1; |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9523 | maxchar = 32; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9524 | } |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9525 | else { |
| 9526 | if (!PyUnicode_Check(separator)) { |
| 9527 | PyErr_Format(PyExc_TypeError, |
| 9528 | "separator: expected str instance," |
| 9529 | " %.80s found", |
| 9530 | Py_TYPE(separator)->tp_name); |
| 9531 | goto onError; |
| 9532 | } |
| 9533 | if (PyUnicode_READY(separator)) |
| 9534 | goto onError; |
| 9535 | sep = separator; |
| 9536 | seplen = PyUnicode_GET_LENGTH(separator); |
| 9537 | maxchar = PyUnicode_MAX_CHAR_VALUE(separator); |
| 9538 | /* inc refcount to keep this code path symmetric with the |
| 9539 | above case of a blank separator */ |
| 9540 | Py_INCREF(sep); |
| 9541 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9542 | last_obj = sep; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9543 | } |
| 9544 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9545 | /* There are at least two things to join, or else we have a subclass |
| 9546 | * of str in the sequence. |
| 9547 | * Do a pre-pass to figure out the total amount of space we'll |
| 9548 | * need (sz), and see whether all argument are strings. |
| 9549 | */ |
| 9550 | sz = 0; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9551 | #ifdef Py_DEBUG |
| 9552 | use_memcpy = 0; |
| 9553 | #else |
| 9554 | use_memcpy = 1; |
| 9555 | #endif |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9556 | for (i = 0; i < seqlen; i++) { |
| 9557 | const Py_ssize_t old_sz = sz; |
| 9558 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9559 | if (!PyUnicode_Check(item)) { |
| 9560 | PyErr_Format(PyExc_TypeError, |
| 9561 | "sequence item %zd: expected str instance," |
| 9562 | " %.80s found", |
| 9563 | i, Py_TYPE(item)->tp_name); |
| 9564 | goto onError; |
| 9565 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9566 | if (PyUnicode_READY(item) == -1) |
| 9567 | goto onError; |
| 9568 | sz += PyUnicode_GET_LENGTH(item); |
| 9569 | item_maxchar = PyUnicode_MAX_CHAR_VALUE(item); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9570 | maxchar = MAX_MAXCHAR(maxchar, item_maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9571 | if (i != 0) |
| 9572 | sz += seplen; |
| 9573 | if (sz < old_sz || sz > PY_SSIZE_T_MAX) { |
| 9574 | PyErr_SetString(PyExc_OverflowError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9575 | "join() result is too long for a Python string"); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9576 | goto onError; |
| 9577 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9578 | if (use_memcpy && last_obj != NULL) { |
| 9579 | if (PyUnicode_KIND(last_obj) != PyUnicode_KIND(item)) |
| 9580 | use_memcpy = 0; |
| 9581 | } |
| 9582 | last_obj = item; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9583 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9584 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9585 | res = PyUnicode_New(sz, maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9586 | if (res == NULL) |
| 9587 | goto onError; |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9588 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9589 | /* Catenate everything. */ |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9590 | #ifdef Py_DEBUG |
| 9591 | use_memcpy = 0; |
| 9592 | #else |
| 9593 | if (use_memcpy) { |
| 9594 | res_data = PyUnicode_1BYTE_DATA(res); |
| 9595 | kind = PyUnicode_KIND(res); |
| 9596 | if (seplen != 0) |
| 9597 | sep_data = PyUnicode_1BYTE_DATA(sep); |
| 9598 | } |
| 9599 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9600 | for (i = 0, res_offset = 0; i < seqlen; ++i) { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9601 | Py_ssize_t itemlen; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9602 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9603 | /* Copy item, and maybe the separator. */ |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9604 | if (i && seplen != 0) { |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9605 | if (use_memcpy) { |
| 9606 | Py_MEMCPY(res_data, |
| 9607 | sep_data, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9608 | kind * seplen); |
| 9609 | res_data += kind * seplen; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9610 | } |
| 9611 | else { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9612 | _PyUnicode_FastCopyCharacters(res, res_offset, sep, 0, seplen); |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9613 | res_offset += seplen; |
| 9614 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9615 | } |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9616 | itemlen = PyUnicode_GET_LENGTH(item); |
| 9617 | if (itemlen != 0) { |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9618 | if (use_memcpy) { |
| 9619 | Py_MEMCPY(res_data, |
| 9620 | PyUnicode_DATA(item), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9621 | kind * itemlen); |
| 9622 | res_data += kind * itemlen; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9623 | } |
| 9624 | else { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9625 | _PyUnicode_FastCopyCharacters(res, res_offset, item, 0, itemlen); |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9626 | res_offset += itemlen; |
| 9627 | } |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9628 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9629 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9630 | if (use_memcpy) |
| 9631 | assert(res_data == PyUnicode_1BYTE_DATA(res) |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9632 | + kind * PyUnicode_GET_LENGTH(res)); |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9633 | else |
| 9634 | assert(res_offset == PyUnicode_GET_LENGTH(res)); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9635 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9636 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9637 | Py_XDECREF(sep); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9638 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9639 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9640 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9641 | onError: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9642 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9643 | Py_XDECREF(sep); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9644 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9645 | return NULL; |
| 9646 | } |
| 9647 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9648 | #define FILL(kind, data, value, start, length) \ |
| 9649 | do { \ |
| 9650 | Py_ssize_t i_ = 0; \ |
| 9651 | assert(kind != PyUnicode_WCHAR_KIND); \ |
| 9652 | switch ((kind)) { \ |
| 9653 | case PyUnicode_1BYTE_KIND: { \ |
| 9654 | unsigned char * to_ = (unsigned char *)((data)) + (start); \ |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 9655 | memset(to_, (unsigned char)value, (length)); \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9656 | break; \ |
| 9657 | } \ |
| 9658 | case PyUnicode_2BYTE_KIND: { \ |
| 9659 | Py_UCS2 * to_ = (Py_UCS2 *)((data)) + (start); \ |
| 9660 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 9661 | break; \ |
| 9662 | } \ |
Benjamin Peterson | e157cf1 | 2012-01-01 15:56:20 -0600 | [diff] [blame] | 9663 | case PyUnicode_4BYTE_KIND: { \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9664 | Py_UCS4 * to_ = (Py_UCS4 *)((data)) + (start); \ |
| 9665 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 9666 | break; \ |
Benjamin Peterson | e157cf1 | 2012-01-01 15:56:20 -0600 | [diff] [blame] | 9667 | default: assert(0); \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9668 | } \ |
| 9669 | } \ |
| 9670 | } while (0) |
| 9671 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9672 | void |
| 9673 | _PyUnicode_FastFill(PyObject *unicode, Py_ssize_t start, Py_ssize_t length, |
| 9674 | Py_UCS4 fill_char) |
| 9675 | { |
| 9676 | const enum PyUnicode_Kind kind = PyUnicode_KIND(unicode); |
| 9677 | const void *data = PyUnicode_DATA(unicode); |
| 9678 | assert(PyUnicode_IS_READY(unicode)); |
| 9679 | assert(unicode_modifiable(unicode)); |
| 9680 | assert(fill_char <= PyUnicode_MAX_CHAR_VALUE(unicode)); |
| 9681 | assert(start >= 0); |
| 9682 | assert(start + length <= PyUnicode_GET_LENGTH(unicode)); |
| 9683 | FILL(kind, data, fill_char, start, length); |
| 9684 | } |
| 9685 | |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 9686 | Py_ssize_t |
| 9687 | PyUnicode_Fill(PyObject *unicode, Py_ssize_t start, Py_ssize_t length, |
| 9688 | Py_UCS4 fill_char) |
| 9689 | { |
| 9690 | Py_ssize_t maxlen; |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 9691 | |
| 9692 | if (!PyUnicode_Check(unicode)) { |
| 9693 | PyErr_BadInternalCall(); |
| 9694 | return -1; |
| 9695 | } |
| 9696 | if (PyUnicode_READY(unicode) == -1) |
| 9697 | return -1; |
| 9698 | if (unicode_check_modifiable(unicode)) |
| 9699 | return -1; |
| 9700 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9701 | if (start < 0) { |
| 9702 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 9703 | return -1; |
| 9704 | } |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 9705 | if (fill_char > PyUnicode_MAX_CHAR_VALUE(unicode)) { |
| 9706 | PyErr_SetString(PyExc_ValueError, |
| 9707 | "fill character is bigger than " |
| 9708 | "the string maximum character"); |
| 9709 | return -1; |
| 9710 | } |
| 9711 | |
| 9712 | maxlen = PyUnicode_GET_LENGTH(unicode) - start; |
| 9713 | length = Py_MIN(maxlen, length); |
| 9714 | if (length <= 0) |
| 9715 | return 0; |
| 9716 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9717 | _PyUnicode_FastFill(unicode, start, length, fill_char); |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 9718 | return length; |
| 9719 | } |
| 9720 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9721 | static PyObject * |
| 9722 | pad(PyObject *self, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9723 | Py_ssize_t left, |
| 9724 | Py_ssize_t right, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9725 | Py_UCS4 fill) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9726 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9727 | PyObject *u; |
| 9728 | Py_UCS4 maxchar; |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9729 | int kind; |
| 9730 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9731 | |
| 9732 | if (left < 0) |
| 9733 | left = 0; |
| 9734 | if (right < 0) |
| 9735 | right = 0; |
| 9736 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 9737 | if (left == 0 && right == 0) |
| 9738 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9739 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9740 | if (left > PY_SSIZE_T_MAX - _PyUnicode_LENGTH(self) || |
| 9741 | right > PY_SSIZE_T_MAX - (left + _PyUnicode_LENGTH(self))) { |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 9742 | PyErr_SetString(PyExc_OverflowError, "padded string is too long"); |
| 9743 | return NULL; |
| 9744 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9745 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9746 | maxchar = MAX_MAXCHAR(maxchar, fill); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9747 | u = PyUnicode_New(left + _PyUnicode_LENGTH(self) + right, maxchar); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9748 | if (!u) |
| 9749 | return NULL; |
| 9750 | |
| 9751 | kind = PyUnicode_KIND(u); |
| 9752 | data = PyUnicode_DATA(u); |
| 9753 | if (left) |
| 9754 | FILL(kind, data, fill, 0, left); |
| 9755 | if (right) |
| 9756 | FILL(kind, data, fill, left + _PyUnicode_LENGTH(self), right); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9757 | _PyUnicode_FastCopyCharacters(u, left, self, 0, _PyUnicode_LENGTH(self)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9758 | assert(_PyUnicode_CheckConsistency(u, 1)); |
| 9759 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9760 | } |
| 9761 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9762 | PyObject * |
| 9763 | PyUnicode_Splitlines(PyObject *string, int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9764 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9765 | PyObject *list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9766 | |
| 9767 | string = PyUnicode_FromObject(string); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9768 | if (string == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9769 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9770 | if (PyUnicode_READY(string) == -1) { |
| 9771 | Py_DECREF(string); |
| 9772 | return NULL; |
| 9773 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9774 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9775 | switch (PyUnicode_KIND(string)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9776 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9777 | if (PyUnicode_IS_ASCII(string)) |
| 9778 | list = asciilib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9779 | string, PyUnicode_1BYTE_DATA(string), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9780 | PyUnicode_GET_LENGTH(string), keepends); |
| 9781 | else |
| 9782 | list = ucs1lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9783 | string, PyUnicode_1BYTE_DATA(string), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9784 | PyUnicode_GET_LENGTH(string), keepends); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9785 | break; |
| 9786 | case PyUnicode_2BYTE_KIND: |
| 9787 | list = ucs2lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9788 | string, PyUnicode_2BYTE_DATA(string), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9789 | PyUnicode_GET_LENGTH(string), keepends); |
| 9790 | break; |
| 9791 | case PyUnicode_4BYTE_KIND: |
| 9792 | list = ucs4lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9793 | string, PyUnicode_4BYTE_DATA(string), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9794 | PyUnicode_GET_LENGTH(string), keepends); |
| 9795 | break; |
| 9796 | default: |
| 9797 | assert(0); |
| 9798 | list = 0; |
| 9799 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9800 | Py_DECREF(string); |
| 9801 | return list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9802 | } |
| 9803 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9804 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9805 | split(PyObject *self, |
| 9806 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9807 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9808 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9809 | int kind1, kind2, kind; |
| 9810 | void *buf1, *buf2; |
| 9811 | Py_ssize_t len1, len2; |
| 9812 | PyObject* out; |
| 9813 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9814 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9815 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9816 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9817 | if (PyUnicode_READY(self) == -1) |
| 9818 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9819 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9820 | if (substring == NULL) |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9821 | switch (PyUnicode_KIND(self)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9822 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9823 | if (PyUnicode_IS_ASCII(self)) |
| 9824 | return asciilib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9825 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9826 | PyUnicode_GET_LENGTH(self), maxcount |
| 9827 | ); |
| 9828 | else |
| 9829 | return ucs1lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9830 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9831 | PyUnicode_GET_LENGTH(self), maxcount |
| 9832 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9833 | case PyUnicode_2BYTE_KIND: |
| 9834 | return ucs2lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9835 | self, PyUnicode_2BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9836 | PyUnicode_GET_LENGTH(self), maxcount |
| 9837 | ); |
| 9838 | case PyUnicode_4BYTE_KIND: |
| 9839 | return ucs4lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9840 | self, PyUnicode_4BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9841 | PyUnicode_GET_LENGTH(self), maxcount |
| 9842 | ); |
| 9843 | default: |
| 9844 | assert(0); |
| 9845 | return NULL; |
| 9846 | } |
| 9847 | |
| 9848 | if (PyUnicode_READY(substring) == -1) |
| 9849 | return NULL; |
| 9850 | |
| 9851 | kind1 = PyUnicode_KIND(self); |
| 9852 | kind2 = PyUnicode_KIND(substring); |
| 9853 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9854 | buf1 = PyUnicode_DATA(self); |
| 9855 | buf2 = PyUnicode_DATA(substring); |
| 9856 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9857 | buf1 = _PyUnicode_AsKind(self, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9858 | if (!buf1) |
| 9859 | return NULL; |
| 9860 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9861 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9862 | if (!buf2) { |
| 9863 | if (kind1 != kind) PyMem_Free(buf1); |
| 9864 | return NULL; |
| 9865 | } |
| 9866 | len1 = PyUnicode_GET_LENGTH(self); |
| 9867 | len2 = PyUnicode_GET_LENGTH(substring); |
| 9868 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9869 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9870 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9871 | if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) |
| 9872 | out = asciilib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9873 | self, buf1, len1, buf2, len2, maxcount); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9874 | else |
| 9875 | out = ucs1lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9876 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9877 | break; |
| 9878 | case PyUnicode_2BYTE_KIND: |
| 9879 | out = ucs2lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9880 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9881 | break; |
| 9882 | case PyUnicode_4BYTE_KIND: |
| 9883 | out = ucs4lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9884 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9885 | break; |
| 9886 | default: |
| 9887 | out = NULL; |
| 9888 | } |
| 9889 | if (kind1 != kind) |
| 9890 | PyMem_Free(buf1); |
| 9891 | if (kind2 != kind) |
| 9892 | PyMem_Free(buf2); |
| 9893 | return out; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9894 | } |
| 9895 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9896 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9897 | rsplit(PyObject *self, |
| 9898 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9899 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9900 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9901 | int kind1, kind2, kind; |
| 9902 | void *buf1, *buf2; |
| 9903 | Py_ssize_t len1, len2; |
| 9904 | PyObject* out; |
| 9905 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9906 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9907 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9908 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9909 | if (PyUnicode_READY(self) == -1) |
| 9910 | return NULL; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9911 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9912 | if (substring == NULL) |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9913 | switch (PyUnicode_KIND(self)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9914 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9915 | if (PyUnicode_IS_ASCII(self)) |
| 9916 | return asciilib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9917 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9918 | PyUnicode_GET_LENGTH(self), maxcount |
| 9919 | ); |
| 9920 | else |
| 9921 | return ucs1lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9922 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9923 | PyUnicode_GET_LENGTH(self), maxcount |
| 9924 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9925 | case PyUnicode_2BYTE_KIND: |
| 9926 | return ucs2lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9927 | self, PyUnicode_2BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9928 | PyUnicode_GET_LENGTH(self), maxcount |
| 9929 | ); |
| 9930 | case PyUnicode_4BYTE_KIND: |
| 9931 | return ucs4lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9932 | self, PyUnicode_4BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9933 | PyUnicode_GET_LENGTH(self), maxcount |
| 9934 | ); |
| 9935 | default: |
| 9936 | assert(0); |
| 9937 | return NULL; |
| 9938 | } |
| 9939 | |
| 9940 | if (PyUnicode_READY(substring) == -1) |
| 9941 | return NULL; |
| 9942 | |
| 9943 | kind1 = PyUnicode_KIND(self); |
| 9944 | kind2 = PyUnicode_KIND(substring); |
| 9945 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9946 | buf1 = PyUnicode_DATA(self); |
| 9947 | buf2 = PyUnicode_DATA(substring); |
| 9948 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9949 | buf1 = _PyUnicode_AsKind(self, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9950 | if (!buf1) |
| 9951 | return NULL; |
| 9952 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9953 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9954 | if (!buf2) { |
| 9955 | if (kind1 != kind) PyMem_Free(buf1); |
| 9956 | return NULL; |
| 9957 | } |
| 9958 | len1 = PyUnicode_GET_LENGTH(self); |
| 9959 | len2 = PyUnicode_GET_LENGTH(substring); |
| 9960 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9961 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9962 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9963 | if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) |
| 9964 | out = asciilib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9965 | self, buf1, len1, buf2, len2, maxcount); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9966 | else |
| 9967 | out = ucs1lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9968 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9969 | break; |
| 9970 | case PyUnicode_2BYTE_KIND: |
| 9971 | out = ucs2lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9972 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9973 | break; |
| 9974 | case PyUnicode_4BYTE_KIND: |
| 9975 | out = ucs4lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9976 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9977 | break; |
| 9978 | default: |
| 9979 | out = NULL; |
| 9980 | } |
| 9981 | if (kind1 != kind) |
| 9982 | PyMem_Free(buf1); |
| 9983 | if (kind2 != kind) |
| 9984 | PyMem_Free(buf2); |
| 9985 | return out; |
| 9986 | } |
| 9987 | |
| 9988 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9989 | anylib_find(int kind, PyObject *str1, void *buf1, Py_ssize_t len1, |
| 9990 | 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] | 9991 | { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9992 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9993 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9994 | if (PyUnicode_IS_ASCII(str1) && PyUnicode_IS_ASCII(str2)) |
| 9995 | return asciilib_find(buf1, len1, buf2, len2, offset); |
| 9996 | else |
| 9997 | return ucs1lib_find(buf1, len1, buf2, len2, offset); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9998 | case PyUnicode_2BYTE_KIND: |
| 9999 | return ucs2lib_find(buf1, len1, buf2, len2, offset); |
| 10000 | case PyUnicode_4BYTE_KIND: |
| 10001 | return ucs4lib_find(buf1, len1, buf2, len2, offset); |
| 10002 | } |
| 10003 | assert(0); |
| 10004 | return -1; |
| 10005 | } |
| 10006 | |
| 10007 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10008 | anylib_count(int kind, PyObject *sstr, void* sbuf, Py_ssize_t slen, |
| 10009 | 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] | 10010 | { |
Benjamin Peterson | c0b95d1 | 2011-12-20 17:24:05 -0600 | [diff] [blame] | 10011 | switch (kind) { |
| 10012 | case PyUnicode_1BYTE_KIND: |
| 10013 | if (PyUnicode_IS_ASCII(sstr) && PyUnicode_IS_ASCII(str1)) |
| 10014 | return asciilib_count(sbuf, slen, buf1, len1, maxcount); |
| 10015 | else |
| 10016 | return ucs1lib_count(sbuf, slen, buf1, len1, maxcount); |
| 10017 | case PyUnicode_2BYTE_KIND: |
| 10018 | return ucs2lib_count(sbuf, slen, buf1, len1, maxcount); |
| 10019 | case PyUnicode_4BYTE_KIND: |
| 10020 | return ucs4lib_count(sbuf, slen, buf1, len1, maxcount); |
| 10021 | } |
| 10022 | assert(0); |
| 10023 | return 0; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10024 | } |
| 10025 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10026 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10027 | replace(PyObject *self, PyObject *str1, |
| 10028 | PyObject *str2, Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10029 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10030 | PyObject *u; |
| 10031 | char *sbuf = PyUnicode_DATA(self); |
| 10032 | char *buf1 = PyUnicode_DATA(str1); |
| 10033 | char *buf2 = PyUnicode_DATA(str2); |
| 10034 | int srelease = 0, release1 = 0, release2 = 0; |
| 10035 | int skind = PyUnicode_KIND(self); |
| 10036 | int kind1 = PyUnicode_KIND(str1); |
| 10037 | int kind2 = PyUnicode_KIND(str2); |
| 10038 | Py_ssize_t slen = PyUnicode_GET_LENGTH(self); |
| 10039 | Py_ssize_t len1 = PyUnicode_GET_LENGTH(str1); |
| 10040 | Py_ssize_t len2 = PyUnicode_GET_LENGTH(str2); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10041 | int mayshrink; |
| 10042 | Py_UCS4 maxchar, maxchar_str2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10043 | |
| 10044 | if (maxcount < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10045 | maxcount = PY_SSIZE_T_MAX; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10046 | else if (maxcount == 0 || slen == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10047 | goto nothing; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10048 | |
Victor Stinner | 59de0ee | 2011-10-07 10:01:28 +0200 | [diff] [blame] | 10049 | if (str1 == str2) |
| 10050 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10051 | if (skind < kind1) |
| 10052 | /* substring too wide to be present */ |
| 10053 | goto nothing; |
| 10054 | |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10055 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
| 10056 | maxchar_str2 = PyUnicode_MAX_CHAR_VALUE(str2); |
| 10057 | /* Replacing str1 with str2 may cause a maxchar reduction in the |
| 10058 | result string. */ |
| 10059 | mayshrink = (maxchar_str2 < maxchar); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 10060 | maxchar = MAX_MAXCHAR(maxchar, maxchar_str2); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10061 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10062 | if (len1 == len2) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10063 | /* same length */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10064 | if (len1 == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10065 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10066 | if (len1 == 1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10067 | /* replace characters */ |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10068 | Py_UCS4 u1, u2; |
| 10069 | int rkind; |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10070 | Py_ssize_t index, pos; |
| 10071 | char *src; |
| 10072 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10073 | u1 = PyUnicode_READ_CHAR(str1, 0); |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10074 | pos = findchar(sbuf, PyUnicode_KIND(self), slen, u1, 1); |
| 10075 | if (pos < 0) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10076 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10077 | u2 = PyUnicode_READ_CHAR(str2, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10078 | u = PyUnicode_New(slen, maxchar); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10079 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10080 | goto error; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 10081 | _PyUnicode_FastCopyCharacters(u, 0, self, 0, slen); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10082 | rkind = PyUnicode_KIND(u); |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10083 | |
| 10084 | PyUnicode_WRITE(rkind, PyUnicode_DATA(u), pos, u2); |
| 10085 | index = 0; |
| 10086 | src = sbuf; |
| 10087 | while (--maxcount) |
| 10088 | { |
| 10089 | pos++; |
| 10090 | src += pos * PyUnicode_KIND(self); |
| 10091 | slen -= pos; |
| 10092 | index += pos; |
| 10093 | pos = findchar(src, PyUnicode_KIND(self), slen, u1, 1); |
| 10094 | if (pos < 0) |
| 10095 | break; |
| 10096 | PyUnicode_WRITE(rkind, PyUnicode_DATA(u), index + pos, u2); |
| 10097 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10098 | } |
| 10099 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10100 | int rkind = skind; |
| 10101 | char *res; |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10102 | Py_ssize_t i; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10103 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10104 | if (kind1 < rkind) { |
| 10105 | /* widen substring */ |
| 10106 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10107 | if (!buf1) goto error; |
| 10108 | release1 = 1; |
| 10109 | } |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10110 | i = anylib_find(rkind, self, sbuf, slen, str1, buf1, len1, 0); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10111 | if (i < 0) |
| 10112 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10113 | if (rkind > kind2) { |
| 10114 | /* widen replacement */ |
| 10115 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 10116 | if (!buf2) goto error; |
| 10117 | release2 = 1; |
| 10118 | } |
| 10119 | else if (rkind < kind2) { |
| 10120 | /* widen self and buf1 */ |
| 10121 | rkind = kind2; |
| 10122 | if (release1) PyMem_Free(buf1); |
| 10123 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 10124 | if (!sbuf) goto error; |
| 10125 | srelease = 1; |
| 10126 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10127 | if (!buf1) goto error; |
| 10128 | release1 = 1; |
| 10129 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10130 | u = PyUnicode_New(slen, maxchar); |
| 10131 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10132 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10133 | assert(PyUnicode_KIND(u) == rkind); |
| 10134 | res = PyUnicode_DATA(u); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10135 | |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10136 | memcpy(res, sbuf, rkind * slen); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10137 | /* change everything in-place, starting with this one */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10138 | memcpy(res + rkind * i, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10139 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10140 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10141 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10142 | |
| 10143 | while ( --maxcount > 0) { |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10144 | i = anylib_find(rkind, self, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10145 | sbuf+rkind*i, slen-i, |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10146 | str1, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10147 | if (i == -1) |
| 10148 | break; |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10149 | memcpy(res + rkind * i, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10150 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10151 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10152 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10153 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10154 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10155 | } |
| 10156 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10157 | Py_ssize_t n, i, j, ires; |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 10158 | Py_ssize_t new_size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10159 | int rkind = skind; |
| 10160 | char *res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10161 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10162 | if (kind1 < rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10163 | /* widen substring */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10164 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10165 | if (!buf1) goto error; |
| 10166 | release1 = 1; |
| 10167 | } |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10168 | n = anylib_count(rkind, self, sbuf, slen, str1, buf1, len1, maxcount); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10169 | if (n == 0) |
| 10170 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10171 | if (kind2 < rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10172 | /* widen replacement */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10173 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 10174 | if (!buf2) goto error; |
| 10175 | release2 = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10176 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10177 | else if (kind2 > rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10178 | /* widen self and buf1 */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10179 | rkind = kind2; |
| 10180 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 10181 | if (!sbuf) goto error; |
| 10182 | srelease = 1; |
| 10183 | if (release1) PyMem_Free(buf1); |
| 10184 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10185 | if (!buf1) goto error; |
| 10186 | release1 = 1; |
| 10187 | } |
| 10188 | /* new_size = PyUnicode_GET_LENGTH(self) + n * (PyUnicode_GET_LENGTH(str2) - |
| 10189 | PyUnicode_GET_LENGTH(str1))); */ |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 10190 | if (len2 > len1 && len2 - len1 > (PY_SSIZE_T_MAX - slen) / n) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10191 | PyErr_SetString(PyExc_OverflowError, |
| 10192 | "replace string is too long"); |
| 10193 | goto error; |
| 10194 | } |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 10195 | new_size = slen + n * (len2 - len1); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10196 | if (new_size == 0) { |
| 10197 | Py_INCREF(unicode_empty); |
| 10198 | u = unicode_empty; |
| 10199 | goto done; |
| 10200 | } |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 10201 | if (new_size > (PY_SSIZE_T_MAX >> (rkind-1))) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10202 | PyErr_SetString(PyExc_OverflowError, |
| 10203 | "replace string is too long"); |
| 10204 | goto error; |
| 10205 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10206 | u = PyUnicode_New(new_size, maxchar); |
| 10207 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10208 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10209 | assert(PyUnicode_KIND(u) == rkind); |
| 10210 | res = PyUnicode_DATA(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10211 | ires = i = 0; |
| 10212 | if (len1 > 0) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10213 | while (n-- > 0) { |
| 10214 | /* look for next match */ |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10215 | j = anylib_find(rkind, self, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10216 | sbuf + rkind * i, slen-i, |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10217 | str1, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10218 | if (j == -1) |
| 10219 | break; |
| 10220 | else if (j > i) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10221 | /* copy unchanged part [i:j] */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10222 | memcpy(res + rkind * ires, |
| 10223 | sbuf + rkind * i, |
| 10224 | rkind * (j-i)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10225 | ires += j - i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10226 | } |
| 10227 | /* copy substitution string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10228 | if (len2 > 0) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10229 | memcpy(res + rkind * ires, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10230 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10231 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10232 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10233 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10234 | i = j + len1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10235 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10236 | if (i < slen) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10237 | /* copy tail [i:] */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10238 | memcpy(res + rkind * ires, |
| 10239 | sbuf + rkind * i, |
| 10240 | rkind * (slen-i)); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10241 | } |
| 10242 | else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10243 | /* interleave */ |
| 10244 | while (n > 0) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10245 | memcpy(res + rkind * ires, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10246 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10247 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10248 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10249 | if (--n <= 0) |
| 10250 | break; |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10251 | memcpy(res + rkind * ires, |
| 10252 | sbuf + rkind * i, |
| 10253 | rkind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10254 | ires++; |
| 10255 | i++; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10256 | } |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10257 | memcpy(res + rkind * ires, |
| 10258 | sbuf + rkind * i, |
| 10259 | rkind * (slen-i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10260 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10261 | } |
| 10262 | |
| 10263 | if (mayshrink) { |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10264 | unicode_adjust_maxchar(&u); |
| 10265 | if (u == NULL) |
| 10266 | goto error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10267 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10268 | |
| 10269 | done: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10270 | if (srelease) |
| 10271 | PyMem_FREE(sbuf); |
| 10272 | if (release1) |
| 10273 | PyMem_FREE(buf1); |
| 10274 | if (release2) |
| 10275 | PyMem_FREE(buf2); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10276 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10277 | return u; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10278 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10279 | nothing: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10280 | /* nothing to replace; return original string (when possible) */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10281 | if (srelease) |
| 10282 | PyMem_FREE(sbuf); |
| 10283 | if (release1) |
| 10284 | PyMem_FREE(buf1); |
| 10285 | if (release2) |
| 10286 | PyMem_FREE(buf2); |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10287 | return unicode_result_unchanged(self); |
| 10288 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10289 | error: |
| 10290 | if (srelease && sbuf) |
| 10291 | PyMem_FREE(sbuf); |
| 10292 | if (release1 && buf1) |
| 10293 | PyMem_FREE(buf1); |
| 10294 | if (release2 && buf2) |
| 10295 | PyMem_FREE(buf2); |
| 10296 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10297 | } |
| 10298 | |
| 10299 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 10300 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10301 | PyDoc_STRVAR(title__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10302 | "S.title() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10303 | \n\ |
| 10304 | 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] | 10305 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10306 | |
| 10307 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10308 | unicode_title(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10309 | { |
Benjamin Peterson | eea4846 | 2012-01-16 14:28:50 -0500 | [diff] [blame] | 10310 | if (PyUnicode_READY(self) == -1) |
| 10311 | return NULL; |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 10312 | return case_operation(self, do_title); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10313 | } |
| 10314 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10315 | PyDoc_STRVAR(capitalize__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10316 | "S.capitalize() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10317 | \n\ |
| 10318 | 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] | 10319 | have upper case and the rest lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10320 | |
| 10321 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10322 | unicode_capitalize(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10323 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 10324 | if (PyUnicode_READY(self) == -1) |
| 10325 | return NULL; |
| 10326 | if (PyUnicode_GET_LENGTH(self) == 0) |
| 10327 | return unicode_result_unchanged(self); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 10328 | return case_operation(self, do_capitalize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10329 | } |
| 10330 | |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 10331 | PyDoc_STRVAR(casefold__doc__, |
| 10332 | "S.casefold() -> str\n\ |
| 10333 | \n\ |
| 10334 | Return a version of S suitable for caseless comparisons."); |
| 10335 | |
| 10336 | static PyObject * |
| 10337 | unicode_casefold(PyObject *self) |
| 10338 | { |
| 10339 | if (PyUnicode_READY(self) == -1) |
| 10340 | return NULL; |
| 10341 | if (PyUnicode_IS_ASCII(self)) |
| 10342 | return ascii_upper_or_lower(self, 1); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 10343 | return case_operation(self, do_casefold); |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 10344 | } |
| 10345 | |
| 10346 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10347 | /* Argument converter. Coerces to a single unicode character */ |
| 10348 | |
| 10349 | static int |
| 10350 | convert_uc(PyObject *obj, void *addr) |
| 10351 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10352 | Py_UCS4 *fillcharloc = (Py_UCS4 *)addr; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10353 | PyObject *uniobj; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10354 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10355 | uniobj = PyUnicode_FromObject(obj); |
| 10356 | if (uniobj == NULL) { |
| 10357 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10358 | "The fill character cannot be converted to Unicode"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10359 | return 0; |
| 10360 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10361 | if (PyUnicode_GET_LENGTH(uniobj) != 1) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10362 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10363 | "The fill character must be exactly one character long"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10364 | Py_DECREF(uniobj); |
| 10365 | return 0; |
| 10366 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10367 | *fillcharloc = PyUnicode_READ_CHAR(uniobj, 0); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10368 | Py_DECREF(uniobj); |
| 10369 | return 1; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10370 | } |
| 10371 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10372 | PyDoc_STRVAR(center__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10373 | "S.center(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10374 | \n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10375 | Return S centered in a string of length width. Padding is\n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10376 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10377 | |
| 10378 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10379 | unicode_center(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10380 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10381 | Py_ssize_t marg, left; |
| 10382 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10383 | Py_UCS4 fillchar = ' '; |
| 10384 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 10385 | 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] | 10386 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10387 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 10388 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10389 | return NULL; |
| 10390 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10391 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 10392 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10393 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10394 | marg = width - PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10395 | left = marg / 2 + (marg & width & 1); |
| 10396 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10397 | return pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10398 | } |
| 10399 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10400 | /* This function assumes that str1 and str2 are readied by the caller. */ |
| 10401 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10402 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10403 | unicode_compare(PyObject *str1, PyObject *str2) |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10404 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10405 | int kind1, kind2; |
| 10406 | void *data1, *data2; |
| 10407 | Py_ssize_t len1, len2, i; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10408 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10409 | kind1 = PyUnicode_KIND(str1); |
| 10410 | kind2 = PyUnicode_KIND(str2); |
| 10411 | data1 = PyUnicode_DATA(str1); |
| 10412 | data2 = PyUnicode_DATA(str2); |
| 10413 | len1 = PyUnicode_GET_LENGTH(str1); |
| 10414 | len2 = PyUnicode_GET_LENGTH(str2); |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10415 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10416 | for (i = 0; i < len1 && i < len2; ++i) { |
| 10417 | Py_UCS4 c1, c2; |
| 10418 | c1 = PyUnicode_READ(kind1, data1, i); |
| 10419 | c2 = PyUnicode_READ(kind2, data2, i); |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 10420 | |
| 10421 | if (c1 != c2) |
| 10422 | return (c1 < c2) ? -1 : 1; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10423 | } |
| 10424 | |
| 10425 | return (len1 < len2) ? -1 : (len1 != len2); |
| 10426 | } |
| 10427 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10428 | int |
| 10429 | PyUnicode_Compare(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10430 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10431 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 10432 | if (PyUnicode_READY(left) == -1 || |
| 10433 | PyUnicode_READY(right) == -1) |
| 10434 | return -1; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10435 | return unicode_compare(left, right); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10436 | } |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 10437 | PyErr_Format(PyExc_TypeError, |
| 10438 | "Can't compare %.100s and %.100s", |
| 10439 | left->ob_type->tp_name, |
| 10440 | right->ob_type->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10441 | return -1; |
| 10442 | } |
| 10443 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10444 | int |
| 10445 | PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str) |
| 10446 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10447 | Py_ssize_t i; |
| 10448 | int kind; |
| 10449 | void *data; |
| 10450 | Py_UCS4 chr; |
| 10451 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 10452 | assert(_PyUnicode_CHECK(uni)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10453 | if (PyUnicode_READY(uni) == -1) |
| 10454 | return -1; |
| 10455 | kind = PyUnicode_KIND(uni); |
| 10456 | data = PyUnicode_DATA(uni); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10457 | /* Compare Unicode string and source character set string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10458 | for (i = 0; (chr = PyUnicode_READ(kind, data, i)) && str[i]; i++) |
| 10459 | if (chr != str[i]) |
| 10460 | return (chr < (unsigned char)(str[i])) ? -1 : 1; |
Benjamin Peterson | 8667a9b | 2010-01-09 21:45:28 +0000 | [diff] [blame] | 10461 | /* This check keeps Python strings that end in '\0' from comparing equal |
| 10462 | to C strings identical up to that point. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10463 | if (PyUnicode_GET_LENGTH(uni) != i || chr) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10464 | return 1; /* uni is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10465 | if (str[i]) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10466 | return -1; /* str is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10467 | return 0; |
| 10468 | } |
| 10469 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10470 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10471 | #define TEST_COND(cond) \ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10472 | ((cond) ? Py_True : Py_False) |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10473 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10474 | PyObject * |
| 10475 | PyUnicode_RichCompare(PyObject *left, PyObject *right, int op) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10476 | { |
| 10477 | int result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10478 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10479 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 10480 | PyObject *v; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10481 | if (PyUnicode_READY(left) == -1 || |
| 10482 | PyUnicode_READY(right) == -1) |
| 10483 | return NULL; |
| 10484 | if (PyUnicode_GET_LENGTH(left) != PyUnicode_GET_LENGTH(right) || |
| 10485 | PyUnicode_KIND(left) != PyUnicode_KIND(right)) { |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10486 | if (op == Py_EQ) { |
| 10487 | Py_INCREF(Py_False); |
| 10488 | return Py_False; |
| 10489 | } |
| 10490 | if (op == Py_NE) { |
| 10491 | Py_INCREF(Py_True); |
| 10492 | return Py_True; |
| 10493 | } |
| 10494 | } |
| 10495 | if (left == right) |
| 10496 | result = 0; |
| 10497 | else |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10498 | result = unicode_compare(left, right); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10499 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10500 | /* Convert the return value to a Boolean */ |
| 10501 | switch (op) { |
| 10502 | case Py_EQ: |
| 10503 | v = TEST_COND(result == 0); |
| 10504 | break; |
| 10505 | case Py_NE: |
| 10506 | v = TEST_COND(result != 0); |
| 10507 | break; |
| 10508 | case Py_LE: |
| 10509 | v = TEST_COND(result <= 0); |
| 10510 | break; |
| 10511 | case Py_GE: |
| 10512 | v = TEST_COND(result >= 0); |
| 10513 | break; |
| 10514 | case Py_LT: |
| 10515 | v = TEST_COND(result == -1); |
| 10516 | break; |
| 10517 | case Py_GT: |
| 10518 | v = TEST_COND(result == 1); |
| 10519 | break; |
| 10520 | default: |
| 10521 | PyErr_BadArgument(); |
| 10522 | return NULL; |
| 10523 | } |
| 10524 | Py_INCREF(v); |
| 10525 | return v; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10526 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10527 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 10528 | Py_RETURN_NOTIMPLEMENTED; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10529 | } |
| 10530 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10531 | int |
| 10532 | PyUnicode_Contains(PyObject *container, PyObject *element) |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10533 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10534 | PyObject *str, *sub; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10535 | int kind1, kind2, kind; |
| 10536 | void *buf1, *buf2; |
| 10537 | Py_ssize_t len1, len2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10538 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10539 | |
| 10540 | /* Coerce the two arguments */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10541 | sub = PyUnicode_FromObject(element); |
| 10542 | if (!sub) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10543 | PyErr_Format(PyExc_TypeError, |
| 10544 | "'in <string>' requires string as left operand, not %s", |
| 10545 | element->ob_type->tp_name); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10546 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10547 | } |
| 10548 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10549 | str = PyUnicode_FromObject(container); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 10550 | if (!str) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10551 | Py_DECREF(sub); |
| 10552 | return -1; |
| 10553 | } |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 10554 | if (PyUnicode_READY(sub) == -1 || PyUnicode_READY(str) == -1) { |
| 10555 | Py_DECREF(sub); |
| 10556 | Py_DECREF(str); |
| 10557 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10558 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10559 | kind1 = PyUnicode_KIND(str); |
| 10560 | kind2 = PyUnicode_KIND(sub); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 10561 | kind = kind1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10562 | buf1 = PyUnicode_DATA(str); |
| 10563 | buf2 = PyUnicode_DATA(sub); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 10564 | if (kind2 != kind) { |
Antoine Pitrou | 758153b | 2012-05-12 15:51:51 +0200 | [diff] [blame] | 10565 | if (kind2 > kind) { |
| 10566 | Py_DECREF(sub); |
| 10567 | Py_DECREF(str); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 10568 | return 0; |
Antoine Pitrou | 758153b | 2012-05-12 15:51:51 +0200 | [diff] [blame] | 10569 | } |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10570 | buf2 = _PyUnicode_AsKind(sub, kind); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 10571 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10572 | if (!buf2) { |
| 10573 | Py_DECREF(sub); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 10574 | Py_DECREF(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10575 | return -1; |
| 10576 | } |
| 10577 | len1 = PyUnicode_GET_LENGTH(str); |
| 10578 | len2 = PyUnicode_GET_LENGTH(sub); |
| 10579 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10580 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10581 | case PyUnicode_1BYTE_KIND: |
| 10582 | result = ucs1lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10583 | break; |
| 10584 | case PyUnicode_2BYTE_KIND: |
| 10585 | result = ucs2lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10586 | break; |
| 10587 | case PyUnicode_4BYTE_KIND: |
| 10588 | result = ucs4lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10589 | break; |
| 10590 | default: |
| 10591 | result = -1; |
| 10592 | assert(0); |
| 10593 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10594 | |
| 10595 | Py_DECREF(str); |
| 10596 | Py_DECREF(sub); |
| 10597 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10598 | if (kind2 != kind) |
| 10599 | PyMem_Free(buf2); |
| 10600 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10601 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10602 | } |
| 10603 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10604 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 10605 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10606 | PyObject * |
| 10607 | PyUnicode_Concat(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10608 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10609 | PyObject *u = NULL, *v = NULL, *w; |
Victor Stinner | 127226b | 2011-10-13 01:12:34 +0200 | [diff] [blame] | 10610 | Py_UCS4 maxchar, maxchar2; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10611 | Py_ssize_t u_len, v_len, new_len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10612 | |
| 10613 | /* Coerce the two arguments */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10614 | u = PyUnicode_FromObject(left); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10615 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10616 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10617 | v = PyUnicode_FromObject(right); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10618 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10619 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10620 | |
| 10621 | /* Shortcuts */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 10622 | if (v == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10623 | Py_DECREF(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10624 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10625 | } |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 10626 | if (u == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10627 | Py_DECREF(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10628 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10629 | } |
| 10630 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10631 | u_len = PyUnicode_GET_LENGTH(u); |
| 10632 | v_len = PyUnicode_GET_LENGTH(v); |
| 10633 | if (u_len > PY_SSIZE_T_MAX - v_len) { |
| 10634 | PyErr_SetString(PyExc_OverflowError, |
| 10635 | "strings are too large to concat"); |
| 10636 | goto onError; |
| 10637 | } |
| 10638 | new_len = u_len + v_len; |
| 10639 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10640 | maxchar = PyUnicode_MAX_CHAR_VALUE(u); |
Victor Stinner | 127226b | 2011-10-13 01:12:34 +0200 | [diff] [blame] | 10641 | maxchar2 = PyUnicode_MAX_CHAR_VALUE(v); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 10642 | maxchar = MAX_MAXCHAR(maxchar, maxchar2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10643 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10644 | /* Concat the two Unicode strings */ |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10645 | w = PyUnicode_New(new_len, maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10646 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10647 | goto onError; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 10648 | _PyUnicode_FastCopyCharacters(w, 0, u, 0, u_len); |
| 10649 | _PyUnicode_FastCopyCharacters(w, u_len, v, 0, v_len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10650 | Py_DECREF(u); |
| 10651 | Py_DECREF(v); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10652 | assert(_PyUnicode_CheckConsistency(w, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10653 | return w; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10654 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10655 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10656 | Py_XDECREF(u); |
| 10657 | Py_XDECREF(v); |
| 10658 | return NULL; |
| 10659 | } |
| 10660 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10661 | void |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10662 | PyUnicode_Append(PyObject **p_left, PyObject *right) |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10663 | { |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10664 | PyObject *left, *res; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10665 | Py_UCS4 maxchar, maxchar2; |
| 10666 | Py_ssize_t left_len, right_len, new_len; |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10667 | |
| 10668 | if (p_left == NULL) { |
| 10669 | if (!PyErr_Occurred()) |
| 10670 | PyErr_BadInternalCall(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10671 | return; |
| 10672 | } |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10673 | left = *p_left; |
| 10674 | if (right == NULL || !PyUnicode_Check(left)) { |
| 10675 | if (!PyErr_Occurred()) |
| 10676 | PyErr_BadInternalCall(); |
| 10677 | goto error; |
| 10678 | } |
| 10679 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 10680 | if (PyUnicode_READY(left) == -1) |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 10681 | goto error; |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 10682 | if (PyUnicode_READY(right) == -1) |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 10683 | goto error; |
| 10684 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10685 | /* Shortcuts */ |
| 10686 | if (left == unicode_empty) { |
| 10687 | Py_DECREF(left); |
| 10688 | Py_INCREF(right); |
| 10689 | *p_left = right; |
| 10690 | return; |
| 10691 | } |
| 10692 | if (right == unicode_empty) |
| 10693 | return; |
| 10694 | |
| 10695 | left_len = PyUnicode_GET_LENGTH(left); |
| 10696 | right_len = PyUnicode_GET_LENGTH(right); |
| 10697 | if (left_len > PY_SSIZE_T_MAX - right_len) { |
| 10698 | PyErr_SetString(PyExc_OverflowError, |
| 10699 | "strings are too large to concat"); |
| 10700 | goto error; |
| 10701 | } |
| 10702 | new_len = left_len + right_len; |
| 10703 | |
| 10704 | if (unicode_modifiable(left) |
| 10705 | && PyUnicode_CheckExact(right) |
| 10706 | && PyUnicode_KIND(right) <= PyUnicode_KIND(left) |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10707 | /* Don't resize for ascii += latin1. Convert ascii to latin1 requires |
| 10708 | to change the structure size, but characters are stored just after |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 10709 | the structure, and so it requires to move all characters which is |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10710 | not so different than duplicating the string. */ |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10711 | && !(PyUnicode_IS_ASCII(left) && !PyUnicode_IS_ASCII(right))) |
| 10712 | { |
| 10713 | /* append inplace */ |
| 10714 | if (unicode_resize(p_left, new_len) != 0) { |
| 10715 | /* XXX if _PyUnicode_Resize() fails, 'left' has been |
| 10716 | * deallocated so it cannot be put back into |
| 10717 | * 'variable'. The MemoryError is raised when there |
| 10718 | * is no value in 'variable', which might (very |
| 10719 | * remotely) be a cause of incompatibilities. |
| 10720 | */ |
| 10721 | goto error; |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10722 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10723 | /* copy 'right' into the newly allocated area of 'left' */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 10724 | _PyUnicode_FastCopyCharacters(*p_left, left_len, right, 0, right_len); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10725 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10726 | else { |
| 10727 | maxchar = PyUnicode_MAX_CHAR_VALUE(left); |
| 10728 | maxchar2 = PyUnicode_MAX_CHAR_VALUE(right); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 10729 | maxchar = MAX_MAXCHAR(maxchar, maxchar2); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10730 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10731 | /* Concat the two Unicode strings */ |
| 10732 | res = PyUnicode_New(new_len, maxchar); |
| 10733 | if (res == NULL) |
| 10734 | goto error; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 10735 | _PyUnicode_FastCopyCharacters(res, 0, left, 0, left_len); |
| 10736 | _PyUnicode_FastCopyCharacters(res, left_len, right, 0, right_len); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10737 | Py_DECREF(left); |
| 10738 | *p_left = res; |
| 10739 | } |
| 10740 | assert(_PyUnicode_CheckConsistency(*p_left, 1)); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10741 | return; |
| 10742 | |
| 10743 | error: |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10744 | Py_CLEAR(*p_left); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10745 | } |
| 10746 | |
| 10747 | void |
| 10748 | PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right) |
| 10749 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10750 | PyUnicode_Append(pleft, right); |
| 10751 | Py_XDECREF(right); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10752 | } |
| 10753 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10754 | PyDoc_STRVAR(count__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10755 | "S.count(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10756 | \n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10757 | Return the number of non-overlapping occurrences of substring sub in\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10758 | 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] | 10759 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10760 | |
| 10761 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10762 | unicode_count(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10763 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10764 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10765 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10766 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10767 | PyObject *result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10768 | int kind1, kind2, kind; |
| 10769 | void *buf1, *buf2; |
| 10770 | Py_ssize_t len1, len2, iresult; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10771 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10772 | if (!stringlib_parse_args_finds_unicode("count", args, &substring, |
| 10773 | &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10774 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10775 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10776 | kind1 = PyUnicode_KIND(self); |
| 10777 | kind2 = PyUnicode_KIND(substring); |
Benjamin Peterson | b63f49f | 2012-05-03 18:31:07 -0400 | [diff] [blame] | 10778 | if (kind2 > kind1) |
| 10779 | return PyLong_FromLong(0); |
| 10780 | kind = kind1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10781 | buf1 = PyUnicode_DATA(self); |
| 10782 | buf2 = PyUnicode_DATA(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10783 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10784 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10785 | if (!buf2) { |
| 10786 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10787 | return NULL; |
| 10788 | } |
| 10789 | len1 = PyUnicode_GET_LENGTH(self); |
| 10790 | len2 = PyUnicode_GET_LENGTH(substring); |
| 10791 | |
| 10792 | ADJUST_INDICES(start, end, len1); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10793 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10794 | case PyUnicode_1BYTE_KIND: |
| 10795 | iresult = ucs1lib_count( |
| 10796 | ((Py_UCS1*)buf1) + start, end - start, |
| 10797 | buf2, len2, PY_SSIZE_T_MAX |
| 10798 | ); |
| 10799 | break; |
| 10800 | case PyUnicode_2BYTE_KIND: |
| 10801 | iresult = ucs2lib_count( |
| 10802 | ((Py_UCS2*)buf1) + start, end - start, |
| 10803 | buf2, len2, PY_SSIZE_T_MAX |
| 10804 | ); |
| 10805 | break; |
| 10806 | case PyUnicode_4BYTE_KIND: |
| 10807 | iresult = ucs4lib_count( |
| 10808 | ((Py_UCS4*)buf1) + start, end - start, |
| 10809 | buf2, len2, PY_SSIZE_T_MAX |
| 10810 | ); |
| 10811 | break; |
| 10812 | default: |
| 10813 | assert(0); iresult = 0; |
| 10814 | } |
| 10815 | |
| 10816 | result = PyLong_FromSsize_t(iresult); |
| 10817 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10818 | if (kind2 != kind) |
| 10819 | PyMem_Free(buf2); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10820 | |
| 10821 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10822 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10823 | return result; |
| 10824 | } |
| 10825 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10826 | PyDoc_STRVAR(encode__doc__, |
Victor Stinner | c911bbf | 2010-11-07 19:04:46 +0000 | [diff] [blame] | 10827 | "S.encode(encoding='utf-8', errors='strict') -> bytes\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10828 | \n\ |
Victor Stinner | e14e212 | 2010-11-07 18:41:46 +0000 | [diff] [blame] | 10829 | Encode S using the codec registered for encoding. Default encoding\n\ |
| 10830 | 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] | 10831 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 10832 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 10833 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 10834 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10835 | |
| 10836 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10837 | unicode_encode(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10838 | { |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 10839 | static char *kwlist[] = {"encoding", "errors", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10840 | char *encoding = NULL; |
| 10841 | char *errors = NULL; |
Guido van Rossum | 35d9428 | 2007-08-27 18:20:11 +0000 | [diff] [blame] | 10842 | |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 10843 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:encode", |
| 10844 | kwlist, &encoding, &errors)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10845 | return NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10846 | return PyUnicode_AsEncodedString(self, encoding, errors); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 10847 | } |
| 10848 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10849 | PyDoc_STRVAR(expandtabs__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10850 | "S.expandtabs([tabsize]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10851 | \n\ |
| 10852 | 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] | 10853 | 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] | 10854 | |
| 10855 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10856 | unicode_expandtabs(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10857 | { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10858 | Py_ssize_t i, j, line_pos, src_len, incr; |
| 10859 | Py_UCS4 ch; |
| 10860 | PyObject *u; |
| 10861 | void *src_data, *dest_data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10862 | int tabsize = 8; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10863 | int kind; |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10864 | int found; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10865 | |
| 10866 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10867 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10868 | |
Antoine Pitrou | 2242522 | 2011-10-04 19:10:51 +0200 | [diff] [blame] | 10869 | if (PyUnicode_READY(self) == -1) |
| 10870 | return NULL; |
| 10871 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 10872 | /* First pass: determine size of output string */ |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10873 | src_len = PyUnicode_GET_LENGTH(self); |
| 10874 | i = j = line_pos = 0; |
| 10875 | kind = PyUnicode_KIND(self); |
| 10876 | src_data = PyUnicode_DATA(self); |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10877 | found = 0; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10878 | for (; i < src_len; i++) { |
| 10879 | ch = PyUnicode_READ(kind, src_data, i); |
| 10880 | if (ch == '\t') { |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10881 | found = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10882 | if (tabsize > 0) { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10883 | incr = tabsize - (line_pos % tabsize); /* cannot overflow */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10884 | if (j > PY_SSIZE_T_MAX - incr) |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10885 | goto overflow; |
| 10886 | line_pos += incr; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10887 | j += incr; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10888 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10889 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10890 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10891 | if (j > PY_SSIZE_T_MAX - 1) |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10892 | goto overflow; |
| 10893 | line_pos++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10894 | j++; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10895 | if (ch == '\n' || ch == '\r') |
| 10896 | line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10897 | } |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10898 | } |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10899 | if (!found) |
| 10900 | return unicode_result_unchanged(self); |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 10901 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10902 | /* Second pass: create output string and fill it */ |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10903 | u = PyUnicode_New(j, PyUnicode_MAX_CHAR_VALUE(self)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10904 | if (!u) |
| 10905 | return NULL; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10906 | dest_data = PyUnicode_DATA(u); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10907 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10908 | i = j = line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10909 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10910 | for (; i < src_len; i++) { |
| 10911 | ch = PyUnicode_READ(kind, src_data, i); |
| 10912 | if (ch == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10913 | if (tabsize > 0) { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10914 | incr = tabsize - (line_pos % tabsize); |
| 10915 | line_pos += incr; |
Victor Stinner | da79e63 | 2012-02-22 13:37:04 +0100 | [diff] [blame] | 10916 | FILL(kind, dest_data, ' ', j, incr); |
| 10917 | j += incr; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10918 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10919 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10920 | else { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10921 | line_pos++; |
| 10922 | PyUnicode_WRITE(kind, dest_data, j, ch); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10923 | j++; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10924 | if (ch == '\n' || ch == '\r') |
| 10925 | line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10926 | } |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10927 | } |
| 10928 | assert (j == PyUnicode_GET_LENGTH(u)); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 10929 | return unicode_result(u); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10930 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10931 | overflow: |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10932 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 10933 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10934 | } |
| 10935 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10936 | PyDoc_STRVAR(find__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10937 | "S.find(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10938 | \n\ |
| 10939 | Return the lowest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 10940 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10941 | arguments start and end are interpreted as in slice notation.\n\ |
| 10942 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10943 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10944 | |
| 10945 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10946 | unicode_find(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10947 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10948 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 10949 | Py_ssize_t start; |
| 10950 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10951 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10952 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10953 | if (!stringlib_parse_args_finds_unicode("find", args, &substring, |
| 10954 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10955 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10956 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10957 | if (PyUnicode_READY(self) == -1) |
| 10958 | return NULL; |
| 10959 | if (PyUnicode_READY(substring) == -1) |
| 10960 | return NULL; |
| 10961 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10962 | result = any_find_slice(1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10963 | |
| 10964 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10965 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10966 | if (result == -2) |
| 10967 | return NULL; |
| 10968 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 10969 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10970 | } |
| 10971 | |
| 10972 | static PyObject * |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 10973 | unicode_getitem(PyObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10974 | { |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 10975 | void *data; |
| 10976 | enum PyUnicode_Kind kind; |
| 10977 | Py_UCS4 ch; |
| 10978 | PyObject *res; |
| 10979 | |
| 10980 | if (!PyUnicode_Check(self) || PyUnicode_READY(self) == -1) { |
| 10981 | PyErr_BadArgument(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10982 | return NULL; |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 10983 | } |
| 10984 | if (index < 0 || index >= PyUnicode_GET_LENGTH(self)) { |
| 10985 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 10986 | return NULL; |
| 10987 | } |
| 10988 | kind = PyUnicode_KIND(self); |
| 10989 | data = PyUnicode_DATA(self); |
| 10990 | ch = PyUnicode_READ(kind, data, index); |
| 10991 | if (ch < 256) |
| 10992 | return get_latin1_char(ch); |
| 10993 | |
| 10994 | res = PyUnicode_New(1, ch); |
| 10995 | if (res == NULL) |
| 10996 | return NULL; |
| 10997 | kind = PyUnicode_KIND(res); |
| 10998 | data = PyUnicode_DATA(res); |
| 10999 | PyUnicode_WRITE(kind, data, 0, ch); |
| 11000 | assert(_PyUnicode_CheckConsistency(res, 1)); |
| 11001 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11002 | } |
| 11003 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11004 | /* Believe it or not, this produces the same value for ASCII strings |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 11005 | as bytes_hash(). */ |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 11006 | static Py_hash_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11007 | unicode_hash(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11008 | { |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11009 | Py_ssize_t len; |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 11010 | Py_uhash_t x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11011 | |
Benjamin Peterson | f6622c8 | 2012-04-09 14:53:07 -0400 | [diff] [blame] | 11012 | #ifdef Py_DEBUG |
Benjamin Peterson | 69e9727 | 2012-02-21 11:08:50 -0500 | [diff] [blame] | 11013 | assert(_Py_HashSecret_Initialized); |
Benjamin Peterson | f6622c8 | 2012-04-09 14:53:07 -0400 | [diff] [blame] | 11014 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11015 | if (_PyUnicode_HASH(self) != -1) |
| 11016 | return _PyUnicode_HASH(self); |
| 11017 | if (PyUnicode_READY(self) == -1) |
| 11018 | return -1; |
| 11019 | len = PyUnicode_GET_LENGTH(self); |
Georg Brandl | 16fa2a1 | 2012-02-21 00:50:13 +0100 | [diff] [blame] | 11020 | /* |
| 11021 | We make the hash of the empty string be 0, rather than using |
| 11022 | (prefix ^ suffix), since this slightly obfuscates the hash secret |
| 11023 | */ |
| 11024 | if (len == 0) { |
| 11025 | _PyUnicode_HASH(self) = 0; |
| 11026 | return 0; |
| 11027 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11028 | |
| 11029 | /* The hash function as a macro, gets expanded three times below. */ |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 11030 | #define HASH(P) \ |
| 11031 | x ^= (Py_uhash_t) *P << 7; \ |
| 11032 | while (--len >= 0) \ |
| 11033 | x = (_PyHASH_MULTIPLIER * x) ^ (Py_uhash_t) *P++; \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11034 | |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 11035 | x = (Py_uhash_t) _Py_HashSecret.prefix; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11036 | switch (PyUnicode_KIND(self)) { |
| 11037 | case PyUnicode_1BYTE_KIND: { |
| 11038 | const unsigned char *c = PyUnicode_1BYTE_DATA(self); |
| 11039 | HASH(c); |
| 11040 | break; |
| 11041 | } |
| 11042 | case PyUnicode_2BYTE_KIND: { |
| 11043 | const Py_UCS2 *s = PyUnicode_2BYTE_DATA(self); |
| 11044 | HASH(s); |
| 11045 | break; |
| 11046 | } |
| 11047 | default: { |
| 11048 | Py_UCS4 *l; |
| 11049 | assert(PyUnicode_KIND(self) == PyUnicode_4BYTE_KIND && |
| 11050 | "Impossible switch case in unicode_hash"); |
| 11051 | l = PyUnicode_4BYTE_DATA(self); |
| 11052 | HASH(l); |
| 11053 | break; |
| 11054 | } |
| 11055 | } |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 11056 | x ^= (Py_uhash_t) PyUnicode_GET_LENGTH(self); |
| 11057 | x ^= (Py_uhash_t) _Py_HashSecret.suffix; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11058 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11059 | if (x == -1) |
| 11060 | x = -2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11061 | _PyUnicode_HASH(self) = x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11062 | return x; |
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 | #undef HASH |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11065 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11066 | PyDoc_STRVAR(index__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11067 | "S.index(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11068 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11069 | 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] | 11070 | |
| 11071 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11072 | unicode_index(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11073 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11074 | Py_ssize_t result; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11075 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 11076 | Py_ssize_t start; |
| 11077 | Py_ssize_t end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11078 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11079 | if (!stringlib_parse_args_finds_unicode("index", args, &substring, |
| 11080 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11081 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11082 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11083 | if (PyUnicode_READY(self) == -1) |
| 11084 | return NULL; |
| 11085 | if (PyUnicode_READY(substring) == -1) |
| 11086 | return NULL; |
| 11087 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11088 | result = any_find_slice(1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11089 | |
| 11090 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11091 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11092 | if (result == -2) |
| 11093 | return NULL; |
| 11094 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11095 | if (result < 0) { |
| 11096 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 11097 | return NULL; |
| 11098 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11099 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11100 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11101 | } |
| 11102 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11103 | PyDoc_STRVAR(islower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11104 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11105 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11106 | 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] | 11107 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11108 | |
| 11109 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11110 | unicode_islower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11111 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11112 | Py_ssize_t i, length; |
| 11113 | int kind; |
| 11114 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11115 | int cased; |
| 11116 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11117 | if (PyUnicode_READY(self) == -1) |
| 11118 | return NULL; |
| 11119 | length = PyUnicode_GET_LENGTH(self); |
| 11120 | kind = PyUnicode_KIND(self); |
| 11121 | data = PyUnicode_DATA(self); |
| 11122 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11123 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11124 | if (length == 1) |
| 11125 | return PyBool_FromLong( |
| 11126 | Py_UNICODE_ISLOWER(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11127 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11128 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11129 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11130 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11131 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11132 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11133 | for (i = 0; i < length; i++) { |
| 11134 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11135 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11136 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 11137 | return PyBool_FromLong(0); |
| 11138 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 11139 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11140 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11141 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11142 | } |
| 11143 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11144 | PyDoc_STRVAR(isupper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11145 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11146 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11147 | 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] | 11148 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11149 | |
| 11150 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11151 | unicode_isupper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11152 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11153 | Py_ssize_t i, length; |
| 11154 | int kind; |
| 11155 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11156 | int cased; |
| 11157 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11158 | if (PyUnicode_READY(self) == -1) |
| 11159 | return NULL; |
| 11160 | length = PyUnicode_GET_LENGTH(self); |
| 11161 | kind = PyUnicode_KIND(self); |
| 11162 | data = PyUnicode_DATA(self); |
| 11163 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11164 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11165 | if (length == 1) |
| 11166 | return PyBool_FromLong( |
| 11167 | Py_UNICODE_ISUPPER(PyUnicode_READ(kind, data, 0)) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11168 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11169 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11170 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11171 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11172 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11173 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11174 | for (i = 0; i < length; i++) { |
| 11175 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11176 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11177 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 11178 | return PyBool_FromLong(0); |
| 11179 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 11180 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11181 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11182 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11183 | } |
| 11184 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11185 | PyDoc_STRVAR(istitle__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11186 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11187 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11188 | Return True if S is a titlecased string and there is at least one\n\ |
| 11189 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 11190 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 11191 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11192 | |
| 11193 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11194 | unicode_istitle(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11195 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11196 | Py_ssize_t i, length; |
| 11197 | int kind; |
| 11198 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11199 | int cased, previous_is_cased; |
| 11200 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11201 | if (PyUnicode_READY(self) == -1) |
| 11202 | return NULL; |
| 11203 | length = PyUnicode_GET_LENGTH(self); |
| 11204 | kind = PyUnicode_KIND(self); |
| 11205 | data = PyUnicode_DATA(self); |
| 11206 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11207 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11208 | if (length == 1) { |
| 11209 | Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11210 | return PyBool_FromLong((Py_UNICODE_ISTITLE(ch) != 0) || |
| 11211 | (Py_UNICODE_ISUPPER(ch) != 0)); |
| 11212 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11213 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11214 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11215 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11216 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11217 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11218 | cased = 0; |
| 11219 | previous_is_cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11220 | for (i = 0; i < length; i++) { |
| 11221 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11222 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11223 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 11224 | if (previous_is_cased) |
| 11225 | return PyBool_FromLong(0); |
| 11226 | previous_is_cased = 1; |
| 11227 | cased = 1; |
| 11228 | } |
| 11229 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 11230 | if (!previous_is_cased) |
| 11231 | return PyBool_FromLong(0); |
| 11232 | previous_is_cased = 1; |
| 11233 | cased = 1; |
| 11234 | } |
| 11235 | else |
| 11236 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11237 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11238 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11239 | } |
| 11240 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11241 | PyDoc_STRVAR(isspace__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11242 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11243 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11244 | Return True if all characters in S are whitespace\n\ |
| 11245 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11246 | |
| 11247 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11248 | unicode_isspace(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11249 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11250 | Py_ssize_t i, length; |
| 11251 | int kind; |
| 11252 | void *data; |
| 11253 | |
| 11254 | if (PyUnicode_READY(self) == -1) |
| 11255 | return NULL; |
| 11256 | length = PyUnicode_GET_LENGTH(self); |
| 11257 | kind = PyUnicode_KIND(self); |
| 11258 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11259 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11260 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11261 | if (length == 1) |
| 11262 | return PyBool_FromLong( |
| 11263 | Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11264 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11265 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11266 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11267 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11268 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11269 | for (i = 0; i < length; i++) { |
| 11270 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11271 | if (!Py_UNICODE_ISSPACE(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11272 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11273 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11274 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11275 | } |
| 11276 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11277 | PyDoc_STRVAR(isalpha__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11278 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11279 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11280 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11281 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11282 | |
| 11283 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11284 | unicode_isalpha(PyObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11285 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11286 | Py_ssize_t i, length; |
| 11287 | int kind; |
| 11288 | void *data; |
| 11289 | |
| 11290 | if (PyUnicode_READY(self) == -1) |
| 11291 | return NULL; |
| 11292 | length = PyUnicode_GET_LENGTH(self); |
| 11293 | kind = PyUnicode_KIND(self); |
| 11294 | data = PyUnicode_DATA(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11295 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11296 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11297 | if (length == 1) |
| 11298 | return PyBool_FromLong( |
| 11299 | Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, 0))); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11300 | |
| 11301 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11302 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11303 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11304 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11305 | for (i = 0; i < length; i++) { |
| 11306 | if (!Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11307 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11308 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11309 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11310 | } |
| 11311 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11312 | PyDoc_STRVAR(isalnum__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11313 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11314 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11315 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11316 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11317 | |
| 11318 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11319 | unicode_isalnum(PyObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11320 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11321 | int kind; |
| 11322 | void *data; |
| 11323 | Py_ssize_t len, i; |
| 11324 | |
| 11325 | if (PyUnicode_READY(self) == -1) |
| 11326 | return NULL; |
| 11327 | |
| 11328 | kind = PyUnicode_KIND(self); |
| 11329 | data = PyUnicode_DATA(self); |
| 11330 | len = PyUnicode_GET_LENGTH(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11331 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11332 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11333 | if (len == 1) { |
| 11334 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11335 | return PyBool_FromLong(Py_UNICODE_ISALNUM(ch)); |
| 11336 | } |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11337 | |
| 11338 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11339 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11340 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11341 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11342 | for (i = 0; i < len; i++) { |
| 11343 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11344 | if (!Py_UNICODE_ISALNUM(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11345 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11346 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11347 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11348 | } |
| 11349 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11350 | PyDoc_STRVAR(isdecimal__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11351 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11352 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11353 | 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] | 11354 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11355 | |
| 11356 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11357 | unicode_isdecimal(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11358 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11359 | Py_ssize_t i, length; |
| 11360 | int kind; |
| 11361 | void *data; |
| 11362 | |
| 11363 | if (PyUnicode_READY(self) == -1) |
| 11364 | return NULL; |
| 11365 | length = PyUnicode_GET_LENGTH(self); |
| 11366 | kind = PyUnicode_KIND(self); |
| 11367 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11368 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11369 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11370 | if (length == 1) |
| 11371 | return PyBool_FromLong( |
| 11372 | Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11373 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11374 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11375 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11376 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11377 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11378 | for (i = 0; i < length; i++) { |
| 11379 | if (!Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11380 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11381 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11382 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11383 | } |
| 11384 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11385 | PyDoc_STRVAR(isdigit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11386 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11387 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11388 | Return True if all characters in S are digits\n\ |
| 11389 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11390 | |
| 11391 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11392 | unicode_isdigit(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11393 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11394 | Py_ssize_t i, length; |
| 11395 | int kind; |
| 11396 | void *data; |
| 11397 | |
| 11398 | if (PyUnicode_READY(self) == -1) |
| 11399 | return NULL; |
| 11400 | length = PyUnicode_GET_LENGTH(self); |
| 11401 | kind = PyUnicode_KIND(self); |
| 11402 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11403 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11404 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11405 | if (length == 1) { |
| 11406 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11407 | return PyBool_FromLong(Py_UNICODE_ISDIGIT(ch)); |
| 11408 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11409 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11410 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11411 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11412 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11413 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11414 | for (i = 0; i < length; i++) { |
| 11415 | if (!Py_UNICODE_ISDIGIT(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11416 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11417 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11418 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11419 | } |
| 11420 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11421 | PyDoc_STRVAR(isnumeric__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11422 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11423 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11424 | 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] | 11425 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11426 | |
| 11427 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11428 | unicode_isnumeric(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11429 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11430 | Py_ssize_t i, length; |
| 11431 | int kind; |
| 11432 | void *data; |
| 11433 | |
| 11434 | if (PyUnicode_READY(self) == -1) |
| 11435 | return NULL; |
| 11436 | length = PyUnicode_GET_LENGTH(self); |
| 11437 | kind = PyUnicode_KIND(self); |
| 11438 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11439 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11440 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11441 | if (length == 1) |
| 11442 | return PyBool_FromLong( |
| 11443 | Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11444 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11445 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11446 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11447 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11448 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11449 | for (i = 0; i < length; i++) { |
| 11450 | if (!Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11451 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11452 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11453 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11454 | } |
| 11455 | |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11456 | int |
| 11457 | PyUnicode_IsIdentifier(PyObject *self) |
| 11458 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11459 | int kind; |
| 11460 | void *data; |
| 11461 | Py_ssize_t i; |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11462 | Py_UCS4 first; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11463 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11464 | if (PyUnicode_READY(self) == -1) { |
| 11465 | Py_FatalError("identifier not ready"); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11466 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11467 | } |
| 11468 | |
| 11469 | /* Special case for empty strings */ |
| 11470 | if (PyUnicode_GET_LENGTH(self) == 0) |
| 11471 | return 0; |
| 11472 | kind = PyUnicode_KIND(self); |
| 11473 | data = PyUnicode_DATA(self); |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11474 | |
| 11475 | /* PEP 3131 says that the first character must be in |
| 11476 | XID_Start and subsequent characters in XID_Continue, |
| 11477 | and for the ASCII range, the 2.x rules apply (i.e |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11478 | start with letters and underscore, continue with |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11479 | letters, digits, underscore). However, given the current |
| 11480 | definition of XID_Start and XID_Continue, it is sufficient |
| 11481 | to check just for these, except that _ must be allowed |
| 11482 | as starting an identifier. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11483 | first = PyUnicode_READ(kind, data, 0); |
Benjamin Peterson | f413b80 | 2011-08-12 22:17:18 -0500 | [diff] [blame] | 11484 | if (!_PyUnicode_IsXidStart(first) && first != 0x5F /* LOW LINE */) |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11485 | return 0; |
| 11486 | |
Benjamin Peterson | 9c6e6a0 | 2011-09-28 08:09:05 -0400 | [diff] [blame] | 11487 | for (i = 1; i < PyUnicode_GET_LENGTH(self); i++) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11488 | if (!_PyUnicode_IsXidContinue(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11489 | return 0; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11490 | return 1; |
| 11491 | } |
| 11492 | |
| 11493 | PyDoc_STRVAR(isidentifier__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11494 | "S.isidentifier() -> bool\n\ |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11495 | \n\ |
| 11496 | Return True if S is a valid identifier according\n\ |
| 11497 | to the language definition."); |
| 11498 | |
| 11499 | static PyObject* |
| 11500 | unicode_isidentifier(PyObject *self) |
| 11501 | { |
| 11502 | return PyBool_FromLong(PyUnicode_IsIdentifier(self)); |
| 11503 | } |
| 11504 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11505 | PyDoc_STRVAR(isprintable__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11506 | "S.isprintable() -> bool\n\ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11507 | \n\ |
| 11508 | Return True if all characters in S are considered\n\ |
| 11509 | printable in repr() or S is empty, False otherwise."); |
| 11510 | |
| 11511 | static PyObject* |
| 11512 | unicode_isprintable(PyObject *self) |
| 11513 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11514 | Py_ssize_t i, length; |
| 11515 | int kind; |
| 11516 | void *data; |
| 11517 | |
| 11518 | if (PyUnicode_READY(self) == -1) |
| 11519 | return NULL; |
| 11520 | length = PyUnicode_GET_LENGTH(self); |
| 11521 | kind = PyUnicode_KIND(self); |
| 11522 | data = PyUnicode_DATA(self); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11523 | |
| 11524 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11525 | if (length == 1) |
| 11526 | return PyBool_FromLong( |
| 11527 | Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, 0))); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11528 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11529 | for (i = 0; i < length; i++) { |
| 11530 | if (!Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, i))) { |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11531 | Py_RETURN_FALSE; |
| 11532 | } |
| 11533 | } |
| 11534 | Py_RETURN_TRUE; |
| 11535 | } |
| 11536 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11537 | PyDoc_STRVAR(join__doc__, |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 11538 | "S.join(iterable) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11539 | \n\ |
| 11540 | 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] | 11541 | iterable. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11542 | |
| 11543 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11544 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11545 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11546 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11547 | } |
| 11548 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11549 | static Py_ssize_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11550 | unicode_length(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11551 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11552 | if (PyUnicode_READY(self) == -1) |
| 11553 | return -1; |
| 11554 | return PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11555 | } |
| 11556 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11557 | PyDoc_STRVAR(ljust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11558 | "S.ljust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11559 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 11560 | 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] | 11561 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11562 | |
| 11563 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11564 | unicode_ljust(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11565 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11566 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11567 | Py_UCS4 fillchar = ' '; |
| 11568 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11569 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11570 | return NULL; |
| 11571 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 11572 | if (PyUnicode_READY(self) == -1) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11573 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11574 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11575 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 11576 | return unicode_result_unchanged(self); |
| 11577 | |
| 11578 | return pad(self, 0, width - PyUnicode_GET_LENGTH(self), fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11579 | } |
| 11580 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11581 | PyDoc_STRVAR(lower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11582 | "S.lower() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11583 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11584 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11585 | |
| 11586 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11587 | unicode_lower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11588 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 11589 | if (PyUnicode_READY(self) == -1) |
| 11590 | return NULL; |
| 11591 | if (PyUnicode_IS_ASCII(self)) |
| 11592 | return ascii_upper_or_lower(self, 1); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 11593 | return case_operation(self, do_lower); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11594 | } |
| 11595 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11596 | #define LEFTSTRIP 0 |
| 11597 | #define RIGHTSTRIP 1 |
| 11598 | #define BOTHSTRIP 2 |
| 11599 | |
| 11600 | /* Arrays indexed by above */ |
| 11601 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 11602 | |
| 11603 | #define STRIPNAME(i) (stripformat[i]+3) |
| 11604 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11605 | /* externally visible for str.strip(unicode) */ |
| 11606 | PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11607 | _PyUnicode_XStrip(PyObject *self, int striptype, PyObject *sepobj) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11608 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11609 | void *data; |
| 11610 | int kind; |
| 11611 | Py_ssize_t i, j, len; |
| 11612 | BLOOM_MASK sepmask; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11613 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11614 | if (PyUnicode_READY(self) == -1 || PyUnicode_READY(sepobj) == -1) |
| 11615 | return NULL; |
| 11616 | |
| 11617 | kind = PyUnicode_KIND(self); |
| 11618 | data = PyUnicode_DATA(self); |
| 11619 | len = PyUnicode_GET_LENGTH(self); |
| 11620 | sepmask = make_bloom_mask(PyUnicode_KIND(sepobj), |
| 11621 | PyUnicode_DATA(sepobj), |
| 11622 | PyUnicode_GET_LENGTH(sepobj)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11623 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11624 | i = 0; |
| 11625 | if (striptype != RIGHTSTRIP) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11626 | while (i < len && |
| 11627 | BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, i), sepobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11628 | i++; |
| 11629 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11630 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11631 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11632 | j = len; |
| 11633 | if (striptype != LEFTSTRIP) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11634 | do { |
| 11635 | j--; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11636 | } while (j >= i && |
| 11637 | BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, j), sepobj)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11638 | j++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11639 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11640 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11641 | return PyUnicode_Substring(self, i, j); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11642 | } |
| 11643 | |
| 11644 | PyObject* |
| 11645 | PyUnicode_Substring(PyObject *self, Py_ssize_t start, Py_ssize_t end) |
| 11646 | { |
| 11647 | unsigned char *data; |
| 11648 | int kind; |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11649 | Py_ssize_t length; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11650 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 11651 | if (PyUnicode_READY(self) == -1) |
| 11652 | return NULL; |
| 11653 | |
Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 11654 | length = PyUnicode_GET_LENGTH(self); |
| 11655 | end = Py_MIN(end, length); |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 11656 | |
Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 11657 | if (start == 0 && end == length) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11658 | return unicode_result_unchanged(self); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11659 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 11660 | if (start < 0 || end < 0) { |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11661 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 11662 | return NULL; |
| 11663 | } |
Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 11664 | if (start >= length || end < start) { |
Victor Stinner | 3a7f7977 | 2012-05-03 03:36:40 +0200 | [diff] [blame] | 11665 | Py_INCREF(unicode_empty); |
| 11666 | return unicode_empty; |
Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 11667 | } |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11668 | |
Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 11669 | length = end - start; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11670 | if (PyUnicode_IS_ASCII(self)) { |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11671 | data = PyUnicode_1BYTE_DATA(self); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 11672 | return _PyUnicode_FromASCII((char*)(data + start), length); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11673 | } |
| 11674 | else { |
| 11675 | kind = PyUnicode_KIND(self); |
| 11676 | data = PyUnicode_1BYTE_DATA(self); |
| 11677 | return PyUnicode_FromKindAndData(kind, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 11678 | data + kind * start, |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11679 | length); |
| 11680 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11681 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11682 | |
| 11683 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11684 | do_strip(PyObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11685 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11686 | int kind; |
| 11687 | void *data; |
| 11688 | Py_ssize_t len, i, j; |
| 11689 | |
| 11690 | if (PyUnicode_READY(self) == -1) |
| 11691 | return NULL; |
| 11692 | |
| 11693 | kind = PyUnicode_KIND(self); |
| 11694 | data = PyUnicode_DATA(self); |
| 11695 | len = PyUnicode_GET_LENGTH(self); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11696 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11697 | i = 0; |
| 11698 | if (striptype != RIGHTSTRIP) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11699 | while (i < len && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, i))) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11700 | i++; |
| 11701 | } |
| 11702 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11703 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11704 | j = len; |
| 11705 | if (striptype != LEFTSTRIP) { |
| 11706 | do { |
| 11707 | j--; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11708 | } while (j >= i && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, j))); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11709 | j++; |
| 11710 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11711 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11712 | return PyUnicode_Substring(self, i, j); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11713 | } |
| 11714 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11715 | |
| 11716 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11717 | do_argstrip(PyObject *self, int striptype, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11718 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11719 | PyObject *sep = NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11720 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11721 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
| 11722 | return NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11723 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11724 | if (sep != NULL && sep != Py_None) { |
| 11725 | if (PyUnicode_Check(sep)) |
| 11726 | return _PyUnicode_XStrip(self, striptype, sep); |
| 11727 | else { |
| 11728 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11729 | "%s arg must be None or str", |
| 11730 | STRIPNAME(striptype)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11731 | return NULL; |
| 11732 | } |
| 11733 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11734 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11735 | return do_strip(self, striptype); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11736 | } |
| 11737 | |
| 11738 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11739 | PyDoc_STRVAR(strip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11740 | "S.strip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11741 | \n\ |
| 11742 | Return a copy of the string S with leading and trailing\n\ |
| 11743 | whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11744 | 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] | 11745 | |
| 11746 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11747 | unicode_strip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11748 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11749 | if (PyTuple_GET_SIZE(args) == 0) |
| 11750 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 11751 | else |
| 11752 | return do_argstrip(self, BOTHSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11753 | } |
| 11754 | |
| 11755 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11756 | PyDoc_STRVAR(lstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11757 | "S.lstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11758 | \n\ |
| 11759 | Return a copy of the string S with leading whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11760 | 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] | 11761 | |
| 11762 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11763 | unicode_lstrip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11764 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11765 | if (PyTuple_GET_SIZE(args) == 0) |
| 11766 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 11767 | else |
| 11768 | return do_argstrip(self, LEFTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11769 | } |
| 11770 | |
| 11771 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11772 | PyDoc_STRVAR(rstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11773 | "S.rstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11774 | \n\ |
| 11775 | Return a copy of the string S with trailing whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11776 | 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] | 11777 | |
| 11778 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11779 | unicode_rstrip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11780 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11781 | if (PyTuple_GET_SIZE(args) == 0) |
| 11782 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 11783 | else |
| 11784 | return do_argstrip(self, RIGHTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11785 | } |
| 11786 | |
| 11787 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11788 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11789 | unicode_repeat(PyObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11790 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11791 | PyObject *u; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11792 | Py_ssize_t nchars, n; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11793 | |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 11794 | if (len < 1) { |
| 11795 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 11796 | return unicode_empty; |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 11797 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11798 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11799 | /* no repeat, return original string */ |
| 11800 | if (len == 1) |
| 11801 | return unicode_result_unchanged(str); |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 11802 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 11803 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11804 | return NULL; |
| 11805 | |
Victor Stinner | c759f3e | 2011-10-01 03:09:58 +0200 | [diff] [blame] | 11806 | if (PyUnicode_GET_LENGTH(str) > PY_SSIZE_T_MAX / len) { |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11807 | PyErr_SetString(PyExc_OverflowError, |
| 11808 | "repeated string is too long"); |
| 11809 | return NULL; |
| 11810 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11811 | nchars = len * PyUnicode_GET_LENGTH(str); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11812 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11813 | u = PyUnicode_New(nchars, PyUnicode_MAX_CHAR_VALUE(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11814 | if (!u) |
| 11815 | return NULL; |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11816 | assert(PyUnicode_KIND(u) == PyUnicode_KIND(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11817 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11818 | if (PyUnicode_GET_LENGTH(str) == 1) { |
| 11819 | const int kind = PyUnicode_KIND(str); |
| 11820 | const Py_UCS4 fill_char = PyUnicode_READ(kind, PyUnicode_DATA(str), 0); |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 11821 | if (kind == PyUnicode_1BYTE_KIND) { |
| 11822 | void *to = PyUnicode_DATA(u); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11823 | memset(to, (unsigned char)fill_char, len); |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 11824 | } |
| 11825 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 11826 | Py_UCS2 *ucs2 = PyUnicode_2BYTE_DATA(u); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11827 | for (n = 0; n < len; ++n) |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 11828 | ucs2[n] = fill_char; |
| 11829 | } else { |
| 11830 | Py_UCS4 *ucs4 = PyUnicode_4BYTE_DATA(u); |
| 11831 | assert(kind == PyUnicode_4BYTE_KIND); |
| 11832 | for (n = 0; n < len; ++n) |
| 11833 | ucs4[n] = fill_char; |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11834 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11835 | } |
| 11836 | else { |
| 11837 | /* number of characters copied this far */ |
| 11838 | Py_ssize_t done = PyUnicode_GET_LENGTH(str); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 11839 | const Py_ssize_t char_size = PyUnicode_KIND(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11840 | char *to = (char *) PyUnicode_DATA(u); |
| 11841 | Py_MEMCPY(to, PyUnicode_DATA(str), |
| 11842 | PyUnicode_GET_LENGTH(str) * char_size); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11843 | while (done < nchars) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11844 | n = (done <= nchars-done) ? done : nchars-done; |
| 11845 | Py_MEMCPY(to + (done * char_size), to, n * char_size); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11846 | done += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11847 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11848 | } |
| 11849 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 11850 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11851 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11852 | } |
| 11853 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11854 | PyObject * |
| 11855 | PyUnicode_Replace(PyObject *obj, |
| 11856 | PyObject *subobj, |
| 11857 | PyObject *replobj, |
| 11858 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11859 | { |
| 11860 | PyObject *self; |
| 11861 | PyObject *str1; |
| 11862 | PyObject *str2; |
| 11863 | PyObject *result; |
| 11864 | |
| 11865 | self = PyUnicode_FromObject(obj); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 11866 | if (self == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11867 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11868 | str1 = PyUnicode_FromObject(subobj); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 11869 | if (str1 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11870 | Py_DECREF(self); |
| 11871 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11872 | } |
| 11873 | str2 = PyUnicode_FromObject(replobj); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 11874 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11875 | Py_DECREF(self); |
| 11876 | Py_DECREF(str1); |
| 11877 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11878 | } |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 11879 | if (PyUnicode_READY(self) == -1 || |
| 11880 | PyUnicode_READY(str1) == -1 || |
| 11881 | PyUnicode_READY(str2) == -1) |
| 11882 | result = NULL; |
| 11883 | else |
| 11884 | result = replace(self, str1, str2, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11885 | Py_DECREF(self); |
| 11886 | Py_DECREF(str1); |
| 11887 | Py_DECREF(str2); |
| 11888 | return result; |
| 11889 | } |
| 11890 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11891 | PyDoc_STRVAR(replace__doc__, |
Ezio Melotti | c1897e7 | 2010-06-26 18:50:39 +0000 | [diff] [blame] | 11892 | "S.replace(old, new[, count]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11893 | \n\ |
| 11894 | Return a copy of S with all occurrences of substring\n\ |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 11895 | old replaced by new. If the optional argument count is\n\ |
| 11896 | given, only the first count occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11897 | |
| 11898 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11899 | unicode_replace(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11900 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11901 | PyObject *str1; |
| 11902 | PyObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11903 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11904 | PyObject *result; |
| 11905 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11906 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11907 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 11908 | if (PyUnicode_READY(self) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11909 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11910 | str1 = PyUnicode_FromObject(str1); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 11911 | if (str1 == NULL) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11912 | return NULL; |
| 11913 | str2 = PyUnicode_FromObject(str2); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 11914 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11915 | Py_DECREF(str1); |
| 11916 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 11917 | } |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 11918 | if (PyUnicode_READY(str1) == -1 || PyUnicode_READY(str2) == -1) |
| 11919 | result = NULL; |
| 11920 | else |
| 11921 | result = replace(self, str1, str2, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11922 | |
| 11923 | Py_DECREF(str1); |
| 11924 | Py_DECREF(str2); |
| 11925 | return result; |
| 11926 | } |
| 11927 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11928 | static PyObject * |
| 11929 | unicode_repr(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11930 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11931 | PyObject *repr; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11932 | Py_ssize_t isize; |
| 11933 | Py_ssize_t osize, squote, dquote, i, o; |
| 11934 | Py_UCS4 max, quote; |
| 11935 | int ikind, okind; |
| 11936 | void *idata, *odata; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11937 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11938 | if (PyUnicode_READY(unicode) == -1) |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11939 | return NULL; |
| 11940 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11941 | isize = PyUnicode_GET_LENGTH(unicode); |
| 11942 | idata = PyUnicode_DATA(unicode); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11943 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11944 | /* Compute length of output, quote characters, and |
| 11945 | maximum character */ |
| 11946 | osize = 2; /* quotes */ |
| 11947 | max = 127; |
| 11948 | squote = dquote = 0; |
| 11949 | ikind = PyUnicode_KIND(unicode); |
| 11950 | for (i = 0; i < isize; i++) { |
| 11951 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
| 11952 | switch (ch) { |
| 11953 | case '\'': squote++; osize++; break; |
| 11954 | case '"': dquote++; osize++; break; |
| 11955 | case '\\': case '\t': case '\r': case '\n': |
| 11956 | osize += 2; break; |
| 11957 | default: |
| 11958 | /* Fast-path ASCII */ |
| 11959 | if (ch < ' ' || ch == 0x7f) |
| 11960 | osize += 4; /* \xHH */ |
| 11961 | else if (ch < 0x7f) |
| 11962 | osize++; |
| 11963 | else if (Py_UNICODE_ISPRINTABLE(ch)) { |
| 11964 | osize++; |
| 11965 | max = ch > max ? ch : max; |
| 11966 | } |
| 11967 | else if (ch < 0x100) |
| 11968 | osize += 4; /* \xHH */ |
| 11969 | else if (ch < 0x10000) |
| 11970 | osize += 6; /* \uHHHH */ |
| 11971 | else |
| 11972 | osize += 10; /* \uHHHHHHHH */ |
| 11973 | } |
| 11974 | } |
| 11975 | |
| 11976 | quote = '\''; |
| 11977 | if (squote) { |
| 11978 | if (dquote) |
| 11979 | /* Both squote and dquote present. Use squote, |
| 11980 | and escape them */ |
| 11981 | osize += squote; |
| 11982 | else |
| 11983 | quote = '"'; |
| 11984 | } |
| 11985 | |
| 11986 | repr = PyUnicode_New(osize, max); |
| 11987 | if (repr == NULL) |
| 11988 | return NULL; |
| 11989 | okind = PyUnicode_KIND(repr); |
| 11990 | odata = PyUnicode_DATA(repr); |
| 11991 | |
| 11992 | PyUnicode_WRITE(okind, odata, 0, quote); |
| 11993 | PyUnicode_WRITE(okind, odata, osize-1, quote); |
| 11994 | |
| 11995 | for (i = 0, o = 1; i < isize; i++) { |
| 11996 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11997 | |
| 11998 | /* Escape quotes and backslashes */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11999 | if ((ch == quote) || (ch == '\\')) { |
| 12000 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12001 | PyUnicode_WRITE(okind, odata, o++, ch); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12002 | continue; |
| 12003 | } |
| 12004 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12005 | /* Map special whitespace to '\t', \n', '\r' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12006 | if (ch == '\t') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12007 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12008 | PyUnicode_WRITE(okind, odata, o++, 't'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12009 | } |
| 12010 | else if (ch == '\n') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12011 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12012 | PyUnicode_WRITE(okind, odata, o++, 'n'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12013 | } |
| 12014 | else if (ch == '\r') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12015 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12016 | PyUnicode_WRITE(okind, odata, o++, 'r'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12017 | } |
| 12018 | |
| 12019 | /* Map non-printable US ASCII to '\xhh' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12020 | else if (ch < ' ' || ch == 0x7F) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12021 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12022 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12023 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0x000F]); |
| 12024 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0x000F]); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12025 | } |
| 12026 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12027 | /* Copy ASCII characters as-is */ |
| 12028 | else if (ch < 0x7F) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12029 | PyUnicode_WRITE(okind, odata, o++, ch); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12030 | } |
| 12031 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12032 | /* Non-ASCII characters */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12033 | else { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12034 | /* Map Unicode whitespace and control characters |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12035 | (categories Z* and C* except ASCII space) |
| 12036 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12037 | if (!Py_UNICODE_ISPRINTABLE(ch)) { |
Kristján Valur Jónsson | 55e5dc8 | 2012-06-06 21:58:08 +0000 | [diff] [blame] | 12038 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12039 | /* Map 8-bit characters to '\xhh' */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12040 | if (ch <= 0xff) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12041 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12042 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0x000F]); |
| 12043 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0x000F]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12044 | } |
Kristján Valur Jónsson | 55e5dc8 | 2012-06-06 21:58:08 +0000 | [diff] [blame] | 12045 | /* Map 16-bit characters to '\uxxxx' */ |
| 12046 | else if (ch <= 0xffff) { |
| 12047 | PyUnicode_WRITE(okind, odata, o++, 'u'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12048 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 12) & 0xF]); |
| 12049 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 8) & 0xF]); |
| 12050 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0xF]); |
| 12051 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0xF]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12052 | } |
Kristján Valur Jónsson | 55e5dc8 | 2012-06-06 21:58:08 +0000 | [diff] [blame] | 12053 | /* Map 21-bit characters to '\U00xxxxxx' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12054 | else { |
Kristján Valur Jónsson | 55e5dc8 | 2012-06-06 21:58:08 +0000 | [diff] [blame] | 12055 | PyUnicode_WRITE(okind, odata, o++, 'U'); |
| 12056 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 28) & 0xF]); |
| 12057 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 24) & 0xF]); |
| 12058 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 20) & 0xF]); |
| 12059 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 16) & 0xF]); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12060 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 12) & 0xF]); |
| 12061 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 8) & 0xF]); |
| 12062 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0xF]); |
| 12063 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0xF]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12064 | } |
| 12065 | } |
| 12066 | /* Copy characters as-is */ |
| 12067 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12068 | PyUnicode_WRITE(okind, odata, o++, ch); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12069 | } |
| 12070 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12071 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12072 | /* Closing quote already added at the beginning */ |
Victor Stinner | 05d1189 | 2011-10-06 01:13:58 +0200 | [diff] [blame] | 12073 | assert(_PyUnicode_CheckConsistency(repr, 1)); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12074 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12075 | } |
| 12076 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12077 | PyDoc_STRVAR(rfind__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12078 | "S.rfind(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12079 | \n\ |
| 12080 | Return the highest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 12081 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12082 | arguments start and end are interpreted as in slice notation.\n\ |
| 12083 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12084 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12085 | |
| 12086 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12087 | unicode_rfind(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12088 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12089 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 12090 | Py_ssize_t start; |
| 12091 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12092 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12093 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12094 | if (!stringlib_parse_args_finds_unicode("rfind", args, &substring, |
| 12095 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12096 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12097 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12098 | if (PyUnicode_READY(self) == -1) |
| 12099 | return NULL; |
| 12100 | if (PyUnicode_READY(substring) == -1) |
| 12101 | return NULL; |
| 12102 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12103 | result = any_find_slice(-1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12104 | |
| 12105 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12106 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12107 | if (result == -2) |
| 12108 | return NULL; |
| 12109 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12110 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12111 | } |
| 12112 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12113 | PyDoc_STRVAR(rindex__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12114 | "S.rindex(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12115 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12116 | 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] | 12117 | |
| 12118 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12119 | unicode_rindex(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12120 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12121 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 12122 | Py_ssize_t start; |
| 12123 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12124 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12125 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12126 | if (!stringlib_parse_args_finds_unicode("rindex", args, &substring, |
| 12127 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12128 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12129 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12130 | if (PyUnicode_READY(self) == -1) |
| 12131 | return NULL; |
| 12132 | if (PyUnicode_READY(substring) == -1) |
| 12133 | return NULL; |
| 12134 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12135 | result = any_find_slice(-1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12136 | |
| 12137 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12138 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12139 | if (result == -2) |
| 12140 | return NULL; |
| 12141 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12142 | if (result < 0) { |
| 12143 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 12144 | return NULL; |
| 12145 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12146 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12147 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12148 | } |
| 12149 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12150 | PyDoc_STRVAR(rjust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12151 | "S.rjust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12152 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 12153 | 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] | 12154 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12155 | |
| 12156 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12157 | unicode_rjust(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12158 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12159 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12160 | Py_UCS4 fillchar = ' '; |
| 12161 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 12162 | 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] | 12163 | return NULL; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 12164 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 12165 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12166 | return NULL; |
| 12167 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12168 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 12169 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12170 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12171 | return pad(self, width - PyUnicode_GET_LENGTH(self), 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12172 | } |
| 12173 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12174 | PyObject * |
| 12175 | PyUnicode_Split(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12176 | { |
| 12177 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 12178 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12179 | s = PyUnicode_FromObject(s); |
| 12180 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12181 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12182 | if (sep != NULL) { |
| 12183 | sep = PyUnicode_FromObject(sep); |
| 12184 | if (sep == NULL) { |
| 12185 | Py_DECREF(s); |
| 12186 | return NULL; |
| 12187 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12188 | } |
| 12189 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12190 | result = split(s, sep, maxsplit); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12191 | |
| 12192 | Py_DECREF(s); |
| 12193 | Py_XDECREF(sep); |
| 12194 | return result; |
| 12195 | } |
| 12196 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12197 | PyDoc_STRVAR(split__doc__, |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12198 | "S.split(sep=None, maxsplit=-1) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12199 | \n\ |
| 12200 | Return a list of the words in S, using sep as the\n\ |
| 12201 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 12202 | 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] | 12203 | whitespace string is a separator and empty strings are\n\ |
| 12204 | removed from the result."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12205 | |
| 12206 | static PyObject* |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12207 | unicode_split(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12208 | { |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12209 | static char *kwlist[] = {"sep", "maxsplit", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12210 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12211 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12212 | |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12213 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|On:split", |
| 12214 | kwlist, &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12215 | return NULL; |
| 12216 | |
| 12217 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12218 | return split(self, NULL, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12219 | else if (PyUnicode_Check(substring)) |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12220 | return split(self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12221 | else |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12222 | return PyUnicode_Split(self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12223 | } |
| 12224 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12225 | PyObject * |
| 12226 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 12227 | { |
| 12228 | PyObject* str_obj; |
| 12229 | PyObject* sep_obj; |
| 12230 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12231 | int kind1, kind2, kind; |
| 12232 | void *buf1 = NULL, *buf2 = NULL; |
| 12233 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12234 | |
| 12235 | str_obj = PyUnicode_FromObject(str_in); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12236 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12237 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12238 | sep_obj = PyUnicode_FromObject(sep_in); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12239 | if (!sep_obj) { |
| 12240 | Py_DECREF(str_obj); |
| 12241 | return NULL; |
| 12242 | } |
| 12243 | if (PyUnicode_READY(sep_obj) == -1 || PyUnicode_READY(str_obj) == -1) { |
| 12244 | Py_DECREF(sep_obj); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12245 | Py_DECREF(str_obj); |
| 12246 | return NULL; |
| 12247 | } |
| 12248 | |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12249 | kind1 = PyUnicode_KIND(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12250 | kind2 = PyUnicode_KIND(sep_obj); |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12251 | kind = Py_MAX(kind1, kind2); |
| 12252 | buf1 = PyUnicode_DATA(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12253 | if (kind1 != kind) |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12254 | buf1 = _PyUnicode_AsKind(str_obj, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12255 | if (!buf1) |
| 12256 | goto onError; |
| 12257 | buf2 = PyUnicode_DATA(sep_obj); |
| 12258 | if (kind2 != kind) |
| 12259 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 12260 | if (!buf2) |
| 12261 | goto onError; |
| 12262 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 12263 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 12264 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 12265 | switch (PyUnicode_KIND(str_obj)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12266 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 12267 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) |
| 12268 | out = asciilib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12269 | else |
| 12270 | 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] | 12271 | break; |
| 12272 | case PyUnicode_2BYTE_KIND: |
| 12273 | out = ucs2lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12274 | break; |
| 12275 | case PyUnicode_4BYTE_KIND: |
| 12276 | out = ucs4lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12277 | break; |
| 12278 | default: |
| 12279 | assert(0); |
| 12280 | out = 0; |
| 12281 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12282 | |
| 12283 | Py_DECREF(sep_obj); |
| 12284 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12285 | if (kind1 != kind) |
| 12286 | PyMem_Free(buf1); |
| 12287 | if (kind2 != kind) |
| 12288 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12289 | |
| 12290 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12291 | onError: |
| 12292 | Py_DECREF(sep_obj); |
| 12293 | Py_DECREF(str_obj); |
| 12294 | if (kind1 != kind && buf1) |
| 12295 | PyMem_Free(buf1); |
| 12296 | if (kind2 != kind && buf2) |
| 12297 | PyMem_Free(buf2); |
| 12298 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12299 | } |
| 12300 | |
| 12301 | |
| 12302 | PyObject * |
| 12303 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 12304 | { |
| 12305 | PyObject* str_obj; |
| 12306 | PyObject* sep_obj; |
| 12307 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12308 | int kind1, kind2, kind; |
| 12309 | void *buf1 = NULL, *buf2 = NULL; |
| 12310 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12311 | |
| 12312 | str_obj = PyUnicode_FromObject(str_in); |
| 12313 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12314 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12315 | sep_obj = PyUnicode_FromObject(sep_in); |
| 12316 | if (!sep_obj) { |
| 12317 | Py_DECREF(str_obj); |
| 12318 | return NULL; |
| 12319 | } |
| 12320 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12321 | kind1 = PyUnicode_KIND(str_in); |
| 12322 | kind2 = PyUnicode_KIND(sep_obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 12323 | kind = Py_MAX(kind1, kind2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12324 | buf1 = PyUnicode_DATA(str_in); |
| 12325 | if (kind1 != kind) |
| 12326 | buf1 = _PyUnicode_AsKind(str_in, kind); |
| 12327 | if (!buf1) |
| 12328 | goto onError; |
| 12329 | buf2 = PyUnicode_DATA(sep_obj); |
| 12330 | if (kind2 != kind) |
| 12331 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 12332 | if (!buf2) |
| 12333 | goto onError; |
| 12334 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 12335 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 12336 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 12337 | switch (PyUnicode_KIND(str_in)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12338 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 12339 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) |
| 12340 | out = asciilib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12341 | else |
| 12342 | 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] | 12343 | break; |
| 12344 | case PyUnicode_2BYTE_KIND: |
| 12345 | out = ucs2lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12346 | break; |
| 12347 | case PyUnicode_4BYTE_KIND: |
| 12348 | out = ucs4lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12349 | break; |
| 12350 | default: |
| 12351 | assert(0); |
| 12352 | out = 0; |
| 12353 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12354 | |
| 12355 | Py_DECREF(sep_obj); |
| 12356 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12357 | if (kind1 != kind) |
| 12358 | PyMem_Free(buf1); |
| 12359 | if (kind2 != kind) |
| 12360 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12361 | |
| 12362 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12363 | onError: |
| 12364 | Py_DECREF(sep_obj); |
| 12365 | Py_DECREF(str_obj); |
| 12366 | if (kind1 != kind && buf1) |
| 12367 | PyMem_Free(buf1); |
| 12368 | if (kind2 != kind && buf2) |
| 12369 | PyMem_Free(buf2); |
| 12370 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12371 | } |
| 12372 | |
| 12373 | PyDoc_STRVAR(partition__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12374 | "S.partition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12375 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 12376 | 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] | 12377 | 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] | 12378 | found, return S and two empty strings."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12379 | |
| 12380 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12381 | unicode_partition(PyObject *self, PyObject *separator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12382 | { |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12383 | return PyUnicode_Partition(self, separator); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12384 | } |
| 12385 | |
| 12386 | PyDoc_STRVAR(rpartition__doc__, |
Ezio Melotti | 5b2b242 | 2010-01-25 11:58:28 +0000 | [diff] [blame] | 12387 | "S.rpartition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12388 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 12389 | 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] | 12390 | 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] | 12391 | separator is not found, return two empty strings and S."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12392 | |
| 12393 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12394 | unicode_rpartition(PyObject *self, PyObject *separator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12395 | { |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12396 | return PyUnicode_RPartition(self, separator); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12397 | } |
| 12398 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12399 | PyObject * |
| 12400 | PyUnicode_RSplit(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12401 | { |
| 12402 | PyObject *result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12403 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12404 | s = PyUnicode_FromObject(s); |
| 12405 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12406 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12407 | if (sep != NULL) { |
| 12408 | sep = PyUnicode_FromObject(sep); |
| 12409 | if (sep == NULL) { |
| 12410 | Py_DECREF(s); |
| 12411 | return NULL; |
| 12412 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12413 | } |
| 12414 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12415 | result = rsplit(s, sep, maxsplit); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12416 | |
| 12417 | Py_DECREF(s); |
| 12418 | Py_XDECREF(sep); |
| 12419 | return result; |
| 12420 | } |
| 12421 | |
| 12422 | PyDoc_STRVAR(rsplit__doc__, |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12423 | "S.rsplit(sep=None, maxsplit=-1) -> list of strings\n\ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12424 | \n\ |
| 12425 | Return a list of the words in S, using sep as the\n\ |
| 12426 | delimiter string, starting at the end of the string and\n\ |
| 12427 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 12428 | splits are done. If sep is not specified, any whitespace string\n\ |
| 12429 | is a separator."); |
| 12430 | |
| 12431 | static PyObject* |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12432 | unicode_rsplit(PyObject *self, PyObject *args, PyObject *kwds) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12433 | { |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12434 | static char *kwlist[] = {"sep", "maxsplit", 0}; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12435 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12436 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12437 | |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12438 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|On:rsplit", |
| 12439 | kwlist, &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12440 | return NULL; |
| 12441 | |
| 12442 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12443 | return rsplit(self, NULL, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12444 | else if (PyUnicode_Check(substring)) |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12445 | return rsplit(self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12446 | else |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12447 | return PyUnicode_RSplit(self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12448 | } |
| 12449 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12450 | PyDoc_STRVAR(splitlines__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12451 | "S.splitlines([keepends]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12452 | \n\ |
| 12453 | 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] | 12454 | 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] | 12455 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12456 | |
| 12457 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12458 | unicode_splitlines(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12459 | { |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12460 | static char *kwlist[] = {"keepends", 0}; |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 12461 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12462 | |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12463 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:splitlines", |
| 12464 | kwlist, &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12465 | return NULL; |
| 12466 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12467 | return PyUnicode_Splitlines(self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12468 | } |
| 12469 | |
| 12470 | static |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 12471 | PyObject *unicode_str(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12472 | { |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12473 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12474 | } |
| 12475 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12476 | PyDoc_STRVAR(swapcase__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12477 | "S.swapcase() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12478 | \n\ |
| 12479 | 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] | 12480 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12481 | |
| 12482 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12483 | unicode_swapcase(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12484 | { |
Benjamin Peterson | eea4846 | 2012-01-16 14:28:50 -0500 | [diff] [blame] | 12485 | if (PyUnicode_READY(self) == -1) |
| 12486 | return NULL; |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 12487 | return case_operation(self, do_swapcase); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12488 | } |
| 12489 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12490 | PyDoc_STRVAR(maketrans__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12491 | "str.maketrans(x[, y[, z]]) -> dict (static method)\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12492 | \n\ |
| 12493 | Return a translation table usable for str.translate().\n\ |
| 12494 | If there is only one argument, it must be a dictionary mapping Unicode\n\ |
| 12495 | ordinals (integers) or characters to Unicode ordinals, strings or None.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12496 | Character keys will be then converted to ordinals.\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12497 | If there are two arguments, they must be strings of equal length, and\n\ |
| 12498 | in the resulting dictionary, each character in x will be mapped to the\n\ |
| 12499 | character at the same position in y. If there is a third argument, it\n\ |
| 12500 | must be a string, whose characters will be mapped to None in the result."); |
| 12501 | |
| 12502 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12503 | unicode_maketrans(PyObject *null, PyObject *args) |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12504 | { |
| 12505 | PyObject *x, *y = NULL, *z = NULL; |
| 12506 | PyObject *new = NULL, *key, *value; |
| 12507 | Py_ssize_t i = 0; |
| 12508 | int res; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12509 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12510 | if (!PyArg_ParseTuple(args, "O|UU:maketrans", &x, &y, &z)) |
| 12511 | return NULL; |
| 12512 | new = PyDict_New(); |
| 12513 | if (!new) |
| 12514 | return NULL; |
| 12515 | if (y != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12516 | int x_kind, y_kind, z_kind; |
| 12517 | void *x_data, *y_data, *z_data; |
| 12518 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12519 | /* x must be a string too, of equal length */ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12520 | if (!PyUnicode_Check(x)) { |
| 12521 | PyErr_SetString(PyExc_TypeError, "first maketrans argument must " |
| 12522 | "be a string if there is a second argument"); |
| 12523 | goto err; |
| 12524 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12525 | if (PyUnicode_GET_LENGTH(x) != PyUnicode_GET_LENGTH(y)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12526 | PyErr_SetString(PyExc_ValueError, "the first two maketrans " |
| 12527 | "arguments must have equal length"); |
| 12528 | goto err; |
| 12529 | } |
| 12530 | /* 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] | 12531 | x_kind = PyUnicode_KIND(x); |
| 12532 | y_kind = PyUnicode_KIND(y); |
| 12533 | x_data = PyUnicode_DATA(x); |
| 12534 | y_data = PyUnicode_DATA(y); |
| 12535 | for (i = 0; i < PyUnicode_GET_LENGTH(x); i++) { |
| 12536 | key = PyLong_FromLong(PyUnicode_READ(x_kind, x_data, i)); |
Benjamin Peterson | 53aa1d7 | 2011-12-20 13:29:45 -0600 | [diff] [blame] | 12537 | if (!key) |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12538 | goto err; |
Benjamin Peterson | 822c790 | 2011-12-20 13:32:50 -0600 | [diff] [blame] | 12539 | value = PyLong_FromLong(PyUnicode_READ(y_kind, y_data, i)); |
Benjamin Peterson | 53aa1d7 | 2011-12-20 13:29:45 -0600 | [diff] [blame] | 12540 | if (!value) { |
| 12541 | Py_DECREF(key); |
| 12542 | goto err; |
| 12543 | } |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12544 | res = PyDict_SetItem(new, key, value); |
| 12545 | Py_DECREF(key); |
| 12546 | Py_DECREF(value); |
| 12547 | if (res < 0) |
| 12548 | goto err; |
| 12549 | } |
| 12550 | /* create entries for deleting chars in z */ |
| 12551 | if (z != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12552 | z_kind = PyUnicode_KIND(z); |
| 12553 | z_data = PyUnicode_DATA(z); |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 12554 | for (i = 0; i < PyUnicode_GET_LENGTH(z); i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12555 | key = PyLong_FromLong(PyUnicode_READ(z_kind, z_data, i)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12556 | if (!key) |
| 12557 | goto err; |
| 12558 | res = PyDict_SetItem(new, key, Py_None); |
| 12559 | Py_DECREF(key); |
| 12560 | if (res < 0) |
| 12561 | goto err; |
| 12562 | } |
| 12563 | } |
| 12564 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12565 | int kind; |
| 12566 | void *data; |
| 12567 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12568 | /* x must be a dict */ |
Raymond Hettinger | 3ad0576 | 2009-05-29 22:11:22 +0000 | [diff] [blame] | 12569 | if (!PyDict_CheckExact(x)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12570 | PyErr_SetString(PyExc_TypeError, "if you give only one argument " |
| 12571 | "to maketrans it must be a dict"); |
| 12572 | goto err; |
| 12573 | } |
| 12574 | /* copy entries into the new dict, converting string keys to int keys */ |
| 12575 | while (PyDict_Next(x, &i, &key, &value)) { |
| 12576 | if (PyUnicode_Check(key)) { |
| 12577 | /* convert string keys to integer keys */ |
| 12578 | PyObject *newkey; |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 12579 | if (PyUnicode_GET_LENGTH(key) != 1) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12580 | PyErr_SetString(PyExc_ValueError, "string keys in translate " |
| 12581 | "table must be of length 1"); |
| 12582 | goto err; |
| 12583 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12584 | kind = PyUnicode_KIND(key); |
| 12585 | data = PyUnicode_DATA(key); |
| 12586 | newkey = PyLong_FromLong(PyUnicode_READ(kind, data, 0)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12587 | if (!newkey) |
| 12588 | goto err; |
| 12589 | res = PyDict_SetItem(new, newkey, value); |
| 12590 | Py_DECREF(newkey); |
| 12591 | if (res < 0) |
| 12592 | goto err; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12593 | } else if (PyLong_Check(key)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12594 | /* just keep integer keys */ |
| 12595 | if (PyDict_SetItem(new, key, value) < 0) |
| 12596 | goto err; |
| 12597 | } else { |
| 12598 | PyErr_SetString(PyExc_TypeError, "keys in translate table must " |
| 12599 | "be strings or integers"); |
| 12600 | goto err; |
| 12601 | } |
| 12602 | } |
| 12603 | } |
| 12604 | return new; |
| 12605 | err: |
| 12606 | Py_DECREF(new); |
| 12607 | return NULL; |
| 12608 | } |
| 12609 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12610 | PyDoc_STRVAR(translate__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12611 | "S.translate(table) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12612 | \n\ |
| 12613 | Return a copy of the string S, where all characters have been mapped\n\ |
| 12614 | through the given translation table, which must be a mapping of\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12615 | Unicode ordinals to Unicode ordinals, strings, or None.\n\ |
Walter Dörwald | 5c1ee17 | 2002-09-04 20:31:32 +0000 | [diff] [blame] | 12616 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 12617 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12618 | |
| 12619 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12620 | unicode_translate(PyObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12621 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12622 | return _PyUnicode_TranslateCharmap(self, table, "ignore"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12623 | } |
| 12624 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12625 | PyDoc_STRVAR(upper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12626 | "S.upper() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12627 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12628 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12629 | |
| 12630 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12631 | unicode_upper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12632 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 12633 | if (PyUnicode_READY(self) == -1) |
| 12634 | return NULL; |
| 12635 | if (PyUnicode_IS_ASCII(self)) |
| 12636 | return ascii_upper_or_lower(self, 0); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 12637 | return case_operation(self, do_upper); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12638 | } |
| 12639 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12640 | PyDoc_STRVAR(zfill__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12641 | "S.zfill(width) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12642 | \n\ |
Benjamin Peterson | 9aa4299 | 2008-09-10 21:57:34 +0000 | [diff] [blame] | 12643 | Pad a numeric string S with zeros on the left, to fill a field\n\ |
| 12644 | of the specified width. The string S is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12645 | |
| 12646 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12647 | unicode_zfill(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12648 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12649 | Py_ssize_t fill; |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12650 | PyObject *u; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12651 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12652 | int kind; |
| 12653 | void *data; |
| 12654 | Py_UCS4 chr; |
| 12655 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12656 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12657 | return NULL; |
| 12658 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 12659 | if (PyUnicode_READY(self) == -1) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12660 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12661 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12662 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 12663 | return unicode_result_unchanged(self); |
| 12664 | |
| 12665 | fill = width - PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12666 | |
| 12667 | u = pad(self, fill, 0, '0'); |
| 12668 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 12669 | if (u == NULL) |
| 12670 | return NULL; |
| 12671 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12672 | kind = PyUnicode_KIND(u); |
| 12673 | data = PyUnicode_DATA(u); |
| 12674 | chr = PyUnicode_READ(kind, data, fill); |
| 12675 | |
| 12676 | if (chr == '+' || chr == '-') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12677 | /* move sign to beginning of string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12678 | PyUnicode_WRITE(kind, data, 0, chr); |
| 12679 | PyUnicode_WRITE(kind, data, fill, '0'); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12680 | } |
| 12681 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12682 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12683 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12684 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12685 | |
| 12686 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12687 | static PyObject * |
| 12688 | unicode__decimal2ascii(PyObject *self) |
| 12689 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12690 | return PyUnicode_TransformDecimalAndSpaceToASCII(self); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12691 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12692 | #endif |
| 12693 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12694 | PyDoc_STRVAR(startswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12695 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12696 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 12697 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 12698 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12699 | With optional end, stop comparing S at that position.\n\ |
| 12700 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12701 | |
| 12702 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12703 | unicode_startswith(PyObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12704 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12705 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12706 | PyObject *subobj; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12707 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12708 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12709 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12710 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12711 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12712 | if (!stringlib_parse_args_finds("startswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12713 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12714 | if (PyTuple_Check(subobj)) { |
| 12715 | Py_ssize_t i; |
| 12716 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12717 | substring = PyUnicode_FromObject(PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12718 | if (substring == NULL) |
| 12719 | return NULL; |
| 12720 | result = tailmatch(self, substring, start, end, -1); |
| 12721 | Py_DECREF(substring); |
| 12722 | if (result) { |
| 12723 | Py_RETURN_TRUE; |
| 12724 | } |
| 12725 | } |
| 12726 | /* nothing matched */ |
| 12727 | Py_RETURN_FALSE; |
| 12728 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12729 | substring = PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12730 | if (substring == NULL) { |
| 12731 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 12732 | PyErr_Format(PyExc_TypeError, "startswith first arg must be str or " |
| 12733 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12734 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12735 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12736 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12737 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12738 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12739 | } |
| 12740 | |
| 12741 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12742 | PyDoc_STRVAR(endswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12743 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12744 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 12745 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 12746 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12747 | With optional end, stop comparing S at that position.\n\ |
| 12748 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12749 | |
| 12750 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12751 | unicode_endswith(PyObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12752 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12753 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12754 | PyObject *subobj; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12755 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12756 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12757 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12758 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12759 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12760 | if (!stringlib_parse_args_finds("endswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12761 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12762 | if (PyTuple_Check(subobj)) { |
| 12763 | Py_ssize_t i; |
| 12764 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12765 | substring = PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12766 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12767 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12768 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12769 | result = tailmatch(self, substring, start, end, +1); |
| 12770 | Py_DECREF(substring); |
| 12771 | if (result) { |
| 12772 | Py_RETURN_TRUE; |
| 12773 | } |
| 12774 | } |
| 12775 | Py_RETURN_FALSE; |
| 12776 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12777 | substring = PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12778 | if (substring == NULL) { |
| 12779 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 12780 | PyErr_Format(PyExc_TypeError, "endswith first arg must be str or " |
| 12781 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12782 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12783 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12784 | result = tailmatch(self, substring, start, end, +1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12785 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12786 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12787 | } |
| 12788 | |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12789 | Py_LOCAL_INLINE(void) |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 12790 | _PyUnicodeWriter_Update(_PyUnicodeWriter *writer) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12791 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12792 | writer->size = PyUnicode_GET_LENGTH(writer->buffer); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12793 | writer->maxchar = PyUnicode_MAX_CHAR_VALUE(writer->buffer); |
| 12794 | writer->data = PyUnicode_DATA(writer->buffer); |
| 12795 | writer->kind = PyUnicode_KIND(writer->buffer); |
| 12796 | } |
| 12797 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12798 | void |
| 12799 | _PyUnicodeWriter_Init(_PyUnicodeWriter *writer, Py_ssize_t min_length) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12800 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12801 | memset(writer, 0, sizeof(*writer)); |
| 12802 | #ifdef Py_DEBUG |
| 12803 | writer->kind = 5; /* invalid kind */ |
| 12804 | #endif |
| 12805 | writer->min_length = Py_MAX(min_length, 100); |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 12806 | writer->overallocate = (min_length > 0); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12807 | } |
| 12808 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12809 | int |
| 12810 | _PyUnicodeWriter_PrepareInternal(_PyUnicodeWriter *writer, |
| 12811 | Py_ssize_t length, Py_UCS4 maxchar) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12812 | { |
| 12813 | Py_ssize_t newlen; |
| 12814 | PyObject *newbuffer; |
| 12815 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12816 | assert(length > 0); |
| 12817 | |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12818 | if (length > PY_SSIZE_T_MAX - writer->pos) { |
| 12819 | PyErr_NoMemory(); |
| 12820 | return -1; |
| 12821 | } |
| 12822 | newlen = writer->pos + length; |
| 12823 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12824 | if (writer->buffer == NULL) { |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 12825 | if (writer->overallocate) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12826 | /* overallocate 25% to limit the number of resize */ |
| 12827 | if (newlen <= (PY_SSIZE_T_MAX - newlen / 4)) |
| 12828 | newlen += newlen / 4; |
| 12829 | if (newlen < writer->min_length) |
| 12830 | newlen = writer->min_length; |
| 12831 | } |
| 12832 | writer->buffer = PyUnicode_New(newlen, maxchar); |
| 12833 | if (writer->buffer == NULL) |
| 12834 | return -1; |
| 12835 | _PyUnicodeWriter_Update(writer); |
| 12836 | return 0; |
| 12837 | } |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12838 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12839 | if (newlen > writer->size) { |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 12840 | if (writer->overallocate) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12841 | /* overallocate 25% to limit the number of resize */ |
| 12842 | if (newlen <= (PY_SSIZE_T_MAX - newlen / 4)) |
| 12843 | newlen += newlen / 4; |
| 12844 | if (newlen < writer->min_length) |
| 12845 | newlen = writer->min_length; |
| 12846 | } |
| 12847 | |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 12848 | if (maxchar > writer->maxchar || writer->readonly) { |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12849 | /* resize + widen */ |
| 12850 | newbuffer = PyUnicode_New(newlen, maxchar); |
| 12851 | if (newbuffer == NULL) |
| 12852 | return -1; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12853 | _PyUnicode_FastCopyCharacters(newbuffer, 0, |
| 12854 | writer->buffer, 0, writer->pos); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12855 | Py_DECREF(writer->buffer); |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 12856 | writer->readonly = 0; |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12857 | } |
| 12858 | else { |
| 12859 | newbuffer = resize_compact(writer->buffer, newlen); |
| 12860 | if (newbuffer == NULL) |
| 12861 | return -1; |
| 12862 | } |
| 12863 | writer->buffer = newbuffer; |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 12864 | _PyUnicodeWriter_Update(writer); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12865 | } |
| 12866 | else if (maxchar > writer->maxchar) { |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 12867 | assert(!writer->readonly); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12868 | newbuffer = PyUnicode_New(writer->size, maxchar); |
| 12869 | if (newbuffer == NULL) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12870 | return -1; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12871 | _PyUnicode_FastCopyCharacters(newbuffer, 0, |
| 12872 | writer->buffer, 0, writer->pos); |
| 12873 | Py_DECREF(writer->buffer); |
| 12874 | writer->buffer = newbuffer; |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 12875 | _PyUnicodeWriter_Update(writer); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12876 | } |
| 12877 | return 0; |
| 12878 | } |
| 12879 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12880 | int |
| 12881 | _PyUnicodeWriter_WriteStr(_PyUnicodeWriter *writer, PyObject *str) |
| 12882 | { |
| 12883 | Py_UCS4 maxchar; |
| 12884 | Py_ssize_t len; |
| 12885 | |
| 12886 | if (PyUnicode_READY(str) == -1) |
| 12887 | return -1; |
| 12888 | len = PyUnicode_GET_LENGTH(str); |
| 12889 | if (len == 0) |
| 12890 | return 0; |
| 12891 | maxchar = PyUnicode_MAX_CHAR_VALUE(str); |
| 12892 | if (maxchar > writer->maxchar || len > writer->size - writer->pos) { |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 12893 | if (writer->buffer == NULL && !writer->overallocate) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12894 | Py_INCREF(str); |
| 12895 | writer->buffer = str; |
| 12896 | _PyUnicodeWriter_Update(writer); |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 12897 | writer->readonly = 1; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12898 | writer->size = 0; |
| 12899 | writer->pos += len; |
| 12900 | return 0; |
| 12901 | } |
| 12902 | if (_PyUnicodeWriter_PrepareInternal(writer, len, maxchar) == -1) |
| 12903 | return -1; |
| 12904 | } |
| 12905 | _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos, |
| 12906 | str, 0, len); |
| 12907 | writer->pos += len; |
| 12908 | return 0; |
| 12909 | } |
| 12910 | |
| 12911 | PyObject * |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 12912 | _PyUnicodeWriter_Finish(_PyUnicodeWriter *writer) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12913 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12914 | if (writer->pos == 0) { |
| 12915 | Py_XDECREF(writer->buffer); |
| 12916 | Py_INCREF(unicode_empty); |
| 12917 | return unicode_empty; |
| 12918 | } |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 12919 | if (writer->readonly) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12920 | assert(PyUnicode_GET_LENGTH(writer->buffer) == writer->pos); |
| 12921 | return writer->buffer; |
| 12922 | } |
| 12923 | if (PyUnicode_GET_LENGTH(writer->buffer) != writer->pos) { |
| 12924 | PyObject *newbuffer; |
| 12925 | newbuffer = resize_compact(writer->buffer, writer->pos); |
| 12926 | if (newbuffer == NULL) { |
| 12927 | Py_DECREF(writer->buffer); |
| 12928 | return NULL; |
| 12929 | } |
| 12930 | writer->buffer = newbuffer; |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12931 | } |
Victor Stinner | f59c28c | 2012-05-09 03:24:14 +0200 | [diff] [blame] | 12932 | assert(_PyUnicode_CheckConsistency(writer->buffer, 1)); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12933 | return writer->buffer; |
| 12934 | } |
| 12935 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12936 | void |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 12937 | _PyUnicodeWriter_Dealloc(_PyUnicodeWriter *writer) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12938 | { |
| 12939 | Py_CLEAR(writer->buffer); |
| 12940 | } |
| 12941 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12942 | #include "stringlib/unicode_format.h" |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12943 | |
| 12944 | PyDoc_STRVAR(format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12945 | "S.format(*args, **kwargs) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12946 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 12947 | Return a formatted version of S, using substitutions from args and kwargs.\n\ |
| 12948 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12949 | |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 12950 | PyDoc_STRVAR(format_map__doc__, |
| 12951 | "S.format_map(mapping) -> str\n\ |
| 12952 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 12953 | Return a formatted version of S, using substitutions from mapping.\n\ |
| 12954 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 12955 | |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12956 | static PyObject * |
| 12957 | unicode__format__(PyObject* self, PyObject* args) |
| 12958 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12959 | PyObject *format_spec; |
| 12960 | _PyUnicodeWriter writer; |
| 12961 | int ret; |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12962 | |
| 12963 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
| 12964 | return NULL; |
| 12965 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12966 | if (PyUnicode_READY(self) == -1) |
| 12967 | return NULL; |
| 12968 | _PyUnicodeWriter_Init(&writer, 0); |
| 12969 | ret = _PyUnicode_FormatAdvancedWriter(&writer, |
| 12970 | self, format_spec, 0, |
| 12971 | PyUnicode_GET_LENGTH(format_spec)); |
| 12972 | if (ret == -1) { |
| 12973 | _PyUnicodeWriter_Dealloc(&writer); |
| 12974 | return NULL; |
| 12975 | } |
| 12976 | return _PyUnicodeWriter_Finish(&writer); |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12977 | } |
| 12978 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12979 | PyDoc_STRVAR(p_format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12980 | "S.__format__(format_spec) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12981 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 12982 | Return a formatted version of S as described by format_spec."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12983 | |
| 12984 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12985 | unicode__sizeof__(PyObject *v) |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12986 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12987 | Py_ssize_t size; |
| 12988 | |
| 12989 | /* If it's a compact object, account for base structure + |
| 12990 | character data. */ |
| 12991 | if (PyUnicode_IS_COMPACT_ASCII(v)) |
| 12992 | size = sizeof(PyASCIIObject) + PyUnicode_GET_LENGTH(v) + 1; |
| 12993 | else if (PyUnicode_IS_COMPACT(v)) |
| 12994 | size = sizeof(PyCompactUnicodeObject) + |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 12995 | (PyUnicode_GET_LENGTH(v) + 1) * PyUnicode_KIND(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12996 | else { |
| 12997 | /* If it is a two-block object, account for base object, and |
| 12998 | for character block if present. */ |
| 12999 | size = sizeof(PyUnicodeObject); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 13000 | if (_PyUnicode_DATA_ANY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13001 | size += (PyUnicode_GET_LENGTH(v) + 1) * |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 13002 | PyUnicode_KIND(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13003 | } |
| 13004 | /* 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] | 13005 | with the data pointer. Check if the data is not shared. */ |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 13006 | if (_PyUnicode_HAS_WSTR_MEMORY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13007 | size += (PyUnicode_WSTR_LENGTH(v) + 1) * sizeof(wchar_t); |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 13008 | if (_PyUnicode_HAS_UTF8_MEMORY(v)) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 13009 | size += PyUnicode_UTF8_LENGTH(v) + 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13010 | |
| 13011 | return PyLong_FromSsize_t(size); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13012 | } |
| 13013 | |
| 13014 | PyDoc_STRVAR(sizeof__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13015 | "S.__sizeof__() -> size of S in memory, in bytes"); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13016 | |
| 13017 | static PyObject * |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 13018 | unicode_getnewargs(PyObject *v) |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 13019 | { |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 13020 | PyObject *copy = _PyUnicode_Copy(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13021 | if (!copy) |
| 13022 | return NULL; |
| 13023 | return Py_BuildValue("(N)", copy); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 13024 | } |
| 13025 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13026 | static PyMethodDef unicode_methods[] = { |
Benjamin Peterson | 28a4dce | 2010-12-12 01:33:04 +0000 | [diff] [blame] | 13027 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS | METH_KEYWORDS, encode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13028 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 13029 | {"split", (PyCFunction) unicode_split, METH_VARARGS | METH_KEYWORDS, split__doc__}, |
| 13030 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS | METH_KEYWORDS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13031 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 13032 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 13033 | {"casefold", (PyCFunction) unicode_casefold, METH_NOARGS, casefold__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13034 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 13035 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 13036 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 13037 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 13038 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13039 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13040 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 13041 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 13042 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 13043 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13044 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 13045 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 13046 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 13047 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13048 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 13049 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS | METH_KEYWORDS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 13050 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13051 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 13052 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 13053 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 13054 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 13055 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 13056 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 13057 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 13058 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 13059 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 13060 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 13061 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 13062 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 13063 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 13064 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 13065 | {"isidentifier", (PyCFunction) unicode_isidentifier, METH_NOARGS, isidentifier__doc__}, |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 13066 | {"isprintable", (PyCFunction) unicode_isprintable, METH_NOARGS, isprintable__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13067 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Eric Smith | 9cd1e09 | 2007-08-31 18:39:38 +0000 | [diff] [blame] | 13068 | {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 13069 | {"format_map", (PyCFunction) do_string_format_map, METH_O, format_map__doc__}, |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13070 | {"__format__", (PyCFunction) unicode__format__, METH_VARARGS, p_format__doc__}, |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 13071 | {"maketrans", (PyCFunction) unicode_maketrans, |
| 13072 | METH_VARARGS | METH_STATIC, maketrans__doc__}, |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13073 | {"__sizeof__", (PyCFunction) unicode__sizeof__, METH_NOARGS, sizeof__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 13074 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 13075 | /* These methods are just used for debugging the implementation. */ |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 13076 | {"_decimal2ascii", (PyCFunction) unicode__decimal2ascii, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13077 | #endif |
| 13078 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13079 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13080 | {NULL, NULL} |
| 13081 | }; |
| 13082 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13083 | static PyObject * |
| 13084 | unicode_mod(PyObject *v, PyObject *w) |
| 13085 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 13086 | if (!PyUnicode_Check(v)) |
| 13087 | Py_RETURN_NOTIMPLEMENTED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13088 | return PyUnicode_Format(v, w); |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13089 | } |
| 13090 | |
| 13091 | static PyNumberMethods unicode_as_number = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13092 | 0, /*nb_add*/ |
| 13093 | 0, /*nb_subtract*/ |
| 13094 | 0, /*nb_multiply*/ |
| 13095 | unicode_mod, /*nb_remainder*/ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13096 | }; |
| 13097 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13098 | static PySequenceMethods unicode_as_sequence = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13099 | (lenfunc) unicode_length, /* sq_length */ |
| 13100 | PyUnicode_Concat, /* sq_concat */ |
| 13101 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 13102 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
| 13103 | 0, /* sq_slice */ |
| 13104 | 0, /* sq_ass_item */ |
| 13105 | 0, /* sq_ass_slice */ |
| 13106 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13107 | }; |
| 13108 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13109 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13110 | unicode_subscript(PyObject* self, PyObject* item) |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13111 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13112 | if (PyUnicode_READY(self) == -1) |
| 13113 | return NULL; |
| 13114 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 13115 | if (PyIndex_Check(item)) { |
| 13116 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13117 | if (i == -1 && PyErr_Occurred()) |
| 13118 | return NULL; |
| 13119 | if (i < 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13120 | i += PyUnicode_GET_LENGTH(self); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13121 | return unicode_getitem(self, i); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13122 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13123 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13124 | PyObject *result; |
| 13125 | void *src_data, *dest_data; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13126 | int src_kind, dest_kind; |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 13127 | Py_UCS4 ch, max_char, kind_limit; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13128 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13129 | if (PySlice_GetIndicesEx(item, PyUnicode_GET_LENGTH(self), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13130 | &start, &stop, &step, &slicelength) < 0) { |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13131 | return NULL; |
| 13132 | } |
| 13133 | |
| 13134 | if (slicelength <= 0) { |
Victor Stinner | 382955f | 2011-12-11 21:44:00 +0100 | [diff] [blame] | 13135 | Py_INCREF(unicode_empty); |
| 13136 | return unicode_empty; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13137 | } else if (start == 0 && step == 1 && |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 13138 | slicelength == PyUnicode_GET_LENGTH(self)) { |
| 13139 | return unicode_result_unchanged(self); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 13140 | } else if (step == 1) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13141 | return PyUnicode_Substring(self, |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 13142 | start, start + slicelength); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13143 | } |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13144 | /* General case */ |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13145 | src_kind = PyUnicode_KIND(self); |
| 13146 | src_data = PyUnicode_DATA(self); |
Victor Stinner | 55c9911 | 2011-10-13 01:17:06 +0200 | [diff] [blame] | 13147 | if (!PyUnicode_IS_ASCII(self)) { |
| 13148 | kind_limit = kind_maxchar_limit(src_kind); |
| 13149 | max_char = 0; |
| 13150 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 13151 | ch = PyUnicode_READ(src_kind, src_data, cur); |
| 13152 | if (ch > max_char) { |
| 13153 | max_char = ch; |
| 13154 | if (max_char >= kind_limit) |
| 13155 | break; |
| 13156 | } |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 13157 | } |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13158 | } |
Victor Stinner | 55c9911 | 2011-10-13 01:17:06 +0200 | [diff] [blame] | 13159 | else |
| 13160 | max_char = 127; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13161 | result = PyUnicode_New(slicelength, max_char); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13162 | if (result == NULL) |
| 13163 | return NULL; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13164 | dest_kind = PyUnicode_KIND(result); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13165 | dest_data = PyUnicode_DATA(result); |
| 13166 | |
| 13167 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13168 | Py_UCS4 ch = PyUnicode_READ(src_kind, src_data, cur); |
| 13169 | PyUnicode_WRITE(dest_kind, dest_data, i, ch); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13170 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13171 | assert(_PyUnicode_CheckConsistency(result, 1)); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13172 | return result; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13173 | } else { |
| 13174 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 13175 | return NULL; |
| 13176 | } |
| 13177 | } |
| 13178 | |
| 13179 | static PyMappingMethods unicode_as_mapping = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13180 | (lenfunc)unicode_length, /* mp_length */ |
| 13181 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 13182 | (objobjargproc)0, /* mp_ass_subscript */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13183 | }; |
| 13184 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13185 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13186 | /* Helpers for PyUnicode_Format() */ |
| 13187 | |
| 13188 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13189 | 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] | 13190 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13191 | Py_ssize_t argidx = *p_argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13192 | if (argidx < arglen) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13193 | (*p_argidx)++; |
| 13194 | if (arglen < 0) |
| 13195 | return args; |
| 13196 | else |
| 13197 | return PyTuple_GetItem(args, argidx); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13198 | } |
| 13199 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13200 | "not enough arguments for format string"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13201 | return NULL; |
| 13202 | } |
| 13203 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13204 | /* 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] | 13205 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13206 | static int |
| 13207 | formatfloat(PyObject *v, int flags, int prec, int type, |
| 13208 | PyObject **p_output, _PyUnicodeWriter *writer) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13209 | { |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13210 | char *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13211 | double x; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13212 | Py_ssize_t len; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 13213 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13214 | x = PyFloat_AsDouble(v); |
| 13215 | if (x == -1.0 && PyErr_Occurred()) |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13216 | return -1; |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13217 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13218 | if (prec < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13219 | prec = 6; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13220 | |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13221 | p = PyOS_double_to_string(x, type, prec, |
| 13222 | (flags & F_ALT) ? Py_DTSF_ALT : 0, NULL); |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13223 | if (p == NULL) |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13224 | return -1; |
| 13225 | len = strlen(p); |
| 13226 | if (writer) { |
Christian Heimes | f4f9939 | 2012-09-10 11:48:41 +0200 | [diff] [blame] | 13227 | if (_PyUnicodeWriter_Prepare(writer, len, 127) == -1) { |
| 13228 | PyMem_Free(p); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13229 | return -1; |
Christian Heimes | f4f9939 | 2012-09-10 11:48:41 +0200 | [diff] [blame] | 13230 | } |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 13231 | unicode_write_cstr(writer->buffer, writer->pos, p, len); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13232 | writer->pos += len; |
| 13233 | } |
| 13234 | else |
| 13235 | *p_output = _PyUnicode_FromASCII(p, len); |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13236 | PyMem_Free(p); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13237 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13238 | } |
| 13239 | |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13240 | /* formatlong() emulates the format codes d, u, o, x and X, and |
| 13241 | * the F_ALT flag, for Python's long (unbounded) ints. It's not used for |
| 13242 | * Python's regular ints. |
| 13243 | * Return value: a new PyUnicodeObject*, or NULL if error. |
| 13244 | * The output string is of the form |
| 13245 | * "-"? ("0x" | "0X")? digit+ |
| 13246 | * "0x"/"0X" are present only for x and X conversions, with F_ALT |
| 13247 | * set in flags. The case of hex digits will be correct, |
| 13248 | * There will be at least prec digits, zero-filled on the left if |
| 13249 | * necessary to get that many. |
| 13250 | * val object to be converted |
| 13251 | * flags bitmask of format flags; only F_ALT is looked at |
| 13252 | * prec minimum number of digits; 0-fill on left if needed |
| 13253 | * type a character in [duoxX]; u acts the same as d |
| 13254 | * |
| 13255 | * CAUTION: o, x and X conversions on regular ints can never |
| 13256 | * produce a '-' sign, but can for Python's unbounded ints. |
| 13257 | */ |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13258 | static PyObject* |
| 13259 | formatlong(PyObject *val, int flags, int prec, int type) |
| 13260 | { |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13261 | PyObject *result = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13262 | char *buf; |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13263 | Py_ssize_t i; |
| 13264 | int sign; /* 1 if '-', else 0 */ |
| 13265 | int len; /* number of characters */ |
| 13266 | Py_ssize_t llen; |
| 13267 | int numdigits; /* len == numnondigits + numdigits */ |
| 13268 | int numnondigits = 0; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13269 | |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13270 | /* Avoid exceeding SSIZE_T_MAX */ |
| 13271 | if (prec > INT_MAX-3) { |
| 13272 | PyErr_SetString(PyExc_OverflowError, |
| 13273 | "precision too large"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13274 | return NULL; |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13275 | } |
| 13276 | |
| 13277 | assert(PyLong_Check(val)); |
| 13278 | |
| 13279 | switch (type) { |
| 13280 | case 'd': |
| 13281 | case 'u': |
| 13282 | /* Special-case boolean: we want 0/1 */ |
Victor Stinner | b11d91d | 2012-04-28 00:25:34 +0200 | [diff] [blame] | 13283 | if (PyBool_Check(val)) |
| 13284 | result = PyNumber_ToBase(val, 10); |
| 13285 | else |
| 13286 | result = Py_TYPE(val)->tp_str(val); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13287 | break; |
| 13288 | case 'o': |
| 13289 | numnondigits = 2; |
| 13290 | result = PyNumber_ToBase(val, 8); |
| 13291 | break; |
| 13292 | case 'x': |
| 13293 | case 'X': |
| 13294 | numnondigits = 2; |
| 13295 | result = PyNumber_ToBase(val, 16); |
| 13296 | break; |
| 13297 | default: |
| 13298 | assert(!"'type' not in [duoxX]"); |
| 13299 | } |
| 13300 | if (!result) |
| 13301 | return NULL; |
| 13302 | |
| 13303 | assert(unicode_modifiable(result)); |
| 13304 | assert(PyUnicode_IS_READY(result)); |
| 13305 | assert(PyUnicode_IS_ASCII(result)); |
| 13306 | |
| 13307 | /* To modify the string in-place, there can only be one reference. */ |
| 13308 | if (Py_REFCNT(result) != 1) { |
| 13309 | PyErr_BadInternalCall(); |
| 13310 | return NULL; |
| 13311 | } |
| 13312 | buf = PyUnicode_DATA(result); |
| 13313 | llen = PyUnicode_GET_LENGTH(result); |
| 13314 | if (llen > INT_MAX) { |
| 13315 | PyErr_SetString(PyExc_ValueError, |
| 13316 | "string too large in _PyBytes_FormatLong"); |
| 13317 | return NULL; |
| 13318 | } |
| 13319 | len = (int)llen; |
| 13320 | sign = buf[0] == '-'; |
| 13321 | numnondigits += sign; |
| 13322 | numdigits = len - numnondigits; |
| 13323 | assert(numdigits > 0); |
| 13324 | |
| 13325 | /* Get rid of base marker unless F_ALT */ |
| 13326 | if (((flags & F_ALT) == 0 && |
| 13327 | (type == 'o' || type == 'x' || type == 'X'))) { |
| 13328 | assert(buf[sign] == '0'); |
| 13329 | assert(buf[sign+1] == 'x' || buf[sign+1] == 'X' || |
| 13330 | buf[sign+1] == 'o'); |
| 13331 | numnondigits -= 2; |
| 13332 | buf += 2; |
| 13333 | len -= 2; |
| 13334 | if (sign) |
| 13335 | buf[0] = '-'; |
| 13336 | assert(len == numnondigits + numdigits); |
| 13337 | assert(numdigits > 0); |
| 13338 | } |
| 13339 | |
| 13340 | /* Fill with leading zeroes to meet minimum width. */ |
| 13341 | if (prec > numdigits) { |
| 13342 | PyObject *r1 = PyBytes_FromStringAndSize(NULL, |
| 13343 | numnondigits + prec); |
| 13344 | char *b1; |
| 13345 | if (!r1) { |
| 13346 | Py_DECREF(result); |
| 13347 | return NULL; |
| 13348 | } |
| 13349 | b1 = PyBytes_AS_STRING(r1); |
| 13350 | for (i = 0; i < numnondigits; ++i) |
| 13351 | *b1++ = *buf++; |
| 13352 | for (i = 0; i < prec - numdigits; i++) |
| 13353 | *b1++ = '0'; |
| 13354 | for (i = 0; i < numdigits; i++) |
| 13355 | *b1++ = *buf++; |
| 13356 | *b1 = '\0'; |
| 13357 | Py_DECREF(result); |
| 13358 | result = r1; |
| 13359 | buf = PyBytes_AS_STRING(result); |
| 13360 | len = numnondigits + prec; |
| 13361 | } |
| 13362 | |
| 13363 | /* Fix up case for hex conversions. */ |
| 13364 | if (type == 'X') { |
| 13365 | /* Need to convert all lower case letters to upper case. |
| 13366 | and need to convert 0x to 0X (and -0x to -0X). */ |
| 13367 | for (i = 0; i < len; i++) |
| 13368 | if (buf[i] >= 'a' && buf[i] <= 'x') |
| 13369 | buf[i] -= 'a'-'A'; |
| 13370 | } |
| 13371 | if (!PyUnicode_Check(result) || len != PyUnicode_GET_LENGTH(result)) { |
| 13372 | PyObject *unicode; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13373 | unicode = _PyUnicode_FromASCII(buf, len); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13374 | Py_DECREF(result); |
| 13375 | result = unicode; |
| 13376 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13377 | return result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13378 | } |
| 13379 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13380 | static Py_UCS4 |
| 13381 | formatchar(PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13382 | { |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 13383 | /* presume that the buffer is at least 3 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 13384 | if (PyUnicode_Check(v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13385 | if (PyUnicode_GET_LENGTH(v) == 1) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13386 | return PyUnicode_READ_CHAR(v, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13387 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13388 | goto onError; |
| 13389 | } |
| 13390 | else { |
| 13391 | /* Integer input truncated to a character */ |
| 13392 | long x; |
| 13393 | x = PyLong_AsLong(v); |
| 13394 | if (x == -1 && PyErr_Occurred()) |
| 13395 | goto onError; |
| 13396 | |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 13397 | if (x < 0 || x > MAX_UNICODE) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13398 | PyErr_SetString(PyExc_OverflowError, |
| 13399 | "%c arg not in range(0x110000)"); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13400 | return (Py_UCS4) -1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13401 | } |
| 13402 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13403 | return (Py_UCS4) x; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13404 | } |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 13405 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13406 | onError: |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 13407 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13408 | "%c requires int or char"); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13409 | return (Py_UCS4) -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13410 | } |
| 13411 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 13412 | PyObject * |
| 13413 | PyUnicode_Format(PyObject *format, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13414 | { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13415 | Py_ssize_t fmtcnt, fmtpos, arglen, argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13416 | int args_owned = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13417 | PyObject *dict = NULL; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13418 | PyObject *temp = NULL; |
| 13419 | PyObject *second = NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13420 | PyObject *uformat; |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13421 | void *fmt; |
| 13422 | enum PyUnicode_Kind kind, fmtkind; |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 13423 | _PyUnicodeWriter writer; |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 13424 | Py_ssize_t sublen; |
| 13425 | Py_UCS4 maxchar; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 13426 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13427 | if (format == NULL || args == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13428 | PyErr_BadInternalCall(); |
| 13429 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13430 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13431 | uformat = PyUnicode_FromObject(format); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 13432 | if (uformat == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13433 | return NULL; |
Victor Stinner | 1929407 | 2012-10-05 00:09:33 +0200 | [diff] [blame] | 13434 | if (PyUnicode_READY(uformat) == -1) { |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 13435 | Py_DECREF(uformat); |
Victor Stinner | 1929407 | 2012-10-05 00:09:33 +0200 | [diff] [blame] | 13436 | return NULL; |
| 13437 | } |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13438 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13439 | fmt = PyUnicode_DATA(uformat); |
| 13440 | fmtkind = PyUnicode_KIND(uformat); |
| 13441 | fmtcnt = PyUnicode_GET_LENGTH(uformat); |
| 13442 | fmtpos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13443 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13444 | _PyUnicodeWriter_Init(&writer, fmtcnt + 100); |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13445 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13446 | if (PyTuple_Check(args)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13447 | arglen = PyTuple_Size(args); |
| 13448 | argidx = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13449 | } |
| 13450 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13451 | arglen = -1; |
| 13452 | argidx = -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13453 | } |
Benjamin Peterson | 28a6cfa | 2012-08-28 17:55:35 -0400 | [diff] [blame] | 13454 | if (PyMapping_Check(args) && !PyTuple_Check(args) && !PyUnicode_Check(args)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13455 | dict = args; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13456 | |
| 13457 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13458 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) != '%') { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13459 | Py_ssize_t nonfmtpos; |
| 13460 | nonfmtpos = fmtpos++; |
| 13461 | while (fmtcnt >= 0 && |
| 13462 | PyUnicode_READ(fmtkind, fmt, fmtpos) != '%') { |
| 13463 | fmtpos++; |
| 13464 | fmtcnt--; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13465 | } |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13466 | if (fmtcnt < 0) |
| 13467 | fmtpos--; |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 13468 | sublen = fmtpos - nonfmtpos; |
| 13469 | maxchar = _PyUnicode_FindMaxChar(uformat, |
| 13470 | nonfmtpos, nonfmtpos + sublen); |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 13471 | if (_PyUnicodeWriter_Prepare(&writer, sublen, maxchar) == -1) |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13472 | goto onError; |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 13473 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13474 | _PyUnicode_FastCopyCharacters(writer.buffer, writer.pos, |
| 13475 | uformat, nonfmtpos, sublen); |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 13476 | writer.pos += sublen; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13477 | } |
| 13478 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13479 | /* Got a format specifier */ |
| 13480 | int flags = 0; |
| 13481 | Py_ssize_t width = -1; |
| 13482 | int prec = -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13483 | Py_UCS4 c = '\0'; |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13484 | Py_UCS4 fill; |
| 13485 | int sign; |
| 13486 | Py_UCS4 signchar; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13487 | int isnumok; |
| 13488 | PyObject *v = NULL; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13489 | void *pbuf = NULL; |
| 13490 | Py_ssize_t pindex, len; |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 13491 | Py_UCS4 bufmaxchar; |
| 13492 | Py_ssize_t buflen; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13493 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13494 | fmtpos++; |
Victor Stinner | 438106b | 2012-05-02 00:41:57 +0200 | [diff] [blame] | 13495 | c = PyUnicode_READ(fmtkind, fmt, fmtpos); |
| 13496 | if (c == '(') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13497 | Py_ssize_t keystart; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13498 | Py_ssize_t keylen; |
| 13499 | PyObject *key; |
| 13500 | int pcount = 1; |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 13501 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13502 | if (dict == NULL) { |
| 13503 | PyErr_SetString(PyExc_TypeError, |
| 13504 | "format requires a mapping"); |
| 13505 | goto onError; |
| 13506 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13507 | ++fmtpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13508 | --fmtcnt; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13509 | keystart = fmtpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13510 | /* Skip over balanced parentheses */ |
| 13511 | while (pcount > 0 && --fmtcnt >= 0) { |
Victor Stinner | bff7c96 | 2012-05-03 01:44:59 +0200 | [diff] [blame] | 13512 | c = PyUnicode_READ(fmtkind, fmt, fmtpos); |
| 13513 | if (c == ')') |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13514 | --pcount; |
Victor Stinner | bff7c96 | 2012-05-03 01:44:59 +0200 | [diff] [blame] | 13515 | else if (c == '(') |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13516 | ++pcount; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13517 | fmtpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13518 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13519 | keylen = fmtpos - keystart - 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13520 | if (fmtcnt < 0 || pcount > 0) { |
| 13521 | PyErr_SetString(PyExc_ValueError, |
| 13522 | "incomplete format key"); |
| 13523 | goto onError; |
| 13524 | } |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 13525 | key = PyUnicode_Substring(uformat, |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 13526 | keystart, keystart + keylen); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13527 | if (key == NULL) |
| 13528 | goto onError; |
| 13529 | if (args_owned) { |
| 13530 | Py_DECREF(args); |
| 13531 | args_owned = 0; |
| 13532 | } |
| 13533 | args = PyObject_GetItem(dict, key); |
| 13534 | Py_DECREF(key); |
| 13535 | if (args == NULL) { |
| 13536 | goto onError; |
| 13537 | } |
| 13538 | args_owned = 1; |
| 13539 | arglen = -1; |
| 13540 | argidx = -2; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13541 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13542 | while (--fmtcnt >= 0) { |
Victor Stinner | 438106b | 2012-05-02 00:41:57 +0200 | [diff] [blame] | 13543 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
| 13544 | switch (c) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13545 | case '-': flags |= F_LJUST; continue; |
| 13546 | case '+': flags |= F_SIGN; continue; |
| 13547 | case ' ': flags |= F_BLANK; continue; |
| 13548 | case '#': flags |= F_ALT; continue; |
| 13549 | case '0': flags |= F_ZERO; continue; |
| 13550 | } |
| 13551 | break; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13552 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13553 | if (c == '*') { |
| 13554 | v = getnextarg(args, arglen, &argidx); |
| 13555 | if (v == NULL) |
| 13556 | goto onError; |
| 13557 | if (!PyLong_Check(v)) { |
| 13558 | PyErr_SetString(PyExc_TypeError, |
| 13559 | "* wants int"); |
| 13560 | goto onError; |
| 13561 | } |
| 13562 | width = PyLong_AsLong(v); |
| 13563 | if (width == -1 && PyErr_Occurred()) |
| 13564 | goto onError; |
| 13565 | if (width < 0) { |
| 13566 | flags |= F_LJUST; |
| 13567 | width = -width; |
| 13568 | } |
| 13569 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13570 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13571 | } |
| 13572 | else if (c >= '0' && c <= '9') { |
| 13573 | width = c - '0'; |
| 13574 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13575 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13576 | if (c < '0' || c > '9') |
| 13577 | break; |
Martin v. Löwis | b05c073 | 2012-05-15 13:45:49 +0200 | [diff] [blame] | 13578 | /* Since c is unsigned, the RHS would end up as unsigned, |
| 13579 | mixing signed and unsigned comparison. Since c is between |
| 13580 | '0' and '9', casting to int is safe. */ |
| 13581 | if (width > (PY_SSIZE_T_MAX - ((int)c - '0')) / 10) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13582 | PyErr_SetString(PyExc_ValueError, |
| 13583 | "width too big"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13584 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13585 | } |
| 13586 | width = width*10 + (c - '0'); |
| 13587 | } |
| 13588 | } |
| 13589 | if (c == '.') { |
| 13590 | prec = 0; |
| 13591 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13592 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13593 | if (c == '*') { |
| 13594 | v = getnextarg(args, arglen, &argidx); |
| 13595 | if (v == NULL) |
| 13596 | goto onError; |
| 13597 | if (!PyLong_Check(v)) { |
| 13598 | PyErr_SetString(PyExc_TypeError, |
| 13599 | "* wants int"); |
| 13600 | goto onError; |
| 13601 | } |
| 13602 | prec = PyLong_AsLong(v); |
| 13603 | if (prec == -1 && PyErr_Occurred()) |
| 13604 | goto onError; |
| 13605 | if (prec < 0) |
| 13606 | prec = 0; |
| 13607 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13608 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13609 | } |
| 13610 | else if (c >= '0' && c <= '9') { |
| 13611 | prec = c - '0'; |
| 13612 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13613 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13614 | if (c < '0' || c > '9') |
| 13615 | break; |
Martin v. Löwis | b05c073 | 2012-05-15 13:45:49 +0200 | [diff] [blame] | 13616 | if (prec > (INT_MAX - ((int)c - '0')) / 10) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13617 | PyErr_SetString(PyExc_ValueError, |
| 13618 | "prec too big"); |
| 13619 | goto onError; |
| 13620 | } |
| 13621 | prec = prec*10 + (c - '0'); |
| 13622 | } |
| 13623 | } |
| 13624 | } /* prec */ |
| 13625 | if (fmtcnt >= 0) { |
| 13626 | if (c == 'h' || c == 'l' || c == 'L') { |
| 13627 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13628 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13629 | } |
| 13630 | } |
| 13631 | if (fmtcnt < 0) { |
| 13632 | PyErr_SetString(PyExc_ValueError, |
| 13633 | "incomplete format"); |
| 13634 | goto onError; |
| 13635 | } |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13636 | if (fmtcnt == 0) |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 13637 | writer.overallocate = 0; |
Victor Stinner | aff3cc6 | 2012-04-30 05:19:21 +0200 | [diff] [blame] | 13638 | |
| 13639 | if (c == '%') { |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 13640 | if (_PyUnicodeWriter_Prepare(&writer, 1, '%') == -1) |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13641 | goto onError; |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 13642 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, '%'); |
| 13643 | writer.pos += 1; |
Victor Stinner | aff3cc6 | 2012-04-30 05:19:21 +0200 | [diff] [blame] | 13644 | continue; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13645 | } |
Victor Stinner | aff3cc6 | 2012-04-30 05:19:21 +0200 | [diff] [blame] | 13646 | |
Victor Stinner | aff3cc6 | 2012-04-30 05:19:21 +0200 | [diff] [blame] | 13647 | v = getnextarg(args, arglen, &argidx); |
| 13648 | if (v == NULL) |
| 13649 | goto onError; |
| 13650 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13651 | sign = 0; |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13652 | signchar = '\0'; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13653 | fill = ' '; |
| 13654 | switch (c) { |
| 13655 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13656 | case 's': |
| 13657 | case 'r': |
| 13658 | case 'a': |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13659 | if (PyLong_CheckExact(v) && width == -1 && prec == -1) { |
| 13660 | /* Fast path */ |
| 13661 | if (_PyLong_FormatWriter(&writer, v, 10, flags & F_ALT) == -1) |
| 13662 | goto onError; |
| 13663 | goto nextarg; |
| 13664 | } |
| 13665 | |
Victor Stinner | 808fc0a | 2010-03-22 12:50:40 +0000 | [diff] [blame] | 13666 | if (PyUnicode_CheckExact(v) && c == 's') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13667 | temp = v; |
| 13668 | Py_INCREF(temp); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13669 | } |
| 13670 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13671 | if (c == 's') |
| 13672 | temp = PyObject_Str(v); |
| 13673 | else if (c == 'r') |
| 13674 | temp = PyObject_Repr(v); |
| 13675 | else |
| 13676 | temp = PyObject_ASCII(v); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13677 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13678 | break; |
| 13679 | |
| 13680 | case 'i': |
| 13681 | case 'd': |
| 13682 | case 'u': |
| 13683 | case 'o': |
| 13684 | case 'x': |
| 13685 | case 'X': |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13686 | if (PyLong_CheckExact(v) |
| 13687 | && width == -1 && prec == -1 |
| 13688 | && !(flags & (F_SIGN | F_BLANK))) |
| 13689 | { |
| 13690 | /* Fast path */ |
| 13691 | switch(c) |
| 13692 | { |
| 13693 | case 'd': |
| 13694 | case 'i': |
| 13695 | case 'u': |
| 13696 | if (_PyLong_FormatWriter(&writer, v, 10, flags & F_ALT) == -1) |
| 13697 | goto onError; |
| 13698 | goto nextarg; |
| 13699 | case 'x': |
| 13700 | if (_PyLong_FormatWriter(&writer, v, 16, flags & F_ALT) == -1) |
| 13701 | goto onError; |
| 13702 | goto nextarg; |
| 13703 | case 'o': |
| 13704 | if (_PyLong_FormatWriter(&writer, v, 8, flags & F_ALT) == -1) |
| 13705 | goto onError; |
| 13706 | goto nextarg; |
| 13707 | default: |
| 13708 | break; |
| 13709 | } |
| 13710 | } |
| 13711 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13712 | isnumok = 0; |
| 13713 | if (PyNumber_Check(v)) { |
| 13714 | PyObject *iobj=NULL; |
| 13715 | |
| 13716 | if (PyLong_Check(v)) { |
| 13717 | iobj = v; |
| 13718 | Py_INCREF(iobj); |
| 13719 | } |
| 13720 | else { |
| 13721 | iobj = PyNumber_Long(v); |
| 13722 | } |
| 13723 | if (iobj!=NULL) { |
| 13724 | if (PyLong_Check(iobj)) { |
| 13725 | isnumok = 1; |
Victor Stinner | aff3cc6 | 2012-04-30 05:19:21 +0200 | [diff] [blame] | 13726 | sign = 1; |
Senthil Kumaran | 9ebe08d | 2011-07-03 21:03:16 -0700 | [diff] [blame] | 13727 | temp = formatlong(iobj, flags, prec, (c == 'i'? 'd': c)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13728 | Py_DECREF(iobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13729 | } |
| 13730 | else { |
| 13731 | Py_DECREF(iobj); |
| 13732 | } |
| 13733 | } |
| 13734 | } |
| 13735 | if (!isnumok) { |
| 13736 | PyErr_Format(PyExc_TypeError, |
| 13737 | "%%%c format: a number is required, " |
| 13738 | "not %.200s", (char)c, Py_TYPE(v)->tp_name); |
| 13739 | goto onError; |
| 13740 | } |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13741 | if (flags & F_ZERO) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13742 | fill = '0'; |
| 13743 | break; |
| 13744 | |
| 13745 | case 'e': |
| 13746 | case 'E': |
| 13747 | case 'f': |
| 13748 | case 'F': |
| 13749 | case 'g': |
| 13750 | case 'G': |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13751 | if (width == -1 && prec == -1 |
| 13752 | && !(flags & (F_SIGN | F_BLANK))) |
| 13753 | { |
| 13754 | /* Fast path */ |
| 13755 | if (formatfloat(v, flags, prec, c, NULL, &writer) == -1) |
| 13756 | goto onError; |
| 13757 | goto nextarg; |
| 13758 | } |
| 13759 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13760 | sign = 1; |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13761 | if (flags & F_ZERO) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13762 | fill = '0'; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13763 | if (formatfloat(v, flags, prec, c, &temp, NULL) == -1) |
| 13764 | temp = NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13765 | break; |
| 13766 | |
| 13767 | case 'c': |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13768 | { |
| 13769 | Py_UCS4 ch = formatchar(v); |
| 13770 | if (ch == (Py_UCS4) -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13771 | goto onError; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13772 | if (width == -1 && prec == -1) { |
| 13773 | /* Fast path */ |
| 13774 | if (_PyUnicodeWriter_Prepare(&writer, 1, ch) == -1) |
| 13775 | goto onError; |
| 13776 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, ch); |
| 13777 | writer.pos += 1; |
| 13778 | goto nextarg; |
| 13779 | } |
Victor Stinner | b5c3ea3 | 2012-05-02 00:29:36 +0200 | [diff] [blame] | 13780 | temp = PyUnicode_FromOrdinal(ch); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13781 | break; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13782 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13783 | |
| 13784 | default: |
| 13785 | PyErr_Format(PyExc_ValueError, |
| 13786 | "unsupported format character '%c' (0x%x) " |
| 13787 | "at index %zd", |
| 13788 | (31<=c && c<=126) ? (char)c : '?', |
| 13789 | (int)c, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13790 | fmtpos - 1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13791 | goto onError; |
| 13792 | } |
Victor Stinner | aff3cc6 | 2012-04-30 05:19:21 +0200 | [diff] [blame] | 13793 | if (temp == NULL) |
| 13794 | goto onError; |
| 13795 | assert (PyUnicode_Check(temp)); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13796 | |
| 13797 | if (width == -1 && prec == -1 |
| 13798 | && !(flags & (F_SIGN | F_BLANK))) |
| 13799 | { |
| 13800 | /* Fast path */ |
| 13801 | if (_PyUnicodeWriter_WriteStr(&writer, temp) == -1) |
| 13802 | goto onError; |
| 13803 | goto nextarg; |
| 13804 | } |
| 13805 | |
Victor Stinner | aff3cc6 | 2012-04-30 05:19:21 +0200 | [diff] [blame] | 13806 | if (PyUnicode_READY(temp) == -1) { |
| 13807 | Py_CLEAR(temp); |
| 13808 | goto onError; |
| 13809 | } |
| 13810 | kind = PyUnicode_KIND(temp); |
| 13811 | pbuf = PyUnicode_DATA(temp); |
| 13812 | len = PyUnicode_GET_LENGTH(temp); |
| 13813 | |
| 13814 | if (c == 's' || c == 'r' || c == 'a') { |
| 13815 | if (prec >= 0 && len > prec) |
| 13816 | len = prec; |
| 13817 | } |
| 13818 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13819 | /* pbuf is initialized here. */ |
| 13820 | pindex = 0; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13821 | if (sign) { |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13822 | Py_UCS4 ch = PyUnicode_READ(kind, pbuf, pindex); |
| 13823 | if (ch == '-' || ch == '+') { |
| 13824 | signchar = ch; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13825 | len--; |
| 13826 | pindex++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13827 | } |
| 13828 | else if (flags & F_SIGN) |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13829 | signchar = '+'; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13830 | else if (flags & F_BLANK) |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13831 | signchar = ' '; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13832 | else |
| 13833 | sign = 0; |
| 13834 | } |
| 13835 | if (width < len) |
| 13836 | width = len; |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 13837 | |
| 13838 | /* Compute the length and maximum character of the |
| 13839 | written characters */ |
| 13840 | bufmaxchar = 127; |
| 13841 | if (!(flags & F_LJUST)) { |
| 13842 | if (sign) { |
| 13843 | if ((width-1) > len) |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13844 | bufmaxchar = MAX_MAXCHAR(bufmaxchar, fill); |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 13845 | } |
| 13846 | else { |
| 13847 | if (width > len) |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13848 | bufmaxchar = MAX_MAXCHAR(bufmaxchar, fill); |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 13849 | } |
| 13850 | } |
| 13851 | maxchar = _PyUnicode_FindMaxChar(temp, 0, pindex+len); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13852 | bufmaxchar = MAX_MAXCHAR(bufmaxchar, maxchar); |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 13853 | |
| 13854 | buflen = width; |
| 13855 | if (sign && len == width) |
| 13856 | buflen++; |
| 13857 | |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 13858 | if (_PyUnicodeWriter_Prepare(&writer, buflen, bufmaxchar) == -1) |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 13859 | goto onError; |
| 13860 | |
| 13861 | /* Write characters */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13862 | if (sign) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13863 | if (fill != ' ') { |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 13864 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, signchar); |
| 13865 | writer.pos += 1; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13866 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13867 | if (width > len) |
| 13868 | width--; |
| 13869 | } |
| 13870 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13871 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13872 | assert(PyUnicode_READ(kind, pbuf, pindex + 1) == c); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13873 | if (fill != ' ') { |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13874 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, '0'); |
| 13875 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos+1, c); |
| 13876 | writer.pos += 2; |
| 13877 | pindex += 2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13878 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13879 | width -= 2; |
| 13880 | if (width < 0) |
| 13881 | width = 0; |
| 13882 | len -= 2; |
| 13883 | } |
| 13884 | if (width > len && !(flags & F_LJUST)) { |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13885 | sublen = width - len; |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13886 | FILL(writer.kind, writer.data, fill, writer.pos, sublen); |
| 13887 | writer.pos += sublen; |
Antoine Pitrou | 978b9d2 | 2011-10-07 12:35:48 +0200 | [diff] [blame] | 13888 | width = len; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13889 | } |
| 13890 | if (fill == ' ') { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13891 | if (sign) { |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 13892 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, signchar); |
| 13893 | writer.pos += 1; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13894 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13895 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13896 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
| 13897 | assert(PyUnicode_READ(kind, pbuf, pindex+1) == c); |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13898 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, '0'); |
| 13899 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos+1, c); |
| 13900 | writer.pos += 2; |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13901 | pindex += 2; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13902 | } |
| 13903 | } |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13904 | |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 13905 | if (len) { |
| 13906 | _PyUnicode_FastCopyCharacters(writer.buffer, writer.pos, |
| 13907 | temp, pindex, len); |
| 13908 | writer.pos += len; |
| 13909 | } |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13910 | if (width > len) { |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 13911 | sublen = width - len; |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13912 | FILL(writer.kind, writer.data, ' ', writer.pos, sublen); |
| 13913 | writer.pos += sublen; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13914 | } |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 13915 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13916 | nextarg: |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13917 | if (dict && (argidx < arglen) && c != '%') { |
| 13918 | PyErr_SetString(PyExc_TypeError, |
| 13919 | "not all arguments converted during string formatting"); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13920 | goto onError; |
| 13921 | } |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13922 | Py_CLEAR(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13923 | } /* '%' */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13924 | } /* until end */ |
| 13925 | if (argidx < arglen && !dict) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13926 | PyErr_SetString(PyExc_TypeError, |
| 13927 | "not all arguments converted during string formatting"); |
| 13928 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13929 | } |
| 13930 | |
| 13931 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13932 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13933 | } |
| 13934 | Py_DECREF(uformat); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13935 | Py_XDECREF(temp); |
| 13936 | Py_XDECREF(second); |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 13937 | return _PyUnicodeWriter_Finish(&writer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13938 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13939 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13940 | Py_DECREF(uformat); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13941 | Py_XDECREF(temp); |
| 13942 | Py_XDECREF(second); |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 13943 | _PyUnicodeWriter_Dealloc(&writer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13944 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13945 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13946 | } |
| 13947 | return NULL; |
| 13948 | } |
| 13949 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 13950 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 13951 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 13952 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13953 | static PyObject * |
| 13954 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 13955 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13956 | PyObject *x = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13957 | static char *kwlist[] = {"object", "encoding", "errors", 0}; |
| 13958 | char *encoding = NULL; |
| 13959 | char *errors = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13960 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13961 | if (type != &PyUnicode_Type) |
| 13962 | return unicode_subtype_new(type, args, kwds); |
| 13963 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:str", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13964 | kwlist, &x, &encoding, &errors)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13965 | return NULL; |
Victor Stinner | 382955f | 2011-12-11 21:44:00 +0100 | [diff] [blame] | 13966 | if (x == NULL) { |
| 13967 | Py_INCREF(unicode_empty); |
| 13968 | return unicode_empty; |
| 13969 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13970 | if (encoding == NULL && errors == NULL) |
| 13971 | return PyObject_Str(x); |
| 13972 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13973 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13974 | } |
| 13975 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 13976 | static PyObject * |
| 13977 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 13978 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13979 | PyObject *unicode, *self; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13980 | Py_ssize_t length, char_size; |
| 13981 | int share_wstr, share_utf8; |
| 13982 | unsigned int kind; |
| 13983 | void *data; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 13984 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13985 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13986 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13987 | unicode = unicode_new(&PyUnicode_Type, args, kwds); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13988 | if (unicode == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13989 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 13990 | assert(_PyUnicode_CHECK(unicode)); |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 13991 | if (PyUnicode_READY(unicode) == -1) { |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 13992 | Py_DECREF(unicode); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13993 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 13994 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13995 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13996 | self = type->tp_alloc(type, 0); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13997 | if (self == NULL) { |
| 13998 | Py_DECREF(unicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13999 | return NULL; |
| 14000 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14001 | kind = PyUnicode_KIND(unicode); |
| 14002 | length = PyUnicode_GET_LENGTH(unicode); |
| 14003 | |
| 14004 | _PyUnicode_LENGTH(self) = length; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 14005 | #ifdef Py_DEBUG |
| 14006 | _PyUnicode_HASH(self) = -1; |
| 14007 | #else |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14008 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 14009 | #endif |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14010 | _PyUnicode_STATE(self).interned = 0; |
| 14011 | _PyUnicode_STATE(self).kind = kind; |
| 14012 | _PyUnicode_STATE(self).compact = 0; |
Victor Stinner | 3cf4637 | 2011-10-03 14:42:15 +0200 | [diff] [blame] | 14013 | _PyUnicode_STATE(self).ascii = _PyUnicode_STATE(unicode).ascii; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14014 | _PyUnicode_STATE(self).ready = 1; |
| 14015 | _PyUnicode_WSTR(self) = NULL; |
| 14016 | _PyUnicode_UTF8_LENGTH(self) = 0; |
| 14017 | _PyUnicode_UTF8(self) = NULL; |
| 14018 | _PyUnicode_WSTR_LENGTH(self) = 0; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 14019 | _PyUnicode_DATA_ANY(self) = NULL; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14020 | |
| 14021 | share_utf8 = 0; |
| 14022 | share_wstr = 0; |
| 14023 | if (kind == PyUnicode_1BYTE_KIND) { |
| 14024 | char_size = 1; |
| 14025 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) |
| 14026 | share_utf8 = 1; |
| 14027 | } |
| 14028 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 14029 | char_size = 2; |
| 14030 | if (sizeof(wchar_t) == 2) |
| 14031 | share_wstr = 1; |
| 14032 | } |
| 14033 | else { |
| 14034 | assert(kind == PyUnicode_4BYTE_KIND); |
| 14035 | char_size = 4; |
| 14036 | if (sizeof(wchar_t) == 4) |
| 14037 | share_wstr = 1; |
| 14038 | } |
| 14039 | |
| 14040 | /* Ensure we won't overflow the length. */ |
| 14041 | if (length > (PY_SSIZE_T_MAX / char_size - 1)) { |
| 14042 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14043 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14044 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14045 | data = PyObject_MALLOC((length + 1) * char_size); |
| 14046 | if (data == NULL) { |
| 14047 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14048 | goto onError; |
| 14049 | } |
| 14050 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 14051 | _PyUnicode_DATA_ANY(self) = data; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14052 | if (share_utf8) { |
| 14053 | _PyUnicode_UTF8_LENGTH(self) = length; |
| 14054 | _PyUnicode_UTF8(self) = data; |
| 14055 | } |
| 14056 | if (share_wstr) { |
| 14057 | _PyUnicode_WSTR_LENGTH(self) = length; |
| 14058 | _PyUnicode_WSTR(self) = (wchar_t *)data; |
| 14059 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14060 | |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14061 | Py_MEMCPY(data, PyUnicode_DATA(unicode), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 14062 | kind * (length + 1)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 14063 | assert(_PyUnicode_CheckConsistency(self, 1)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 14064 | #ifdef Py_DEBUG |
| 14065 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
| 14066 | #endif |
Victor Stinner | dd18d3a | 2011-10-22 11:08:10 +0200 | [diff] [blame] | 14067 | Py_DECREF(unicode); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 14068 | return self; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14069 | |
| 14070 | onError: |
| 14071 | Py_DECREF(unicode); |
| 14072 | Py_DECREF(self); |
| 14073 | return NULL; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14074 | } |
| 14075 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 14076 | PyDoc_STRVAR(unicode_doc, |
Chris Jerdonek | 83fe2e1 | 2012-10-07 14:48:36 -0700 | [diff] [blame] | 14077 | "str(object='') -> str\n\ |
| 14078 | str(bytes_or_buffer[, encoding[, errors]]) -> str\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14079 | \n\ |
Nick Coghlan | 573b1fd | 2012-08-16 14:13:07 +1000 | [diff] [blame] | 14080 | Create a new string object from the given object. If encoding or\n\ |
| 14081 | errors is specified, then the object must expose a data buffer\n\ |
| 14082 | that will be decoded using the given encoding and error handler.\n\ |
| 14083 | Otherwise, returns the result of object.__str__() (if defined)\n\ |
| 14084 | or repr(object).\n\ |
| 14085 | encoding defaults to sys.getdefaultencoding().\n\ |
| 14086 | errors defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14087 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14088 | static PyObject *unicode_iter(PyObject *seq); |
| 14089 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14090 | PyTypeObject PyUnicode_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 14091 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14092 | "str", /* tp_name */ |
| 14093 | sizeof(PyUnicodeObject), /* tp_size */ |
| 14094 | 0, /* tp_itemsize */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14095 | /* Slots */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14096 | (destructor)unicode_dealloc, /* tp_dealloc */ |
| 14097 | 0, /* tp_print */ |
| 14098 | 0, /* tp_getattr */ |
| 14099 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 14100 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14101 | unicode_repr, /* tp_repr */ |
| 14102 | &unicode_as_number, /* tp_as_number */ |
| 14103 | &unicode_as_sequence, /* tp_as_sequence */ |
| 14104 | &unicode_as_mapping, /* tp_as_mapping */ |
| 14105 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 14106 | 0, /* tp_call*/ |
| 14107 | (reprfunc) unicode_str, /* tp_str */ |
| 14108 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 14109 | 0, /* tp_setattro */ |
| 14110 | 0, /* tp_as_buffer */ |
| 14111 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14112 | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14113 | unicode_doc, /* tp_doc */ |
| 14114 | 0, /* tp_traverse */ |
| 14115 | 0, /* tp_clear */ |
| 14116 | PyUnicode_RichCompare, /* tp_richcompare */ |
| 14117 | 0, /* tp_weaklistoffset */ |
| 14118 | unicode_iter, /* tp_iter */ |
| 14119 | 0, /* tp_iternext */ |
| 14120 | unicode_methods, /* tp_methods */ |
| 14121 | 0, /* tp_members */ |
| 14122 | 0, /* tp_getset */ |
| 14123 | &PyBaseObject_Type, /* tp_base */ |
| 14124 | 0, /* tp_dict */ |
| 14125 | 0, /* tp_descr_get */ |
| 14126 | 0, /* tp_descr_set */ |
| 14127 | 0, /* tp_dictoffset */ |
| 14128 | 0, /* tp_init */ |
| 14129 | 0, /* tp_alloc */ |
| 14130 | unicode_new, /* tp_new */ |
| 14131 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14132 | }; |
| 14133 | |
| 14134 | /* Initialize the Unicode implementation */ |
| 14135 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 14136 | int _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14137 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 14138 | int i; |
| 14139 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14140 | /* XXX - move this array to unicodectype.c ? */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14141 | Py_UCS2 linebreak[] = { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14142 | 0x000A, /* LINE FEED */ |
| 14143 | 0x000D, /* CARRIAGE RETURN */ |
| 14144 | 0x001C, /* FILE SEPARATOR */ |
| 14145 | 0x001D, /* GROUP SEPARATOR */ |
| 14146 | 0x001E, /* RECORD SEPARATOR */ |
| 14147 | 0x0085, /* NEXT LINE */ |
| 14148 | 0x2028, /* LINE SEPARATOR */ |
| 14149 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 14150 | }; |
| 14151 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 14152 | /* Init the implementation */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 14153 | unicode_empty = PyUnicode_New(0, 0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 14154 | if (!unicode_empty) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14155 | Py_FatalError("Can't create empty string"); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 14156 | assert(_PyUnicode_CheckConsistency(unicode_empty, 1)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 14157 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 14158 | for (i = 0; i < 256; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14159 | unicode_latin1[i] = NULL; |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 14160 | if (PyType_Ready(&PyUnicode_Type) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14161 | Py_FatalError("Can't initialize 'unicode'"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14162 | |
| 14163 | /* initialize the linebreak bloom filter */ |
| 14164 | bloom_linebreak = make_bloom_mask( |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14165 | PyUnicode_2BYTE_KIND, linebreak, |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 14166 | Py_ARRAY_LENGTH(linebreak)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 14167 | |
| 14168 | PyType_Ready(&EncodingMapType); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 14169 | |
| 14170 | #ifdef HAVE_MBCS |
| 14171 | winver.dwOSVersionInfoSize = sizeof(winver); |
| 14172 | if (!GetVersionEx((OSVERSIONINFO*)&winver)) { |
| 14173 | PyErr_SetFromWindowsErr(0); |
| 14174 | return -1; |
| 14175 | } |
| 14176 | #endif |
| 14177 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14178 | } |
| 14179 | |
| 14180 | /* Finalize the Unicode implementation */ |
| 14181 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 14182 | int |
| 14183 | PyUnicode_ClearFreeList(void) |
| 14184 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14185 | return 0; |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 14186 | } |
| 14187 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14188 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 14189 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14190 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 14191 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14192 | |
Guido van Rossum | 4ae8ef8 | 2000-10-03 18:09:04 +0000 | [diff] [blame] | 14193 | Py_XDECREF(unicode_empty); |
| 14194 | unicode_empty = NULL; |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 14195 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 14196 | for (i = 0; i < 256; i++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14197 | if (unicode_latin1[i]) { |
| 14198 | Py_DECREF(unicode_latin1[i]); |
| 14199 | unicode_latin1[i] = NULL; |
| 14200 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 14201 | } |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 14202 | _PyUnicode_ClearStaticStrings(); |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 14203 | (void)PyUnicode_ClearFreeList(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14204 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 14205 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14206 | void |
| 14207 | PyUnicode_InternInPlace(PyObject **p) |
| 14208 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14209 | register PyObject *s = *p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14210 | PyObject *t; |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 14211 | #ifdef Py_DEBUG |
| 14212 | assert(s != NULL); |
| 14213 | assert(_PyUnicode_CHECK(s)); |
| 14214 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14215 | if (s == NULL || !PyUnicode_Check(s)) |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 14216 | return; |
| 14217 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14218 | /* If it's a subclass, we don't really know what putting |
| 14219 | it in the interned dict might do. */ |
| 14220 | if (!PyUnicode_CheckExact(s)) |
| 14221 | return; |
| 14222 | if (PyUnicode_CHECK_INTERNED(s)) |
| 14223 | return; |
| 14224 | if (interned == NULL) { |
| 14225 | interned = PyDict_New(); |
| 14226 | if (interned == NULL) { |
| 14227 | PyErr_Clear(); /* Don't leave an exception */ |
| 14228 | return; |
| 14229 | } |
| 14230 | } |
| 14231 | /* It might be that the GetItem call fails even |
| 14232 | though the key is present in the dictionary, |
| 14233 | namely when this happens during a stack overflow. */ |
| 14234 | Py_ALLOW_RECURSION |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 14235 | t = PyDict_GetItem(interned, s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14236 | Py_END_ALLOW_RECURSION |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 14237 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14238 | if (t) { |
| 14239 | Py_INCREF(t); |
| 14240 | Py_DECREF(*p); |
| 14241 | *p = t; |
| 14242 | return; |
| 14243 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14244 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14245 | PyThreadState_GET()->recursion_critical = 1; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 14246 | if (PyDict_SetItem(interned, s, s) < 0) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14247 | PyErr_Clear(); |
| 14248 | PyThreadState_GET()->recursion_critical = 0; |
| 14249 | return; |
| 14250 | } |
| 14251 | PyThreadState_GET()->recursion_critical = 0; |
| 14252 | /* The two references in interned are not counted by refcnt. |
| 14253 | The deallocator will take care of this */ |
| 14254 | Py_REFCNT(s) -= 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14255 | _PyUnicode_STATE(s).interned = SSTATE_INTERNED_MORTAL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14256 | } |
| 14257 | |
| 14258 | void |
| 14259 | PyUnicode_InternImmortal(PyObject **p) |
| 14260 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14261 | PyUnicode_InternInPlace(p); |
| 14262 | if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { |
Victor Stinner | af9e4b8 | 2011-10-23 20:07:00 +0200 | [diff] [blame] | 14263 | _PyUnicode_STATE(*p).interned = SSTATE_INTERNED_IMMORTAL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14264 | Py_INCREF(*p); |
| 14265 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14266 | } |
| 14267 | |
| 14268 | PyObject * |
| 14269 | PyUnicode_InternFromString(const char *cp) |
| 14270 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14271 | PyObject *s = PyUnicode_FromString(cp); |
| 14272 | if (s == NULL) |
| 14273 | return NULL; |
| 14274 | PyUnicode_InternInPlace(&s); |
| 14275 | return s; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14276 | } |
| 14277 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 14278 | void |
| 14279 | _Py_ReleaseInternedUnicodeStrings(void) |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14280 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14281 | PyObject *keys; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14282 | PyObject *s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14283 | Py_ssize_t i, n; |
| 14284 | Py_ssize_t immortal_size = 0, mortal_size = 0; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14285 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14286 | if (interned == NULL || !PyDict_Check(interned)) |
| 14287 | return; |
| 14288 | keys = PyDict_Keys(interned); |
| 14289 | if (keys == NULL || !PyList_Check(keys)) { |
| 14290 | PyErr_Clear(); |
| 14291 | return; |
| 14292 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14293 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14294 | /* Since _Py_ReleaseInternedUnicodeStrings() is intended to help a leak |
| 14295 | detector, interned unicode strings are not forcibly deallocated; |
| 14296 | rather, we give them their stolen references back, and then clear |
| 14297 | and DECREF the interned dict. */ |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14298 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14299 | n = PyList_GET_SIZE(keys); |
| 14300 | fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14301 | n); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14302 | for (i = 0; i < n; i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14303 | s = PyList_GET_ITEM(keys, i); |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 14304 | if (PyUnicode_READY(s) == -1) { |
| 14305 | assert(0 && "could not ready string"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14306 | fprintf(stderr, "could not ready string\n"); |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 14307 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14308 | switch (PyUnicode_CHECK_INTERNED(s)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14309 | case SSTATE_NOT_INTERNED: |
| 14310 | /* XXX Shouldn't happen */ |
| 14311 | break; |
| 14312 | case SSTATE_INTERNED_IMMORTAL: |
| 14313 | Py_REFCNT(s) += 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14314 | immortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14315 | break; |
| 14316 | case SSTATE_INTERNED_MORTAL: |
| 14317 | Py_REFCNT(s) += 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14318 | mortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14319 | break; |
| 14320 | default: |
| 14321 | Py_FatalError("Inconsistent interned string state."); |
| 14322 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14323 | _PyUnicode_STATE(s).interned = SSTATE_NOT_INTERNED; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14324 | } |
| 14325 | fprintf(stderr, "total size of all interned strings: " |
| 14326 | "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " |
| 14327 | "mortal/immortal\n", mortal_size, immortal_size); |
| 14328 | Py_DECREF(keys); |
| 14329 | PyDict_Clear(interned); |
| 14330 | Py_DECREF(interned); |
| 14331 | interned = NULL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14332 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14333 | |
| 14334 | |
| 14335 | /********************* Unicode Iterator **************************/ |
| 14336 | |
| 14337 | typedef struct { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14338 | PyObject_HEAD |
| 14339 | Py_ssize_t it_index; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14340 | PyObject *it_seq; /* Set to NULL when iterator is exhausted */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14341 | } unicodeiterobject; |
| 14342 | |
| 14343 | static void |
| 14344 | unicodeiter_dealloc(unicodeiterobject *it) |
| 14345 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14346 | _PyObject_GC_UNTRACK(it); |
| 14347 | Py_XDECREF(it->it_seq); |
| 14348 | PyObject_GC_Del(it); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14349 | } |
| 14350 | |
| 14351 | static int |
| 14352 | unicodeiter_traverse(unicodeiterobject *it, visitproc visit, void *arg) |
| 14353 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14354 | Py_VISIT(it->it_seq); |
| 14355 | return 0; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14356 | } |
| 14357 | |
| 14358 | static PyObject * |
| 14359 | unicodeiter_next(unicodeiterobject *it) |
| 14360 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14361 | PyObject *seq, *item; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14362 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14363 | assert(it != NULL); |
| 14364 | seq = it->it_seq; |
| 14365 | if (seq == NULL) |
| 14366 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 14367 | assert(_PyUnicode_CHECK(seq)); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14368 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14369 | if (it->it_index < PyUnicode_GET_LENGTH(seq)) { |
| 14370 | int kind = PyUnicode_KIND(seq); |
| 14371 | void *data = PyUnicode_DATA(seq); |
| 14372 | Py_UCS4 chr = PyUnicode_READ(kind, data, it->it_index); |
| 14373 | item = PyUnicode_FromOrdinal(chr); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14374 | if (item != NULL) |
| 14375 | ++it->it_index; |
| 14376 | return item; |
| 14377 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14378 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14379 | Py_DECREF(seq); |
| 14380 | it->it_seq = NULL; |
| 14381 | return NULL; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14382 | } |
| 14383 | |
| 14384 | static PyObject * |
| 14385 | unicodeiter_len(unicodeiterobject *it) |
| 14386 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14387 | Py_ssize_t len = 0; |
| 14388 | if (it->it_seq) |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 14389 | len = PyUnicode_GET_LENGTH(it->it_seq) - it->it_index; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14390 | return PyLong_FromSsize_t(len); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14391 | } |
| 14392 | |
| 14393 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
| 14394 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 14395 | static PyObject * |
| 14396 | unicodeiter_reduce(unicodeiterobject *it) |
| 14397 | { |
| 14398 | if (it->it_seq != NULL) { |
Antoine Pitrou | a701388 | 2012-04-05 00:04:20 +0200 | [diff] [blame] | 14399 | return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("iter"), |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 14400 | it->it_seq, it->it_index); |
| 14401 | } else { |
| 14402 | PyObject *u = PyUnicode_FromUnicode(NULL, 0); |
| 14403 | if (u == NULL) |
| 14404 | return NULL; |
Antoine Pitrou | a701388 | 2012-04-05 00:04:20 +0200 | [diff] [blame] | 14405 | return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), u); |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 14406 | } |
| 14407 | } |
| 14408 | |
| 14409 | PyDoc_STRVAR(reduce_doc, "Return state information for pickling."); |
| 14410 | |
| 14411 | static PyObject * |
| 14412 | unicodeiter_setstate(unicodeiterobject *it, PyObject *state) |
| 14413 | { |
| 14414 | Py_ssize_t index = PyLong_AsSsize_t(state); |
| 14415 | if (index == -1 && PyErr_Occurred()) |
| 14416 | return NULL; |
| 14417 | if (index < 0) |
| 14418 | index = 0; |
| 14419 | it->it_index = index; |
| 14420 | Py_RETURN_NONE; |
| 14421 | } |
| 14422 | |
| 14423 | PyDoc_STRVAR(setstate_doc, "Set state information for unpickling."); |
| 14424 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14425 | static PyMethodDef unicodeiter_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14426 | {"__length_hint__", (PyCFunction)unicodeiter_len, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14427 | length_hint_doc}, |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 14428 | {"__reduce__", (PyCFunction)unicodeiter_reduce, METH_NOARGS, |
| 14429 | reduce_doc}, |
| 14430 | {"__setstate__", (PyCFunction)unicodeiter_setstate, METH_O, |
| 14431 | setstate_doc}, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14432 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14433 | }; |
| 14434 | |
| 14435 | PyTypeObject PyUnicodeIter_Type = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14436 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 14437 | "str_iterator", /* tp_name */ |
| 14438 | sizeof(unicodeiterobject), /* tp_basicsize */ |
| 14439 | 0, /* tp_itemsize */ |
| 14440 | /* methods */ |
| 14441 | (destructor)unicodeiter_dealloc, /* tp_dealloc */ |
| 14442 | 0, /* tp_print */ |
| 14443 | 0, /* tp_getattr */ |
| 14444 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 14445 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14446 | 0, /* tp_repr */ |
| 14447 | 0, /* tp_as_number */ |
| 14448 | 0, /* tp_as_sequence */ |
| 14449 | 0, /* tp_as_mapping */ |
| 14450 | 0, /* tp_hash */ |
| 14451 | 0, /* tp_call */ |
| 14452 | 0, /* tp_str */ |
| 14453 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 14454 | 0, /* tp_setattro */ |
| 14455 | 0, /* tp_as_buffer */ |
| 14456 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 14457 | 0, /* tp_doc */ |
| 14458 | (traverseproc)unicodeiter_traverse, /* tp_traverse */ |
| 14459 | 0, /* tp_clear */ |
| 14460 | 0, /* tp_richcompare */ |
| 14461 | 0, /* tp_weaklistoffset */ |
| 14462 | PyObject_SelfIter, /* tp_iter */ |
| 14463 | (iternextfunc)unicodeiter_next, /* tp_iternext */ |
| 14464 | unicodeiter_methods, /* tp_methods */ |
| 14465 | 0, |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14466 | }; |
| 14467 | |
| 14468 | static PyObject * |
| 14469 | unicode_iter(PyObject *seq) |
| 14470 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14471 | unicodeiterobject *it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14472 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14473 | if (!PyUnicode_Check(seq)) { |
| 14474 | PyErr_BadInternalCall(); |
| 14475 | return NULL; |
| 14476 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14477 | if (PyUnicode_READY(seq) == -1) |
| 14478 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14479 | it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type); |
| 14480 | if (it == NULL) |
| 14481 | return NULL; |
| 14482 | it->it_index = 0; |
| 14483 | Py_INCREF(seq); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14484 | it->it_seq = seq; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14485 | _PyObject_GC_TRACK(it); |
| 14486 | return (PyObject *)it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14487 | } |
| 14488 | |
Martin v. Löwis | 0d3072e | 2011-10-31 08:40:56 +0100 | [diff] [blame] | 14489 | |
| 14490 | size_t |
| 14491 | Py_UNICODE_strlen(const Py_UNICODE *u) |
| 14492 | { |
| 14493 | int res = 0; |
| 14494 | while(*u++) |
| 14495 | res++; |
| 14496 | return res; |
| 14497 | } |
| 14498 | |
| 14499 | Py_UNICODE* |
| 14500 | Py_UNICODE_strcpy(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 14501 | { |
| 14502 | Py_UNICODE *u = s1; |
| 14503 | while ((*u++ = *s2++)); |
| 14504 | return s1; |
| 14505 | } |
| 14506 | |
| 14507 | Py_UNICODE* |
| 14508 | Py_UNICODE_strncpy(Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 14509 | { |
| 14510 | Py_UNICODE *u = s1; |
| 14511 | while ((*u++ = *s2++)) |
| 14512 | if (n-- == 0) |
| 14513 | break; |
| 14514 | return s1; |
| 14515 | } |
| 14516 | |
| 14517 | Py_UNICODE* |
| 14518 | Py_UNICODE_strcat(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 14519 | { |
| 14520 | Py_UNICODE *u1 = s1; |
| 14521 | u1 += Py_UNICODE_strlen(u1); |
| 14522 | Py_UNICODE_strcpy(u1, s2); |
| 14523 | return s1; |
| 14524 | } |
| 14525 | |
| 14526 | int |
| 14527 | Py_UNICODE_strcmp(const Py_UNICODE *s1, const Py_UNICODE *s2) |
| 14528 | { |
| 14529 | while (*s1 && *s2 && *s1 == *s2) |
| 14530 | s1++, s2++; |
| 14531 | if (*s1 && *s2) |
| 14532 | return (*s1 < *s2) ? -1 : +1; |
| 14533 | if (*s1) |
| 14534 | return 1; |
| 14535 | if (*s2) |
| 14536 | return -1; |
| 14537 | return 0; |
| 14538 | } |
| 14539 | |
| 14540 | int |
| 14541 | Py_UNICODE_strncmp(const Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 14542 | { |
| 14543 | register Py_UNICODE u1, u2; |
| 14544 | for (; n != 0; n--) { |
| 14545 | u1 = *s1; |
| 14546 | u2 = *s2; |
| 14547 | if (u1 != u2) |
| 14548 | return (u1 < u2) ? -1 : +1; |
| 14549 | if (u1 == '\0') |
| 14550 | return 0; |
| 14551 | s1++; |
| 14552 | s2++; |
| 14553 | } |
| 14554 | return 0; |
| 14555 | } |
| 14556 | |
| 14557 | Py_UNICODE* |
| 14558 | Py_UNICODE_strchr(const Py_UNICODE *s, Py_UNICODE c) |
| 14559 | { |
| 14560 | const Py_UNICODE *p; |
| 14561 | for (p = s; *p; p++) |
| 14562 | if (*p == c) |
| 14563 | return (Py_UNICODE*)p; |
| 14564 | return NULL; |
| 14565 | } |
| 14566 | |
| 14567 | Py_UNICODE* |
| 14568 | Py_UNICODE_strrchr(const Py_UNICODE *s, Py_UNICODE c) |
| 14569 | { |
| 14570 | const Py_UNICODE *p; |
| 14571 | p = s + Py_UNICODE_strlen(s); |
| 14572 | while (p != s) { |
| 14573 | p--; |
| 14574 | if (*p == c) |
| 14575 | return (Py_UNICODE*)p; |
| 14576 | } |
| 14577 | return NULL; |
| 14578 | } |
Victor Stinner | 331ea92 | 2010-08-10 16:37:20 +0000 | [diff] [blame] | 14579 | |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14580 | Py_UNICODE* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14581 | PyUnicode_AsUnicodeCopy(PyObject *unicode) |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14582 | { |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 14583 | Py_UNICODE *u, *copy; |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14584 | Py_ssize_t len, size; |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14585 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14586 | if (!PyUnicode_Check(unicode)) { |
| 14587 | PyErr_BadArgument(); |
| 14588 | return NULL; |
| 14589 | } |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14590 | u = PyUnicode_AsUnicodeAndSize(unicode, &len); |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 14591 | if (u == NULL) |
| 14592 | return NULL; |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14593 | /* Ensure we won't overflow the size. */ |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14594 | if (len > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14595 | PyErr_NoMemory(); |
| 14596 | return NULL; |
| 14597 | } |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14598 | size = len + 1; /* copy the null character */ |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14599 | size *= sizeof(Py_UNICODE); |
| 14600 | copy = PyMem_Malloc(size); |
| 14601 | if (copy == NULL) { |
| 14602 | PyErr_NoMemory(); |
| 14603 | return NULL; |
| 14604 | } |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 14605 | memcpy(copy, u, size); |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14606 | return copy; |
| 14607 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 14608 | |
Georg Brandl | 66c221e | 2010-10-14 07:04:07 +0000 | [diff] [blame] | 14609 | /* A _string module, to export formatter_parser and formatter_field_name_split |
| 14610 | to the string.Formatter class implemented in Python. */ |
| 14611 | |
| 14612 | static PyMethodDef _string_methods[] = { |
| 14613 | {"formatter_field_name_split", (PyCFunction) formatter_field_name_split, |
| 14614 | METH_O, PyDoc_STR("split the argument as a field name")}, |
| 14615 | {"formatter_parser", (PyCFunction) formatter_parser, |
| 14616 | METH_O, PyDoc_STR("parse the argument as a format string")}, |
| 14617 | {NULL, NULL} |
| 14618 | }; |
| 14619 | |
| 14620 | static struct PyModuleDef _string_module = { |
| 14621 | PyModuleDef_HEAD_INIT, |
| 14622 | "_string", |
| 14623 | PyDoc_STR("string helper module"), |
| 14624 | 0, |
| 14625 | _string_methods, |
| 14626 | NULL, |
| 14627 | NULL, |
| 14628 | NULL, |
| 14629 | NULL |
| 14630 | }; |
| 14631 | |
| 14632 | PyMODINIT_FUNC |
| 14633 | PyInit__string(void) |
| 14634 | { |
| 14635 | return PyModule_Create(&_string_module); |
| 14636 | } |
| 14637 | |
| 14638 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 14639 | #ifdef __cplusplus |
| 14640 | } |
| 14641 | #endif |