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 = \ |
| 162 | _iter + (n & ~ (Py_ssize_t) 3); \ |
| 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 | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1151 | if (from_kind == to_kind) { |
| 1152 | if (!PyUnicode_IS_ASCII(from) && PyUnicode_IS_ASCII(to)) { |
| 1153 | /* Writing Latin-1 characters into an ASCII string requires to |
| 1154 | check that all written characters are pure ASCII */ |
| 1155 | #ifndef Py_DEBUG |
| 1156 | if (check_maxchar) { |
| 1157 | Py_UCS4 max_char; |
| 1158 | max_char = ucs1lib_find_max_char(from_data, |
| 1159 | (char*)from_data + how_many); |
| 1160 | if (max_char >= 128) |
| 1161 | return -1; |
| 1162 | } |
| 1163 | #else |
| 1164 | const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to); |
| 1165 | Py_UCS4 ch; |
| 1166 | Py_ssize_t i; |
| 1167 | for (i=0; i < how_many; i++) { |
| 1168 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
| 1169 | assert(ch <= to_maxchar); |
| 1170 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1171 | #endif |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1172 | } |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 1173 | Py_MEMCPY((char*)to_data + to_kind * to_start, |
| 1174 | (char*)from_data + from_kind * from_start, |
| 1175 | to_kind * how_many); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1176 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1177 | else if (from_kind == PyUnicode_1BYTE_KIND |
| 1178 | && to_kind == PyUnicode_2BYTE_KIND) |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1179 | { |
| 1180 | _PyUnicode_CONVERT_BYTES( |
| 1181 | Py_UCS1, Py_UCS2, |
| 1182 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 1183 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 1184 | PyUnicode_2BYTE_DATA(to) + to_start |
| 1185 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1186 | } |
Victor Stinner | 157f83f | 2011-09-28 21:41:31 +0200 | [diff] [blame] | 1187 | else if (from_kind == PyUnicode_1BYTE_KIND |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1188 | && to_kind == PyUnicode_4BYTE_KIND) |
| 1189 | { |
| 1190 | _PyUnicode_CONVERT_BYTES( |
| 1191 | Py_UCS1, Py_UCS4, |
| 1192 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 1193 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 1194 | PyUnicode_4BYTE_DATA(to) + to_start |
| 1195 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1196 | } |
| 1197 | else if (from_kind == PyUnicode_2BYTE_KIND |
| 1198 | && to_kind == PyUnicode_4BYTE_KIND) |
| 1199 | { |
| 1200 | _PyUnicode_CONVERT_BYTES( |
| 1201 | Py_UCS2, Py_UCS4, |
| 1202 | PyUnicode_2BYTE_DATA(from) + from_start, |
| 1203 | PyUnicode_2BYTE_DATA(from) + from_start + how_many, |
| 1204 | PyUnicode_4BYTE_DATA(to) + to_start |
| 1205 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1206 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1207 | else { |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1208 | assert (PyUnicode_MAX_CHAR_VALUE(from) > PyUnicode_MAX_CHAR_VALUE(to)); |
| 1209 | |
| 1210 | #ifndef Py_DEBUG |
| 1211 | if (!check_maxchar) { |
| 1212 | if (from_kind == PyUnicode_2BYTE_KIND |
| 1213 | && to_kind == PyUnicode_1BYTE_KIND) |
| 1214 | { |
| 1215 | _PyUnicode_CONVERT_BYTES( |
| 1216 | Py_UCS2, Py_UCS1, |
| 1217 | PyUnicode_2BYTE_DATA(from) + from_start, |
| 1218 | PyUnicode_2BYTE_DATA(from) + from_start + how_many, |
| 1219 | PyUnicode_1BYTE_DATA(to) + to_start |
| 1220 | ); |
| 1221 | } |
| 1222 | else if (from_kind == PyUnicode_4BYTE_KIND |
| 1223 | && to_kind == PyUnicode_1BYTE_KIND) |
| 1224 | { |
| 1225 | _PyUnicode_CONVERT_BYTES( |
| 1226 | Py_UCS4, Py_UCS1, |
| 1227 | PyUnicode_4BYTE_DATA(from) + from_start, |
| 1228 | PyUnicode_4BYTE_DATA(from) + from_start + how_many, |
| 1229 | PyUnicode_1BYTE_DATA(to) + to_start |
| 1230 | ); |
| 1231 | } |
| 1232 | else if (from_kind == PyUnicode_4BYTE_KIND |
| 1233 | && to_kind == PyUnicode_2BYTE_KIND) |
| 1234 | { |
| 1235 | _PyUnicode_CONVERT_BYTES( |
| 1236 | Py_UCS4, Py_UCS2, |
| 1237 | PyUnicode_4BYTE_DATA(from) + from_start, |
| 1238 | PyUnicode_4BYTE_DATA(from) + from_start + how_many, |
| 1239 | PyUnicode_2BYTE_DATA(to) + to_start |
| 1240 | ); |
| 1241 | } |
| 1242 | else { |
| 1243 | assert(0); |
| 1244 | return -1; |
| 1245 | } |
| 1246 | } |
| 1247 | else |
| 1248 | #endif |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1249 | { |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1250 | const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1251 | Py_UCS4 ch; |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1252 | Py_ssize_t i; |
| 1253 | |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1254 | for (i=0; i < how_many; i++) { |
| 1255 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1256 | #ifndef Py_DEBUG |
Victor Stinner | 56c161a | 2011-10-06 02:47:11 +0200 | [diff] [blame] | 1257 | assert(ch <= to_maxchar); |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1258 | #else |
| 1259 | if (ch > to_maxchar) |
| 1260 | return -1; |
| 1261 | #endif |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1262 | PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); |
| 1263 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1264 | } |
| 1265 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1266 | return 0; |
| 1267 | } |
| 1268 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1269 | void |
| 1270 | _PyUnicode_FastCopyCharacters( |
| 1271 | PyObject *to, Py_ssize_t to_start, |
| 1272 | PyObject *from, Py_ssize_t from_start, Py_ssize_t how_many) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1273 | { |
| 1274 | (void)_copy_characters(to, to_start, from, from_start, how_many, 0); |
| 1275 | } |
| 1276 | |
| 1277 | Py_ssize_t |
| 1278 | PyUnicode_CopyCharacters(PyObject *to, Py_ssize_t to_start, |
| 1279 | PyObject *from, Py_ssize_t from_start, |
| 1280 | Py_ssize_t how_many) |
| 1281 | { |
| 1282 | int err; |
| 1283 | |
| 1284 | if (!PyUnicode_Check(from) || !PyUnicode_Check(to)) { |
| 1285 | PyErr_BadInternalCall(); |
| 1286 | return -1; |
| 1287 | } |
| 1288 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 1289 | if (PyUnicode_READY(from) == -1) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1290 | return -1; |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 1291 | if (PyUnicode_READY(to) == -1) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1292 | return -1; |
| 1293 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1294 | if (from_start < 0) { |
| 1295 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 1296 | return -1; |
| 1297 | } |
| 1298 | if (to_start < 0) { |
| 1299 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 1300 | return -1; |
| 1301 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1302 | how_many = Py_MIN(PyUnicode_GET_LENGTH(from), how_many); |
| 1303 | if (to_start + how_many > PyUnicode_GET_LENGTH(to)) { |
| 1304 | PyErr_Format(PyExc_SystemError, |
| 1305 | "Cannot write %zi characters at %zi " |
| 1306 | "in a string of %zi characters", |
| 1307 | how_many, to_start, PyUnicode_GET_LENGTH(to)); |
| 1308 | return -1; |
| 1309 | } |
| 1310 | |
| 1311 | if (how_many == 0) |
| 1312 | return 0; |
| 1313 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1314 | if (unicode_check_modifiable(to)) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1315 | return -1; |
| 1316 | |
| 1317 | err = _copy_characters(to, to_start, from, from_start, how_many, 1); |
| 1318 | if (err) { |
| 1319 | PyErr_Format(PyExc_SystemError, |
| 1320 | "Cannot copy %s characters " |
| 1321 | "into a string of %s characters", |
| 1322 | unicode_kind_name(from), |
| 1323 | unicode_kind_name(to)); |
| 1324 | return -1; |
| 1325 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1326 | return how_many; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1327 | } |
| 1328 | |
Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 1329 | /* Find the maximum code point and count the number of surrogate pairs so a |
| 1330 | correct string length can be computed before converting a string to UCS4. |
| 1331 | This function counts single surrogates as a character and not as a pair. |
| 1332 | |
| 1333 | Return 0 on success, or -1 on error. */ |
| 1334 | static int |
| 1335 | find_maxchar_surrogates(const wchar_t *begin, const wchar_t *end, |
| 1336 | Py_UCS4 *maxchar, Py_ssize_t *num_surrogates) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1337 | { |
| 1338 | const wchar_t *iter; |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1339 | Py_UCS4 ch; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1340 | |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1341 | assert(num_surrogates != NULL && maxchar != NULL); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1342 | *num_surrogates = 0; |
| 1343 | *maxchar = 0; |
| 1344 | |
| 1345 | for (iter = begin; iter < end; ) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1346 | #if SIZEOF_WCHAR_T == 2 |
Victor Stinner | ca4f207 | 2011-11-22 03:38:40 +0100 | [diff] [blame] | 1347 | if (Py_UNICODE_IS_HIGH_SURROGATE(iter[0]) |
| 1348 | && (iter+1) < end |
| 1349 | && Py_UNICODE_IS_LOW_SURROGATE(iter[1])) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1350 | { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1351 | ch = Py_UNICODE_JOIN_SURROGATES(iter[0], iter[1]); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1352 | ++(*num_surrogates); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1353 | iter += 2; |
| 1354 | } |
| 1355 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1356 | #endif |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1357 | { |
| 1358 | ch = *iter; |
| 1359 | iter++; |
| 1360 | } |
| 1361 | if (ch > *maxchar) { |
| 1362 | *maxchar = ch; |
| 1363 | if (*maxchar > MAX_UNICODE) { |
| 1364 | PyErr_Format(PyExc_ValueError, |
| 1365 | "character U+%x is not in range [U+0000; U+10ffff]", |
| 1366 | ch); |
| 1367 | return -1; |
| 1368 | } |
| 1369 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1370 | } |
| 1371 | return 0; |
| 1372 | } |
| 1373 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1374 | int |
| 1375 | _PyUnicode_Ready(PyObject *unicode) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1376 | { |
| 1377 | wchar_t *end; |
| 1378 | Py_UCS4 maxchar = 0; |
| 1379 | Py_ssize_t num_surrogates; |
| 1380 | #if SIZEOF_WCHAR_T == 2 |
| 1381 | Py_ssize_t length_wo_surrogates; |
| 1382 | #endif |
| 1383 | |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 1384 | /* _PyUnicode_Ready() is only intended for old-style API usage where |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1385 | strings were created using _PyObject_New() and where no canonical |
| 1386 | representation (the str field) has been set yet aka strings |
| 1387 | which are not yet ready. */ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1388 | assert(_PyUnicode_CHECK(unicode)); |
| 1389 | assert(_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1390 | assert(_PyUnicode_WSTR(unicode) != NULL); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1391 | assert(_PyUnicode_DATA_ANY(unicode) == NULL); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1392 | assert(_PyUnicode_UTF8(unicode) == NULL); |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1393 | /* Actually, it should neither be interned nor be anything else: */ |
| 1394 | assert(_PyUnicode_STATE(unicode).interned == SSTATE_NOT_INTERNED); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1395 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1396 | end = _PyUnicode_WSTR(unicode) + _PyUnicode_WSTR_LENGTH(unicode); |
Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 1397 | if (find_maxchar_surrogates(_PyUnicode_WSTR(unicode), end, |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1398 | &maxchar, &num_surrogates) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1399 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1400 | |
| 1401 | if (maxchar < 256) { |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1402 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(_PyUnicode_WSTR_LENGTH(unicode) + 1); |
| 1403 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1404 | PyErr_NoMemory(); |
| 1405 | return -1; |
| 1406 | } |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1407 | _PyUnicode_CONVERT_BYTES(wchar_t, unsigned char, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1408 | _PyUnicode_WSTR(unicode), end, |
| 1409 | PyUnicode_1BYTE_DATA(unicode)); |
| 1410 | PyUnicode_1BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1411 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1412 | _PyUnicode_STATE(unicode).kind = PyUnicode_1BYTE_KIND; |
| 1413 | if (maxchar < 128) { |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1414 | _PyUnicode_STATE(unicode).ascii = 1; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1415 | _PyUnicode_UTF8(unicode) = _PyUnicode_DATA_ANY(unicode); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1416 | _PyUnicode_UTF8_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1417 | } |
| 1418 | else { |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1419 | _PyUnicode_STATE(unicode).ascii = 0; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1420 | _PyUnicode_UTF8(unicode) = NULL; |
| 1421 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1422 | } |
| 1423 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1424 | _PyUnicode_WSTR(unicode) = NULL; |
| 1425 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1426 | } |
| 1427 | /* In this case we might have to convert down from 4-byte native |
| 1428 | wchar_t to 2-byte unicode. */ |
| 1429 | else if (maxchar < 65536) { |
| 1430 | assert(num_surrogates == 0 && |
| 1431 | "FindMaxCharAndNumSurrogatePairs() messed up"); |
| 1432 | |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1433 | #if SIZEOF_WCHAR_T == 2 |
| 1434 | /* We can share representations and are done. */ |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1435 | _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1436 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1437 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1438 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1439 | _PyUnicode_UTF8(unicode) = NULL; |
| 1440 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1441 | #else |
| 1442 | /* sizeof(wchar_t) == 4 */ |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1443 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC( |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1444 | 2 * (_PyUnicode_WSTR_LENGTH(unicode) + 1)); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1445 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1446 | PyErr_NoMemory(); |
| 1447 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1448 | } |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1449 | _PyUnicode_CONVERT_BYTES(wchar_t, Py_UCS2, |
| 1450 | _PyUnicode_WSTR(unicode), end, |
| 1451 | PyUnicode_2BYTE_DATA(unicode)); |
| 1452 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1453 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1454 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1455 | _PyUnicode_UTF8(unicode) = NULL; |
| 1456 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1457 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1458 | _PyUnicode_WSTR(unicode) = NULL; |
| 1459 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1460 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1461 | } |
| 1462 | /* maxchar exeeds 16 bit, wee need 4 bytes for unicode characters */ |
| 1463 | else { |
| 1464 | #if SIZEOF_WCHAR_T == 2 |
| 1465 | /* in case the native representation is 2-bytes, we need to allocate a |
| 1466 | new normalized 4-byte version. */ |
| 1467 | length_wo_surrogates = _PyUnicode_WSTR_LENGTH(unicode) - num_surrogates; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1468 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(4 * (length_wo_surrogates + 1)); |
| 1469 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1470 | PyErr_NoMemory(); |
| 1471 | return -1; |
| 1472 | } |
| 1473 | _PyUnicode_LENGTH(unicode) = length_wo_surrogates; |
| 1474 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1475 | _PyUnicode_UTF8(unicode) = NULL; |
| 1476 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 126c559 | 2011-10-03 04:17:10 +0200 | [diff] [blame] | 1477 | /* unicode_convert_wchar_to_ucs4() requires a ready string */ |
| 1478 | _PyUnicode_STATE(unicode).ready = 1; |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1479 | unicode_convert_wchar_to_ucs4(_PyUnicode_WSTR(unicode), end, unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1480 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1481 | _PyUnicode_WSTR(unicode) = NULL; |
| 1482 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1483 | #else |
| 1484 | assert(num_surrogates == 0); |
| 1485 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1486 | _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1487 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1488 | _PyUnicode_UTF8(unicode) = NULL; |
| 1489 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1490 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; |
| 1491 | #endif |
| 1492 | PyUnicode_4BYTE_DATA(unicode)[_PyUnicode_LENGTH(unicode)] = '\0'; |
| 1493 | } |
| 1494 | _PyUnicode_STATE(unicode).ready = 1; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1495 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1496 | return 0; |
| 1497 | } |
| 1498 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1499 | static void |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1500 | unicode_dealloc(register PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1501 | { |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1502 | switch (PyUnicode_CHECK_INTERNED(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1503 | case SSTATE_NOT_INTERNED: |
| 1504 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1505 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1506 | case SSTATE_INTERNED_MORTAL: |
| 1507 | /* revive dead object temporarily for DelItem */ |
| 1508 | Py_REFCNT(unicode) = 3; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 1509 | if (PyDict_DelItem(interned, unicode) != 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1510 | Py_FatalError( |
| 1511 | "deletion of interned string failed"); |
| 1512 | break; |
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 | case SSTATE_INTERNED_IMMORTAL: |
| 1515 | Py_FatalError("Immortal interned string died."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1516 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1517 | default: |
| 1518 | Py_FatalError("Inconsistent interned string state."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1519 | } |
| 1520 | |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 1521 | if (_PyUnicode_HAS_WSTR_MEMORY(unicode)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1522 | PyObject_DEL(_PyUnicode_WSTR(unicode)); |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 1523 | if (_PyUnicode_HAS_UTF8_MEMORY(unicode)) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1524 | PyObject_DEL(_PyUnicode_UTF8(unicode)); |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1525 | if (!PyUnicode_IS_COMPACT(unicode) && _PyUnicode_DATA_ANY(unicode)) |
| 1526 | PyObject_DEL(_PyUnicode_DATA_ANY(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1527 | |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1528 | Py_TYPE(unicode)->tp_free(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1529 | } |
| 1530 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1531 | #ifdef Py_DEBUG |
| 1532 | static int |
| 1533 | unicode_is_singleton(PyObject *unicode) |
| 1534 | { |
| 1535 | PyASCIIObject *ascii = (PyASCIIObject *)unicode; |
| 1536 | if (unicode == unicode_empty) |
| 1537 | return 1; |
| 1538 | if (ascii->state.kind != PyUnicode_WCHAR_KIND && ascii->length == 1) |
| 1539 | { |
| 1540 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, 0); |
| 1541 | if (ch < 256 && unicode_latin1[ch] == unicode) |
| 1542 | return 1; |
| 1543 | } |
| 1544 | return 0; |
| 1545 | } |
| 1546 | #endif |
| 1547 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1548 | static int |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1549 | unicode_modifiable(PyObject *unicode) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1550 | { |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1551 | assert(_PyUnicode_CHECK(unicode)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1552 | if (Py_REFCNT(unicode) != 1) |
| 1553 | return 0; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1554 | if (_PyUnicode_HASH(unicode) != -1) |
| 1555 | return 0; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1556 | if (PyUnicode_CHECK_INTERNED(unicode)) |
| 1557 | return 0; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1558 | if (!PyUnicode_CheckExact(unicode)) |
| 1559 | return 0; |
Victor Stinner | 77bb47b | 2011-10-03 20:06:05 +0200 | [diff] [blame] | 1560 | #ifdef Py_DEBUG |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1561 | /* singleton refcount is greater than 1 */ |
| 1562 | assert(!unicode_is_singleton(unicode)); |
Victor Stinner | 77bb47b | 2011-10-03 20:06:05 +0200 | [diff] [blame] | 1563 | #endif |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1564 | return 1; |
| 1565 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1566 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1567 | static int |
| 1568 | unicode_resize(PyObject **p_unicode, Py_ssize_t length) |
| 1569 | { |
| 1570 | PyObject *unicode; |
| 1571 | Py_ssize_t old_length; |
| 1572 | |
| 1573 | assert(p_unicode != NULL); |
| 1574 | unicode = *p_unicode; |
| 1575 | |
| 1576 | assert(unicode != NULL); |
| 1577 | assert(PyUnicode_Check(unicode)); |
| 1578 | assert(0 <= length); |
| 1579 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1580 | if (_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1581 | old_length = PyUnicode_WSTR_LENGTH(unicode); |
| 1582 | else |
| 1583 | old_length = PyUnicode_GET_LENGTH(unicode); |
| 1584 | if (old_length == length) |
| 1585 | return 0; |
| 1586 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1587 | if (length == 0) { |
| 1588 | Py_DECREF(*p_unicode); |
| 1589 | *p_unicode = unicode_empty; |
| 1590 | Py_INCREF(*p_unicode); |
| 1591 | return 0; |
| 1592 | } |
| 1593 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1594 | if (!unicode_modifiable(unicode)) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1595 | PyObject *copy = resize_copy(unicode, length); |
| 1596 | if (copy == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1597 | return -1; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1598 | Py_DECREF(*p_unicode); |
| 1599 | *p_unicode = copy; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1600 | return 0; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1601 | } |
| 1602 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1603 | if (PyUnicode_IS_COMPACT(unicode)) { |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1604 | PyObject *new_unicode = resize_compact(unicode, length); |
| 1605 | if (new_unicode == NULL) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1606 | return -1; |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1607 | *p_unicode = new_unicode; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1608 | return 0; |
Benjamin Peterson | 4bfce8f | 2011-10-03 19:35:07 -0400 | [diff] [blame] | 1609 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1610 | return resize_inplace(unicode, length); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1611 | } |
| 1612 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1613 | int |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1614 | PyUnicode_Resize(PyObject **p_unicode, Py_ssize_t length) |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1615 | { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1616 | PyObject *unicode; |
| 1617 | if (p_unicode == NULL) { |
| 1618 | PyErr_BadInternalCall(); |
| 1619 | return -1; |
| 1620 | } |
| 1621 | unicode = *p_unicode; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1622 | if (unicode == NULL || !PyUnicode_Check(unicode) || length < 0) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1623 | { |
| 1624 | PyErr_BadInternalCall(); |
| 1625 | return -1; |
| 1626 | } |
| 1627 | return unicode_resize(p_unicode, length); |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1628 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1629 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1630 | static int |
Victor Stinner | 1b487b4 | 2012-05-03 12:29:04 +0200 | [diff] [blame] | 1631 | unicode_widen(PyObject **p_unicode, Py_ssize_t length, |
| 1632 | unsigned int maxchar) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1633 | { |
| 1634 | PyObject *result; |
| 1635 | assert(PyUnicode_IS_READY(*p_unicode)); |
Victor Stinner | 1b487b4 | 2012-05-03 12:29:04 +0200 | [diff] [blame] | 1636 | assert(length <= PyUnicode_GET_LENGTH(*p_unicode)); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1637 | if (maxchar <= PyUnicode_MAX_CHAR_VALUE(*p_unicode)) |
| 1638 | return 0; |
| 1639 | result = PyUnicode_New(PyUnicode_GET_LENGTH(*p_unicode), |
| 1640 | maxchar); |
| 1641 | if (result == NULL) |
| 1642 | return -1; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1643 | _PyUnicode_FastCopyCharacters(result, 0, *p_unicode, 0, length); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1644 | Py_DECREF(*p_unicode); |
| 1645 | *p_unicode = result; |
| 1646 | return 0; |
| 1647 | } |
| 1648 | |
| 1649 | static int |
| 1650 | unicode_putchar(PyObject **p_unicode, Py_ssize_t *pos, |
| 1651 | Py_UCS4 ch) |
| 1652 | { |
Victor Stinner | 15e9ed2 | 2012-02-22 13:36:20 +0100 | [diff] [blame] | 1653 | assert(ch <= MAX_UNICODE); |
Victor Stinner | 1b487b4 | 2012-05-03 12:29:04 +0200 | [diff] [blame] | 1654 | if (unicode_widen(p_unicode, *pos, ch) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1655 | return -1; |
| 1656 | PyUnicode_WRITE(PyUnicode_KIND(*p_unicode), |
| 1657 | PyUnicode_DATA(*p_unicode), |
| 1658 | (*pos)++, ch); |
| 1659 | return 0; |
| 1660 | } |
| 1661 | |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1662 | /* Copy a ASCII or latin1 char* string into a Python Unicode string. |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1663 | |
Victor Stinner | b429d3b | 2012-02-22 21:22:20 +0100 | [diff] [blame] | 1664 | WARNING: The function doesn't copy the terminating null character and |
| 1665 | doesn't check the maximum character (may write a latin1 character in an |
| 1666 | ASCII string). */ |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1667 | static void |
| 1668 | unicode_write_cstr(PyObject *unicode, Py_ssize_t index, |
| 1669 | const char *str, Py_ssize_t len) |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1670 | { |
| 1671 | enum PyUnicode_Kind kind = PyUnicode_KIND(unicode); |
| 1672 | void *data = PyUnicode_DATA(unicode); |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1673 | const char *end = str + len; |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1674 | |
| 1675 | switch (kind) { |
| 1676 | case PyUnicode_1BYTE_KIND: { |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1677 | assert(index + len <= PyUnicode_GET_LENGTH(unicode)); |
Antoine Pitrou | ba6bafc | 2012-02-22 16:41:50 +0100 | [diff] [blame] | 1678 | memcpy((char *) data + index, str, len); |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1679 | break; |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1680 | } |
| 1681 | case PyUnicode_2BYTE_KIND: { |
| 1682 | Py_UCS2 *start = (Py_UCS2 *)data + index; |
| 1683 | Py_UCS2 *ucs2 = start; |
| 1684 | assert(index <= PyUnicode_GET_LENGTH(unicode)); |
| 1685 | |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1686 | for (; str < end; ++ucs2, ++str) |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1687 | *ucs2 = (Py_UCS2)*str; |
| 1688 | |
| 1689 | assert((ucs2 - start) <= PyUnicode_GET_LENGTH(unicode)); |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1690 | break; |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1691 | } |
| 1692 | default: { |
| 1693 | Py_UCS4 *start = (Py_UCS4 *)data + index; |
| 1694 | Py_UCS4 *ucs4 = start; |
| 1695 | assert(kind == PyUnicode_4BYTE_KIND); |
| 1696 | assert(index <= PyUnicode_GET_LENGTH(unicode)); |
| 1697 | |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1698 | for (; str < end; ++ucs4, ++str) |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1699 | *ucs4 = (Py_UCS4)*str; |
| 1700 | |
| 1701 | assert((ucs4 - start) <= PyUnicode_GET_LENGTH(unicode)); |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1702 | } |
| 1703 | } |
| 1704 | } |
| 1705 | |
| 1706 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1707 | static PyObject* |
| 1708 | get_latin1_char(unsigned char ch) |
| 1709 | { |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1710 | PyObject *unicode = unicode_latin1[ch]; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1711 | if (!unicode) { |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1712 | unicode = PyUnicode_New(1, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1713 | if (!unicode) |
| 1714 | return NULL; |
| 1715 | PyUnicode_1BYTE_DATA(unicode)[0] = ch; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1716 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1717 | unicode_latin1[ch] = unicode; |
| 1718 | } |
| 1719 | Py_INCREF(unicode); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1720 | return unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1721 | } |
| 1722 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1723 | PyObject * |
| 1724 | PyUnicode_FromUnicode(const Py_UNICODE *u, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1725 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1726 | PyObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1727 | Py_UCS4 maxchar = 0; |
| 1728 | Py_ssize_t num_surrogates; |
| 1729 | |
| 1730 | if (u == NULL) |
| 1731 | return (PyObject*)_PyUnicode_New(size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1732 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1733 | /* If the Unicode data is known at construction time, we can apply |
| 1734 | some optimizations which share commonly used objects. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1735 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1736 | /* Optimization for empty strings */ |
| 1737 | if (size == 0 && unicode_empty != NULL) { |
| 1738 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1739 | return unicode_empty; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1740 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1741 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1742 | /* Single character Unicode objects in the Latin-1 range are |
| 1743 | shared when using this constructor */ |
| 1744 | if (size == 1 && *u < 256) |
| 1745 | return get_latin1_char((unsigned char)*u); |
| 1746 | |
| 1747 | /* If not empty and not single character, copy the Unicode data |
| 1748 | into the new object */ |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1749 | if (find_maxchar_surrogates(u, u + size, |
| 1750 | &maxchar, &num_surrogates) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1751 | return NULL; |
| 1752 | |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1753 | unicode = PyUnicode_New(size - num_surrogates, maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1754 | if (!unicode) |
| 1755 | return NULL; |
| 1756 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1757 | switch (PyUnicode_KIND(unicode)) { |
| 1758 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1759 | _PyUnicode_CONVERT_BYTES(Py_UNICODE, unsigned char, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1760 | u, u + size, PyUnicode_1BYTE_DATA(unicode)); |
| 1761 | break; |
| 1762 | case PyUnicode_2BYTE_KIND: |
| 1763 | #if Py_UNICODE_SIZE == 2 |
| 1764 | Py_MEMCPY(PyUnicode_2BYTE_DATA(unicode), u, size * 2); |
| 1765 | #else |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1766 | _PyUnicode_CONVERT_BYTES(Py_UNICODE, Py_UCS2, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1767 | u, u + size, PyUnicode_2BYTE_DATA(unicode)); |
| 1768 | #endif |
| 1769 | break; |
| 1770 | case PyUnicode_4BYTE_KIND: |
| 1771 | #if SIZEOF_WCHAR_T == 2 |
| 1772 | /* This is the only case which has to process surrogates, thus |
| 1773 | a simple copy loop is not enough and we need a function. */ |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1774 | unicode_convert_wchar_to_ucs4(u, u + size, unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1775 | #else |
| 1776 | assert(num_surrogates == 0); |
| 1777 | Py_MEMCPY(PyUnicode_4BYTE_DATA(unicode), u, size * 4); |
| 1778 | #endif |
| 1779 | break; |
| 1780 | default: |
| 1781 | assert(0 && "Impossible state"); |
| 1782 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1783 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1784 | return unicode_result(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1785 | } |
| 1786 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1787 | PyObject * |
| 1788 | PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size) |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1789 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1790 | if (size < 0) { |
| 1791 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1792 | "Negative size passed to PyUnicode_FromStringAndSize"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1793 | return NULL; |
| 1794 | } |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 1795 | if (u != NULL) |
| 1796 | return PyUnicode_DecodeUTF8Stateful(u, size, NULL, NULL); |
| 1797 | else |
| 1798 | return (PyObject *)_PyUnicode_New(size); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1799 | } |
| 1800 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1801 | PyObject * |
| 1802 | PyUnicode_FromString(const char *u) |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1803 | { |
| 1804 | size_t size = strlen(u); |
| 1805 | if (size > PY_SSIZE_T_MAX) { |
| 1806 | PyErr_SetString(PyExc_OverflowError, "input too long"); |
| 1807 | return NULL; |
| 1808 | } |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 1809 | return PyUnicode_DecodeUTF8Stateful(u, (Py_ssize_t)size, NULL, NULL); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1810 | } |
| 1811 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1812 | PyObject * |
| 1813 | _PyUnicode_FromId(_Py_Identifier *id) |
| 1814 | { |
| 1815 | if (!id->object) { |
Victor Stinner | d1cd99b | 2012-02-07 23:05:55 +0100 | [diff] [blame] | 1816 | id->object = PyUnicode_DecodeUTF8Stateful(id->string, |
| 1817 | strlen(id->string), |
| 1818 | NULL, NULL); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1819 | if (!id->object) |
| 1820 | return NULL; |
| 1821 | PyUnicode_InternInPlace(&id->object); |
| 1822 | assert(!id->next); |
| 1823 | id->next = static_strings; |
| 1824 | static_strings = id; |
| 1825 | } |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1826 | return id->object; |
| 1827 | } |
| 1828 | |
| 1829 | void |
| 1830 | _PyUnicode_ClearStaticStrings() |
| 1831 | { |
| 1832 | _Py_Identifier *i; |
| 1833 | for (i = static_strings; i; i = i->next) { |
| 1834 | Py_DECREF(i->object); |
| 1835 | i->object = NULL; |
| 1836 | i->next = NULL; |
| 1837 | } |
| 1838 | } |
| 1839 | |
Benjamin Peterson | 0df5429 | 2012-03-26 14:50:32 -0400 | [diff] [blame] | 1840 | /* Internal function, doesn't check maximum character */ |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1841 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1842 | PyObject* |
| 1843 | _PyUnicode_FromASCII(const char *buffer, Py_ssize_t size) |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1844 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1845 | const unsigned char *s = (const unsigned char *)buffer; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 1846 | PyObject *unicode; |
Victor Stinner | e6b2d44 | 2011-12-11 21:54:30 +0100 | [diff] [blame] | 1847 | if (size == 1) { |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1848 | #ifdef Py_DEBUG |
Victor Stinner | e6b2d44 | 2011-12-11 21:54:30 +0100 | [diff] [blame] | 1849 | assert(s[0] < 128); |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1850 | #endif |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1851 | return get_latin1_char(s[0]); |
Victor Stinner | e6b2d44 | 2011-12-11 21:54:30 +0100 | [diff] [blame] | 1852 | } |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 1853 | unicode = PyUnicode_New(size, 127); |
| 1854 | if (!unicode) |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1855 | return NULL; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 1856 | memcpy(PyUnicode_1BYTE_DATA(unicode), s, size); |
| 1857 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 1858 | return unicode; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1859 | } |
| 1860 | |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 1861 | static Py_UCS4 |
| 1862 | kind_maxchar_limit(unsigned int kind) |
| 1863 | { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 1864 | switch (kind) { |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 1865 | case PyUnicode_1BYTE_KIND: |
| 1866 | return 0x80; |
| 1867 | case PyUnicode_2BYTE_KIND: |
| 1868 | return 0x100; |
| 1869 | case PyUnicode_4BYTE_KIND: |
| 1870 | return 0x10000; |
| 1871 | default: |
| 1872 | assert(0 && "invalid kind"); |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1873 | return MAX_UNICODE; |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 1874 | } |
| 1875 | } |
| 1876 | |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 1877 | Py_LOCAL_INLINE(Py_UCS4) |
| 1878 | align_maxchar(Py_UCS4 maxchar) |
| 1879 | { |
| 1880 | if (maxchar <= 127) |
| 1881 | return 127; |
| 1882 | else if (maxchar <= 255) |
| 1883 | return 255; |
| 1884 | else if (maxchar <= 65535) |
| 1885 | return 65535; |
| 1886 | else |
| 1887 | return MAX_UNICODE; |
| 1888 | } |
| 1889 | |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1890 | static PyObject* |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1891 | _PyUnicode_FromUCS1(const unsigned char* u, Py_ssize_t size) |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1892 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1893 | PyObject *res; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1894 | unsigned char max_char; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1895 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1896 | if (size == 0) { |
| 1897 | Py_INCREF(unicode_empty); |
| 1898 | return unicode_empty; |
| 1899 | } |
| 1900 | assert(size > 0); |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1901 | if (size == 1) |
| 1902 | return get_latin1_char(u[0]); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1903 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1904 | max_char = ucs1lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1905 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1906 | if (!res) |
| 1907 | return NULL; |
| 1908 | memcpy(PyUnicode_1BYTE_DATA(res), u, size); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1909 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1910 | return res; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1911 | } |
| 1912 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1913 | static PyObject* |
| 1914 | _PyUnicode_FromUCS2(const Py_UCS2 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1915 | { |
| 1916 | PyObject *res; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1917 | Py_UCS2 max_char; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1918 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1919 | if (size == 0) { |
| 1920 | Py_INCREF(unicode_empty); |
| 1921 | return unicode_empty; |
| 1922 | } |
| 1923 | assert(size > 0); |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 1924 | if (size == 1) { |
| 1925 | Py_UCS4 ch = u[0]; |
| 1926 | if (ch < 256) |
| 1927 | return get_latin1_char((unsigned char)ch); |
| 1928 | |
| 1929 | res = PyUnicode_New(1, ch); |
| 1930 | if (res == NULL) |
| 1931 | return NULL; |
| 1932 | PyUnicode_WRITE(PyUnicode_KIND(res), PyUnicode_DATA(res), 0, ch); |
| 1933 | assert(_PyUnicode_CheckConsistency(res, 1)); |
| 1934 | return res; |
| 1935 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1936 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1937 | max_char = ucs2lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1938 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1939 | if (!res) |
| 1940 | return NULL; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1941 | if (max_char >= 256) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1942 | memcpy(PyUnicode_2BYTE_DATA(res), u, sizeof(Py_UCS2)*size); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1943 | else { |
| 1944 | _PyUnicode_CONVERT_BYTES( |
| 1945 | Py_UCS2, Py_UCS1, u, u + size, PyUnicode_1BYTE_DATA(res)); |
| 1946 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1947 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1948 | return res; |
| 1949 | } |
| 1950 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1951 | static PyObject* |
| 1952 | _PyUnicode_FromUCS4(const Py_UCS4 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1953 | { |
| 1954 | PyObject *res; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1955 | Py_UCS4 max_char; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1956 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1957 | if (size == 0) { |
| 1958 | Py_INCREF(unicode_empty); |
| 1959 | return unicode_empty; |
| 1960 | } |
| 1961 | assert(size > 0); |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 1962 | if (size == 1) { |
| 1963 | Py_UCS4 ch = u[0]; |
| 1964 | if (ch < 256) |
| 1965 | return get_latin1_char((unsigned char)ch); |
| 1966 | |
| 1967 | res = PyUnicode_New(1, ch); |
| 1968 | if (res == NULL) |
| 1969 | return NULL; |
| 1970 | PyUnicode_WRITE(PyUnicode_KIND(res), PyUnicode_DATA(res), 0, ch); |
| 1971 | assert(_PyUnicode_CheckConsistency(res, 1)); |
| 1972 | return res; |
| 1973 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1974 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1975 | max_char = ucs4lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1976 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1977 | if (!res) |
| 1978 | return NULL; |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 1979 | if (max_char < 256) |
| 1980 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS1, u, u + size, |
| 1981 | PyUnicode_1BYTE_DATA(res)); |
| 1982 | else if (max_char < 0x10000) |
| 1983 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS2, u, u + size, |
| 1984 | PyUnicode_2BYTE_DATA(res)); |
| 1985 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1986 | memcpy(PyUnicode_4BYTE_DATA(res), u, sizeof(Py_UCS4)*size); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1987 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1988 | return res; |
| 1989 | } |
| 1990 | |
| 1991 | PyObject* |
| 1992 | PyUnicode_FromKindAndData(int kind, const void *buffer, Py_ssize_t size) |
| 1993 | { |
Victor Stinner | cfed46e | 2011-11-22 01:29:14 +0100 | [diff] [blame] | 1994 | if (size < 0) { |
| 1995 | PyErr_SetString(PyExc_ValueError, "size must be positive"); |
| 1996 | return NULL; |
| 1997 | } |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 1998 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1999 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 2000 | return _PyUnicode_FromUCS1(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2001 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 2002 | return _PyUnicode_FromUCS2(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2003 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 2004 | return _PyUnicode_FromUCS4(buffer, size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 2005 | default: |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 2006 | PyErr_SetString(PyExc_SystemError, "invalid kind"); |
| 2007 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2008 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2009 | } |
| 2010 | |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2011 | Py_UCS4 |
| 2012 | _PyUnicode_FindMaxChar(PyObject *unicode, Py_ssize_t start, Py_ssize_t end) |
| 2013 | { |
| 2014 | enum PyUnicode_Kind kind; |
| 2015 | void *startptr, *endptr; |
| 2016 | |
| 2017 | assert(PyUnicode_IS_READY(unicode)); |
| 2018 | assert(0 <= start); |
| 2019 | assert(end <= PyUnicode_GET_LENGTH(unicode)); |
| 2020 | assert(start <= end); |
| 2021 | |
| 2022 | if (start == 0 && end == PyUnicode_GET_LENGTH(unicode)) |
| 2023 | return PyUnicode_MAX_CHAR_VALUE(unicode); |
| 2024 | |
| 2025 | if (start == end) |
| 2026 | return 127; |
| 2027 | |
Victor Stinner | 94d558b | 2012-04-27 22:26:58 +0200 | [diff] [blame] | 2028 | if (PyUnicode_IS_ASCII(unicode)) |
| 2029 | return 127; |
| 2030 | |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2031 | kind = PyUnicode_KIND(unicode); |
Benjamin Peterson | f3b7d86 | 2012-04-23 18:07:01 -0400 | [diff] [blame] | 2032 | startptr = PyUnicode_DATA(unicode); |
Benjamin Peterson | b9f4c9d | 2012-04-23 21:45:40 -0400 | [diff] [blame] | 2033 | endptr = (char *)startptr + end * kind; |
| 2034 | startptr = (char *)startptr + start * kind; |
Benjamin Peterson | 2844a7a | 2012-04-23 18:00:25 -0400 | [diff] [blame] | 2035 | switch(kind) { |
| 2036 | case PyUnicode_1BYTE_KIND: |
| 2037 | return ucs1lib_find_max_char(startptr, endptr); |
| 2038 | case PyUnicode_2BYTE_KIND: |
| 2039 | return ucs2lib_find_max_char(startptr, endptr); |
| 2040 | case PyUnicode_4BYTE_KIND: |
| 2041 | return ucs4lib_find_max_char(startptr, endptr); |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2042 | default: |
Benjamin Peterson | 2844a7a | 2012-04-23 18:00:25 -0400 | [diff] [blame] | 2043 | assert(0); |
| 2044 | return 0; |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2045 | } |
| 2046 | } |
| 2047 | |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2048 | /* Ensure that a string uses the most efficient storage, if it is not the |
| 2049 | case: create a new string with of the right kind. Write NULL into *p_unicode |
| 2050 | on error. */ |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 2051 | static void |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2052 | unicode_adjust_maxchar(PyObject **p_unicode) |
| 2053 | { |
| 2054 | PyObject *unicode, *copy; |
| 2055 | Py_UCS4 max_char; |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2056 | Py_ssize_t len; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2057 | unsigned int kind; |
| 2058 | |
| 2059 | assert(p_unicode != NULL); |
| 2060 | unicode = *p_unicode; |
| 2061 | assert(PyUnicode_IS_READY(unicode)); |
| 2062 | if (PyUnicode_IS_ASCII(unicode)) |
| 2063 | return; |
| 2064 | |
| 2065 | len = PyUnicode_GET_LENGTH(unicode); |
| 2066 | kind = PyUnicode_KIND(unicode); |
| 2067 | if (kind == PyUnicode_1BYTE_KIND) { |
| 2068 | const Py_UCS1 *u = PyUnicode_1BYTE_DATA(unicode); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2069 | max_char = ucs1lib_find_max_char(u, u + len); |
| 2070 | if (max_char >= 128) |
| 2071 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2072 | } |
| 2073 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 2074 | const Py_UCS2 *u = PyUnicode_2BYTE_DATA(unicode); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2075 | max_char = ucs2lib_find_max_char(u, u + len); |
| 2076 | if (max_char >= 256) |
| 2077 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2078 | } |
| 2079 | else { |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2080 | const Py_UCS4 *u = PyUnicode_4BYTE_DATA(unicode); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2081 | assert(kind == PyUnicode_4BYTE_KIND); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2082 | max_char = ucs4lib_find_max_char(u, u + len); |
| 2083 | if (max_char >= 0x10000) |
| 2084 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2085 | } |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2086 | copy = PyUnicode_New(len, max_char); |
Victor Stinner | ca439ee | 2012-06-16 03:17:34 +0200 | [diff] [blame^] | 2087 | if (copy != NULL) |
| 2088 | _PyUnicode_FastCopyCharacters(copy, 0, unicode, 0, len); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2089 | Py_DECREF(unicode); |
| 2090 | *p_unicode = copy; |
| 2091 | } |
| 2092 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2093 | PyObject* |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 2094 | _PyUnicode_Copy(PyObject *unicode) |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2095 | { |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 2096 | Py_ssize_t length; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2097 | PyObject *copy; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2098 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2099 | if (!PyUnicode_Check(unicode)) { |
| 2100 | PyErr_BadInternalCall(); |
| 2101 | return NULL; |
| 2102 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 2103 | if (PyUnicode_READY(unicode) == -1) |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2104 | return NULL; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2105 | |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 2106 | length = PyUnicode_GET_LENGTH(unicode); |
| 2107 | copy = PyUnicode_New(length, PyUnicode_MAX_CHAR_VALUE(unicode)); |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2108 | if (!copy) |
| 2109 | return NULL; |
| 2110 | assert(PyUnicode_KIND(copy) == PyUnicode_KIND(unicode)); |
| 2111 | |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 2112 | Py_MEMCPY(PyUnicode_DATA(copy), PyUnicode_DATA(unicode), |
| 2113 | length * PyUnicode_KIND(unicode)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2114 | assert(_PyUnicode_CheckConsistency(copy, 1)); |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2115 | return copy; |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2116 | } |
| 2117 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2118 | |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2119 | /* Widen Unicode objects to larger buffers. Don't write terminating null |
| 2120 | character. Return NULL on error. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2121 | |
| 2122 | void* |
| 2123 | _PyUnicode_AsKind(PyObject *s, unsigned int kind) |
| 2124 | { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2125 | Py_ssize_t len; |
| 2126 | void *result; |
| 2127 | unsigned int skind; |
| 2128 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 2129 | if (PyUnicode_READY(s) == -1) |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2130 | return NULL; |
| 2131 | |
| 2132 | len = PyUnicode_GET_LENGTH(s); |
| 2133 | skind = PyUnicode_KIND(s); |
| 2134 | if (skind >= kind) { |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 2135 | PyErr_SetString(PyExc_SystemError, "invalid widening attempt"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2136 | return NULL; |
| 2137 | } |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 2138 | switch (kind) { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2139 | case PyUnicode_2BYTE_KIND: |
| 2140 | result = PyMem_Malloc(len * sizeof(Py_UCS2)); |
| 2141 | if (!result) |
| 2142 | return PyErr_NoMemory(); |
| 2143 | assert(skind == PyUnicode_1BYTE_KIND); |
| 2144 | _PyUnicode_CONVERT_BYTES( |
| 2145 | Py_UCS1, Py_UCS2, |
| 2146 | PyUnicode_1BYTE_DATA(s), |
| 2147 | PyUnicode_1BYTE_DATA(s) + len, |
| 2148 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2149 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2150 | case PyUnicode_4BYTE_KIND: |
| 2151 | result = PyMem_Malloc(len * sizeof(Py_UCS4)); |
| 2152 | if (!result) |
| 2153 | return PyErr_NoMemory(); |
| 2154 | if (skind == PyUnicode_2BYTE_KIND) { |
| 2155 | _PyUnicode_CONVERT_BYTES( |
| 2156 | Py_UCS2, Py_UCS4, |
| 2157 | PyUnicode_2BYTE_DATA(s), |
| 2158 | PyUnicode_2BYTE_DATA(s) + len, |
| 2159 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2160 | } |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2161 | else { |
| 2162 | assert(skind == PyUnicode_1BYTE_KIND); |
| 2163 | _PyUnicode_CONVERT_BYTES( |
| 2164 | Py_UCS1, Py_UCS4, |
| 2165 | PyUnicode_1BYTE_DATA(s), |
| 2166 | PyUnicode_1BYTE_DATA(s) + len, |
| 2167 | result); |
| 2168 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2169 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2170 | default: |
| 2171 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2172 | } |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 2173 | PyErr_SetString(PyExc_SystemError, "invalid kind"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2174 | return NULL; |
| 2175 | } |
| 2176 | |
| 2177 | static Py_UCS4* |
| 2178 | as_ucs4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 2179 | int copy_null) |
| 2180 | { |
| 2181 | int kind; |
| 2182 | void *data; |
| 2183 | Py_ssize_t len, targetlen; |
| 2184 | if (PyUnicode_READY(string) == -1) |
| 2185 | return NULL; |
| 2186 | kind = PyUnicode_KIND(string); |
| 2187 | data = PyUnicode_DATA(string); |
| 2188 | len = PyUnicode_GET_LENGTH(string); |
| 2189 | targetlen = len; |
| 2190 | if (copy_null) |
| 2191 | targetlen++; |
| 2192 | if (!target) { |
| 2193 | if (PY_SSIZE_T_MAX / sizeof(Py_UCS4) < targetlen) { |
| 2194 | PyErr_NoMemory(); |
| 2195 | return NULL; |
| 2196 | } |
| 2197 | target = PyMem_Malloc(targetlen * sizeof(Py_UCS4)); |
| 2198 | if (!target) { |
| 2199 | PyErr_NoMemory(); |
| 2200 | return NULL; |
| 2201 | } |
| 2202 | } |
| 2203 | else { |
| 2204 | if (targetsize < targetlen) { |
| 2205 | PyErr_Format(PyExc_SystemError, |
| 2206 | "string is longer than the buffer"); |
| 2207 | if (copy_null && 0 < targetsize) |
| 2208 | target[0] = 0; |
| 2209 | return NULL; |
| 2210 | } |
| 2211 | } |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2212 | if (kind == PyUnicode_1BYTE_KIND) { |
| 2213 | Py_UCS1 *start = (Py_UCS1 *) data; |
| 2214 | _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] | 2215 | } |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2216 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 2217 | Py_UCS2 *start = (Py_UCS2 *) data; |
| 2218 | _PyUnicode_CONVERT_BYTES(Py_UCS2, Py_UCS4, start, start + len, target); |
| 2219 | } |
| 2220 | else { |
| 2221 | assert(kind == PyUnicode_4BYTE_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2222 | Py_MEMCPY(target, data, len * sizeof(Py_UCS4)); |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2223 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2224 | if (copy_null) |
| 2225 | target[len] = 0; |
| 2226 | return target; |
| 2227 | } |
| 2228 | |
| 2229 | Py_UCS4* |
| 2230 | PyUnicode_AsUCS4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 2231 | int copy_null) |
| 2232 | { |
Antoine Pitrou | de20b0b | 2011-11-10 21:47:38 +0100 | [diff] [blame] | 2233 | if (target == NULL || targetsize < 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2234 | PyErr_BadInternalCall(); |
| 2235 | return NULL; |
| 2236 | } |
| 2237 | return as_ucs4(string, target, targetsize, copy_null); |
| 2238 | } |
| 2239 | |
| 2240 | Py_UCS4* |
| 2241 | PyUnicode_AsUCS4Copy(PyObject *string) |
| 2242 | { |
| 2243 | return as_ucs4(string, NULL, 0, 1); |
| 2244 | } |
| 2245 | |
| 2246 | #ifdef HAVE_WCHAR_H |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 2247 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2248 | PyObject * |
| 2249 | PyUnicode_FromWideChar(register const wchar_t *w, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2250 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2251 | if (w == NULL) { |
Victor Stinner | 382955f | 2011-12-11 21:44:00 +0100 | [diff] [blame] | 2252 | if (size == 0) { |
| 2253 | Py_INCREF(unicode_empty); |
| 2254 | return unicode_empty; |
| 2255 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2256 | PyErr_BadInternalCall(); |
| 2257 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2258 | } |
| 2259 | |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 2260 | if (size == -1) { |
| 2261 | size = wcslen(w); |
| 2262 | } |
| 2263 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2264 | return PyUnicode_FromUnicode(w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2265 | } |
| 2266 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2267 | #endif /* HAVE_WCHAR_H */ |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 2268 | |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2269 | static void |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2270 | makefmt(char *fmt, int longflag, int longlongflag, int size_tflag, |
| 2271 | int zeropad, int width, int precision, char c) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2272 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2273 | *fmt++ = '%'; |
| 2274 | if (width) { |
| 2275 | if (zeropad) |
| 2276 | *fmt++ = '0'; |
| 2277 | fmt += sprintf(fmt, "%d", width); |
| 2278 | } |
| 2279 | if (precision) |
| 2280 | fmt += sprintf(fmt, ".%d", precision); |
| 2281 | if (longflag) |
| 2282 | *fmt++ = 'l'; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2283 | else if (longlongflag) { |
| 2284 | /* longlongflag should only ever be nonzero on machines with |
| 2285 | HAVE_LONG_LONG defined */ |
| 2286 | #ifdef HAVE_LONG_LONG |
| 2287 | char *f = PY_FORMAT_LONG_LONG; |
| 2288 | while (*f) |
| 2289 | *fmt++ = *f++; |
| 2290 | #else |
| 2291 | /* we shouldn't ever get here */ |
| 2292 | assert(0); |
| 2293 | *fmt++ = 'l'; |
| 2294 | #endif |
| 2295 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2296 | else if (size_tflag) { |
| 2297 | char *f = PY_FORMAT_SIZE_T; |
| 2298 | while (*f) |
| 2299 | *fmt++ = *f++; |
| 2300 | } |
| 2301 | *fmt++ = c; |
| 2302 | *fmt = '\0'; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2303 | } |
| 2304 | |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2305 | /* helper for PyUnicode_FromFormatV() */ |
| 2306 | |
| 2307 | static const char* |
| 2308 | parse_format_flags(const char *f, |
| 2309 | int *p_width, int *p_precision, |
| 2310 | int *p_longflag, int *p_longlongflag, int *p_size_tflag) |
| 2311 | { |
| 2312 | int width, precision, longflag, longlongflag, size_tflag; |
| 2313 | |
| 2314 | /* parse the width.precision part, e.g. "%2.5s" => width=2, precision=5 */ |
| 2315 | f++; |
| 2316 | width = 0; |
| 2317 | while (Py_ISDIGIT((unsigned)*f)) |
| 2318 | width = (width*10) + *f++ - '0'; |
| 2319 | precision = 0; |
| 2320 | if (*f == '.') { |
| 2321 | f++; |
| 2322 | while (Py_ISDIGIT((unsigned)*f)) |
| 2323 | precision = (precision*10) + *f++ - '0'; |
| 2324 | if (*f == '%') { |
| 2325 | /* "%.3%s" => f points to "3" */ |
| 2326 | f--; |
| 2327 | } |
| 2328 | } |
| 2329 | if (*f == '\0') { |
| 2330 | /* bogus format "%.1" => go backward, f points to "1" */ |
| 2331 | f--; |
| 2332 | } |
| 2333 | if (p_width != NULL) |
| 2334 | *p_width = width; |
| 2335 | if (p_precision != NULL) |
| 2336 | *p_precision = precision; |
| 2337 | |
| 2338 | /* Handle %ld, %lu, %lld and %llu. */ |
| 2339 | longflag = 0; |
| 2340 | longlongflag = 0; |
Victor Stinner | e7faec1 | 2011-03-02 00:01:53 +0000 | [diff] [blame] | 2341 | size_tflag = 0; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2342 | |
| 2343 | if (*f == 'l') { |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2344 | if (f[1] == 'd' || f[1] == 'u' || f[1] == 'i') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2345 | longflag = 1; |
| 2346 | ++f; |
| 2347 | } |
| 2348 | #ifdef HAVE_LONG_LONG |
| 2349 | else if (f[1] == 'l' && |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2350 | (f[2] == 'd' || f[2] == 'u' || f[2] == 'i')) { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2351 | longlongflag = 1; |
| 2352 | f += 2; |
| 2353 | } |
| 2354 | #endif |
| 2355 | } |
| 2356 | /* handle the size_t flag. */ |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2357 | 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] | 2358 | size_tflag = 1; |
| 2359 | ++f; |
| 2360 | } |
| 2361 | if (p_longflag != NULL) |
| 2362 | *p_longflag = longflag; |
| 2363 | if (p_longlongflag != NULL) |
| 2364 | *p_longlongflag = longlongflag; |
| 2365 | if (p_size_tflag != NULL) |
| 2366 | *p_size_tflag = size_tflag; |
| 2367 | return f; |
| 2368 | } |
| 2369 | |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2370 | /* maximum number of characters required for output of %ld. 21 characters |
| 2371 | allows for 64-bit integers (in decimal) and an optional sign. */ |
| 2372 | #define MAX_LONG_CHARS 21 |
| 2373 | /* maximum number of characters required for output of %lld. |
| 2374 | We need at most ceil(log10(256)*SIZEOF_LONG_LONG) digits, |
| 2375 | plus 1 for the sign. 53/22 is an upper bound for log10(256). */ |
| 2376 | #define MAX_LONG_LONG_CHARS (2 + (SIZEOF_LONG_LONG*53-1) / 22) |
| 2377 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2378 | PyObject * |
| 2379 | PyUnicode_FromFormatV(const char *format, va_list vargs) |
| 2380 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2381 | va_list count; |
| 2382 | Py_ssize_t callcount = 0; |
| 2383 | PyObject **callresults = NULL; |
| 2384 | PyObject **callresult = NULL; |
| 2385 | Py_ssize_t n = 0; |
| 2386 | int width = 0; |
| 2387 | int precision = 0; |
| 2388 | int zeropad; |
| 2389 | const char* f; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2390 | PyObject *string; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2391 | /* used by sprintf */ |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2392 | char fmt[61]; /* should be enough for %0width.precisionlld */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2393 | Py_UCS4 maxchar = 127; /* result is ASCII by default */ |
| 2394 | Py_UCS4 argmaxchar; |
| 2395 | Py_ssize_t numbersize = 0; |
| 2396 | char *numberresults = NULL; |
| 2397 | char *numberresult = NULL; |
| 2398 | Py_ssize_t i; |
| 2399 | int kind; |
| 2400 | void *data; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2401 | |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 2402 | Py_VA_COPY(count, vargs); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2403 | /* step 1: count the number of %S/%R/%A/%s format specifications |
| 2404 | * (we call PyObject_Str()/PyObject_Repr()/PyObject_ASCII()/ |
| 2405 | * 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] | 2406 | * result in an array) |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 2407 | * also estimate a upper bound for all the number formats in the string, |
| 2408 | * 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] | 2409 | * buffer before putting everything together. */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2410 | for (f = format; *f; f++) { |
| 2411 | if (*f == '%') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2412 | int longlongflag; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2413 | /* skip width or width.precision (eg. "1.2" of "%1.2f") */ |
| 2414 | f = parse_format_flags(f, &width, NULL, NULL, &longlongflag, NULL); |
| 2415 | if (*f == 's' || *f=='S' || *f=='R' || *f=='A' || *f=='V') |
| 2416 | ++callcount; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2417 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2418 | else if (*f == 'd' || *f=='u' || *f=='i' || *f=='x' || *f=='p') { |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2419 | #ifdef HAVE_LONG_LONG |
| 2420 | if (longlongflag) { |
| 2421 | if (width < MAX_LONG_LONG_CHARS) |
| 2422 | width = MAX_LONG_LONG_CHARS; |
| 2423 | } |
| 2424 | else |
| 2425 | #endif |
| 2426 | /* MAX_LONG_CHARS is enough to hold a 64-bit integer, |
| 2427 | including sign. Decimal takes the most space. This |
| 2428 | isn't enough for octal. If a width is specified we |
| 2429 | need more (which we allocate later). */ |
| 2430 | if (width < MAX_LONG_CHARS) |
| 2431 | width = MAX_LONG_CHARS; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2432 | |
| 2433 | /* account for the size + '\0' to separate numbers |
| 2434 | inside of the numberresults buffer */ |
| 2435 | numbersize += (width + 1); |
| 2436 | } |
| 2437 | } |
| 2438 | else if ((unsigned char)*f > 127) { |
| 2439 | PyErr_Format(PyExc_ValueError, |
| 2440 | "PyUnicode_FromFormatV() expects an ASCII-encoded format " |
| 2441 | "string, got a non-ASCII byte: 0x%02x", |
| 2442 | (unsigned char)*f); |
| 2443 | return NULL; |
| 2444 | } |
| 2445 | } |
| 2446 | /* step 2: allocate memory for the results of |
| 2447 | * PyObject_Str()/PyObject_Repr()/PyUnicode_DecodeUTF8() calls */ |
| 2448 | if (callcount) { |
| 2449 | callresults = PyObject_Malloc(sizeof(PyObject *) * callcount); |
| 2450 | if (!callresults) { |
| 2451 | PyErr_NoMemory(); |
| 2452 | return NULL; |
| 2453 | } |
| 2454 | callresult = callresults; |
| 2455 | } |
| 2456 | /* step 2.5: allocate memory for the results of formating numbers */ |
| 2457 | if (numbersize) { |
| 2458 | numberresults = PyObject_Malloc(numbersize); |
| 2459 | if (!numberresults) { |
| 2460 | PyErr_NoMemory(); |
| 2461 | goto fail; |
| 2462 | } |
| 2463 | numberresult = numberresults; |
| 2464 | } |
| 2465 | |
| 2466 | /* step 3: format numbers and figure out how large a buffer we need */ |
| 2467 | for (f = format; *f; f++) { |
| 2468 | if (*f == '%') { |
| 2469 | const char* p; |
| 2470 | int longflag; |
| 2471 | int longlongflag; |
| 2472 | int size_tflag; |
| 2473 | int numprinted; |
| 2474 | |
| 2475 | p = f; |
| 2476 | zeropad = (f[1] == '0'); |
| 2477 | f = parse_format_flags(f, &width, &precision, |
| 2478 | &longflag, &longlongflag, &size_tflag); |
| 2479 | switch (*f) { |
| 2480 | case 'c': |
| 2481 | { |
| 2482 | Py_UCS4 ordinal = va_arg(count, int); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 2483 | maxchar = MAX_MAXCHAR(maxchar, ordinal); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2484 | n++; |
| 2485 | break; |
| 2486 | } |
| 2487 | case '%': |
| 2488 | n++; |
| 2489 | break; |
| 2490 | case 'i': |
| 2491 | case 'd': |
| 2492 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, |
| 2493 | width, precision, *f); |
| 2494 | if (longflag) |
| 2495 | numprinted = sprintf(numberresult, fmt, |
| 2496 | va_arg(count, long)); |
| 2497 | #ifdef HAVE_LONG_LONG |
| 2498 | else if (longlongflag) |
| 2499 | numprinted = sprintf(numberresult, fmt, |
| 2500 | va_arg(count, PY_LONG_LONG)); |
| 2501 | #endif |
| 2502 | else if (size_tflag) |
| 2503 | numprinted = sprintf(numberresult, fmt, |
| 2504 | va_arg(count, Py_ssize_t)); |
| 2505 | else |
| 2506 | numprinted = sprintf(numberresult, fmt, |
| 2507 | va_arg(count, int)); |
| 2508 | n += numprinted; |
| 2509 | /* advance by +1 to skip over the '\0' */ |
| 2510 | numberresult += (numprinted + 1); |
| 2511 | assert(*(numberresult - 1) == '\0'); |
| 2512 | assert(*(numberresult - 2) != '\0'); |
| 2513 | assert(numprinted >= 0); |
| 2514 | assert(numberresult <= numberresults + numbersize); |
| 2515 | break; |
| 2516 | case 'u': |
| 2517 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, |
| 2518 | width, precision, 'u'); |
| 2519 | if (longflag) |
| 2520 | numprinted = sprintf(numberresult, fmt, |
| 2521 | va_arg(count, unsigned long)); |
| 2522 | #ifdef HAVE_LONG_LONG |
| 2523 | else if (longlongflag) |
| 2524 | numprinted = sprintf(numberresult, fmt, |
| 2525 | va_arg(count, unsigned PY_LONG_LONG)); |
| 2526 | #endif |
| 2527 | else if (size_tflag) |
| 2528 | numprinted = sprintf(numberresult, fmt, |
| 2529 | va_arg(count, size_t)); |
| 2530 | else |
| 2531 | numprinted = sprintf(numberresult, fmt, |
| 2532 | va_arg(count, unsigned int)); |
| 2533 | n += numprinted; |
| 2534 | numberresult += (numprinted + 1); |
| 2535 | assert(*(numberresult - 1) == '\0'); |
| 2536 | assert(*(numberresult - 2) != '\0'); |
| 2537 | assert(numprinted >= 0); |
| 2538 | assert(numberresult <= numberresults + numbersize); |
| 2539 | break; |
| 2540 | case 'x': |
| 2541 | makefmt(fmt, 0, 0, 0, zeropad, width, precision, 'x'); |
| 2542 | numprinted = sprintf(numberresult, fmt, va_arg(count, int)); |
| 2543 | n += numprinted; |
| 2544 | numberresult += (numprinted + 1); |
| 2545 | assert(*(numberresult - 1) == '\0'); |
| 2546 | assert(*(numberresult - 2) != '\0'); |
| 2547 | assert(numprinted >= 0); |
| 2548 | assert(numberresult <= numberresults + numbersize); |
| 2549 | break; |
| 2550 | case 'p': |
| 2551 | numprinted = sprintf(numberresult, "%p", va_arg(count, void*)); |
| 2552 | /* %p is ill-defined: ensure leading 0x. */ |
| 2553 | if (numberresult[1] == 'X') |
| 2554 | numberresult[1] = 'x'; |
| 2555 | else if (numberresult[1] != 'x') { |
| 2556 | memmove(numberresult + 2, numberresult, |
| 2557 | strlen(numberresult) + 1); |
| 2558 | numberresult[0] = '0'; |
| 2559 | numberresult[1] = 'x'; |
| 2560 | numprinted += 2; |
| 2561 | } |
| 2562 | n += numprinted; |
| 2563 | numberresult += (numprinted + 1); |
| 2564 | assert(*(numberresult - 1) == '\0'); |
| 2565 | assert(*(numberresult - 2) != '\0'); |
| 2566 | assert(numprinted >= 0); |
| 2567 | assert(numberresult <= numberresults + numbersize); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2568 | break; |
| 2569 | case 's': |
| 2570 | { |
| 2571 | /* UTF-8 */ |
Georg Brandl | 780b2a6 | 2009-05-05 09:19:59 +0000 | [diff] [blame] | 2572 | const char *s = va_arg(count, const char*); |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 2573 | PyObject *str = PyUnicode_DecodeUTF8Stateful(s, strlen(s), "replace", NULL); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2574 | if (!str) |
| 2575 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2576 | /* since PyUnicode_DecodeUTF8 returns already flexible |
| 2577 | unicode objects, there is no need to call ready on them */ |
| 2578 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 2579 | maxchar = MAX_MAXCHAR(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2580 | n += PyUnicode_GET_LENGTH(str); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2581 | /* Remember the str and switch to the next slot */ |
| 2582 | *callresult++ = str; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2583 | break; |
| 2584 | } |
| 2585 | case 'U': |
| 2586 | { |
| 2587 | PyObject *obj = va_arg(count, PyObject *); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 2588 | assert(obj && _PyUnicode_CHECK(obj)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2589 | if (PyUnicode_READY(obj) == -1) |
| 2590 | goto fail; |
| 2591 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(obj); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 2592 | maxchar = MAX_MAXCHAR(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2593 | n += PyUnicode_GET_LENGTH(obj); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2594 | break; |
| 2595 | } |
| 2596 | case 'V': |
| 2597 | { |
| 2598 | PyObject *obj = va_arg(count, PyObject *); |
| 2599 | const char *str = va_arg(count, const char *); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2600 | PyObject *str_obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2601 | assert(obj || str); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 2602 | assert(!obj || _PyUnicode_CHECK(obj)); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2603 | if (obj) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2604 | if (PyUnicode_READY(obj) == -1) |
| 2605 | goto fail; |
| 2606 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(obj); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 2607 | maxchar = MAX_MAXCHAR(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2608 | n += PyUnicode_GET_LENGTH(obj); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2609 | *callresult++ = NULL; |
| 2610 | } |
| 2611 | else { |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 2612 | str_obj = PyUnicode_DecodeUTF8Stateful(str, strlen(str), "replace", NULL); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2613 | if (!str_obj) |
| 2614 | goto fail; |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 2615 | if (PyUnicode_READY(str_obj) == -1) { |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 2616 | Py_DECREF(str_obj); |
| 2617 | goto fail; |
| 2618 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2619 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str_obj); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 2620 | maxchar = MAX_MAXCHAR(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2621 | n += PyUnicode_GET_LENGTH(str_obj); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2622 | *callresult++ = str_obj; |
| 2623 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2624 | break; |
| 2625 | } |
| 2626 | case 'S': |
| 2627 | { |
| 2628 | PyObject *obj = va_arg(count, PyObject *); |
| 2629 | PyObject *str; |
| 2630 | assert(obj); |
| 2631 | str = PyObject_Str(obj); |
Benjamin Peterson | c8d8b88 | 2012-01-14 13:37:31 -0500 | [diff] [blame] | 2632 | if (!str) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2633 | goto fail; |
Benjamin Peterson | c8d8b88 | 2012-01-14 13:37:31 -0500 | [diff] [blame] | 2634 | if (PyUnicode_READY(str) == -1) { |
| 2635 | Py_DECREF(str); |
| 2636 | goto fail; |
| 2637 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2638 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 2639 | maxchar = MAX_MAXCHAR(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2640 | n += PyUnicode_GET_LENGTH(str); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2641 | /* Remember the str and switch to the next slot */ |
| 2642 | *callresult++ = str; |
| 2643 | break; |
| 2644 | } |
| 2645 | case 'R': |
| 2646 | { |
| 2647 | PyObject *obj = va_arg(count, PyObject *); |
| 2648 | PyObject *repr; |
| 2649 | assert(obj); |
| 2650 | repr = PyObject_Repr(obj); |
Benjamin Peterson | c8d8b88 | 2012-01-14 13:37:31 -0500 | [diff] [blame] | 2651 | if (!repr) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2652 | goto fail; |
Benjamin Peterson | c8d8b88 | 2012-01-14 13:37:31 -0500 | [diff] [blame] | 2653 | if (PyUnicode_READY(repr) == -1) { |
| 2654 | Py_DECREF(repr); |
| 2655 | goto fail; |
| 2656 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2657 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(repr); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 2658 | maxchar = MAX_MAXCHAR(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2659 | n += PyUnicode_GET_LENGTH(repr); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2660 | /* Remember the repr and switch to the next slot */ |
| 2661 | *callresult++ = repr; |
| 2662 | break; |
| 2663 | } |
| 2664 | case 'A': |
| 2665 | { |
| 2666 | PyObject *obj = va_arg(count, PyObject *); |
| 2667 | PyObject *ascii; |
| 2668 | assert(obj); |
| 2669 | ascii = PyObject_ASCII(obj); |
Benjamin Peterson | c8d8b88 | 2012-01-14 13:37:31 -0500 | [diff] [blame] | 2670 | if (!ascii) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2671 | goto fail; |
Benjamin Peterson | c8d8b88 | 2012-01-14 13:37:31 -0500 | [diff] [blame] | 2672 | if (PyUnicode_READY(ascii) == -1) { |
| 2673 | Py_DECREF(ascii); |
| 2674 | goto fail; |
| 2675 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2676 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(ascii); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 2677 | maxchar = MAX_MAXCHAR(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2678 | n += PyUnicode_GET_LENGTH(ascii); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2679 | /* Remember the repr and switch to the next slot */ |
| 2680 | *callresult++ = ascii; |
| 2681 | break; |
| 2682 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2683 | default: |
| 2684 | /* if we stumble upon an unknown |
| 2685 | formatting code, copy the rest of |
| 2686 | the format string to the output |
| 2687 | string. (we cannot just skip the |
| 2688 | code, since there's no way to know |
| 2689 | what's in the argument list) */ |
| 2690 | n += strlen(p); |
| 2691 | goto expand; |
| 2692 | } |
| 2693 | } else |
| 2694 | n++; |
| 2695 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2696 | expand: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2697 | /* step 4: fill the buffer */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2698 | /* Since we've analyzed how much space we need, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2699 | we don't have to resize the string. |
| 2700 | There can be no errors beyond this point. */ |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2701 | string = PyUnicode_New(n, maxchar); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2702 | if (!string) |
| 2703 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2704 | kind = PyUnicode_KIND(string); |
| 2705 | data = PyUnicode_DATA(string); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2706 | callresult = callresults; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2707 | numberresult = numberresults; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2708 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2709 | for (i = 0, f = format; *f; f++) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2710 | if (*f == '%') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2711 | const char* p; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2712 | |
| 2713 | p = f; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2714 | f = parse_format_flags(f, NULL, NULL, NULL, NULL, NULL); |
| 2715 | /* checking for == because the last argument could be a empty |
| 2716 | string, which causes i to point to end, the assert at the end of |
| 2717 | the loop */ |
| 2718 | assert(i <= PyUnicode_GET_LENGTH(string)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2719 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2720 | switch (*f) { |
| 2721 | case 'c': |
Victor Stinner | 5ed8b2c | 2011-02-21 21:13:44 +0000 | [diff] [blame] | 2722 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2723 | const int ordinal = va_arg(vargs, int); |
| 2724 | PyUnicode_WRITE(kind, data, i++, ordinal); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2725 | break; |
Victor Stinner | 5ed8b2c | 2011-02-21 21:13:44 +0000 | [diff] [blame] | 2726 | } |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2727 | case 'i': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2728 | case 'd': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2729 | case 'u': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2730 | case 'x': |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2731 | case 'p': |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 2732 | { |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 2733 | Py_ssize_t len; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2734 | /* unused, since we already have the result */ |
| 2735 | if (*f == 'p') |
| 2736 | (void) va_arg(vargs, void *); |
| 2737 | else |
| 2738 | (void) va_arg(vargs, int); |
| 2739 | /* extract the result from numberresults and append. */ |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 2740 | len = strlen(numberresult); |
| 2741 | unicode_write_cstr(string, i, numberresult, len); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2742 | /* skip over the separating '\0' */ |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 2743 | i += len; |
| 2744 | numberresult += len; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2745 | assert(*numberresult == '\0'); |
| 2746 | numberresult++; |
| 2747 | assert(numberresult <= numberresults + numbersize); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2748 | break; |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 2749 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2750 | case 's': |
| 2751 | { |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2752 | /* unused, since we already have the result */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2753 | Py_ssize_t size; |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2754 | (void) va_arg(vargs, char *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2755 | size = PyUnicode_GET_LENGTH(*callresult); |
| 2756 | assert(PyUnicode_KIND(*callresult) <= PyUnicode_KIND(string)); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 2757 | _PyUnicode_FastCopyCharacters(string, i, *callresult, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2758 | i += size; |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2759 | /* We're done with the unicode()/repr() => forget it */ |
| 2760 | Py_DECREF(*callresult); |
| 2761 | /* switch to next unicode()/repr() result */ |
| 2762 | ++callresult; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2763 | break; |
| 2764 | } |
| 2765 | case 'U': |
| 2766 | { |
| 2767 | PyObject *obj = va_arg(vargs, PyObject *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2768 | Py_ssize_t size; |
| 2769 | assert(PyUnicode_KIND(obj) <= PyUnicode_KIND(string)); |
| 2770 | size = PyUnicode_GET_LENGTH(obj); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 2771 | _PyUnicode_FastCopyCharacters(string, i, obj, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2772 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2773 | break; |
| 2774 | } |
| 2775 | case 'V': |
| 2776 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2777 | Py_ssize_t size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2778 | PyObject *obj = va_arg(vargs, PyObject *); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2779 | va_arg(vargs, const char *); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2780 | if (obj) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2781 | size = PyUnicode_GET_LENGTH(obj); |
| 2782 | assert(PyUnicode_KIND(obj) <= PyUnicode_KIND(string)); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 2783 | _PyUnicode_FastCopyCharacters(string, i, obj, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2784 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2785 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2786 | size = PyUnicode_GET_LENGTH(*callresult); |
| 2787 | assert(PyUnicode_KIND(*callresult) <= |
| 2788 | PyUnicode_KIND(string)); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 2789 | _PyUnicode_FastCopyCharacters(string, i, *callresult, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2790 | i += size; |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2791 | Py_DECREF(*callresult); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2792 | } |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2793 | ++callresult; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2794 | break; |
| 2795 | } |
| 2796 | case 'S': |
| 2797 | case 'R': |
Victor Stinner | 9a90900 | 2010-10-18 20:59:24 +0000 | [diff] [blame] | 2798 | case 'A': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2799 | { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2800 | Py_ssize_t size = PyUnicode_GET_LENGTH(*callresult); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2801 | /* unused, since we already have the result */ |
| 2802 | (void) va_arg(vargs, PyObject *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2803 | assert(PyUnicode_KIND(*callresult) <= PyUnicode_KIND(string)); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 2804 | _PyUnicode_FastCopyCharacters(string, i, *callresult, 0, size); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2805 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2806 | /* We're done with the unicode()/repr() => forget it */ |
| 2807 | Py_DECREF(*callresult); |
| 2808 | /* switch to next unicode()/repr() result */ |
| 2809 | ++callresult; |
| 2810 | break; |
| 2811 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2812 | case '%': |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2813 | PyUnicode_WRITE(kind, data, i++, '%'); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2814 | break; |
| 2815 | default: |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 2816 | { |
| 2817 | Py_ssize_t len = strlen(p); |
| 2818 | unicode_write_cstr(string, i, p, len); |
| 2819 | i += len; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2820 | assert(i == PyUnicode_GET_LENGTH(string)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2821 | goto end; |
| 2822 | } |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 2823 | } |
Victor Stinner | 1205f27 | 2010-09-11 00:54:47 +0000 | [diff] [blame] | 2824 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2825 | else { |
| 2826 | assert(i < PyUnicode_GET_LENGTH(string)); |
| 2827 | PyUnicode_WRITE(kind, data, i++, *f); |
| 2828 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2829 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2830 | assert(i == PyUnicode_GET_LENGTH(string)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2831 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2832 | end: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2833 | if (callresults) |
| 2834 | PyObject_Free(callresults); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2835 | if (numberresults) |
| 2836 | PyObject_Free(numberresults); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2837 | return unicode_result(string); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2838 | fail: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2839 | if (callresults) { |
| 2840 | PyObject **callresult2 = callresults; |
| 2841 | while (callresult2 < callresult) { |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2842 | Py_XDECREF(*callresult2); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2843 | ++callresult2; |
| 2844 | } |
| 2845 | PyObject_Free(callresults); |
| 2846 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2847 | if (numberresults) |
| 2848 | PyObject_Free(numberresults); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2849 | return NULL; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2850 | } |
| 2851 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2852 | PyObject * |
| 2853 | PyUnicode_FromFormat(const char *format, ...) |
| 2854 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2855 | PyObject* ret; |
| 2856 | va_list vargs; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2857 | |
| 2858 | #ifdef HAVE_STDARG_PROTOTYPES |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2859 | va_start(vargs, format); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2860 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2861 | va_start(vargs); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2862 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2863 | ret = PyUnicode_FromFormatV(format, vargs); |
| 2864 | va_end(vargs); |
| 2865 | return ret; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2866 | } |
| 2867 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2868 | #ifdef HAVE_WCHAR_H |
| 2869 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2870 | /* Helper function for PyUnicode_AsWideChar() and PyUnicode_AsWideCharString(): |
| 2871 | convert a Unicode object to a wide character string. |
| 2872 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2873 | - 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] | 2874 | character) required to convert the unicode object. Ignore size argument. |
| 2875 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2876 | - Otherwise: return the number of wide characters (excluding the null |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2877 | character) written into w. Write at most size wide characters (including |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2878 | the null character). */ |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2879 | static Py_ssize_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2880 | unicode_aswidechar(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2881 | wchar_t *w, |
| 2882 | Py_ssize_t size) |
| 2883 | { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2884 | Py_ssize_t res; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2885 | const wchar_t *wstr; |
| 2886 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2887 | wstr = PyUnicode_AsUnicodeAndSize(unicode, &res); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2888 | if (wstr == NULL) |
| 2889 | return -1; |
| 2890 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2891 | if (w != NULL) { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2892 | if (size > res) |
| 2893 | size = res + 1; |
| 2894 | else |
| 2895 | res = size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2896 | Py_MEMCPY(w, wstr, size * sizeof(wchar_t)); |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2897 | return res; |
| 2898 | } |
| 2899 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2900 | return res + 1; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2901 | } |
| 2902 | |
| 2903 | Py_ssize_t |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2904 | PyUnicode_AsWideChar(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2905 | wchar_t *w, |
| 2906 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2907 | { |
| 2908 | if (unicode == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2909 | PyErr_BadInternalCall(); |
| 2910 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2911 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2912 | return unicode_aswidechar(unicode, w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2913 | } |
| 2914 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2915 | wchar_t* |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 2916 | PyUnicode_AsWideCharString(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2917 | Py_ssize_t *size) |
| 2918 | { |
| 2919 | wchar_t* buffer; |
| 2920 | Py_ssize_t buflen; |
| 2921 | |
| 2922 | if (unicode == NULL) { |
| 2923 | PyErr_BadInternalCall(); |
| 2924 | return NULL; |
| 2925 | } |
| 2926 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2927 | buflen = unicode_aswidechar(unicode, NULL, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2928 | if (buflen == -1) |
| 2929 | return NULL; |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2930 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < buflen) { |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2931 | PyErr_NoMemory(); |
| 2932 | return NULL; |
| 2933 | } |
| 2934 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2935 | buffer = PyMem_MALLOC(buflen * sizeof(wchar_t)); |
| 2936 | if (buffer == NULL) { |
| 2937 | PyErr_NoMemory(); |
| 2938 | return NULL; |
| 2939 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2940 | buflen = unicode_aswidechar(unicode, buffer, buflen); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2941 | if (buflen == -1) |
| 2942 | return NULL; |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2943 | if (size != NULL) |
| 2944 | *size = buflen; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2945 | return buffer; |
| 2946 | } |
| 2947 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2948 | #endif /* HAVE_WCHAR_H */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2949 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2950 | PyObject * |
| 2951 | PyUnicode_FromOrdinal(int ordinal) |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2952 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2953 | PyObject *v; |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 2954 | if (ordinal < 0 || ordinal > MAX_UNICODE) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2955 | PyErr_SetString(PyExc_ValueError, |
| 2956 | "chr() arg not in range(0x110000)"); |
| 2957 | return NULL; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2958 | } |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 2959 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2960 | if (ordinal < 256) |
| 2961 | return get_latin1_char(ordinal); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2962 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2963 | v = PyUnicode_New(1, ordinal); |
| 2964 | if (v == NULL) |
| 2965 | return NULL; |
| 2966 | PyUnicode_WRITE(PyUnicode_KIND(v), PyUnicode_DATA(v), 0, ordinal); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2967 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2968 | return v; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2969 | } |
| 2970 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2971 | PyObject * |
| 2972 | PyUnicode_FromObject(register PyObject *obj) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2973 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2974 | /* XXX Perhaps we should make this API an alias of |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2975 | PyObject_Str() instead ?! */ |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2976 | if (PyUnicode_CheckExact(obj)) { |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 2977 | if (PyUnicode_READY(obj) == -1) |
Victor Stinner | d3a83d5 | 2011-10-01 03:09:33 +0200 | [diff] [blame] | 2978 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2979 | Py_INCREF(obj); |
| 2980 | return obj; |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2981 | } |
| 2982 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2983 | /* For a Unicode subtype that's not a Unicode object, |
| 2984 | return a true Unicode object with the same data. */ |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 2985 | return _PyUnicode_Copy(obj); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2986 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2987 | PyErr_Format(PyExc_TypeError, |
| 2988 | "Can't convert '%.100s' object to str implicitly", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2989 | Py_TYPE(obj)->tp_name); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2990 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2991 | } |
| 2992 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2993 | PyObject * |
| 2994 | PyUnicode_FromEncodedObject(register PyObject *obj, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2995 | const char *encoding, |
| 2996 | const char *errors) |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2997 | { |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2998 | Py_buffer buffer; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2999 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3000 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3001 | if (obj == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3002 | PyErr_BadInternalCall(); |
| 3003 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3004 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 3005 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 3006 | /* Decoding bytes objects is the most common case and should be fast */ |
| 3007 | if (PyBytes_Check(obj)) { |
| 3008 | if (PyBytes_GET_SIZE(obj) == 0) { |
| 3009 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 3010 | v = unicode_empty; |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 3011 | } |
| 3012 | else { |
| 3013 | v = PyUnicode_Decode( |
| 3014 | PyBytes_AS_STRING(obj), PyBytes_GET_SIZE(obj), |
| 3015 | encoding, errors); |
| 3016 | } |
| 3017 | return v; |
| 3018 | } |
| 3019 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 3020 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3021 | PyErr_SetString(PyExc_TypeError, |
| 3022 | "decoding str is not supported"); |
| 3023 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3024 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 3025 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 3026 | /* Retrieve a bytes buffer view through the PEP 3118 buffer interface */ |
| 3027 | if (PyObject_GetBuffer(obj, &buffer, PyBUF_SIMPLE) < 0) { |
| 3028 | PyErr_Format(PyExc_TypeError, |
| 3029 | "coercing to str: need bytes, bytearray " |
| 3030 | "or buffer-like object, %.80s found", |
| 3031 | Py_TYPE(obj)->tp_name); |
| 3032 | return NULL; |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 3033 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3034 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 3035 | if (buffer.len == 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3036 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 3037 | v = unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3038 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3039 | else |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 3040 | v = PyUnicode_Decode((char*) buffer.buf, buffer.len, encoding, errors); |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 3041 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 3042 | PyBuffer_Release(&buffer); |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 3043 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3044 | } |
| 3045 | |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3046 | /* Convert encoding to lower case and replace '_' with '-' in order to |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3047 | catch e.g. UTF_8. Return 0 on error (encoding is longer than lower_len-1), |
| 3048 | 1 on success. */ |
| 3049 | static int |
| 3050 | normalize_encoding(const char *encoding, |
| 3051 | char *lower, |
| 3052 | size_t lower_len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3053 | { |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 3054 | const char *e; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3055 | char *l; |
| 3056 | char *l_end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3057 | |
Benjamin Peterson | 7a6debe | 2011-10-15 09:25:28 -0400 | [diff] [blame] | 3058 | if (encoding == NULL) { |
| 3059 | strcpy(lower, "utf-8"); |
| 3060 | return 1; |
| 3061 | } |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 3062 | e = encoding; |
| 3063 | l = lower; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3064 | l_end = &lower[lower_len - 1]; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3065 | while (*e) { |
| 3066 | if (l == l_end) |
| 3067 | return 0; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 3068 | if (Py_ISUPPER(*e)) { |
| 3069 | *l++ = Py_TOLOWER(*e++); |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 3070 | } |
| 3071 | else if (*e == '_') { |
| 3072 | *l++ = '-'; |
| 3073 | e++; |
| 3074 | } |
| 3075 | else { |
| 3076 | *l++ = *e++; |
| 3077 | } |
| 3078 | } |
| 3079 | *l = '\0'; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3080 | return 1; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3081 | } |
| 3082 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3083 | PyObject * |
| 3084 | PyUnicode_Decode(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3085 | Py_ssize_t size, |
| 3086 | const char *encoding, |
| 3087 | const char *errors) |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3088 | { |
| 3089 | PyObject *buffer = NULL, *unicode; |
| 3090 | Py_buffer info; |
| 3091 | char lower[11]; /* Enough for any encoding shortcut */ |
| 3092 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3093 | /* Shortcuts for common default encodings */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3094 | if (normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3095 | if ((strcmp(lower, "utf-8") == 0) || |
| 3096 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 3097 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3098 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3099 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3100 | (strcmp(lower, "iso-8859-1") == 0)) |
| 3101 | return PyUnicode_DecodeLatin1(s, size, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3102 | #ifdef HAVE_MBCS |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3103 | else if (strcmp(lower, "mbcs") == 0) |
| 3104 | return PyUnicode_DecodeMBCS(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 3105 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3106 | else if (strcmp(lower, "ascii") == 0) |
| 3107 | return PyUnicode_DecodeASCII(s, size, errors); |
| 3108 | else if (strcmp(lower, "utf-16") == 0) |
| 3109 | return PyUnicode_DecodeUTF16(s, size, errors, 0); |
| 3110 | else if (strcmp(lower, "utf-32") == 0) |
| 3111 | return PyUnicode_DecodeUTF32(s, size, errors, 0); |
| 3112 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3113 | |
| 3114 | /* Decode via the codec registry */ |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 3115 | buffer = NULL; |
Antoine Pitrou | c3b3924 | 2009-01-03 16:59:18 +0000 | [diff] [blame] | 3116 | 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] | 3117 | goto onError; |
Antoine Pitrou | ee58fa4 | 2008-08-19 18:22:14 +0000 | [diff] [blame] | 3118 | buffer = PyMemoryView_FromBuffer(&info); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3119 | if (buffer == NULL) |
| 3120 | goto onError; |
| 3121 | unicode = PyCodec_Decode(buffer, encoding, errors); |
| 3122 | if (unicode == NULL) |
| 3123 | goto onError; |
| 3124 | if (!PyUnicode_Check(unicode)) { |
| 3125 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3126 | "decoder did not return a str object (type=%.400s)", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 3127 | Py_TYPE(unicode)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3128 | Py_DECREF(unicode); |
| 3129 | goto onError; |
| 3130 | } |
| 3131 | Py_DECREF(buffer); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 3132 | return unicode_result(unicode); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3133 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3134 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3135 | Py_XDECREF(buffer); |
| 3136 | return NULL; |
| 3137 | } |
| 3138 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3139 | PyObject * |
| 3140 | PyUnicode_AsDecodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3141 | const char *encoding, |
| 3142 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3143 | { |
| 3144 | PyObject *v; |
| 3145 | |
| 3146 | if (!PyUnicode_Check(unicode)) { |
| 3147 | PyErr_BadArgument(); |
| 3148 | goto onError; |
| 3149 | } |
| 3150 | |
| 3151 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3152 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3153 | |
| 3154 | /* Decode via the codec registry */ |
| 3155 | v = PyCodec_Decode(unicode, encoding, errors); |
| 3156 | if (v == NULL) |
| 3157 | goto onError; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 3158 | return unicode_result(v); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3159 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3160 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3161 | return NULL; |
| 3162 | } |
| 3163 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3164 | PyObject * |
| 3165 | PyUnicode_AsDecodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3166 | const char *encoding, |
| 3167 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3168 | { |
| 3169 | PyObject *v; |
| 3170 | |
| 3171 | if (!PyUnicode_Check(unicode)) { |
| 3172 | PyErr_BadArgument(); |
| 3173 | goto onError; |
| 3174 | } |
| 3175 | |
| 3176 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3177 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3178 | |
| 3179 | /* Decode via the codec registry */ |
| 3180 | v = PyCodec_Decode(unicode, encoding, errors); |
| 3181 | if (v == NULL) |
| 3182 | goto onError; |
| 3183 | if (!PyUnicode_Check(v)) { |
| 3184 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3185 | "decoder did not return a str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3186 | Py_TYPE(v)->tp_name); |
| 3187 | Py_DECREF(v); |
| 3188 | goto onError; |
| 3189 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 3190 | return unicode_result(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3191 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3192 | onError: |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3193 | return NULL; |
| 3194 | } |
| 3195 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3196 | PyObject * |
| 3197 | PyUnicode_Encode(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3198 | Py_ssize_t size, |
| 3199 | const char *encoding, |
| 3200 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3201 | { |
| 3202 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3203 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3204 | unicode = PyUnicode_FromUnicode(s, size); |
| 3205 | if (unicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3206 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3207 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 3208 | Py_DECREF(unicode); |
| 3209 | return v; |
| 3210 | } |
| 3211 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3212 | PyObject * |
| 3213 | PyUnicode_AsEncodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3214 | const char *encoding, |
| 3215 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3216 | { |
| 3217 | PyObject *v; |
| 3218 | |
| 3219 | if (!PyUnicode_Check(unicode)) { |
| 3220 | PyErr_BadArgument(); |
| 3221 | goto onError; |
| 3222 | } |
| 3223 | |
| 3224 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3225 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3226 | |
| 3227 | /* Encode via the codec registry */ |
| 3228 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3229 | if (v == NULL) |
| 3230 | goto onError; |
| 3231 | return v; |
| 3232 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3233 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3234 | return NULL; |
| 3235 | } |
| 3236 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3237 | static size_t |
| 3238 | wcstombs_errorpos(const wchar_t *wstr) |
| 3239 | { |
| 3240 | size_t len; |
| 3241 | #if SIZEOF_WCHAR_T == 2 |
| 3242 | wchar_t buf[3]; |
| 3243 | #else |
| 3244 | wchar_t buf[2]; |
| 3245 | #endif |
| 3246 | char outbuf[MB_LEN_MAX]; |
| 3247 | const wchar_t *start, *previous; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3248 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3249 | #if SIZEOF_WCHAR_T == 2 |
| 3250 | buf[2] = 0; |
| 3251 | #else |
| 3252 | buf[1] = 0; |
| 3253 | #endif |
| 3254 | start = wstr; |
| 3255 | while (*wstr != L'\0') |
| 3256 | { |
| 3257 | previous = wstr; |
| 3258 | #if SIZEOF_WCHAR_T == 2 |
| 3259 | if (Py_UNICODE_IS_HIGH_SURROGATE(wstr[0]) |
| 3260 | && Py_UNICODE_IS_LOW_SURROGATE(wstr[1])) |
| 3261 | { |
| 3262 | buf[0] = wstr[0]; |
| 3263 | buf[1] = wstr[1]; |
| 3264 | wstr += 2; |
| 3265 | } |
| 3266 | else { |
| 3267 | buf[0] = *wstr; |
| 3268 | buf[1] = 0; |
| 3269 | wstr++; |
| 3270 | } |
| 3271 | #else |
| 3272 | buf[0] = *wstr; |
| 3273 | wstr++; |
| 3274 | #endif |
| 3275 | len = wcstombs(outbuf, buf, sizeof(outbuf)); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3276 | if (len == (size_t)-1) |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3277 | return previous - start; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3278 | } |
| 3279 | |
| 3280 | /* failed to find the unencodable character */ |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3281 | return 0; |
| 3282 | } |
| 3283 | |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3284 | static int |
| 3285 | locale_error_handler(const char *errors, int *surrogateescape) |
| 3286 | { |
| 3287 | if (errors == NULL) { |
| 3288 | *surrogateescape = 0; |
| 3289 | return 0; |
| 3290 | } |
| 3291 | |
| 3292 | if (strcmp(errors, "strict") == 0) { |
| 3293 | *surrogateescape = 0; |
| 3294 | return 0; |
| 3295 | } |
| 3296 | if (strcmp(errors, "surrogateescape") == 0) { |
| 3297 | *surrogateescape = 1; |
| 3298 | return 0; |
| 3299 | } |
| 3300 | PyErr_Format(PyExc_ValueError, |
| 3301 | "only 'strict' and 'surrogateescape' error handlers " |
| 3302 | "are supported, not '%s'", |
| 3303 | errors); |
| 3304 | return -1; |
| 3305 | } |
| 3306 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3307 | PyObject * |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3308 | PyUnicode_EncodeLocale(PyObject *unicode, const char *errors) |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3309 | { |
| 3310 | Py_ssize_t wlen, wlen2; |
| 3311 | wchar_t *wstr; |
| 3312 | PyObject *bytes = NULL; |
| 3313 | char *errmsg; |
Victor Stinner | 1f33f2b | 2011-12-17 04:45:09 +0100 | [diff] [blame] | 3314 | PyObject *reason; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3315 | PyObject *exc; |
| 3316 | size_t error_pos; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3317 | int surrogateescape; |
| 3318 | |
| 3319 | if (locale_error_handler(errors, &surrogateescape) < 0) |
| 3320 | return NULL; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3321 | |
| 3322 | wstr = PyUnicode_AsWideCharString(unicode, &wlen); |
| 3323 | if (wstr == NULL) |
| 3324 | return NULL; |
| 3325 | |
| 3326 | wlen2 = wcslen(wstr); |
| 3327 | if (wlen2 != wlen) { |
| 3328 | PyMem_Free(wstr); |
| 3329 | PyErr_SetString(PyExc_TypeError, "embedded null character"); |
| 3330 | return NULL; |
| 3331 | } |
| 3332 | |
| 3333 | if (surrogateescape) { |
| 3334 | /* locale encoding with surrogateescape */ |
| 3335 | char *str; |
| 3336 | |
| 3337 | str = _Py_wchar2char(wstr, &error_pos); |
| 3338 | if (str == NULL) { |
| 3339 | if (error_pos == (size_t)-1) { |
| 3340 | PyErr_NoMemory(); |
| 3341 | PyMem_Free(wstr); |
| 3342 | return NULL; |
| 3343 | } |
| 3344 | else { |
| 3345 | goto encode_error; |
| 3346 | } |
| 3347 | } |
| 3348 | PyMem_Free(wstr); |
| 3349 | |
| 3350 | bytes = PyBytes_FromString(str); |
| 3351 | PyMem_Free(str); |
| 3352 | } |
| 3353 | else { |
| 3354 | size_t len, len2; |
| 3355 | |
| 3356 | len = wcstombs(NULL, wstr, 0); |
| 3357 | if (len == (size_t)-1) { |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3358 | error_pos = (size_t)-1; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3359 | goto encode_error; |
| 3360 | } |
| 3361 | |
| 3362 | bytes = PyBytes_FromStringAndSize(NULL, len); |
| 3363 | if (bytes == NULL) { |
| 3364 | PyMem_Free(wstr); |
| 3365 | return NULL; |
| 3366 | } |
| 3367 | |
| 3368 | len2 = wcstombs(PyBytes_AS_STRING(bytes), wstr, len+1); |
| 3369 | if (len2 == (size_t)-1 || len2 > len) { |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3370 | error_pos = (size_t)-1; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3371 | goto encode_error; |
| 3372 | } |
| 3373 | PyMem_Free(wstr); |
| 3374 | } |
| 3375 | return bytes; |
| 3376 | |
| 3377 | encode_error: |
| 3378 | errmsg = strerror(errno); |
| 3379 | assert(errmsg != NULL); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3380 | |
| 3381 | if (error_pos == (size_t)-1) |
| 3382 | error_pos = wcstombs_errorpos(wstr); |
| 3383 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3384 | PyMem_Free(wstr); |
| 3385 | Py_XDECREF(bytes); |
| 3386 | |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3387 | if (errmsg != NULL) { |
| 3388 | size_t errlen; |
| 3389 | wstr = _Py_char2wchar(errmsg, &errlen); |
| 3390 | if (wstr != NULL) { |
| 3391 | reason = PyUnicode_FromWideChar(wstr, errlen); |
| 3392 | PyMem_Free(wstr); |
| 3393 | } else |
| 3394 | errmsg = NULL; |
| 3395 | } |
| 3396 | if (errmsg == NULL) |
Victor Stinner | 1f33f2b | 2011-12-17 04:45:09 +0100 | [diff] [blame] | 3397 | reason = PyUnicode_FromString( |
| 3398 | "wcstombs() encountered an unencodable " |
| 3399 | "wide character"); |
| 3400 | if (reason == NULL) |
| 3401 | return NULL; |
| 3402 | |
| 3403 | exc = PyObject_CallFunction(PyExc_UnicodeEncodeError, "sOnnO", |
| 3404 | "locale", unicode, |
| 3405 | (Py_ssize_t)error_pos, |
| 3406 | (Py_ssize_t)(error_pos+1), |
| 3407 | reason); |
| 3408 | Py_DECREF(reason); |
| 3409 | if (exc != NULL) { |
| 3410 | PyCodec_StrictErrors(exc); |
| 3411 | Py_XDECREF(exc); |
| 3412 | } |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3413 | return NULL; |
| 3414 | } |
| 3415 | |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3416 | PyObject * |
| 3417 | PyUnicode_EncodeFSDefault(PyObject *unicode) |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3418 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3419 | #ifdef HAVE_MBCS |
Victor Stinner | ac931b1 | 2011-11-20 18:27:03 +0100 | [diff] [blame] | 3420 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, NULL); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3421 | #elif defined(__APPLE__) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3422 | return _PyUnicode_AsUTF8String(unicode, "surrogateescape"); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3423 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 3424 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 3425 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 3426 | cannot use it to encode and decode filenames before it is loaded. Load |
| 3427 | the Python codec requires to encode at least its own filename. Use the C |
| 3428 | version of the locale codec until the codec registry is initialized and |
| 3429 | the Python codec is loaded. |
| 3430 | |
| 3431 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 3432 | cannot only rely on it: check also interp->fscodec_initialized for |
| 3433 | subinterpreters. */ |
| 3434 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3435 | return PyUnicode_AsEncodedString(unicode, |
| 3436 | Py_FileSystemDefaultEncoding, |
| 3437 | "surrogateescape"); |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 3438 | } |
| 3439 | else { |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3440 | return PyUnicode_EncodeLocale(unicode, "surrogateescape"); |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 3441 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3442 | #endif |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3443 | } |
| 3444 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3445 | PyObject * |
| 3446 | PyUnicode_AsEncodedString(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3447 | const char *encoding, |
| 3448 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3449 | { |
| 3450 | PyObject *v; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3451 | char lower[11]; /* Enough for any encoding shortcut */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3452 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3453 | if (!PyUnicode_Check(unicode)) { |
| 3454 | PyErr_BadArgument(); |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3455 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3456 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3457 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3458 | /* Shortcuts for common default encodings */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3459 | if (normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3460 | if ((strcmp(lower, "utf-8") == 0) || |
| 3461 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3462 | { |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3463 | if (errors == NULL || strcmp(errors, "strict") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3464 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3465 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3466 | return _PyUnicode_AsUTF8String(unicode, errors); |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3467 | } |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3468 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3469 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3470 | (strcmp(lower, "iso-8859-1") == 0)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3471 | return _PyUnicode_AsLatin1String(unicode, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3472 | #ifdef HAVE_MBCS |
Victor Stinner | ac931b1 | 2011-11-20 18:27:03 +0100 | [diff] [blame] | 3473 | else if (strcmp(lower, "mbcs") == 0) |
| 3474 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 3475 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3476 | else if (strcmp(lower, "ascii") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3477 | return _PyUnicode_AsASCIIString(unicode, errors); |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3478 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3479 | |
| 3480 | /* Encode via the codec registry */ |
| 3481 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3482 | if (v == NULL) |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3483 | return NULL; |
| 3484 | |
| 3485 | /* The normal path */ |
| 3486 | if (PyBytes_Check(v)) |
| 3487 | return v; |
| 3488 | |
| 3489 | /* 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] | 3490 | if (PyByteArray_Check(v)) { |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3491 | int error; |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3492 | PyObject *b; |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3493 | |
| 3494 | error = PyErr_WarnFormat(PyExc_RuntimeWarning, 1, |
| 3495 | "encoder %s returned bytearray instead of bytes", |
| 3496 | encoding); |
| 3497 | if (error) { |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3498 | Py_DECREF(v); |
| 3499 | return NULL; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3500 | } |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3501 | |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3502 | b = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(v), Py_SIZE(v)); |
| 3503 | Py_DECREF(v); |
| 3504 | return b; |
| 3505 | } |
| 3506 | |
| 3507 | PyErr_Format(PyExc_TypeError, |
| 3508 | "encoder did not return a bytes object (type=%.400s)", |
| 3509 | Py_TYPE(v)->tp_name); |
| 3510 | Py_DECREF(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3511 | return NULL; |
| 3512 | } |
| 3513 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3514 | PyObject * |
| 3515 | PyUnicode_AsEncodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3516 | const char *encoding, |
| 3517 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3518 | { |
| 3519 | PyObject *v; |
| 3520 | |
| 3521 | if (!PyUnicode_Check(unicode)) { |
| 3522 | PyErr_BadArgument(); |
| 3523 | goto onError; |
| 3524 | } |
| 3525 | |
| 3526 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3527 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3528 | |
| 3529 | /* Encode via the codec registry */ |
| 3530 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3531 | if (v == NULL) |
| 3532 | goto onError; |
| 3533 | if (!PyUnicode_Check(v)) { |
| 3534 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3535 | "encoder did not return an str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3536 | Py_TYPE(v)->tp_name); |
| 3537 | Py_DECREF(v); |
| 3538 | goto onError; |
| 3539 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3540 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3541 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3542 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3543 | return NULL; |
| 3544 | } |
| 3545 | |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3546 | static size_t |
| 3547 | mbstowcs_errorpos(const char *str, size_t len) |
| 3548 | { |
| 3549 | #ifdef HAVE_MBRTOWC |
| 3550 | const char *start = str; |
| 3551 | mbstate_t mbs; |
| 3552 | size_t converted; |
| 3553 | wchar_t ch; |
| 3554 | |
| 3555 | memset(&mbs, 0, sizeof mbs); |
| 3556 | while (len) |
| 3557 | { |
| 3558 | converted = mbrtowc(&ch, (char*)str, len, &mbs); |
| 3559 | if (converted == 0) |
| 3560 | /* Reached end of string */ |
| 3561 | break; |
| 3562 | if (converted == (size_t)-1 || converted == (size_t)-2) { |
| 3563 | /* Conversion error or incomplete character */ |
| 3564 | return str - start; |
| 3565 | } |
| 3566 | else { |
| 3567 | str += converted; |
| 3568 | len -= converted; |
| 3569 | } |
| 3570 | } |
| 3571 | /* failed to find the undecodable byte sequence */ |
| 3572 | return 0; |
| 3573 | #endif |
| 3574 | return 0; |
| 3575 | } |
| 3576 | |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3577 | PyObject* |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3578 | PyUnicode_DecodeLocaleAndSize(const char *str, Py_ssize_t len, |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3579 | const char *errors) |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3580 | { |
| 3581 | wchar_t smallbuf[256]; |
| 3582 | size_t smallbuf_len = Py_ARRAY_LENGTH(smallbuf); |
| 3583 | wchar_t *wstr; |
| 3584 | size_t wlen, wlen2; |
| 3585 | PyObject *unicode; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3586 | int surrogateescape; |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3587 | size_t error_pos; |
| 3588 | char *errmsg; |
| 3589 | PyObject *reason, *exc; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3590 | |
| 3591 | if (locale_error_handler(errors, &surrogateescape) < 0) |
| 3592 | return NULL; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3593 | |
| 3594 | if (str[len] != '\0' || len != strlen(str)) { |
| 3595 | PyErr_SetString(PyExc_TypeError, "embedded null character"); |
| 3596 | return NULL; |
| 3597 | } |
| 3598 | |
| 3599 | if (surrogateescape) |
| 3600 | { |
| 3601 | wstr = _Py_char2wchar(str, &wlen); |
| 3602 | if (wstr == NULL) { |
| 3603 | if (wlen == (size_t)-1) |
| 3604 | PyErr_NoMemory(); |
| 3605 | else |
| 3606 | PyErr_SetFromErrno(PyExc_OSError); |
| 3607 | return NULL; |
| 3608 | } |
| 3609 | |
| 3610 | unicode = PyUnicode_FromWideChar(wstr, wlen); |
| 3611 | PyMem_Free(wstr); |
| 3612 | } |
| 3613 | else { |
| 3614 | #ifndef HAVE_BROKEN_MBSTOWCS |
| 3615 | wlen = mbstowcs(NULL, str, 0); |
| 3616 | #else |
| 3617 | wlen = len; |
| 3618 | #endif |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3619 | if (wlen == (size_t)-1) |
| 3620 | goto decode_error; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3621 | if (wlen+1 <= smallbuf_len) { |
| 3622 | wstr = smallbuf; |
| 3623 | } |
| 3624 | else { |
| 3625 | if (wlen > PY_SSIZE_T_MAX / sizeof(wchar_t) - 1) |
| 3626 | return PyErr_NoMemory(); |
| 3627 | |
| 3628 | wstr = PyMem_Malloc((wlen+1) * sizeof(wchar_t)); |
| 3629 | if (!wstr) |
| 3630 | return PyErr_NoMemory(); |
| 3631 | } |
| 3632 | |
| 3633 | /* This shouldn't fail now */ |
| 3634 | wlen2 = mbstowcs(wstr, str, wlen+1); |
| 3635 | if (wlen2 == (size_t)-1) { |
| 3636 | if (wstr != smallbuf) |
| 3637 | PyMem_Free(wstr); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3638 | goto decode_error; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3639 | } |
| 3640 | #ifdef HAVE_BROKEN_MBSTOWCS |
| 3641 | assert(wlen2 == wlen); |
| 3642 | #endif |
| 3643 | unicode = PyUnicode_FromWideChar(wstr, wlen2); |
| 3644 | if (wstr != smallbuf) |
| 3645 | PyMem_Free(wstr); |
| 3646 | } |
| 3647 | return unicode; |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3648 | |
| 3649 | decode_error: |
| 3650 | errmsg = strerror(errno); |
| 3651 | assert(errmsg != NULL); |
| 3652 | |
| 3653 | error_pos = mbstowcs_errorpos(str, len); |
| 3654 | if (errmsg != NULL) { |
| 3655 | size_t errlen; |
| 3656 | wstr = _Py_char2wchar(errmsg, &errlen); |
| 3657 | if (wstr != NULL) { |
| 3658 | reason = PyUnicode_FromWideChar(wstr, errlen); |
| 3659 | PyMem_Free(wstr); |
| 3660 | } else |
| 3661 | errmsg = NULL; |
| 3662 | } |
| 3663 | if (errmsg == NULL) |
| 3664 | reason = PyUnicode_FromString( |
| 3665 | "mbstowcs() encountered an invalid multibyte sequence"); |
| 3666 | if (reason == NULL) |
| 3667 | return NULL; |
| 3668 | |
| 3669 | exc = PyObject_CallFunction(PyExc_UnicodeDecodeError, "sy#nnO", |
| 3670 | "locale", str, len, |
| 3671 | (Py_ssize_t)error_pos, |
| 3672 | (Py_ssize_t)(error_pos+1), |
| 3673 | reason); |
| 3674 | Py_DECREF(reason); |
| 3675 | if (exc != NULL) { |
| 3676 | PyCodec_StrictErrors(exc); |
| 3677 | Py_XDECREF(exc); |
| 3678 | } |
| 3679 | return NULL; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3680 | } |
| 3681 | |
| 3682 | PyObject* |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3683 | PyUnicode_DecodeLocale(const char *str, const char *errors) |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3684 | { |
| 3685 | Py_ssize_t size = (Py_ssize_t)strlen(str); |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3686 | return PyUnicode_DecodeLocaleAndSize(str, size, errors); |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3687 | } |
| 3688 | |
| 3689 | |
| 3690 | PyObject* |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3691 | PyUnicode_DecodeFSDefault(const char *s) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3692 | Py_ssize_t size = (Py_ssize_t)strlen(s); |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3693 | return PyUnicode_DecodeFSDefaultAndSize(s, size); |
| 3694 | } |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3695 | |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3696 | PyObject* |
| 3697 | PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size) |
| 3698 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3699 | #ifdef HAVE_MBCS |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3700 | return PyUnicode_DecodeMBCS(s, size, NULL); |
| 3701 | #elif defined(__APPLE__) |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 3702 | return PyUnicode_DecodeUTF8Stateful(s, size, "surrogateescape", NULL); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3703 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 3704 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 3705 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 3706 | cannot use it to encode and decode filenames before it is loaded. Load |
| 3707 | the Python codec requires to encode at least its own filename. Use the C |
| 3708 | version of the locale codec until the codec registry is initialized and |
| 3709 | the Python codec is loaded. |
| 3710 | |
| 3711 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 3712 | cannot only rely on it: check also interp->fscodec_initialized for |
| 3713 | subinterpreters. */ |
| 3714 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3715 | return PyUnicode_Decode(s, size, |
| 3716 | Py_FileSystemDefaultEncoding, |
Victor Stinner | b9a20ad | 2010-04-30 16:37:52 +0000 | [diff] [blame] | 3717 | "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3718 | } |
| 3719 | else { |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3720 | return PyUnicode_DecodeLocaleAndSize(s, size, "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3721 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3722 | #endif |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3723 | } |
| 3724 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3725 | |
| 3726 | int |
Antoine Pitrou | 1334884 | 2012-01-29 18:36:34 +0100 | [diff] [blame] | 3727 | _PyUnicode_HasNULChars(PyObject* s) |
| 3728 | { |
| 3729 | static PyObject *nul = NULL; |
| 3730 | |
| 3731 | if (nul == NULL) |
| 3732 | nul = PyUnicode_FromStringAndSize("\0", 1); |
| 3733 | if (nul == NULL) |
| 3734 | return -1; |
| 3735 | return PyUnicode_Contains(s, nul); |
| 3736 | } |
| 3737 | |
| 3738 | |
| 3739 | int |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3740 | PyUnicode_FSConverter(PyObject* arg, void* addr) |
| 3741 | { |
| 3742 | PyObject *output = NULL; |
| 3743 | Py_ssize_t size; |
| 3744 | void *data; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3745 | if (arg == NULL) { |
| 3746 | Py_DECREF(*(PyObject**)addr); |
| 3747 | return 1; |
| 3748 | } |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 3749 | if (PyBytes_Check(arg)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3750 | output = arg; |
| 3751 | Py_INCREF(output); |
| 3752 | } |
| 3753 | else { |
| 3754 | arg = PyUnicode_FromObject(arg); |
| 3755 | if (!arg) |
| 3756 | return 0; |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3757 | output = PyUnicode_EncodeFSDefault(arg); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3758 | Py_DECREF(arg); |
| 3759 | if (!output) |
| 3760 | return 0; |
| 3761 | if (!PyBytes_Check(output)) { |
| 3762 | Py_DECREF(output); |
| 3763 | PyErr_SetString(PyExc_TypeError, "encoder failed to return bytes"); |
| 3764 | return 0; |
| 3765 | } |
| 3766 | } |
Victor Stinner | 0ea2a46 | 2010-04-30 00:22:08 +0000 | [diff] [blame] | 3767 | size = PyBytes_GET_SIZE(output); |
| 3768 | data = PyBytes_AS_STRING(output); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3769 | if (size != strlen(data)) { |
Benjamin Peterson | 7a6b44a | 2011-08-18 13:51:47 -0500 | [diff] [blame] | 3770 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3771 | Py_DECREF(output); |
| 3772 | return 0; |
| 3773 | } |
| 3774 | *(PyObject**)addr = output; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3775 | return Py_CLEANUP_SUPPORTED; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3776 | } |
| 3777 | |
| 3778 | |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3779 | int |
| 3780 | PyUnicode_FSDecoder(PyObject* arg, void* addr) |
| 3781 | { |
| 3782 | PyObject *output = NULL; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3783 | if (arg == NULL) { |
| 3784 | Py_DECREF(*(PyObject**)addr); |
| 3785 | return 1; |
| 3786 | } |
| 3787 | if (PyUnicode_Check(arg)) { |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 3788 | if (PyUnicode_READY(arg) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3789 | return 0; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3790 | output = arg; |
| 3791 | Py_INCREF(output); |
| 3792 | } |
| 3793 | else { |
| 3794 | arg = PyBytes_FromObject(arg); |
| 3795 | if (!arg) |
| 3796 | return 0; |
| 3797 | output = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AS_STRING(arg), |
| 3798 | PyBytes_GET_SIZE(arg)); |
| 3799 | Py_DECREF(arg); |
| 3800 | if (!output) |
| 3801 | return 0; |
| 3802 | if (!PyUnicode_Check(output)) { |
| 3803 | Py_DECREF(output); |
| 3804 | PyErr_SetString(PyExc_TypeError, "decoder failed to return unicode"); |
| 3805 | return 0; |
| 3806 | } |
| 3807 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 3808 | if (PyUnicode_READY(output) == -1) { |
Victor Stinner | 065836e | 2011-10-27 01:56:33 +0200 | [diff] [blame] | 3809 | Py_DECREF(output); |
| 3810 | return 0; |
| 3811 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3812 | if (findchar(PyUnicode_DATA(output), PyUnicode_KIND(output), |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 3813 | PyUnicode_GET_LENGTH(output), 0, 1) >= 0) { |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3814 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 3815 | Py_DECREF(output); |
| 3816 | return 0; |
| 3817 | } |
| 3818 | *(PyObject**)addr = output; |
| 3819 | return Py_CLEANUP_SUPPORTED; |
| 3820 | } |
| 3821 | |
| 3822 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3823 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3824 | PyUnicode_AsUTF8AndSize(PyObject *unicode, Py_ssize_t *psize) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3825 | { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 3826 | PyObject *bytes; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3827 | |
Neal Norwitz | e0a0a6e | 2007-08-25 01:04:21 +0000 | [diff] [blame] | 3828 | if (!PyUnicode_Check(unicode)) { |
| 3829 | PyErr_BadArgument(); |
| 3830 | return NULL; |
| 3831 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3832 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3833 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3834 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3835 | if (PyUnicode_UTF8(unicode) == NULL) { |
| 3836 | assert(!PyUnicode_IS_COMPACT_ASCII(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3837 | bytes = _PyUnicode_AsUTF8String(unicode, "strict"); |
| 3838 | if (bytes == NULL) |
| 3839 | return NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3840 | _PyUnicode_UTF8(unicode) = PyObject_MALLOC(PyBytes_GET_SIZE(bytes) + 1); |
| 3841 | if (_PyUnicode_UTF8(unicode) == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3842 | Py_DECREF(bytes); |
| 3843 | return NULL; |
| 3844 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3845 | _PyUnicode_UTF8_LENGTH(unicode) = PyBytes_GET_SIZE(bytes); |
| 3846 | Py_MEMCPY(_PyUnicode_UTF8(unicode), |
| 3847 | PyBytes_AS_STRING(bytes), |
| 3848 | _PyUnicode_UTF8_LENGTH(unicode) + 1); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3849 | Py_DECREF(bytes); |
| 3850 | } |
| 3851 | |
| 3852 | if (psize) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3853 | *psize = PyUnicode_UTF8_LENGTH(unicode); |
| 3854 | return PyUnicode_UTF8(unicode); |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3855 | } |
| 3856 | |
| 3857 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3858 | PyUnicode_AsUTF8(PyObject *unicode) |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3859 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3860 | return PyUnicode_AsUTF8AndSize(unicode, NULL); |
| 3861 | } |
| 3862 | |
| 3863 | #ifdef Py_DEBUG |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 3864 | static int unicode_as_unicode_calls = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3865 | #endif |
| 3866 | |
| 3867 | |
| 3868 | Py_UNICODE * |
| 3869 | PyUnicode_AsUnicodeAndSize(PyObject *unicode, Py_ssize_t *size) |
| 3870 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3871 | const unsigned char *one_byte; |
| 3872 | #if SIZEOF_WCHAR_T == 4 |
| 3873 | const Py_UCS2 *two_bytes; |
| 3874 | #else |
| 3875 | const Py_UCS4 *four_bytes; |
| 3876 | const Py_UCS4 *ucs4_end; |
| 3877 | Py_ssize_t num_surrogates; |
| 3878 | #endif |
| 3879 | wchar_t *w; |
| 3880 | wchar_t *wchar_end; |
| 3881 | |
| 3882 | if (!PyUnicode_Check(unicode)) { |
| 3883 | PyErr_BadArgument(); |
| 3884 | return NULL; |
| 3885 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3886 | if (_PyUnicode_WSTR(unicode) == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3887 | /* Non-ASCII compact unicode object */ |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3888 | assert(_PyUnicode_KIND(unicode) != 0); |
| 3889 | assert(PyUnicode_IS_READY(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3890 | |
| 3891 | #ifdef Py_DEBUG |
| 3892 | ++unicode_as_unicode_calls; |
| 3893 | #endif |
| 3894 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3895 | if (PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3896 | #if SIZEOF_WCHAR_T == 2 |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3897 | four_bytes = PyUnicode_4BYTE_DATA(unicode); |
| 3898 | ucs4_end = four_bytes + _PyUnicode_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3899 | num_surrogates = 0; |
| 3900 | |
| 3901 | for (; four_bytes < ucs4_end; ++four_bytes) { |
| 3902 | if (*four_bytes > 0xFFFF) |
| 3903 | ++num_surrogates; |
| 3904 | } |
| 3905 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3906 | _PyUnicode_WSTR(unicode) = (wchar_t *) PyObject_MALLOC( |
| 3907 | sizeof(wchar_t) * (_PyUnicode_LENGTH(unicode) + 1 + num_surrogates)); |
| 3908 | if (!_PyUnicode_WSTR(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3909 | PyErr_NoMemory(); |
| 3910 | return NULL; |
| 3911 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3912 | _PyUnicode_WSTR_LENGTH(unicode) = _PyUnicode_LENGTH(unicode) + num_surrogates; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3913 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3914 | w = _PyUnicode_WSTR(unicode); |
| 3915 | wchar_end = w + _PyUnicode_WSTR_LENGTH(unicode); |
| 3916 | four_bytes = PyUnicode_4BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3917 | for (; four_bytes < ucs4_end; ++four_bytes, ++w) { |
| 3918 | if (*four_bytes > 0xFFFF) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 3919 | assert(*four_bytes <= MAX_UNICODE); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3920 | /* encode surrogate pair in this case */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 3921 | *w++ = Py_UNICODE_HIGH_SURROGATE(*four_bytes); |
| 3922 | *w = Py_UNICODE_LOW_SURROGATE(*four_bytes); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3923 | } |
| 3924 | else |
| 3925 | *w = *four_bytes; |
| 3926 | |
| 3927 | if (w > wchar_end) { |
| 3928 | assert(0 && "Miscalculated string end"); |
| 3929 | } |
| 3930 | } |
| 3931 | *w = 0; |
| 3932 | #else |
| 3933 | /* sizeof(wchar_t) == 4 */ |
| 3934 | Py_FatalError("Impossible unicode object state, wstr and str " |
| 3935 | "should share memory already."); |
| 3936 | return NULL; |
| 3937 | #endif |
| 3938 | } |
| 3939 | else { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3940 | _PyUnicode_WSTR(unicode) = (wchar_t *) PyObject_MALLOC(sizeof(wchar_t) * |
| 3941 | (_PyUnicode_LENGTH(unicode) + 1)); |
| 3942 | if (!_PyUnicode_WSTR(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3943 | PyErr_NoMemory(); |
| 3944 | return NULL; |
| 3945 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3946 | if (!PyUnicode_IS_COMPACT_ASCII(unicode)) |
| 3947 | _PyUnicode_WSTR_LENGTH(unicode) = _PyUnicode_LENGTH(unicode); |
| 3948 | w = _PyUnicode_WSTR(unicode); |
| 3949 | wchar_end = w + _PyUnicode_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3950 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3951 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) { |
| 3952 | one_byte = PyUnicode_1BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3953 | for (; w < wchar_end; ++one_byte, ++w) |
| 3954 | *w = *one_byte; |
| 3955 | /* null-terminate the wstr */ |
| 3956 | *w = 0; |
| 3957 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3958 | else if (PyUnicode_KIND(unicode) == PyUnicode_2BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3959 | #if SIZEOF_WCHAR_T == 4 |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3960 | two_bytes = PyUnicode_2BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3961 | for (; w < wchar_end; ++two_bytes, ++w) |
| 3962 | *w = *two_bytes; |
| 3963 | /* null-terminate the wstr */ |
| 3964 | *w = 0; |
| 3965 | #else |
| 3966 | /* sizeof(wchar_t) == 2 */ |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3967 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 3968 | _PyUnicode_WSTR(unicode) = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3969 | Py_FatalError("Impossible unicode object state, wstr " |
| 3970 | "and str should share memory already."); |
| 3971 | return NULL; |
| 3972 | #endif |
| 3973 | } |
| 3974 | else { |
| 3975 | assert(0 && "This should never happen."); |
| 3976 | } |
| 3977 | } |
| 3978 | } |
| 3979 | if (size != NULL) |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3980 | *size = PyUnicode_WSTR_LENGTH(unicode); |
| 3981 | return _PyUnicode_WSTR(unicode); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3982 | } |
| 3983 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3984 | Py_UNICODE * |
| 3985 | PyUnicode_AsUnicode(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3986 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3987 | return PyUnicode_AsUnicodeAndSize(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3988 | } |
| 3989 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3990 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3991 | Py_ssize_t |
| 3992 | PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3993 | { |
| 3994 | if (!PyUnicode_Check(unicode)) { |
| 3995 | PyErr_BadArgument(); |
| 3996 | goto onError; |
| 3997 | } |
| 3998 | return PyUnicode_GET_SIZE(unicode); |
| 3999 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4000 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4001 | return -1; |
| 4002 | } |
| 4003 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4004 | Py_ssize_t |
| 4005 | PyUnicode_GetLength(PyObject *unicode) |
| 4006 | { |
Victor Stinner | 5a706cf | 2011-10-02 00:36:53 +0200 | [diff] [blame] | 4007 | if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4008 | PyErr_BadArgument(); |
| 4009 | return -1; |
| 4010 | } |
| 4011 | |
| 4012 | return PyUnicode_GET_LENGTH(unicode); |
| 4013 | } |
| 4014 | |
| 4015 | Py_UCS4 |
| 4016 | PyUnicode_ReadChar(PyObject *unicode, Py_ssize_t index) |
| 4017 | { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 4018 | if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) { |
| 4019 | PyErr_BadArgument(); |
| 4020 | return (Py_UCS4)-1; |
| 4021 | } |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 4022 | if (index < 0 || index >= PyUnicode_GET_LENGTH(unicode)) { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 4023 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4024 | return (Py_UCS4)-1; |
| 4025 | } |
| 4026 | return PyUnicode_READ_CHAR(unicode, index); |
| 4027 | } |
| 4028 | |
| 4029 | int |
| 4030 | PyUnicode_WriteChar(PyObject *unicode, Py_ssize_t index, Py_UCS4 ch) |
| 4031 | { |
| 4032 | if (!PyUnicode_Check(unicode) || !PyUnicode_IS_COMPACT(unicode)) { |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 4033 | PyErr_BadArgument(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4034 | return -1; |
| 4035 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 4036 | assert(PyUnicode_IS_READY(unicode)); |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 4037 | if (index < 0 || index >= PyUnicode_GET_LENGTH(unicode)) { |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 4038 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 4039 | return -1; |
| 4040 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 4041 | if (unicode_check_modifiable(unicode)) |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 4042 | return -1; |
Victor Stinner | c9590ad | 2012-03-04 01:34:37 +0100 | [diff] [blame] | 4043 | if (ch > PyUnicode_MAX_CHAR_VALUE(unicode)) { |
| 4044 | PyErr_SetString(PyExc_ValueError, "character out of range"); |
| 4045 | return -1; |
| 4046 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4047 | PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), |
| 4048 | index, ch); |
| 4049 | return 0; |
| 4050 | } |
| 4051 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4052 | const char * |
| 4053 | PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 4054 | { |
Victor Stinner | 42cb462 | 2010-09-01 19:39:01 +0000 | [diff] [blame] | 4055 | return "utf-8"; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 4056 | } |
| 4057 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4058 | /* create or adjust a UnicodeDecodeError */ |
| 4059 | static void |
| 4060 | make_decode_exception(PyObject **exceptionObject, |
| 4061 | const char *encoding, |
| 4062 | const char *input, Py_ssize_t length, |
| 4063 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 4064 | const char *reason) |
| 4065 | { |
| 4066 | if (*exceptionObject == NULL) { |
| 4067 | *exceptionObject = PyUnicodeDecodeError_Create( |
| 4068 | encoding, input, length, startpos, endpos, reason); |
| 4069 | } |
| 4070 | else { |
| 4071 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, startpos)) |
| 4072 | goto onError; |
| 4073 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, endpos)) |
| 4074 | goto onError; |
| 4075 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 4076 | goto onError; |
| 4077 | } |
| 4078 | return; |
| 4079 | |
| 4080 | onError: |
| 4081 | Py_DECREF(*exceptionObject); |
| 4082 | *exceptionObject = NULL; |
| 4083 | } |
| 4084 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4085 | /* error handling callback helper: |
| 4086 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 4087 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4088 | and adjust various state variables. |
| 4089 | return 0 on success, -1 on error |
| 4090 | */ |
| 4091 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4092 | static int |
| 4093 | unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4094 | const char *encoding, const char *reason, |
| 4095 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 4096 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4097 | PyObject **output, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4098 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 4099 | 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] | 4100 | |
| 4101 | PyObject *restuple = NULL; |
| 4102 | PyObject *repunicode = NULL; |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 4103 | Py_ssize_t outsize; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4104 | Py_ssize_t insize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4105 | Py_ssize_t requiredsize; |
| 4106 | Py_ssize_t newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4107 | PyObject *inputobj = NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4108 | int res = -1; |
| 4109 | |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 4110 | if (_PyUnicode_KIND(*output) != PyUnicode_WCHAR_KIND) |
| 4111 | outsize = PyUnicode_GET_LENGTH(*output); |
| 4112 | else |
| 4113 | outsize = _PyUnicode_WSTR_LENGTH(*output); |
| 4114 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4115 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4116 | *errorHandler = PyCodec_LookupError(errors); |
| 4117 | if (*errorHandler == NULL) |
| 4118 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4119 | } |
| 4120 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4121 | make_decode_exception(exceptionObject, |
| 4122 | encoding, |
| 4123 | *input, *inend - *input, |
| 4124 | *startinpos, *endinpos, |
| 4125 | reason); |
| 4126 | if (*exceptionObject == NULL) |
| 4127 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4128 | |
| 4129 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 4130 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4131 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4132 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 4133 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4134 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4135 | } |
| 4136 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4137 | goto onError; |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 4138 | if (PyUnicode_READY(repunicode) == -1) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4139 | goto onError; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4140 | |
| 4141 | /* Copy back the bytes variables, which might have been modified by the |
| 4142 | callback */ |
| 4143 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 4144 | if (!inputobj) |
| 4145 | goto onError; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4146 | if (!PyBytes_Check(inputobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4147 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4148 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4149 | *input = PyBytes_AS_STRING(inputobj); |
| 4150 | insize = PyBytes_GET_SIZE(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4151 | *inend = *input + insize; |
Walter Dörwald | 36f938f | 2007-08-10 10:11:43 +0000 | [diff] [blame] | 4152 | /* we can DECREF safely, as the exception has another reference, |
| 4153 | so the object won't go away. */ |
| 4154 | Py_DECREF(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4155 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4156 | if (newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4157 | newpos = insize+newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4158 | if (newpos<0 || newpos>insize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4159 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos); |
| 4160 | goto onError; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4161 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4162 | |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 4163 | if (_PyUnicode_KIND(*output) != PyUnicode_WCHAR_KIND) { |
| 4164 | /* need more space? (at least enough for what we |
| 4165 | have+the replacement+the rest of the string (starting |
| 4166 | at the new input position), so we won't have to check space |
| 4167 | when there are no errors in the rest of the string) */ |
| 4168 | Py_ssize_t replen = PyUnicode_GET_LENGTH(repunicode); |
| 4169 | requiredsize = *outpos + replen + insize-newpos; |
| 4170 | if (requiredsize > outsize) { |
| 4171 | if (requiredsize<2*outsize) |
| 4172 | requiredsize = 2*outsize; |
| 4173 | if (unicode_resize(output, requiredsize) < 0) |
| 4174 | goto onError; |
| 4175 | } |
Victor Stinner | 1b487b4 | 2012-05-03 12:29:04 +0200 | [diff] [blame] | 4176 | if (unicode_widen(output, *outpos, |
| 4177 | PyUnicode_MAX_CHAR_VALUE(repunicode)) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4178 | goto onError; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 4179 | _PyUnicode_FastCopyCharacters(*output, *outpos, repunicode, 0, replen); |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 4180 | *outpos += replen; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4181 | } |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 4182 | else { |
| 4183 | wchar_t *repwstr; |
| 4184 | Py_ssize_t repwlen; |
| 4185 | repwstr = PyUnicode_AsUnicodeAndSize(repunicode, &repwlen); |
| 4186 | if (repwstr == NULL) |
| 4187 | goto onError; |
| 4188 | /* need more space? (at least enough for what we |
| 4189 | have+the replacement+the rest of the string (starting |
| 4190 | at the new input position), so we won't have to check space |
| 4191 | when there are no errors in the rest of the string) */ |
| 4192 | requiredsize = *outpos + repwlen + insize-newpos; |
| 4193 | if (requiredsize > outsize) { |
| 4194 | if (requiredsize < 2*outsize) |
| 4195 | requiredsize = 2*outsize; |
| 4196 | if (unicode_resize(output, requiredsize) < 0) |
| 4197 | goto onError; |
| 4198 | } |
| 4199 | wcsncpy(_PyUnicode_WSTR(*output) + *outpos, repwstr, repwlen); |
| 4200 | *outpos += repwlen; |
| 4201 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4202 | *endinpos = newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4203 | *inptr = *input + newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4204 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4205 | /* we made it! */ |
| 4206 | res = 0; |
| 4207 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4208 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4209 | Py_XDECREF(restuple); |
| 4210 | return res; |
| 4211 | } |
| 4212 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4213 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 4214 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4215 | /* See RFC2152 for details. We encode conservatively and decode liberally. */ |
| 4216 | |
| 4217 | /* Three simple macros defining base-64. */ |
| 4218 | |
| 4219 | /* Is c a base-64 character? */ |
| 4220 | |
| 4221 | #define IS_BASE64(c) \ |
| 4222 | (((c) >= 'A' && (c) <= 'Z') || \ |
| 4223 | ((c) >= 'a' && (c) <= 'z') || \ |
| 4224 | ((c) >= '0' && (c) <= '9') || \ |
| 4225 | (c) == '+' || (c) == '/') |
| 4226 | |
| 4227 | /* given that c is a base-64 character, what is its base-64 value? */ |
| 4228 | |
| 4229 | #define FROM_BASE64(c) \ |
| 4230 | (((c) >= 'A' && (c) <= 'Z') ? (c) - 'A' : \ |
| 4231 | ((c) >= 'a' && (c) <= 'z') ? (c) - 'a' + 26 : \ |
| 4232 | ((c) >= '0' && (c) <= '9') ? (c) - '0' + 52 : \ |
| 4233 | (c) == '+' ? 62 : 63) |
| 4234 | |
| 4235 | /* What is the base-64 character of the bottom 6 bits of n? */ |
| 4236 | |
| 4237 | #define TO_BASE64(n) \ |
| 4238 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 4239 | |
| 4240 | /* DECODE_DIRECT: this byte encountered in a UTF-7 string should be |
| 4241 | * decoded as itself. We are permissive on decoding; the only ASCII |
| 4242 | * byte not decoding to itself is the + which begins a base64 |
| 4243 | * string. */ |
| 4244 | |
| 4245 | #define DECODE_DIRECT(c) \ |
| 4246 | ((c) <= 127 && (c) != '+') |
| 4247 | |
| 4248 | /* The UTF-7 encoder treats ASCII characters differently according to |
| 4249 | * whether they are Set D, Set O, Whitespace, or special (i.e. none of |
| 4250 | * the above). See RFC2152. This array identifies these different |
| 4251 | * sets: |
| 4252 | * 0 : "Set D" |
| 4253 | * alphanumeric and '(),-./:? |
| 4254 | * 1 : "Set O" |
| 4255 | * !"#$%&*;<=>@[]^_`{|} |
| 4256 | * 2 : "whitespace" |
| 4257 | * ht nl cr sp |
| 4258 | * 3 : special (must be base64 encoded) |
| 4259 | * everything else (i.e. +\~ and non-printing codes 0-8 11-12 14-31 127) |
| 4260 | */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4261 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4262 | static |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4263 | char utf7_category[128] = { |
| 4264 | /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */ |
| 4265 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 3, 3, 2, 3, 3, |
| 4266 | /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */ |
| 4267 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 4268 | /* sp ! " # $ % & ' ( ) * + , - . / */ |
| 4269 | 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 3, 0, 0, 0, 0, |
| 4270 | /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */ |
| 4271 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, |
| 4272 | /* @ A B C D E F G H I J K L M N O */ |
| 4273 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4274 | /* P Q R S T U V W X Y Z [ \ ] ^ _ */ |
| 4275 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 1, 1, 1, |
| 4276 | /* ` a b c d e f g h i j k l m n o */ |
| 4277 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4278 | /* p q r s t u v w x y z { | } ~ del */ |
| 4279 | 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] | 4280 | }; |
| 4281 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4282 | /* ENCODE_DIRECT: this character should be encoded as itself. The |
| 4283 | * answer depends on whether we are encoding set O as itself, and also |
| 4284 | * on whether we are encoding whitespace as itself. RFC2152 makes it |
| 4285 | * clear that the answers to these questions vary between |
| 4286 | * applications, so this code needs to be flexible. */ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 4287 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4288 | #define ENCODE_DIRECT(c, directO, directWS) \ |
| 4289 | ((c) < 128 && (c) > 0 && \ |
| 4290 | ((utf7_category[(c)] == 0) || \ |
| 4291 | (directWS && (utf7_category[(c)] == 2)) || \ |
| 4292 | (directO && (utf7_category[(c)] == 1)))) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4293 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4294 | PyObject * |
| 4295 | PyUnicode_DecodeUTF7(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4296 | Py_ssize_t size, |
| 4297 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4298 | { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4299 | return PyUnicode_DecodeUTF7Stateful(s, size, errors, NULL); |
| 4300 | } |
| 4301 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4302 | /* The decoder. The only state we preserve is our read position, |
| 4303 | * i.e. how many characters we have consumed. So if we end in the |
| 4304 | * middle of a shift sequence we have to back off the read position |
| 4305 | * and the output to the beginning of the sequence, otherwise we lose |
| 4306 | * all the shift state (seen bits, number of bits seen, high |
| 4307 | * surrogate). */ |
| 4308 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4309 | PyObject * |
| 4310 | PyUnicode_DecodeUTF7Stateful(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4311 | Py_ssize_t size, |
| 4312 | const char *errors, |
| 4313 | Py_ssize_t *consumed) |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4314 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4315 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4316 | Py_ssize_t startinpos; |
| 4317 | Py_ssize_t endinpos; |
| 4318 | Py_ssize_t outpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4319 | const char *e; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4320 | PyObject *unicode; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4321 | const char *errmsg = ""; |
| 4322 | int inShift = 0; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4323 | Py_ssize_t shiftOutStart; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4324 | unsigned int base64bits = 0; |
| 4325 | unsigned long base64buffer = 0; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 4326 | Py_UCS4 surrogate = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4327 | PyObject *errorHandler = NULL; |
| 4328 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4329 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4330 | /* Start off assuming it's all ASCII. Widen later as necessary. */ |
| 4331 | unicode = PyUnicode_New(size, 127); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4332 | if (!unicode) |
| 4333 | return NULL; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4334 | if (size == 0) { |
| 4335 | if (consumed) |
| 4336 | *consumed = 0; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4337 | return unicode; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4338 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4339 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4340 | shiftOutStart = outpos = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4341 | e = s + size; |
| 4342 | |
| 4343 | while (s < e) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4344 | Py_UCS4 ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4345 | restart: |
Antoine Pitrou | 5ffd9e9 | 2008-07-25 18:05:24 +0000 | [diff] [blame] | 4346 | ch = (unsigned char) *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4347 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4348 | if (inShift) { /* in a base-64 section */ |
| 4349 | if (IS_BASE64(ch)) { /* consume a base-64 character */ |
| 4350 | base64buffer = (base64buffer << 6) | FROM_BASE64(ch); |
| 4351 | base64bits += 6; |
| 4352 | s++; |
| 4353 | if (base64bits >= 16) { |
| 4354 | /* we have enough bits for a UTF-16 value */ |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 4355 | Py_UCS4 outCh = (Py_UCS4)(base64buffer >> (base64bits-16)); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4356 | base64bits -= 16; |
| 4357 | base64buffer &= (1 << base64bits) - 1; /* clear high bits */ |
| 4358 | if (surrogate) { |
| 4359 | /* expecting a second surrogate */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4360 | if (Py_UNICODE_IS_LOW_SURROGATE(outCh)) { |
| 4361 | Py_UCS4 ch2 = Py_UNICODE_JOIN_SURROGATES(surrogate, outCh); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4362 | if (unicode_putchar(&unicode, &outpos, ch2) < 0) |
| 4363 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4364 | surrogate = 0; |
Antoine Pitrou | 5418ee0 | 2011-11-15 01:42:21 +0100 | [diff] [blame] | 4365 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4366 | } |
| 4367 | else { |
Antoine Pitrou | 78edf75 | 2011-11-15 01:44:16 +0100 | [diff] [blame] | 4368 | if (unicode_putchar(&unicode, &outpos, surrogate) < 0) |
| 4369 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4370 | surrogate = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4371 | } |
| 4372 | } |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4373 | if (Py_UNICODE_IS_HIGH_SURROGATE(outCh)) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4374 | /* first surrogate */ |
| 4375 | surrogate = outCh; |
| 4376 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4377 | else { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4378 | if (unicode_putchar(&unicode, &outpos, outCh) < 0) |
| 4379 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4380 | } |
| 4381 | } |
| 4382 | } |
| 4383 | else { /* now leaving a base-64 section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4384 | inShift = 0; |
| 4385 | s++; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4386 | if (surrogate) { |
Antoine Pitrou | 78edf75 | 2011-11-15 01:44:16 +0100 | [diff] [blame] | 4387 | if (unicode_putchar(&unicode, &outpos, surrogate) < 0) |
| 4388 | goto onError; |
Antoine Pitrou | 5418ee0 | 2011-11-15 01:42:21 +0100 | [diff] [blame] | 4389 | surrogate = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4390 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4391 | if (base64bits > 0) { /* left-over bits */ |
| 4392 | if (base64bits >= 6) { |
| 4393 | /* We've seen at least one base-64 character */ |
| 4394 | errmsg = "partial character in shift sequence"; |
| 4395 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4396 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4397 | else { |
| 4398 | /* Some bits remain; they should be zero */ |
| 4399 | if (base64buffer != 0) { |
| 4400 | errmsg = "non-zero padding bits in shift sequence"; |
| 4401 | goto utf7Error; |
| 4402 | } |
| 4403 | } |
| 4404 | } |
| 4405 | if (ch != '-') { |
| 4406 | /* '-' is absorbed; other terminating |
| 4407 | characters are preserved */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4408 | if (unicode_putchar(&unicode, &outpos, ch) < 0) |
| 4409 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4410 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4411 | } |
| 4412 | } |
| 4413 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4414 | startinpos = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4415 | s++; /* consume '+' */ |
| 4416 | if (s < e && *s == '-') { /* '+-' encodes '+' */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4417 | s++; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4418 | if (unicode_putchar(&unicode, &outpos, '+') < 0) |
| 4419 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4420 | } |
| 4421 | else { /* begin base64-encoded section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4422 | inShift = 1; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4423 | shiftOutStart = outpos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4424 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4425 | } |
| 4426 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4427 | else if (DECODE_DIRECT(ch)) { /* character decodes as itself */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4428 | if (unicode_putchar(&unicode, &outpos, ch) < 0) |
| 4429 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4430 | s++; |
| 4431 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4432 | else { |
| 4433 | startinpos = s-starts; |
| 4434 | s++; |
| 4435 | errmsg = "unexpected special character"; |
| 4436 | goto utf7Error; |
| 4437 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4438 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4439 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4440 | endinpos = s-starts; |
| 4441 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4442 | errors, &errorHandler, |
| 4443 | "utf7", errmsg, |
| 4444 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4445 | &unicode, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4446 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4447 | } |
| 4448 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4449 | /* end of string */ |
| 4450 | |
| 4451 | if (inShift && !consumed) { /* in shift sequence, no more to follow */ |
| 4452 | /* if we're in an inconsistent state, that's an error */ |
| 4453 | if (surrogate || |
| 4454 | (base64bits >= 6) || |
| 4455 | (base64bits > 0 && base64buffer != 0)) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4456 | endinpos = size; |
| 4457 | if (unicode_decode_call_errorhandler( |
| 4458 | errors, &errorHandler, |
| 4459 | "utf7", "unterminated shift sequence", |
| 4460 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4461 | &unicode, &outpos)) |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4462 | goto onError; |
| 4463 | if (s < e) |
| 4464 | goto restart; |
| 4465 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4466 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4467 | |
| 4468 | /* return state */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4469 | if (consumed) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4470 | if (inShift) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4471 | outpos = shiftOutStart; /* back off output */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4472 | *consumed = startinpos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4473 | } |
| 4474 | else { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4475 | *consumed = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4476 | } |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4477 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4478 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4479 | if (unicode_resize(&unicode, outpos) < 0) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4480 | goto onError; |
| 4481 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4482 | Py_XDECREF(errorHandler); |
| 4483 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 4484 | return unicode_result(unicode); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4485 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4486 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4487 | Py_XDECREF(errorHandler); |
| 4488 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4489 | Py_DECREF(unicode); |
| 4490 | return NULL; |
| 4491 | } |
| 4492 | |
| 4493 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4494 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4495 | _PyUnicode_EncodeUTF7(PyObject *str, |
| 4496 | int base64SetO, |
| 4497 | int base64WhiteSpace, |
| 4498 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4499 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4500 | int kind; |
| 4501 | void *data; |
| 4502 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4503 | PyObject *v; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4504 | Py_ssize_t allocated; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4505 | int inShift = 0; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4506 | Py_ssize_t i; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4507 | unsigned int base64bits = 0; |
| 4508 | unsigned long base64buffer = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4509 | char * out; |
| 4510 | char * start; |
| 4511 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 4512 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4513 | return NULL; |
| 4514 | kind = PyUnicode_KIND(str); |
| 4515 | data = PyUnicode_DATA(str); |
| 4516 | len = PyUnicode_GET_LENGTH(str); |
| 4517 | |
| 4518 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4519 | return PyBytes_FromStringAndSize(NULL, 0); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4520 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4521 | /* It might be possible to tighten this worst case */ |
| 4522 | allocated = 8 * len; |
| 4523 | if (allocated / 8 != len) |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4524 | return PyErr_NoMemory(); |
| 4525 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4526 | v = PyBytes_FromStringAndSize(NULL, allocated); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4527 | if (v == NULL) |
| 4528 | return NULL; |
| 4529 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4530 | start = out = PyBytes_AS_STRING(v); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4531 | for (i = 0; i < len; ++i) { |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 4532 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4533 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4534 | if (inShift) { |
| 4535 | if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 4536 | /* shifting out */ |
| 4537 | if (base64bits) { /* output remaining bits */ |
| 4538 | *out++ = TO_BASE64(base64buffer << (6-base64bits)); |
| 4539 | base64buffer = 0; |
| 4540 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4541 | } |
| 4542 | inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4543 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 4544 | so no '-' is required, except if the character is itself a '-' */ |
| 4545 | if (IS_BASE64(ch) || ch == '-') { |
| 4546 | *out++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4547 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4548 | *out++ = (char) ch; |
| 4549 | } |
| 4550 | else { |
| 4551 | goto encode_char; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4552 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4553 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4554 | else { /* not in a shift sequence */ |
| 4555 | if (ch == '+') { |
| 4556 | *out++ = '+'; |
| 4557 | *out++ = '-'; |
| 4558 | } |
| 4559 | else if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 4560 | *out++ = (char) ch; |
| 4561 | } |
| 4562 | else { |
| 4563 | *out++ = '+'; |
| 4564 | inShift = 1; |
| 4565 | goto encode_char; |
| 4566 | } |
| 4567 | } |
| 4568 | continue; |
| 4569 | encode_char: |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4570 | if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 4571 | assert(ch <= MAX_UNICODE); |
Victor Stinner | 0d3721d | 2011-11-22 03:27:53 +0100 | [diff] [blame] | 4572 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4573 | /* code first surrogate */ |
| 4574 | base64bits += 16; |
| 4575 | base64buffer = (base64buffer << 16) | 0xd800 | ((ch-0x10000) >> 10); |
| 4576 | while (base64bits >= 6) { |
| 4577 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 4578 | base64bits -= 6; |
| 4579 | } |
| 4580 | /* prepare second surrogate */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4581 | ch = Py_UNICODE_LOW_SURROGATE(ch); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4582 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4583 | base64bits += 16; |
| 4584 | base64buffer = (base64buffer << 16) | ch; |
| 4585 | while (base64bits >= 6) { |
| 4586 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 4587 | base64bits -= 6; |
| 4588 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 4589 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4590 | if (base64bits) |
| 4591 | *out++= TO_BASE64(base64buffer << (6-base64bits) ); |
| 4592 | if (inShift) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4593 | *out++ = '-'; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4594 | if (_PyBytes_Resize(&v, out - start) < 0) |
| 4595 | return NULL; |
| 4596 | return v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4597 | } |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4598 | PyObject * |
| 4599 | PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
| 4600 | Py_ssize_t size, |
| 4601 | int base64SetO, |
| 4602 | int base64WhiteSpace, |
| 4603 | const char *errors) |
| 4604 | { |
| 4605 | PyObject *result; |
| 4606 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 4607 | if (tmp == NULL) |
| 4608 | return NULL; |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 4609 | result = _PyUnicode_EncodeUTF7(tmp, base64SetO, |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4610 | base64WhiteSpace, errors); |
| 4611 | Py_DECREF(tmp); |
| 4612 | return result; |
| 4613 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4614 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4615 | #undef IS_BASE64 |
| 4616 | #undef FROM_BASE64 |
| 4617 | #undef TO_BASE64 |
| 4618 | #undef DECODE_DIRECT |
| 4619 | #undef ENCODE_DIRECT |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4620 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4621 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 4622 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4623 | PyObject * |
| 4624 | PyUnicode_DecodeUTF8(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4625 | Py_ssize_t size, |
| 4626 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4627 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4628 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 4629 | } |
| 4630 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4631 | #include "stringlib/asciilib.h" |
| 4632 | #include "stringlib/codecs.h" |
| 4633 | #include "stringlib/undef.h" |
| 4634 | |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4635 | #include "stringlib/ucs1lib.h" |
| 4636 | #include "stringlib/codecs.h" |
| 4637 | #include "stringlib/undef.h" |
| 4638 | |
| 4639 | #include "stringlib/ucs2lib.h" |
| 4640 | #include "stringlib/codecs.h" |
| 4641 | #include "stringlib/undef.h" |
| 4642 | |
| 4643 | #include "stringlib/ucs4lib.h" |
| 4644 | #include "stringlib/codecs.h" |
| 4645 | #include "stringlib/undef.h" |
| 4646 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4647 | /* Mask to check or force alignment of a pointer to C 'long' boundaries */ |
| 4648 | #define LONG_PTR_MASK (size_t) (SIZEOF_LONG - 1) |
| 4649 | |
| 4650 | /* Mask to quickly check whether a C 'long' contains a |
| 4651 | non-ASCII, UTF8-encoded char. */ |
| 4652 | #if (SIZEOF_LONG == 8) |
| 4653 | # define ASCII_CHAR_MASK 0x8080808080808080L |
| 4654 | #elif (SIZEOF_LONG == 4) |
| 4655 | # define ASCII_CHAR_MASK 0x80808080L |
| 4656 | #else |
| 4657 | # error C 'long' size should be either 4 or 8! |
| 4658 | #endif |
| 4659 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4660 | static Py_ssize_t |
| 4661 | 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] | 4662 | { |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4663 | const char *p = start; |
| 4664 | const char *aligned_end = (const char *) ((size_t) end & ~LONG_PTR_MASK); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4665 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4666 | #if SIZEOF_LONG <= SIZEOF_VOID_P |
| 4667 | assert(!((size_t) dest & LONG_PTR_MASK)); |
| 4668 | if (!((size_t) p & LONG_PTR_MASK)) { |
| 4669 | /* Fast path, see in STRINGLIB(utf8_decode) for |
| 4670 | an explanation. */ |
| 4671 | /* Help register allocation */ |
| 4672 | register const char *_p = p; |
| 4673 | register Py_UCS1 * q = dest; |
| 4674 | while (_p < aligned_end) { |
| 4675 | unsigned long value = *(const unsigned long *) _p; |
| 4676 | if (value & ASCII_CHAR_MASK) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4677 | break; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4678 | *((unsigned long *)q) = value; |
| 4679 | _p += SIZEOF_LONG; |
| 4680 | q += SIZEOF_LONG; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4681 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4682 | p = _p; |
| 4683 | while (p < end) { |
| 4684 | if ((unsigned char)*p & 0x80) |
| 4685 | break; |
| 4686 | *q++ = *p++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4687 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4688 | return p - start; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4689 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4690 | #endif |
| 4691 | while (p < end) { |
| 4692 | /* Fast path, see in STRINGLIB(utf8_decode) in stringlib/codecs.h |
| 4693 | for an explanation. */ |
| 4694 | if (!((size_t) p & LONG_PTR_MASK)) { |
| 4695 | /* Help register allocation */ |
| 4696 | register const char *_p = p; |
| 4697 | while (_p < aligned_end) { |
| 4698 | unsigned long value = *(unsigned long *) _p; |
| 4699 | if (value & ASCII_CHAR_MASK) |
| 4700 | break; |
| 4701 | _p += SIZEOF_LONG; |
| 4702 | } |
| 4703 | p = _p; |
| 4704 | if (_p == end) |
| 4705 | break; |
| 4706 | } |
| 4707 | if ((unsigned char)*p & 0x80) |
| 4708 | break; |
| 4709 | ++p; |
| 4710 | } |
| 4711 | memcpy(dest, start, p - start); |
| 4712 | return p - start; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4713 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4714 | |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4715 | PyObject * |
| 4716 | PyUnicode_DecodeUTF8Stateful(const char *s, |
| 4717 | Py_ssize_t size, |
| 4718 | const char *errors, |
| 4719 | Py_ssize_t *consumed) |
| 4720 | { |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4721 | PyObject *unicode; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4722 | const char *starts = s; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4723 | const char *end = s + size; |
| 4724 | Py_ssize_t outpos; |
| 4725 | |
| 4726 | Py_ssize_t startinpos; |
| 4727 | Py_ssize_t endinpos; |
| 4728 | const char *errmsg = ""; |
| 4729 | PyObject *errorHandler = NULL; |
| 4730 | PyObject *exc = NULL; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4731 | |
| 4732 | if (size == 0) { |
| 4733 | if (consumed) |
| 4734 | *consumed = 0; |
Victor Stinner | 382955f | 2011-12-11 21:44:00 +0100 | [diff] [blame] | 4735 | Py_INCREF(unicode_empty); |
| 4736 | return unicode_empty; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4737 | } |
| 4738 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4739 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
| 4740 | if (size == 1 && (unsigned char)s[0] < 128) { |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4741 | if (consumed) |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4742 | *consumed = 1; |
| 4743 | return get_latin1_char((unsigned char)s[0]); |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4744 | } |
| 4745 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4746 | unicode = PyUnicode_New(size, 127); |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4747 | if (!unicode) |
| 4748 | return NULL; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4749 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4750 | outpos = ascii_decode(s, end, PyUnicode_1BYTE_DATA(unicode)); |
| 4751 | s += outpos; |
| 4752 | while (s < end) { |
| 4753 | Py_UCS4 ch; |
| 4754 | int kind = PyUnicode_KIND(unicode); |
| 4755 | if (kind == PyUnicode_1BYTE_KIND) { |
| 4756 | if (PyUnicode_IS_ASCII(unicode)) |
| 4757 | ch = asciilib_utf8_decode(&s, end, |
| 4758 | PyUnicode_1BYTE_DATA(unicode), &outpos); |
| 4759 | else |
| 4760 | ch = ucs1lib_utf8_decode(&s, end, |
| 4761 | PyUnicode_1BYTE_DATA(unicode), &outpos); |
| 4762 | } else if (kind == PyUnicode_2BYTE_KIND) { |
| 4763 | ch = ucs2lib_utf8_decode(&s, end, |
| 4764 | PyUnicode_2BYTE_DATA(unicode), &outpos); |
| 4765 | } else { |
| 4766 | assert(kind == PyUnicode_4BYTE_KIND); |
| 4767 | ch = ucs4lib_utf8_decode(&s, end, |
| 4768 | PyUnicode_4BYTE_DATA(unicode), &outpos); |
| 4769 | } |
| 4770 | |
| 4771 | switch (ch) { |
| 4772 | case 0: |
| 4773 | if (s == end || consumed) |
| 4774 | goto End; |
| 4775 | errmsg = "unexpected end of data"; |
| 4776 | startinpos = s - starts; |
| 4777 | endinpos = startinpos + 1; |
| 4778 | while (endinpos < size && (starts[endinpos] & 0xC0) == 0x80) |
| 4779 | endinpos++; |
| 4780 | break; |
| 4781 | case 1: |
| 4782 | errmsg = "invalid start byte"; |
| 4783 | startinpos = s - starts; |
| 4784 | endinpos = startinpos + 1; |
| 4785 | break; |
| 4786 | case 2: |
| 4787 | errmsg = "invalid continuation byte"; |
| 4788 | startinpos = s - starts; |
| 4789 | endinpos = startinpos + 1; |
| 4790 | while (endinpos < size && (starts[endinpos] & 0xC0) == 0x80) |
| 4791 | endinpos++; |
| 4792 | break; |
| 4793 | default: |
| 4794 | if (unicode_putchar(&unicode, &outpos, ch) < 0) |
| 4795 | goto onError; |
| 4796 | continue; |
| 4797 | } |
| 4798 | |
| 4799 | if (unicode_decode_call_errorhandler( |
| 4800 | errors, &errorHandler, |
| 4801 | "utf-8", errmsg, |
| 4802 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 4803 | &unicode, &outpos)) |
| 4804 | goto onError; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4805 | } |
| 4806 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4807 | End: |
| 4808 | if (unicode_resize(&unicode, outpos) < 0) |
| 4809 | goto onError; |
| 4810 | |
| 4811 | if (consumed) |
| 4812 | *consumed = s - starts; |
| 4813 | |
| 4814 | Py_XDECREF(errorHandler); |
| 4815 | Py_XDECREF(exc); |
| 4816 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 4817 | return unicode; |
| 4818 | |
| 4819 | onError: |
| 4820 | Py_XDECREF(errorHandler); |
| 4821 | Py_XDECREF(exc); |
| 4822 | Py_XDECREF(unicode); |
| 4823 | return NULL; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4824 | } |
| 4825 | |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4826 | #ifdef __APPLE__ |
| 4827 | |
| 4828 | /* Simplified UTF-8 decoder using surrogateescape error handler, |
| 4829 | used to decode the command line arguments on Mac OS X. */ |
| 4830 | |
| 4831 | wchar_t* |
| 4832 | _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size) |
| 4833 | { |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4834 | const char *e; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4835 | wchar_t *unicode; |
| 4836 | Py_ssize_t outpos; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4837 | |
| 4838 | /* Note: size will always be longer than the resulting Unicode |
| 4839 | character count */ |
| 4840 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < (size + 1)) { |
| 4841 | PyErr_NoMemory(); |
| 4842 | return NULL; |
| 4843 | } |
| 4844 | unicode = PyMem_Malloc((size + 1) * sizeof(wchar_t)); |
| 4845 | if (!unicode) |
| 4846 | return NULL; |
| 4847 | |
| 4848 | /* Unpack UTF-8 encoded data */ |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4849 | e = s + size; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4850 | outpos = 0; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4851 | while (s < e) { |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4852 | Py_UCS4 ch; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4853 | #if SIZEOF_WCHAR_T == 4 |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4854 | ch = ucs4lib_utf8_decode(&s, e, (Py_UCS4 *)unicode, &outpos); |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4855 | #else |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4856 | ch = ucs2lib_utf8_decode(&s, e, (Py_UCS2 *)unicode, &outpos); |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4857 | #endif |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4858 | if (ch > 0xFF) { |
| 4859 | #if SIZEOF_WCHAR_T == 4 |
| 4860 | assert(0); |
| 4861 | #else |
| 4862 | assert(Py_UNICODE_IS_SURROGATE(ch)); |
| 4863 | /* compute and append the two surrogates: */ |
| 4864 | unicode[outpos++] = (wchar_t)Py_UNICODE_HIGH_SURROGATE(ch); |
| 4865 | unicode[outpos++] = (wchar_t)Py_UNICODE_LOW_SURROGATE(ch); |
| 4866 | #endif |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4867 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4868 | else { |
| 4869 | if (!ch && s == e) |
| 4870 | break; |
| 4871 | /* surrogateescape */ |
| 4872 | unicode[outpos++] = 0xDC00 + (unsigned char)*s++; |
| 4873 | } |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4874 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4875 | unicode[outpos] = L'\0'; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4876 | return unicode; |
| 4877 | } |
| 4878 | |
| 4879 | #endif /* __APPLE__ */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4880 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4881 | /* Primary internal function which creates utf8 encoded bytes objects. |
| 4882 | |
| 4883 | Allocation strategy: if the string is short, convert into a stack buffer |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4884 | and allocate exactly as much space needed at the end. Else allocate the |
| 4885 | maximum possible needed (4 result bytes per Unicode character), and return |
| 4886 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4887 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 4888 | PyObject * |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4889 | _PyUnicode_AsUTF8String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4890 | { |
Victor Stinner | 6099a03 | 2011-12-18 14:22:26 +0100 | [diff] [blame] | 4891 | enum PyUnicode_Kind kind; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4892 | void *data; |
| 4893 | Py_ssize_t size; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 4894 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4895 | if (!PyUnicode_Check(unicode)) { |
| 4896 | PyErr_BadArgument(); |
| 4897 | return NULL; |
| 4898 | } |
| 4899 | |
| 4900 | if (PyUnicode_READY(unicode) == -1) |
| 4901 | return NULL; |
| 4902 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 4903 | if (PyUnicode_UTF8(unicode)) |
| 4904 | return PyBytes_FromStringAndSize(PyUnicode_UTF8(unicode), |
| 4905 | PyUnicode_UTF8_LENGTH(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4906 | |
| 4907 | kind = PyUnicode_KIND(unicode); |
| 4908 | data = PyUnicode_DATA(unicode); |
| 4909 | size = PyUnicode_GET_LENGTH(unicode); |
| 4910 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 4911 | switch (kind) { |
Victor Stinner | 6099a03 | 2011-12-18 14:22:26 +0100 | [diff] [blame] | 4912 | default: |
| 4913 | assert(0); |
| 4914 | case PyUnicode_1BYTE_KIND: |
| 4915 | /* the string cannot be ASCII, or PyUnicode_UTF8() would be set */ |
| 4916 | assert(!PyUnicode_IS_ASCII(unicode)); |
| 4917 | return ucs1lib_utf8_encoder(unicode, data, size, errors); |
| 4918 | case PyUnicode_2BYTE_KIND: |
| 4919 | return ucs2lib_utf8_encoder(unicode, data, size, errors); |
| 4920 | case PyUnicode_4BYTE_KIND: |
| 4921 | return ucs4lib_utf8_encoder(unicode, data, size, errors); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4922 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4923 | } |
| 4924 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4925 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4926 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
| 4927 | Py_ssize_t size, |
| 4928 | const char *errors) |
| 4929 | { |
| 4930 | PyObject *v, *unicode; |
| 4931 | |
| 4932 | unicode = PyUnicode_FromUnicode(s, size); |
| 4933 | if (unicode == NULL) |
| 4934 | return NULL; |
| 4935 | v = _PyUnicode_AsUTF8String(unicode, errors); |
| 4936 | Py_DECREF(unicode); |
| 4937 | return v; |
| 4938 | } |
| 4939 | |
| 4940 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4941 | PyUnicode_AsUTF8String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4942 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4943 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4944 | } |
| 4945 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4946 | /* --- UTF-32 Codec ------------------------------------------------------- */ |
| 4947 | |
| 4948 | PyObject * |
| 4949 | PyUnicode_DecodeUTF32(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4950 | Py_ssize_t size, |
| 4951 | const char *errors, |
| 4952 | int *byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4953 | { |
| 4954 | return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); |
| 4955 | } |
| 4956 | |
| 4957 | PyObject * |
| 4958 | PyUnicode_DecodeUTF32Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4959 | Py_ssize_t size, |
| 4960 | const char *errors, |
| 4961 | int *byteorder, |
| 4962 | Py_ssize_t *consumed) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4963 | { |
| 4964 | const char *starts = s; |
| 4965 | Py_ssize_t startinpos; |
| 4966 | Py_ssize_t endinpos; |
| 4967 | Py_ssize_t outpos; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4968 | PyObject *unicode; |
Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 4969 | const unsigned char *q, *e; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4970 | int bo = 0; /* assume native ordering by default */ |
| 4971 | const char *errmsg = ""; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4972 | /* Offsets from q for retrieving bytes in the right order. */ |
| 4973 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 4974 | int iorder[] = {0, 1, 2, 3}; |
| 4975 | #else |
| 4976 | int iorder[] = {3, 2, 1, 0}; |
| 4977 | #endif |
| 4978 | PyObject *errorHandler = NULL; |
| 4979 | PyObject *exc = NULL; |
Victor Stinner | 313a120 | 2010-06-11 23:56:51 +0000 | [diff] [blame] | 4980 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4981 | q = (unsigned char *)s; |
| 4982 | e = q + size; |
| 4983 | |
| 4984 | if (byteorder) |
| 4985 | bo = *byteorder; |
| 4986 | |
| 4987 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 4988 | byte order setting accordingly. In native mode, the leading BOM |
| 4989 | mark is skipped, in all other modes, it is copied to the output |
| 4990 | stream as-is (giving a ZWNBSP character). */ |
| 4991 | if (bo == 0) { |
| 4992 | if (size >= 4) { |
| 4993 | const Py_UCS4 bom = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4994 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4995 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4996 | if (bom == 0x0000FEFF) { |
| 4997 | q += 4; |
| 4998 | bo = -1; |
| 4999 | } |
| 5000 | else if (bom == 0xFFFE0000) { |
| 5001 | q += 4; |
| 5002 | bo = 1; |
| 5003 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5004 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5005 | if (bom == 0x0000FEFF) { |
| 5006 | q += 4; |
| 5007 | bo = 1; |
| 5008 | } |
| 5009 | else if (bom == 0xFFFE0000) { |
| 5010 | q += 4; |
| 5011 | bo = -1; |
| 5012 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5013 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5014 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5015 | } |
| 5016 | |
| 5017 | if (bo == -1) { |
| 5018 | /* force LE */ |
| 5019 | iorder[0] = 0; |
| 5020 | iorder[1] = 1; |
| 5021 | iorder[2] = 2; |
| 5022 | iorder[3] = 3; |
| 5023 | } |
| 5024 | else if (bo == 1) { |
| 5025 | /* force BE */ |
| 5026 | iorder[0] = 3; |
| 5027 | iorder[1] = 2; |
| 5028 | iorder[2] = 1; |
| 5029 | iorder[3] = 0; |
| 5030 | } |
| 5031 | |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 5032 | /* This might be one to much, because of a BOM */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5033 | unicode = PyUnicode_New((size+3)/4, 127); |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 5034 | if (!unicode) |
| 5035 | return NULL; |
| 5036 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5037 | return unicode; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5038 | outpos = 0; |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 5039 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5040 | while (q < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5041 | Py_UCS4 ch; |
| 5042 | /* remaining bytes at the end? (size should be divisible by 4) */ |
| 5043 | if (e-q<4) { |
| 5044 | if (consumed) |
| 5045 | break; |
| 5046 | errmsg = "truncated data"; |
| 5047 | startinpos = ((const char *)q)-starts; |
| 5048 | endinpos = ((const char *)e)-starts; |
| 5049 | goto utf32Error; |
| 5050 | /* The remaining input chars are ignored if the callback |
| 5051 | chooses to skip the input */ |
| 5052 | } |
| 5053 | ch = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
| 5054 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5055 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5056 | if (ch >= 0x110000) |
| 5057 | { |
| 5058 | errmsg = "codepoint not in range(0x110000)"; |
| 5059 | startinpos = ((const char *)q)-starts; |
| 5060 | endinpos = startinpos+4; |
| 5061 | goto utf32Error; |
| 5062 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5063 | if (unicode_putchar(&unicode, &outpos, ch) < 0) |
| 5064 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5065 | q += 4; |
| 5066 | continue; |
| 5067 | utf32Error: |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5068 | if (unicode_decode_call_errorhandler( |
| 5069 | errors, &errorHandler, |
| 5070 | "utf32", errmsg, |
| 5071 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5072 | &unicode, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5073 | goto onError; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5074 | } |
| 5075 | |
| 5076 | if (byteorder) |
| 5077 | *byteorder = bo; |
| 5078 | |
| 5079 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5080 | *consumed = (const char *)q-starts; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5081 | |
| 5082 | /* Adjust length */ |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 5083 | if (unicode_resize(&unicode, outpos) < 0) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5084 | goto onError; |
| 5085 | |
| 5086 | Py_XDECREF(errorHandler); |
| 5087 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 5088 | return unicode_result(unicode); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5089 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5090 | onError: |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5091 | Py_DECREF(unicode); |
| 5092 | Py_XDECREF(errorHandler); |
| 5093 | Py_XDECREF(exc); |
| 5094 | return NULL; |
| 5095 | } |
| 5096 | |
| 5097 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5098 | _PyUnicode_EncodeUTF32(PyObject *str, |
| 5099 | const char *errors, |
| 5100 | int byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5101 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5102 | int kind; |
| 5103 | void *data; |
| 5104 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5105 | PyObject *v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5106 | unsigned char *p; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5107 | Py_ssize_t nsize, bytesize, i; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5108 | /* Offsets from p for storing byte pairs in the right order. */ |
| 5109 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5110 | int iorder[] = {0, 1, 2, 3}; |
| 5111 | #else |
| 5112 | int iorder[] = {3, 2, 1, 0}; |
| 5113 | #endif |
| 5114 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5115 | #define STORECHAR(CH) \ |
| 5116 | do { \ |
| 5117 | p[iorder[3]] = ((CH) >> 24) & 0xff; \ |
| 5118 | p[iorder[2]] = ((CH) >> 16) & 0xff; \ |
| 5119 | p[iorder[1]] = ((CH) >> 8) & 0xff; \ |
| 5120 | p[iorder[0]] = (CH) & 0xff; \ |
| 5121 | p += 4; \ |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5122 | } while(0) |
| 5123 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5124 | if (!PyUnicode_Check(str)) { |
| 5125 | PyErr_BadArgument(); |
| 5126 | return NULL; |
| 5127 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 5128 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5129 | return NULL; |
| 5130 | kind = PyUnicode_KIND(str); |
| 5131 | data = PyUnicode_DATA(str); |
| 5132 | len = PyUnicode_GET_LENGTH(str); |
| 5133 | |
| 5134 | nsize = len + (byteorder == 0); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5135 | bytesize = nsize * 4; |
| 5136 | if (bytesize / 4 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5137 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5138 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5139 | if (v == NULL) |
| 5140 | return NULL; |
| 5141 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5142 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5143 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5144 | STORECHAR(0xFEFF); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5145 | if (len == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5146 | goto done; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5147 | |
| 5148 | if (byteorder == -1) { |
| 5149 | /* force LE */ |
| 5150 | iorder[0] = 0; |
| 5151 | iorder[1] = 1; |
| 5152 | iorder[2] = 2; |
| 5153 | iorder[3] = 3; |
| 5154 | } |
| 5155 | else if (byteorder == 1) { |
| 5156 | /* force BE */ |
| 5157 | iorder[0] = 3; |
| 5158 | iorder[1] = 2; |
| 5159 | iorder[2] = 1; |
| 5160 | iorder[3] = 0; |
| 5161 | } |
| 5162 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5163 | for (i = 0; i < len; i++) |
| 5164 | STORECHAR(PyUnicode_READ(kind, data, i)); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5165 | |
| 5166 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5167 | return v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5168 | #undef STORECHAR |
| 5169 | } |
| 5170 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5171 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5172 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
| 5173 | Py_ssize_t size, |
| 5174 | const char *errors, |
| 5175 | int byteorder) |
| 5176 | { |
| 5177 | PyObject *result; |
| 5178 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5179 | if (tmp == NULL) |
| 5180 | return NULL; |
| 5181 | result = _PyUnicode_EncodeUTF32(tmp, errors, byteorder); |
| 5182 | Py_DECREF(tmp); |
| 5183 | return result; |
| 5184 | } |
| 5185 | |
| 5186 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5187 | PyUnicode_AsUTF32String(PyObject *unicode) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5188 | { |
Victor Stinner | b960b34 | 2011-11-20 19:12:52 +0100 | [diff] [blame] | 5189 | return _PyUnicode_EncodeUTF32(unicode, NULL, 0); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5190 | } |
| 5191 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5192 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 5193 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5194 | PyObject * |
| 5195 | PyUnicode_DecodeUTF16(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5196 | Py_ssize_t size, |
| 5197 | const char *errors, |
| 5198 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5199 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5200 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 5201 | } |
| 5202 | |
| 5203 | PyObject * |
| 5204 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5205 | Py_ssize_t size, |
| 5206 | const char *errors, |
| 5207 | int *byteorder, |
| 5208 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5209 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5210 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5211 | Py_ssize_t startinpos; |
| 5212 | Py_ssize_t endinpos; |
| 5213 | Py_ssize_t outpos; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5214 | PyObject *unicode; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5215 | const unsigned char *q, *e; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5216 | int bo = 0; /* assume native ordering by default */ |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5217 | int native_ordering; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 5218 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5219 | PyObject *errorHandler = NULL; |
| 5220 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5221 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5222 | q = (unsigned char *)s; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5223 | e = q + size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5224 | |
| 5225 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5226 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5227 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5228 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 5229 | byte order setting accordingly. In native mode, the leading BOM |
| 5230 | mark is skipped, in all other modes, it is copied to the output |
| 5231 | stream as-is (giving a ZWNBSP character). */ |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5232 | if (bo == 0 && size >= 2) { |
| 5233 | const Py_UCS4 bom = (q[1] << 8) | q[0]; |
| 5234 | if (bom == 0xFEFF) { |
| 5235 | q += 2; |
| 5236 | bo = -1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5237 | } |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5238 | else if (bom == 0xFFFE) { |
| 5239 | q += 2; |
| 5240 | bo = 1; |
| 5241 | } |
| 5242 | if (byteorder) |
| 5243 | *byteorder = bo; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5244 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5245 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5246 | if (q == e) { |
| 5247 | if (consumed) |
| 5248 | *consumed = size; |
| 5249 | Py_INCREF(unicode_empty); |
| 5250 | return unicode_empty; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5251 | } |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5252 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5253 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5254 | native_ordering = bo <= 0; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5255 | #else |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5256 | native_ordering = bo >= 0; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5257 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5258 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5259 | /* Note: size will always be longer than the resulting Unicode |
| 5260 | character count */ |
| 5261 | unicode = PyUnicode_New((e - q + 1) / 2, 127); |
| 5262 | if (!unicode) |
| 5263 | return NULL; |
| 5264 | |
| 5265 | outpos = 0; |
| 5266 | while (1) { |
| 5267 | Py_UCS4 ch = 0; |
| 5268 | if (e - q >= 2) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5269 | int kind = PyUnicode_KIND(unicode); |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5270 | if (kind == PyUnicode_1BYTE_KIND) { |
| 5271 | if (PyUnicode_IS_ASCII(unicode)) |
| 5272 | ch = asciilib_utf16_decode(&q, e, |
| 5273 | PyUnicode_1BYTE_DATA(unicode), &outpos, |
| 5274 | native_ordering); |
| 5275 | else |
| 5276 | ch = ucs1lib_utf16_decode(&q, e, |
| 5277 | PyUnicode_1BYTE_DATA(unicode), &outpos, |
| 5278 | native_ordering); |
| 5279 | } else if (kind == PyUnicode_2BYTE_KIND) { |
| 5280 | ch = ucs2lib_utf16_decode(&q, e, |
| 5281 | PyUnicode_2BYTE_DATA(unicode), &outpos, |
| 5282 | native_ordering); |
| 5283 | } else { |
| 5284 | assert(kind == PyUnicode_4BYTE_KIND); |
| 5285 | ch = ucs4lib_utf16_decode(&q, e, |
| 5286 | PyUnicode_4BYTE_DATA(unicode), &outpos, |
| 5287 | native_ordering); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5288 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5289 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5290 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5291 | switch (ch) |
| 5292 | { |
| 5293 | case 0: |
| 5294 | /* remaining byte at the end? (size should be even) */ |
| 5295 | if (q == e || consumed) |
| 5296 | goto End; |
| 5297 | errmsg = "truncated data"; |
| 5298 | startinpos = ((const char *)q) - starts; |
| 5299 | endinpos = ((const char *)e) - starts; |
| 5300 | break; |
| 5301 | /* The remaining input chars are ignored if the callback |
| 5302 | chooses to skip the input */ |
| 5303 | case 1: |
| 5304 | errmsg = "unexpected end of data"; |
| 5305 | startinpos = ((const char *)q) - 2 - starts; |
| 5306 | endinpos = ((const char *)e) - starts; |
| 5307 | break; |
| 5308 | case 2: |
| 5309 | errmsg = "illegal encoding"; |
| 5310 | startinpos = ((const char *)q) - 2 - starts; |
| 5311 | endinpos = startinpos + 2; |
| 5312 | break; |
| 5313 | case 3: |
| 5314 | errmsg = "illegal UTF-16 surrogate"; |
| 5315 | startinpos = ((const char *)q) - 4 - starts; |
| 5316 | endinpos = startinpos + 2; |
| 5317 | break; |
| 5318 | default: |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5319 | if (unicode_putchar(&unicode, &outpos, ch) < 0) |
| 5320 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5321 | continue; |
| 5322 | } |
| 5323 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5324 | if (unicode_decode_call_errorhandler( |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5325 | errors, |
| 5326 | &errorHandler, |
| 5327 | "utf16", errmsg, |
| 5328 | &starts, |
| 5329 | (const char **)&e, |
| 5330 | &startinpos, |
| 5331 | &endinpos, |
| 5332 | &exc, |
| 5333 | (const char **)&q, |
| 5334 | &unicode, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5335 | &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5336 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5337 | } |
| 5338 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5339 | End: |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5340 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5341 | *consumed = (const char *)q-starts; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5342 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5343 | /* Adjust length */ |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 5344 | if (unicode_resize(&unicode, outpos) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5345 | goto onError; |
| 5346 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5347 | Py_XDECREF(errorHandler); |
| 5348 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 5349 | return unicode_result(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5350 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5351 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5352 | Py_DECREF(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5353 | Py_XDECREF(errorHandler); |
| 5354 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5355 | return NULL; |
| 5356 | } |
| 5357 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5358 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5359 | _PyUnicode_EncodeUTF16(PyObject *str, |
| 5360 | const char *errors, |
| 5361 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5362 | { |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5363 | enum PyUnicode_Kind kind; |
| 5364 | const void *data; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5365 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5366 | PyObject *v; |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5367 | unsigned short *out; |
| 5368 | Py_ssize_t bytesize; |
| 5369 | Py_ssize_t pairs; |
| 5370 | #ifdef WORDS_BIGENDIAN |
| 5371 | int native_ordering = byteorder >= 0; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5372 | #else |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5373 | int native_ordering = byteorder <= 0; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5374 | #endif |
| 5375 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5376 | if (!PyUnicode_Check(str)) { |
| 5377 | PyErr_BadArgument(); |
| 5378 | return NULL; |
| 5379 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 5380 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5381 | return NULL; |
| 5382 | kind = PyUnicode_KIND(str); |
| 5383 | data = PyUnicode_DATA(str); |
| 5384 | len = PyUnicode_GET_LENGTH(str); |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 5385 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5386 | pairs = 0; |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5387 | if (kind == PyUnicode_4BYTE_KIND) { |
| 5388 | const Py_UCS4 *in = (const Py_UCS4 *)data; |
| 5389 | const Py_UCS4 *end = in + len; |
| 5390 | while (in < end) |
| 5391 | if (*in++ >= 0x10000) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5392 | pairs++; |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5393 | } |
| 5394 | if (len > PY_SSIZE_T_MAX / 2 - pairs - (byteorder == 0)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5395 | return PyErr_NoMemory(); |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5396 | bytesize = (len + pairs + (byteorder == 0)) * 2; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5397 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5398 | if (v == NULL) |
| 5399 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5400 | |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5401 | /* output buffer is 2-bytes aligned */ |
| 5402 | assert(((Py_uintptr_t)PyBytes_AS_STRING(v) & 1) == 0); |
| 5403 | out = (unsigned short *)PyBytes_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5404 | if (byteorder == 0) |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5405 | *out++ = 0xFEFF; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5406 | if (len == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5407 | goto done; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5408 | |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5409 | switch (kind) { |
| 5410 | case PyUnicode_1BYTE_KIND: { |
| 5411 | ucs1lib_utf16_encode(out, (const Py_UCS1 *)data, len, native_ordering); |
| 5412 | break; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5413 | } |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5414 | case PyUnicode_2BYTE_KIND: { |
| 5415 | ucs2lib_utf16_encode(out, (const Py_UCS2 *)data, len, native_ordering); |
| 5416 | break; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5417 | } |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5418 | case PyUnicode_4BYTE_KIND: { |
| 5419 | ucs4lib_utf16_encode(out, (const Py_UCS4 *)data, len, native_ordering); |
| 5420 | break; |
| 5421 | } |
| 5422 | default: |
| 5423 | assert(0); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5424 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5425 | |
| 5426 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5427 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5428 | } |
| 5429 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5430 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5431 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
| 5432 | Py_ssize_t size, |
| 5433 | const char *errors, |
| 5434 | int byteorder) |
| 5435 | { |
| 5436 | PyObject *result; |
| 5437 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5438 | if (tmp == NULL) |
| 5439 | return NULL; |
| 5440 | result = _PyUnicode_EncodeUTF16(tmp, errors, byteorder); |
| 5441 | Py_DECREF(tmp); |
| 5442 | return result; |
| 5443 | } |
| 5444 | |
| 5445 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5446 | PyUnicode_AsUTF16String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5447 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5448 | return _PyUnicode_EncodeUTF16(unicode, NULL, 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5449 | } |
| 5450 | |
| 5451 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 5452 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5453 | /* Helper function for PyUnicode_DecodeUnicodeEscape, determines |
| 5454 | if all the escapes in the string make it still a valid ASCII string. |
| 5455 | Returns -1 if any escapes were found which cause the string to |
| 5456 | pop out of ASCII range. Otherwise returns the length of the |
| 5457 | required buffer to hold the string. |
| 5458 | */ |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 5459 | static Py_ssize_t |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5460 | length_of_escaped_ascii_string(const char *s, Py_ssize_t size) |
| 5461 | { |
| 5462 | const unsigned char *p = (const unsigned char *)s; |
| 5463 | const unsigned char *end = p + size; |
| 5464 | Py_ssize_t length = 0; |
| 5465 | |
| 5466 | if (size < 0) |
| 5467 | return -1; |
| 5468 | |
| 5469 | for (; p < end; ++p) { |
| 5470 | if (*p > 127) { |
| 5471 | /* Non-ASCII */ |
| 5472 | return -1; |
| 5473 | } |
| 5474 | else if (*p != '\\') { |
| 5475 | /* Normal character */ |
| 5476 | ++length; |
| 5477 | } |
| 5478 | else { |
| 5479 | /* Backslash-escape, check next char */ |
| 5480 | ++p; |
| 5481 | /* Escape sequence reaches till end of string or |
| 5482 | non-ASCII follow-up. */ |
| 5483 | if (p >= end || *p > 127) |
| 5484 | return -1; |
| 5485 | switch (*p) { |
| 5486 | case '\n': |
| 5487 | /* backslash + \n result in zero characters */ |
| 5488 | break; |
| 5489 | case '\\': case '\'': case '\"': |
| 5490 | case 'b': case 'f': case 't': |
| 5491 | case 'n': case 'r': case 'v': case 'a': |
| 5492 | ++length; |
| 5493 | break; |
| 5494 | case '0': case '1': case '2': case '3': |
| 5495 | case '4': case '5': case '6': case '7': |
| 5496 | case 'x': case 'u': case 'U': case 'N': |
| 5497 | /* these do not guarantee ASCII characters */ |
| 5498 | return -1; |
| 5499 | default: |
| 5500 | /* count the backslash + the other character */ |
| 5501 | length += 2; |
| 5502 | } |
| 5503 | } |
| 5504 | } |
| 5505 | return length; |
| 5506 | } |
| 5507 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5508 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 5509 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5510 | PyObject * |
| 5511 | PyUnicode_DecodeUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5512 | Py_ssize_t size, |
Victor Stinner | c17f540 | 2011-09-29 00:16:58 +0200 | [diff] [blame] | 5513 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5514 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5515 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5516 | Py_ssize_t startinpos; |
| 5517 | Py_ssize_t endinpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5518 | int j; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5519 | PyObject *v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5520 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5521 | char* message; |
| 5522 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5523 | PyObject *errorHandler = NULL; |
| 5524 | PyObject *exc = NULL; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5525 | Py_ssize_t len; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5526 | Py_ssize_t i; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5527 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5528 | len = length_of_escaped_ascii_string(s, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5529 | |
| 5530 | /* After length_of_escaped_ascii_string() there are two alternatives, |
| 5531 | either the string is pure ASCII with named escapes like \n, etc. |
| 5532 | and we determined it's exact size (common case) |
| 5533 | or it contains \x, \u, ... escape sequences. then we create a |
| 5534 | 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] | 5535 | if (len >= 0) { |
| 5536 | v = PyUnicode_New(len, 127); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5537 | if (!v) |
| 5538 | goto onError; |
| 5539 | assert(PyUnicode_KIND(v) == PyUnicode_1BYTE_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5540 | } |
| 5541 | else { |
| 5542 | /* Escaped strings will always be longer than the resulting |
| 5543 | Unicode string, so we start with size here and then reduce the |
| 5544 | length after conversion to the true value. |
| 5545 | (but if the error callback returns a long replacement string |
| 5546 | we'll have to allocate more space) */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5547 | v = PyUnicode_New(size, 127); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5548 | if (!v) |
| 5549 | goto onError; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5550 | len = size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5551 | } |
| 5552 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5553 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5554 | return v; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5555 | i = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5556 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5557 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5558 | while (s < end) { |
| 5559 | unsigned char c; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 5560 | Py_UCS4 x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5561 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5562 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5563 | /* The only case in which i == ascii_length is a backslash |
| 5564 | followed by a newline. */ |
| 5565 | assert(i <= len); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5566 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5567 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 5568 | if (*s != '\\') { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5569 | if (unicode_putchar(&v, &i, (unsigned char) *s++) < 0) |
| 5570 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5571 | continue; |
| 5572 | } |
| 5573 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5574 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5575 | /* \ - Escapes */ |
| 5576 | s++; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5577 | c = *s++; |
| 5578 | if (s > end) |
| 5579 | c = '\0'; /* Invalid after \ */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5580 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5581 | /* The only case in which i == ascii_length is a backslash |
| 5582 | followed by a newline. */ |
| 5583 | assert(i < len || (i == len && c == '\n')); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5584 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5585 | switch (c) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5586 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5587 | /* \x escapes */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5588 | #define WRITECHAR(ch) \ |
| 5589 | do { \ |
| 5590 | if (unicode_putchar(&v, &i, ch) < 0) \ |
| 5591 | goto onError; \ |
| 5592 | }while(0) |
| 5593 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5594 | case '\n': break; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5595 | case '\\': WRITECHAR('\\'); break; |
| 5596 | case '\'': WRITECHAR('\''); break; |
| 5597 | case '\"': WRITECHAR('\"'); break; |
| 5598 | case 'b': WRITECHAR('\b'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5599 | /* FF */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5600 | case 'f': WRITECHAR('\014'); break; |
| 5601 | case 't': WRITECHAR('\t'); break; |
| 5602 | case 'n': WRITECHAR('\n'); break; |
| 5603 | case 'r': WRITECHAR('\r'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5604 | /* VT */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5605 | case 'v': WRITECHAR('\013'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5606 | /* BEL, not classic C */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5607 | case 'a': WRITECHAR('\007'); break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5608 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5609 | /* \OOO (octal) escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5610 | case '0': case '1': case '2': case '3': |
| 5611 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5612 | x = s[-1] - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5613 | if (s < end && '0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5614 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5615 | if (s < end && '0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5616 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5617 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5618 | WRITECHAR(x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5619 | break; |
| 5620 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5621 | /* hex escapes */ |
| 5622 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5623 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5624 | digits = 2; |
| 5625 | message = "truncated \\xXX escape"; |
| 5626 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5627 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5628 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5629 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5630 | digits = 4; |
| 5631 | message = "truncated \\uXXXX escape"; |
| 5632 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5633 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5634 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5635 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5636 | digits = 8; |
| 5637 | message = "truncated \\UXXXXXXXX escape"; |
| 5638 | hexescape: |
| 5639 | chr = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5640 | if (s+digits>end) { |
| 5641 | endinpos = size; |
| 5642 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5643 | errors, &errorHandler, |
| 5644 | "unicodeescape", "end of string in escape sequence", |
| 5645 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5646 | &v, &i)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5647 | goto onError; |
| 5648 | goto nextByte; |
| 5649 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5650 | for (j = 0; j < digits; ++j) { |
| 5651 | c = (unsigned char) s[j]; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 5652 | if (!Py_ISXDIGIT(c)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5653 | endinpos = (s+j+1)-starts; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5654 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5655 | errors, &errorHandler, |
| 5656 | "unicodeescape", message, |
| 5657 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5658 | &v, &i)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5659 | goto onError; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5660 | len = PyUnicode_GET_LENGTH(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5661 | goto nextByte; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5662 | } |
| 5663 | chr = (chr<<4) & ~0xF; |
| 5664 | if (c >= '0' && c <= '9') |
| 5665 | chr += c - '0'; |
| 5666 | else if (c >= 'a' && c <= 'f') |
| 5667 | chr += 10 + c - 'a'; |
| 5668 | else |
| 5669 | chr += 10 + c - 'A'; |
| 5670 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5671 | s += j; |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 5672 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5673 | /* _decoding_error will have already written into the |
| 5674 | target buffer. */ |
| 5675 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5676 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5677 | /* when we get here, chr is a 32-bit unicode character */ |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 5678 | if (chr <= MAX_UNICODE) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5679 | WRITECHAR(chr); |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5680 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5681 | endinpos = s-starts; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5682 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5683 | errors, &errorHandler, |
| 5684 | "unicodeescape", "illegal Unicode character", |
| 5685 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5686 | &v, &i)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5687 | goto onError; |
| 5688 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5689 | break; |
| 5690 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5691 | /* \N{name} */ |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5692 | case 'N': |
| 5693 | message = "malformed \\N character escape"; |
| 5694 | if (ucnhash_CAPI == NULL) { |
| 5695 | /* load the unicode data module */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5696 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCapsule_Import( |
| 5697 | PyUnicodeData_CAPSULE_NAME, 1); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5698 | if (ucnhash_CAPI == NULL) |
| 5699 | goto ucnhashError; |
| 5700 | } |
| 5701 | if (*s == '{') { |
| 5702 | const char *start = s+1; |
| 5703 | /* look for the closing brace */ |
| 5704 | while (*s != '}' && s < end) |
| 5705 | s++; |
| 5706 | if (s > start && s < end && *s == '}') { |
| 5707 | /* found a name. look it up in the unicode database */ |
| 5708 | message = "unknown Unicode character name"; |
| 5709 | s++; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5710 | if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), |
Ezio Melotti | 931b8aa | 2011-10-21 21:57:36 +0300 | [diff] [blame] | 5711 | &chr, 0)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5712 | goto store; |
| 5713 | } |
| 5714 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5715 | endinpos = s-starts; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5716 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5717 | errors, &errorHandler, |
| 5718 | "unicodeescape", message, |
| 5719 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5720 | &v, &i)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5721 | goto onError; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5722 | break; |
| 5723 | |
| 5724 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5725 | if (s > end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5726 | message = "\\ at end of string"; |
| 5727 | s--; |
| 5728 | endinpos = s-starts; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5729 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5730 | errors, &errorHandler, |
| 5731 | "unicodeescape", message, |
| 5732 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5733 | &v, &i)) |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5734 | goto onError; |
| 5735 | } |
| 5736 | else { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5737 | WRITECHAR('\\'); |
| 5738 | WRITECHAR(s[-1]); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5739 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5740 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5741 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5742 | nextByte: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5743 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5744 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5745 | #undef WRITECHAR |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5746 | |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 5747 | if (unicode_resize(&v, i) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5748 | goto onError; |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 5749 | Py_XDECREF(errorHandler); |
| 5750 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 5751 | return unicode_result(v); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5752 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5753 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5754 | PyErr_SetString( |
| 5755 | PyExc_UnicodeError, |
| 5756 | "\\N escapes not supported (can't load unicodedata module)" |
| 5757 | ); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 5758 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5759 | Py_XDECREF(errorHandler); |
| 5760 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 5761 | return NULL; |
| 5762 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5763 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5764 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5765 | Py_XDECREF(errorHandler); |
| 5766 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5767 | return NULL; |
| 5768 | } |
| 5769 | |
| 5770 | /* Return a Unicode-Escape string version of the Unicode object. |
| 5771 | |
| 5772 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 5773 | appropriate. |
| 5774 | |
| 5775 | */ |
| 5776 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5777 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5778 | PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5779 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5780 | Py_ssize_t i, len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5781 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5782 | char *p; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5783 | int kind; |
| 5784 | void *data; |
| 5785 | Py_ssize_t expandsize = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5786 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5787 | /* Initial allocation is based on the longest-possible unichr |
| 5788 | escape. |
| 5789 | |
| 5790 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 5791 | unichr, so in this case it's the longest unichr escape. In |
| 5792 | narrow (UTF-16) builds this is five chars per source unichr |
| 5793 | since there are two unichrs in the surrogate pair, so in narrow |
| 5794 | (UTF-16) builds it's not the longest unichr escape. |
| 5795 | |
| 5796 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 5797 | so in the narrow (UTF-16) build case it's the longest unichr |
| 5798 | escape. |
| 5799 | */ |
| 5800 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5801 | if (!PyUnicode_Check(unicode)) { |
| 5802 | PyErr_BadArgument(); |
| 5803 | return NULL; |
| 5804 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 5805 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5806 | return NULL; |
| 5807 | len = PyUnicode_GET_LENGTH(unicode); |
| 5808 | kind = PyUnicode_KIND(unicode); |
| 5809 | data = PyUnicode_DATA(unicode); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 5810 | switch (kind) { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5811 | case PyUnicode_1BYTE_KIND: expandsize = 4; break; |
| 5812 | case PyUnicode_2BYTE_KIND: expandsize = 6; break; |
| 5813 | case PyUnicode_4BYTE_KIND: expandsize = 10; break; |
| 5814 | } |
| 5815 | |
| 5816 | if (len == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5817 | return PyBytes_FromStringAndSize(NULL, 0); |
| 5818 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5819 | if (len > (PY_SSIZE_T_MAX - 2 - 1) / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5820 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5821 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5822 | repr = PyBytes_FromStringAndSize(NULL, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5823 | 2 |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5824 | + expandsize*len |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5825 | + 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5826 | if (repr == NULL) |
| 5827 | return NULL; |
| 5828 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5829 | p = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5830 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5831 | for (i = 0; i < len; i++) { |
Victor Stinner | 3326cb6 | 2011-11-10 20:15:25 +0100 | [diff] [blame] | 5832 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5833 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5834 | /* Escape backslashes */ |
| 5835 | if (ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5836 | *p++ = '\\'; |
| 5837 | *p++ = (char) ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5838 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5839 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5840 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5841 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 5842 | else if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 5843 | assert(ch <= MAX_UNICODE); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5844 | *p++ = '\\'; |
| 5845 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 5846 | *p++ = Py_hexdigits[(ch >> 28) & 0x0000000F]; |
| 5847 | *p++ = Py_hexdigits[(ch >> 24) & 0x0000000F]; |
| 5848 | *p++ = Py_hexdigits[(ch >> 20) & 0x0000000F]; |
| 5849 | *p++ = Py_hexdigits[(ch >> 16) & 0x0000000F]; |
| 5850 | *p++ = Py_hexdigits[(ch >> 12) & 0x0000000F]; |
| 5851 | *p++ = Py_hexdigits[(ch >> 8) & 0x0000000F]; |
| 5852 | *p++ = Py_hexdigits[(ch >> 4) & 0x0000000F]; |
| 5853 | *p++ = Py_hexdigits[ch & 0x0000000F]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5854 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5855 | } |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5856 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5857 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5858 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5859 | *p++ = '\\'; |
| 5860 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 5861 | *p++ = Py_hexdigits[(ch >> 12) & 0x000F]; |
| 5862 | *p++ = Py_hexdigits[(ch >> 8) & 0x000F]; |
| 5863 | *p++ = Py_hexdigits[(ch >> 4) & 0x000F]; |
| 5864 | *p++ = Py_hexdigits[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5865 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5866 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5867 | /* Map special whitespace to '\t', \n', '\r' */ |
| 5868 | else if (ch == '\t') { |
| 5869 | *p++ = '\\'; |
| 5870 | *p++ = 't'; |
| 5871 | } |
| 5872 | else if (ch == '\n') { |
| 5873 | *p++ = '\\'; |
| 5874 | *p++ = 'n'; |
| 5875 | } |
| 5876 | else if (ch == '\r') { |
| 5877 | *p++ = '\\'; |
| 5878 | *p++ = 'r'; |
| 5879 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5880 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5881 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 5882 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5883 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5884 | *p++ = 'x'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 5885 | *p++ = Py_hexdigits[(ch >> 4) & 0x000F]; |
| 5886 | *p++ = Py_hexdigits[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5887 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5888 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5889 | /* Copy everything else as-is */ |
| 5890 | else |
| 5891 | *p++ = (char) ch; |
| 5892 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5893 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5894 | assert(p - PyBytes_AS_STRING(repr) > 0); |
| 5895 | if (_PyBytes_Resize(&repr, p - PyBytes_AS_STRING(repr)) < 0) |
| 5896 | return NULL; |
| 5897 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5898 | } |
| 5899 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5900 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5901 | PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
| 5902 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5903 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5904 | PyObject *result; |
| 5905 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5906 | if (tmp == NULL) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5907 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5908 | result = PyUnicode_AsUnicodeEscapeString(tmp); |
| 5909 | Py_DECREF(tmp); |
| 5910 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5911 | } |
| 5912 | |
| 5913 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 5914 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5915 | PyObject * |
| 5916 | PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5917 | Py_ssize_t size, |
| 5918 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5919 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5920 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5921 | Py_ssize_t startinpos; |
| 5922 | Py_ssize_t endinpos; |
| 5923 | Py_ssize_t outpos; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5924 | PyObject *v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5925 | const char *end; |
| 5926 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5927 | PyObject *errorHandler = NULL; |
| 5928 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5929 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5930 | /* Escaped strings will always be longer than the resulting |
| 5931 | 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] | 5932 | length after conversion to the true value. (But decoding error |
| 5933 | handler might have to resize the string) */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5934 | v = PyUnicode_New(size, 127); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5935 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5936 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5937 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5938 | return v; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5939 | outpos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5940 | end = s + size; |
| 5941 | while (s < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5942 | unsigned char c; |
| 5943 | Py_UCS4 x; |
| 5944 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5945 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5946 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5947 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 5948 | if (*s != '\\') { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5949 | if (unicode_putchar(&v, &outpos, (unsigned char)*s++) < 0) |
| 5950 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5951 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5952 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5953 | startinpos = s-starts; |
| 5954 | |
| 5955 | /* \u-escapes are only interpreted iff the number of leading |
| 5956 | backslashes if odd */ |
| 5957 | bs = s; |
| 5958 | for (;s < end;) { |
| 5959 | if (*s != '\\') |
| 5960 | break; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5961 | if (unicode_putchar(&v, &outpos, (unsigned char)*s++) < 0) |
| 5962 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5963 | } |
| 5964 | if (((s - bs) & 1) == 0 || |
| 5965 | s >= end || |
| 5966 | (*s != 'u' && *s != 'U')) { |
| 5967 | continue; |
| 5968 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5969 | outpos--; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5970 | count = *s=='u' ? 4 : 8; |
| 5971 | s++; |
| 5972 | |
| 5973 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5974 | for (x = 0, i = 0; i < count; ++i, ++s) { |
| 5975 | c = (unsigned char)*s; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 5976 | if (!Py_ISXDIGIT(c)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5977 | endinpos = s-starts; |
| 5978 | if (unicode_decode_call_errorhandler( |
| 5979 | errors, &errorHandler, |
| 5980 | "rawunicodeescape", "truncated \\uXXXX", |
| 5981 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5982 | &v, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5983 | goto onError; |
| 5984 | goto nextByte; |
| 5985 | } |
| 5986 | x = (x<<4) & ~0xF; |
| 5987 | if (c >= '0' && c <= '9') |
| 5988 | x += c - '0'; |
| 5989 | else if (c >= 'a' && c <= 'f') |
| 5990 | x += 10 + c - 'a'; |
| 5991 | else |
| 5992 | x += 10 + c - 'A'; |
| 5993 | } |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 5994 | if (x <= MAX_UNICODE) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5995 | if (unicode_putchar(&v, &outpos, x) < 0) |
| 5996 | goto onError; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 5997 | } else { |
| 5998 | endinpos = s-starts; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5999 | if (unicode_decode_call_errorhandler( |
| 6000 | errors, &errorHandler, |
| 6001 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6002 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6003 | &v, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6004 | goto onError; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6005 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6006 | nextByte: |
| 6007 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6008 | } |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 6009 | if (unicode_resize(&v, outpos) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6010 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6011 | Py_XDECREF(errorHandler); |
| 6012 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 6013 | return unicode_result(v); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6014 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6015 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6016 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6017 | Py_XDECREF(errorHandler); |
| 6018 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6019 | return NULL; |
| 6020 | } |
| 6021 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6022 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6023 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6024 | PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6025 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6026 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6027 | char *p; |
| 6028 | char *q; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6029 | Py_ssize_t expandsize, pos; |
| 6030 | int kind; |
| 6031 | void *data; |
| 6032 | Py_ssize_t len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6033 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6034 | if (!PyUnicode_Check(unicode)) { |
| 6035 | PyErr_BadArgument(); |
| 6036 | return NULL; |
| 6037 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6038 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6039 | return NULL; |
| 6040 | kind = PyUnicode_KIND(unicode); |
| 6041 | data = PyUnicode_DATA(unicode); |
| 6042 | len = PyUnicode_GET_LENGTH(unicode); |
Benjamin Peterson | 1518e87 | 2011-11-23 10:44:52 -0600 | [diff] [blame] | 6043 | /* 4 byte characters can take up 10 bytes, 2 byte characters can take up 6 |
| 6044 | bytes, and 1 byte characters 4. */ |
| 6045 | expandsize = kind * 2 + 2; |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 6046 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6047 | if (len > PY_SSIZE_T_MAX / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6048 | return PyErr_NoMemory(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6049 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6050 | repr = PyBytes_FromStringAndSize(NULL, expandsize * len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6051 | if (repr == NULL) |
| 6052 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6053 | if (len == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6054 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6055 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6056 | p = q = PyBytes_AS_STRING(repr); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6057 | for (pos = 0; pos < len; pos++) { |
| 6058 | Py_UCS4 ch = PyUnicode_READ(kind, data, pos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6059 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 6060 | if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 6061 | assert(ch <= MAX_UNICODE); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6062 | *p++ = '\\'; |
| 6063 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6064 | *p++ = Py_hexdigits[(ch >> 28) & 0xf]; |
| 6065 | *p++ = Py_hexdigits[(ch >> 24) & 0xf]; |
| 6066 | *p++ = Py_hexdigits[(ch >> 20) & 0xf]; |
| 6067 | *p++ = Py_hexdigits[(ch >> 16) & 0xf]; |
| 6068 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 6069 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 6070 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 6071 | *p++ = Py_hexdigits[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6072 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6073 | /* Map 16-bit characters to '\uxxxx' */ |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6074 | else if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6075 | *p++ = '\\'; |
| 6076 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6077 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 6078 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 6079 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 6080 | *p++ = Py_hexdigits[ch & 15]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6081 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6082 | /* Copy everything else as-is */ |
| 6083 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6084 | *p++ = (char) ch; |
| 6085 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6086 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6087 | assert(p > q); |
| 6088 | if (_PyBytes_Resize(&repr, p - q) < 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6089 | return NULL; |
| 6090 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6091 | } |
| 6092 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6093 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6094 | PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
| 6095 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6096 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6097 | PyObject *result; |
| 6098 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 6099 | if (tmp == NULL) |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 6100 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6101 | result = PyUnicode_AsRawUnicodeEscapeString(tmp); |
| 6102 | Py_DECREF(tmp); |
| 6103 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6104 | } |
| 6105 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6106 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 6107 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6108 | PyObject * |
| 6109 | _PyUnicode_DecodeUnicodeInternal(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6110 | Py_ssize_t size, |
| 6111 | const char *errors) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6112 | { |
| 6113 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6114 | Py_ssize_t startinpos; |
| 6115 | Py_ssize_t endinpos; |
| 6116 | Py_ssize_t outpos; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6117 | PyObject *v; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6118 | const char *end; |
| 6119 | const char *reason; |
| 6120 | PyObject *errorHandler = NULL; |
| 6121 | PyObject *exc = NULL; |
| 6122 | |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6123 | if (PyErr_WarnEx(PyExc_DeprecationWarning, |
Ezio Melotti | 11060a4 | 2011-11-16 09:39:10 +0200 | [diff] [blame] | 6124 | "unicode_internal codec has been deprecated", |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6125 | 1)) |
| 6126 | return NULL; |
| 6127 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6128 | /* XXX overflow detection missing */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6129 | 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] | 6130 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6131 | goto onError; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6132 | if (PyUnicode_GET_LENGTH(v) == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6133 | return v; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6134 | outpos = 0; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6135 | end = s + size; |
| 6136 | |
| 6137 | while (s < end) { |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6138 | Py_UNICODE uch; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6139 | Py_UCS4 ch; |
| 6140 | /* We copy the raw representation one byte at a time because the |
| 6141 | pointer may be unaligned (see test_codeccallbacks). */ |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6142 | ((char *) &uch)[0] = s[0]; |
| 6143 | ((char *) &uch)[1] = s[1]; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6144 | #ifdef Py_UNICODE_WIDE |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6145 | ((char *) &uch)[2] = s[2]; |
| 6146 | ((char *) &uch)[3] = s[3]; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6147 | #endif |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6148 | ch = uch; |
| 6149 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6150 | /* We have to sanity check the raw data, otherwise doom looms for |
| 6151 | some malformed UCS-4 data. */ |
| 6152 | if ( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6153 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6154 | ch > 0x10ffff || |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6155 | #endif |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6156 | end-s < Py_UNICODE_SIZE |
| 6157 | ) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6158 | { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6159 | startinpos = s - starts; |
| 6160 | if (end-s < Py_UNICODE_SIZE) { |
| 6161 | endinpos = end-starts; |
| 6162 | reason = "truncated input"; |
| 6163 | } |
| 6164 | else { |
| 6165 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 6166 | reason = "illegal code point (> 0x10FFFF)"; |
| 6167 | } |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6168 | if (unicode_decode_call_errorhandler( |
| 6169 | errors, &errorHandler, |
| 6170 | "unicode_internal", reason, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 6171 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6172 | &v, &outpos)) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6173 | goto onError; |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6174 | continue; |
| 6175 | } |
| 6176 | |
| 6177 | s += Py_UNICODE_SIZE; |
| 6178 | #ifndef Py_UNICODE_WIDE |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 6179 | if (Py_UNICODE_IS_HIGH_SURROGATE(ch) && s < end) |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6180 | { |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6181 | Py_UNICODE uch2; |
| 6182 | ((char *) &uch2)[0] = s[0]; |
| 6183 | ((char *) &uch2)[1] = s[1]; |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 6184 | if (Py_UNICODE_IS_LOW_SURROGATE(uch2)) |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6185 | { |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 6186 | ch = Py_UNICODE_JOIN_SURROGATES(uch, uch2); |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6187 | s += Py_UNICODE_SIZE; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6188 | } |
| 6189 | } |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6190 | #endif |
| 6191 | |
| 6192 | if (unicode_putchar(&v, &outpos, ch) < 0) |
| 6193 | goto onError; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6194 | } |
| 6195 | |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 6196 | if (unicode_resize(&v, outpos) < 0) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6197 | goto onError; |
| 6198 | Py_XDECREF(errorHandler); |
| 6199 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 6200 | return unicode_result(v); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6201 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6202 | onError: |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6203 | Py_XDECREF(v); |
| 6204 | Py_XDECREF(errorHandler); |
| 6205 | Py_XDECREF(exc); |
| 6206 | return NULL; |
| 6207 | } |
| 6208 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6209 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 6210 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6211 | PyObject * |
| 6212 | PyUnicode_DecodeLatin1(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6213 | Py_ssize_t size, |
| 6214 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6215 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6216 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 6217 | return _PyUnicode_FromUCS1((unsigned char*)s, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6218 | } |
| 6219 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6220 | /* create or adjust a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6221 | static void |
| 6222 | make_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6223 | const char *encoding, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6224 | PyObject *unicode, |
| 6225 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6226 | const char *reason) |
| 6227 | { |
| 6228 | if (*exceptionObject == NULL) { |
| 6229 | *exceptionObject = PyObject_CallFunction( |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6230 | PyExc_UnicodeEncodeError, "sOnns", |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6231 | encoding, unicode, startpos, endpos, reason); |
| 6232 | } |
| 6233 | else { |
| 6234 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 6235 | goto onError; |
| 6236 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 6237 | goto onError; |
| 6238 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 6239 | goto onError; |
| 6240 | return; |
| 6241 | onError: |
| 6242 | Py_DECREF(*exceptionObject); |
| 6243 | *exceptionObject = NULL; |
| 6244 | } |
| 6245 | } |
| 6246 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6247 | /* raises a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6248 | static void |
| 6249 | raise_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6250 | const char *encoding, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6251 | PyObject *unicode, |
| 6252 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6253 | const char *reason) |
| 6254 | { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6255 | make_encode_exception(exceptionObject, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6256 | encoding, unicode, startpos, endpos, reason); |
| 6257 | if (*exceptionObject != NULL) |
| 6258 | PyCodec_StrictErrors(*exceptionObject); |
| 6259 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6260 | |
| 6261 | /* error handling callback helper: |
| 6262 | build arguments, call the callback and check the arguments, |
| 6263 | put the result into newpos and return the replacement string, which |
| 6264 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6265 | static PyObject * |
| 6266 | unicode_encode_call_errorhandler(const char *errors, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6267 | PyObject **errorHandler, |
| 6268 | const char *encoding, const char *reason, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6269 | PyObject *unicode, PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6270 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6271 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6272 | { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6273 | 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] | 6274 | Py_ssize_t len; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6275 | PyObject *restuple; |
| 6276 | PyObject *resunicode; |
| 6277 | |
| 6278 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6279 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6280 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6281 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6282 | } |
| 6283 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6284 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6285 | return NULL; |
| 6286 | len = PyUnicode_GET_LENGTH(unicode); |
| 6287 | |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6288 | make_encode_exception(exceptionObject, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6289 | encoding, unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6290 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6291 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6292 | |
| 6293 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6294 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6295 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6296 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6297 | if (!PyTuple_Check(restuple)) { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6298 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6299 | Py_DECREF(restuple); |
| 6300 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6301 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6302 | if (!PyArg_ParseTuple(restuple, argparse, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6303 | &resunicode, newpos)) { |
| 6304 | Py_DECREF(restuple); |
| 6305 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6306 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6307 | if (!PyUnicode_Check(resunicode) && !PyBytes_Check(resunicode)) { |
| 6308 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
| 6309 | Py_DECREF(restuple); |
| 6310 | return NULL; |
| 6311 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6312 | if (*newpos<0) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6313 | *newpos = len + *newpos; |
| 6314 | if (*newpos<0 || *newpos>len) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6315 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 6316 | Py_DECREF(restuple); |
| 6317 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 6318 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6319 | Py_INCREF(resunicode); |
| 6320 | Py_DECREF(restuple); |
| 6321 | return resunicode; |
| 6322 | } |
| 6323 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6324 | static PyObject * |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6325 | unicode_encode_ucs1(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6326 | const char *errors, |
Victor Stinner | fcd9653 | 2011-11-04 00:28:50 +0100 | [diff] [blame] | 6327 | unsigned int limit) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6328 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6329 | /* input state */ |
| 6330 | Py_ssize_t pos=0, size; |
| 6331 | int kind; |
| 6332 | void *data; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6333 | /* output object */ |
| 6334 | PyObject *res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6335 | /* pointer into the output */ |
| 6336 | char *str; |
| 6337 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6338 | Py_ssize_t ressize; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6339 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 6340 | 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] | 6341 | PyObject *errorHandler = NULL; |
| 6342 | PyObject *exc = NULL; |
| 6343 | /* the following variable is used for caching string comparisons |
| 6344 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 6345 | int known_errorHandler = -1; |
| 6346 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6347 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6348 | return NULL; |
| 6349 | size = PyUnicode_GET_LENGTH(unicode); |
| 6350 | kind = PyUnicode_KIND(unicode); |
| 6351 | data = PyUnicode_DATA(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6352 | /* allocate enough for a simple encoding without |
| 6353 | replacements, if we need more, we'll resize */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6354 | if (size == 0) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6355 | return PyBytes_FromStringAndSize(NULL, 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6356 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6357 | if (res == NULL) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6358 | return NULL; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6359 | str = PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6360 | ressize = size; |
| 6361 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6362 | while (pos < size) { |
| 6363 | Py_UCS4 c = PyUnicode_READ(kind, data, pos); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6364 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6365 | /* can we encode this? */ |
| 6366 | if (c<limit) { |
| 6367 | /* no overflow check, because we know that the space is enough */ |
| 6368 | *str++ = (char)c; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6369 | ++pos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6370 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6371 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6372 | Py_ssize_t requiredsize; |
| 6373 | PyObject *repunicode; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6374 | Py_ssize_t repsize, newpos, respos, i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6375 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6376 | Py_ssize_t collstart = pos; |
| 6377 | Py_ssize_t collend = pos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6378 | /* find all unecodable characters */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6379 | while ((collend < size) && (PyUnicode_READ(kind, data, collend)>=limit)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6380 | ++collend; |
| 6381 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 6382 | if (known_errorHandler==-1) { |
| 6383 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 6384 | known_errorHandler = 1; |
| 6385 | else if (!strcmp(errors, "replace")) |
| 6386 | known_errorHandler = 2; |
| 6387 | else if (!strcmp(errors, "ignore")) |
| 6388 | known_errorHandler = 3; |
| 6389 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 6390 | known_errorHandler = 4; |
| 6391 | else |
| 6392 | known_errorHandler = 0; |
| 6393 | } |
| 6394 | switch (known_errorHandler) { |
| 6395 | case 1: /* strict */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6396 | raise_encode_exception(&exc, encoding, unicode, collstart, collend, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6397 | goto onError; |
| 6398 | case 2: /* replace */ |
| 6399 | while (collstart++<collend) |
| 6400 | *str++ = '?'; /* fall through */ |
| 6401 | case 3: /* ignore */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6402 | pos = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6403 | break; |
| 6404 | case 4: /* xmlcharrefreplace */ |
| 6405 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6406 | /* determine replacement size */ |
| 6407 | for (i = collstart, repsize = 0; i < collend; ++i) { |
| 6408 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 6409 | if (ch < 10) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6410 | repsize += 2+1+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6411 | else if (ch < 100) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6412 | repsize += 2+2+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6413 | else if (ch < 1000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6414 | repsize += 2+3+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6415 | else if (ch < 10000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6416 | repsize += 2+4+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6417 | else if (ch < 100000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6418 | repsize += 2+5+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6419 | else if (ch < 1000000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6420 | repsize += 2+6+1; |
Victor Stinner | 0d3721d | 2011-11-22 03:27:53 +0100 | [diff] [blame] | 6421 | else { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 6422 | assert(ch <= MAX_UNICODE); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6423 | repsize += 2+7+1; |
Victor Stinner | 0d3721d | 2011-11-22 03:27:53 +0100 | [diff] [blame] | 6424 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6425 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6426 | requiredsize = respos+repsize+(size-collend); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6427 | if (requiredsize > ressize) { |
| 6428 | if (requiredsize<2*ressize) |
| 6429 | requiredsize = 2*ressize; |
| 6430 | if (_PyBytes_Resize(&res, requiredsize)) |
| 6431 | goto onError; |
| 6432 | str = PyBytes_AS_STRING(res) + respos; |
| 6433 | ressize = requiredsize; |
| 6434 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6435 | /* generate replacement */ |
| 6436 | for (i = collstart; i < collend; ++i) { |
| 6437 | str += sprintf(str, "&#%d;", PyUnicode_READ(kind, data, i)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6438 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6439 | pos = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6440 | break; |
| 6441 | default: |
| 6442 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6443 | encoding, reason, unicode, &exc, |
| 6444 | collstart, collend, &newpos); |
| 6445 | if (repunicode == NULL || (PyUnicode_Check(repunicode) && |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6446 | PyUnicode_READY(repunicode) == -1)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6447 | goto onError; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6448 | if (PyBytes_Check(repunicode)) { |
| 6449 | /* Directly copy bytes result to output. */ |
| 6450 | repsize = PyBytes_Size(repunicode); |
| 6451 | if (repsize > 1) { |
| 6452 | /* Make room for all additional bytes. */ |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6453 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6454 | if (_PyBytes_Resize(&res, ressize+repsize-1)) { |
| 6455 | Py_DECREF(repunicode); |
| 6456 | goto onError; |
| 6457 | } |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6458 | str = PyBytes_AS_STRING(res) + respos; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6459 | ressize += repsize-1; |
| 6460 | } |
| 6461 | memcpy(str, PyBytes_AsString(repunicode), repsize); |
| 6462 | str += repsize; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6463 | pos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6464 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6465 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6466 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6467 | /* need more space? (at least enough for what we |
| 6468 | have+the replacement+the rest of the string, so |
| 6469 | we won't have to check space for encodable characters) */ |
| 6470 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6471 | repsize = PyUnicode_GET_LENGTH(repunicode); |
| 6472 | requiredsize = respos+repsize+(size-collend); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6473 | if (requiredsize > ressize) { |
| 6474 | if (requiredsize<2*ressize) |
| 6475 | requiredsize = 2*ressize; |
| 6476 | if (_PyBytes_Resize(&res, requiredsize)) { |
| 6477 | Py_DECREF(repunicode); |
| 6478 | goto onError; |
| 6479 | } |
| 6480 | str = PyBytes_AS_STRING(res) + respos; |
| 6481 | ressize = requiredsize; |
| 6482 | } |
| 6483 | /* check if there is anything unencodable in the replacement |
| 6484 | and copy it to the output */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6485 | for (i = 0; repsize-->0; ++i, ++str) { |
| 6486 | c = PyUnicode_READ_CHAR(repunicode, i); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6487 | if (c >= limit) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6488 | raise_encode_exception(&exc, encoding, unicode, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6489 | pos, pos+1, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6490 | Py_DECREF(repunicode); |
| 6491 | goto onError; |
| 6492 | } |
| 6493 | *str = (char)c; |
| 6494 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6495 | pos = newpos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6496 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6497 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6498 | } |
| 6499 | } |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6500 | /* Resize if we allocated to much */ |
| 6501 | size = str - PyBytes_AS_STRING(res); |
| 6502 | if (size < ressize) { /* If this falls res will be NULL */ |
Alexandre Vassalotti | bad1b92 | 2008-12-27 09:49:09 +0000 | [diff] [blame] | 6503 | assert(size >= 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6504 | if (_PyBytes_Resize(&res, size) < 0) |
| 6505 | goto onError; |
| 6506 | } |
| 6507 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6508 | Py_XDECREF(errorHandler); |
| 6509 | Py_XDECREF(exc); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6510 | return res; |
| 6511 | |
| 6512 | onError: |
| 6513 | Py_XDECREF(res); |
| 6514 | Py_XDECREF(errorHandler); |
| 6515 | Py_XDECREF(exc); |
| 6516 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6517 | } |
| 6518 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6519 | /* Deprecated */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6520 | PyObject * |
| 6521 | PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6522 | Py_ssize_t size, |
| 6523 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6524 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6525 | PyObject *result; |
| 6526 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 6527 | if (unicode == NULL) |
| 6528 | return NULL; |
| 6529 | result = unicode_encode_ucs1(unicode, errors, 256); |
| 6530 | Py_DECREF(unicode); |
| 6531 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6532 | } |
| 6533 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6534 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6535 | _PyUnicode_AsLatin1String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6536 | { |
| 6537 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6538 | PyErr_BadArgument(); |
| 6539 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6540 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6541 | if (PyUnicode_READY(unicode) == -1) |
| 6542 | return NULL; |
| 6543 | /* Fast path: if it is a one-byte string, construct |
| 6544 | bytes object directly. */ |
| 6545 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) |
| 6546 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6547 | PyUnicode_GET_LENGTH(unicode)); |
| 6548 | /* Non-Latin-1 characters present. Defer to above function to |
| 6549 | raise the exception. */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6550 | return unicode_encode_ucs1(unicode, errors, 256); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6551 | } |
| 6552 | |
| 6553 | PyObject* |
| 6554 | PyUnicode_AsLatin1String(PyObject *unicode) |
| 6555 | { |
| 6556 | return _PyUnicode_AsLatin1String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6557 | } |
| 6558 | |
| 6559 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 6560 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6561 | PyObject * |
| 6562 | PyUnicode_DecodeASCII(const char *s, |
| 6563 | Py_ssize_t size, |
| 6564 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6565 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6566 | const char *starts = s; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6567 | PyObject *unicode; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6568 | int kind; |
| 6569 | void *data; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6570 | Py_ssize_t startinpos; |
| 6571 | Py_ssize_t endinpos; |
| 6572 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6573 | const char *e; |
| 6574 | PyObject *errorHandler = NULL; |
| 6575 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6576 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 6577 | if (size == 0) { |
| 6578 | Py_INCREF(unicode_empty); |
| 6579 | return unicode_empty; |
| 6580 | } |
| 6581 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6582 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6583 | if (size == 1 && (unsigned char)s[0] < 128) |
| 6584 | return get_latin1_char((unsigned char)s[0]); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6585 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6586 | unicode = PyUnicode_New(size, 127); |
| 6587 | if (unicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6588 | goto onError; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6589 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6590 | e = s + size; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6591 | data = PyUnicode_1BYTE_DATA(unicode); |
| 6592 | outpos = ascii_decode(s, e, (Py_UCS1 *)data); |
| 6593 | if (outpos == size) |
| 6594 | return unicode; |
| 6595 | |
| 6596 | s += outpos; |
| 6597 | kind = PyUnicode_1BYTE_KIND; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6598 | while (s < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6599 | register unsigned char c = (unsigned char)*s; |
| 6600 | if (c < 128) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6601 | PyUnicode_WRITE(kind, data, outpos++, c); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6602 | ++s; |
| 6603 | } |
| 6604 | else { |
| 6605 | startinpos = s-starts; |
| 6606 | endinpos = startinpos + 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6607 | if (unicode_decode_call_errorhandler( |
| 6608 | errors, &errorHandler, |
| 6609 | "ascii", "ordinal not in range(128)", |
| 6610 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6611 | &unicode, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6612 | goto onError; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6613 | kind = PyUnicode_KIND(unicode); |
| 6614 | data = PyUnicode_DATA(unicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6615 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6616 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6617 | if (unicode_resize(&unicode, outpos) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6618 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6619 | Py_XDECREF(errorHandler); |
| 6620 | Py_XDECREF(exc); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6621 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 6622 | return unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6623 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6624 | onError: |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6625 | Py_XDECREF(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6626 | Py_XDECREF(errorHandler); |
| 6627 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6628 | return NULL; |
| 6629 | } |
| 6630 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6631 | /* Deprecated */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6632 | PyObject * |
| 6633 | PyUnicode_EncodeASCII(const Py_UNICODE *p, |
| 6634 | Py_ssize_t size, |
| 6635 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6636 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6637 | PyObject *result; |
| 6638 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 6639 | if (unicode == NULL) |
| 6640 | return NULL; |
| 6641 | result = unicode_encode_ucs1(unicode, errors, 128); |
| 6642 | Py_DECREF(unicode); |
| 6643 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6644 | } |
| 6645 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6646 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6647 | _PyUnicode_AsASCIIString(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6648 | { |
| 6649 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6650 | PyErr_BadArgument(); |
| 6651 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6652 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6653 | if (PyUnicode_READY(unicode) == -1) |
| 6654 | return NULL; |
| 6655 | /* Fast path: if it is an ASCII-only string, construct bytes object |
| 6656 | directly. Else defer to above function to raise the exception. */ |
| 6657 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) |
| 6658 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6659 | PyUnicode_GET_LENGTH(unicode)); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6660 | return unicode_encode_ucs1(unicode, errors, 128); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6661 | } |
| 6662 | |
| 6663 | PyObject * |
| 6664 | PyUnicode_AsASCIIString(PyObject *unicode) |
| 6665 | { |
| 6666 | return _PyUnicode_AsASCIIString(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6667 | } |
| 6668 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 6669 | #ifdef HAVE_MBCS |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6670 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6671 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6672 | |
Hirokazu Yamamoto | 3530246 | 2009-03-21 13:23:27 +0000 | [diff] [blame] | 6673 | #if SIZEOF_INT < SIZEOF_SIZE_T |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6674 | #define NEED_RETRY |
| 6675 | #endif |
| 6676 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6677 | #ifndef WC_ERR_INVALID_CHARS |
| 6678 | # define WC_ERR_INVALID_CHARS 0x0080 |
| 6679 | #endif |
| 6680 | |
| 6681 | static char* |
| 6682 | code_page_name(UINT code_page, PyObject **obj) |
| 6683 | { |
| 6684 | *obj = NULL; |
| 6685 | if (code_page == CP_ACP) |
| 6686 | return "mbcs"; |
| 6687 | if (code_page == CP_UTF7) |
| 6688 | return "CP_UTF7"; |
| 6689 | if (code_page == CP_UTF8) |
| 6690 | return "CP_UTF8"; |
| 6691 | |
| 6692 | *obj = PyBytes_FromFormat("cp%u", code_page); |
| 6693 | if (*obj == NULL) |
| 6694 | return NULL; |
| 6695 | return PyBytes_AS_STRING(*obj); |
| 6696 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6697 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6698 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6699 | is_dbcs_lead_byte(UINT code_page, const char *s, int offset) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6700 | { |
| 6701 | const char *curr = s + offset; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6702 | const char *prev; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6703 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6704 | if (!IsDBCSLeadByteEx(code_page, *curr)) |
| 6705 | return 0; |
| 6706 | |
| 6707 | prev = CharPrevExA(code_page, s, curr, 0); |
| 6708 | if (prev == curr) |
| 6709 | return 1; |
| 6710 | /* FIXME: This code is limited to "true" double-byte encodings, |
| 6711 | as it assumes an incomplete character consists of a single |
| 6712 | byte. */ |
| 6713 | if (curr - prev == 2) |
| 6714 | return 1; |
| 6715 | if (!IsDBCSLeadByteEx(code_page, *prev)) |
| 6716 | return 1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6717 | return 0; |
| 6718 | } |
| 6719 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6720 | static DWORD |
| 6721 | decode_code_page_flags(UINT code_page) |
| 6722 | { |
| 6723 | if (code_page == CP_UTF7) { |
| 6724 | /* The CP_UTF7 decoder only supports flags=0 */ |
| 6725 | return 0; |
| 6726 | } |
| 6727 | else |
| 6728 | return MB_ERR_INVALID_CHARS; |
| 6729 | } |
| 6730 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6731 | /* |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6732 | * Decode a byte string from a Windows code page into unicode object in strict |
| 6733 | * mode. |
| 6734 | * |
| 6735 | * Returns consumed size if succeed, returns -2 on decode error, or raise a |
| 6736 | * WindowsError and returns -1 on other error. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6737 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6738 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6739 | decode_code_page_strict(UINT code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6740 | PyObject **v, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6741 | const char *in, |
| 6742 | int insize) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6743 | { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6744 | const DWORD flags = decode_code_page_flags(code_page); |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 6745 | wchar_t *out; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6746 | DWORD outsize; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6747 | |
| 6748 | /* First get the size of the result */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6749 | assert(insize > 0); |
| 6750 | outsize = MultiByteToWideChar(code_page, flags, in, insize, NULL, 0); |
| 6751 | if (outsize <= 0) |
| 6752 | goto error; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6753 | |
| 6754 | if (*v == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6755 | /* Create unicode object */ |
Victor Stinner | ab59594 | 2011-12-17 04:59:06 +0100 | [diff] [blame] | 6756 | /* FIXME: don't use _PyUnicode_New(), but allocate a wchar_t* buffer */ |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6757 | *v = (PyObject*)_PyUnicode_New(outsize); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6758 | if (*v == NULL) |
| 6759 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6760 | out = PyUnicode_AS_UNICODE(*v); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6761 | } |
| 6762 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6763 | /* Extend unicode object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6764 | Py_ssize_t n = PyUnicode_GET_SIZE(*v); |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 6765 | if (unicode_resize(v, n + outsize) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6766 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6767 | out = PyUnicode_AS_UNICODE(*v) + n; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6768 | } |
| 6769 | |
| 6770 | /* Do the conversion */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6771 | outsize = MultiByteToWideChar(code_page, flags, in, insize, out, outsize); |
| 6772 | if (outsize <= 0) |
| 6773 | goto error; |
| 6774 | return insize; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6775 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6776 | error: |
| 6777 | if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION) |
| 6778 | return -2; |
| 6779 | PyErr_SetFromWindowsErr(0); |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6780 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6781 | } |
| 6782 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6783 | /* |
| 6784 | * Decode a byte string from a code page into unicode object with an error |
| 6785 | * handler. |
| 6786 | * |
| 6787 | * Returns consumed size if succeed, or raise a WindowsError or |
| 6788 | * UnicodeDecodeError exception and returns -1 on error. |
| 6789 | */ |
| 6790 | static int |
| 6791 | decode_code_page_errors(UINT code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6792 | PyObject **v, |
| 6793 | const char *in, const int size, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6794 | const char *errors) |
| 6795 | { |
| 6796 | const char *startin = in; |
| 6797 | const char *endin = in + size; |
| 6798 | const DWORD flags = decode_code_page_flags(code_page); |
| 6799 | /* Ideally, we should get reason from FormatMessage. This is the Windows |
| 6800 | 2000 English version of the message. */ |
| 6801 | const char *reason = "No mapping for the Unicode character exists " |
| 6802 | "in the target code page."; |
| 6803 | /* each step cannot decode more than 1 character, but a character can be |
| 6804 | represented as a surrogate pair */ |
| 6805 | wchar_t buffer[2], *startout, *out; |
| 6806 | int insize, outsize; |
| 6807 | PyObject *errorHandler = NULL; |
| 6808 | PyObject *exc = NULL; |
| 6809 | PyObject *encoding_obj = NULL; |
| 6810 | char *encoding; |
| 6811 | DWORD err; |
| 6812 | int ret = -1; |
| 6813 | |
| 6814 | assert(size > 0); |
| 6815 | |
| 6816 | encoding = code_page_name(code_page, &encoding_obj); |
| 6817 | if (encoding == NULL) |
| 6818 | return -1; |
| 6819 | |
| 6820 | if (errors == NULL || strcmp(errors, "strict") == 0) { |
| 6821 | /* The last error was ERROR_NO_UNICODE_TRANSLATION, then we raise a |
| 6822 | UnicodeDecodeError. */ |
| 6823 | make_decode_exception(&exc, encoding, in, size, 0, 0, reason); |
| 6824 | if (exc != NULL) { |
| 6825 | PyCodec_StrictErrors(exc); |
| 6826 | Py_CLEAR(exc); |
| 6827 | } |
| 6828 | goto error; |
| 6829 | } |
| 6830 | |
| 6831 | if (*v == NULL) { |
| 6832 | /* Create unicode object */ |
| 6833 | if (size > PY_SSIZE_T_MAX / (Py_ssize_t)Py_ARRAY_LENGTH(buffer)) { |
| 6834 | PyErr_NoMemory(); |
| 6835 | goto error; |
| 6836 | } |
Victor Stinner | ab59594 | 2011-12-17 04:59:06 +0100 | [diff] [blame] | 6837 | /* FIXME: don't use _PyUnicode_New(), but allocate a wchar_t* buffer */ |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6838 | *v = (PyObject*)_PyUnicode_New(size * Py_ARRAY_LENGTH(buffer)); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6839 | if (*v == NULL) |
| 6840 | goto error; |
| 6841 | startout = PyUnicode_AS_UNICODE(*v); |
| 6842 | } |
| 6843 | else { |
| 6844 | /* Extend unicode object */ |
| 6845 | Py_ssize_t n = PyUnicode_GET_SIZE(*v); |
| 6846 | if (size > (PY_SSIZE_T_MAX - n) / (Py_ssize_t)Py_ARRAY_LENGTH(buffer)) { |
| 6847 | PyErr_NoMemory(); |
| 6848 | goto error; |
| 6849 | } |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 6850 | if (unicode_resize(v, n + size * Py_ARRAY_LENGTH(buffer)) < 0) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6851 | goto error; |
| 6852 | startout = PyUnicode_AS_UNICODE(*v) + n; |
| 6853 | } |
| 6854 | |
| 6855 | /* Decode the byte string character per character */ |
| 6856 | out = startout; |
| 6857 | while (in < endin) |
| 6858 | { |
| 6859 | /* Decode a character */ |
| 6860 | insize = 1; |
| 6861 | do |
| 6862 | { |
| 6863 | outsize = MultiByteToWideChar(code_page, flags, |
| 6864 | in, insize, |
| 6865 | buffer, Py_ARRAY_LENGTH(buffer)); |
| 6866 | if (outsize > 0) |
| 6867 | break; |
| 6868 | err = GetLastError(); |
| 6869 | if (err != ERROR_NO_UNICODE_TRANSLATION |
| 6870 | && err != ERROR_INSUFFICIENT_BUFFER) |
| 6871 | { |
| 6872 | PyErr_SetFromWindowsErr(0); |
| 6873 | goto error; |
| 6874 | } |
| 6875 | insize++; |
| 6876 | } |
| 6877 | /* 4=maximum length of a UTF-8 sequence */ |
| 6878 | while (insize <= 4 && (in + insize) <= endin); |
| 6879 | |
| 6880 | if (outsize <= 0) { |
| 6881 | Py_ssize_t startinpos, endinpos, outpos; |
| 6882 | |
| 6883 | startinpos = in - startin; |
| 6884 | endinpos = startinpos + 1; |
| 6885 | outpos = out - PyUnicode_AS_UNICODE(*v); |
| 6886 | if (unicode_decode_call_errorhandler( |
| 6887 | errors, &errorHandler, |
| 6888 | encoding, reason, |
| 6889 | &startin, &endin, &startinpos, &endinpos, &exc, &in, |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 6890 | v, &outpos)) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6891 | { |
| 6892 | goto error; |
| 6893 | } |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 6894 | out = PyUnicode_AS_UNICODE(*v) + outpos; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6895 | } |
| 6896 | else { |
| 6897 | in += insize; |
| 6898 | memcpy(out, buffer, outsize * sizeof(wchar_t)); |
| 6899 | out += outsize; |
| 6900 | } |
| 6901 | } |
| 6902 | |
| 6903 | /* write a NUL character at the end */ |
| 6904 | *out = 0; |
| 6905 | |
| 6906 | /* Extend unicode object */ |
| 6907 | outsize = out - startout; |
| 6908 | assert(outsize <= PyUnicode_WSTR_LENGTH(*v)); |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 6909 | if (unicode_resize(v, outsize) < 0) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6910 | goto error; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6911 | ret = size; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6912 | |
| 6913 | error: |
| 6914 | Py_XDECREF(encoding_obj); |
| 6915 | Py_XDECREF(errorHandler); |
| 6916 | Py_XDECREF(exc); |
| 6917 | return ret; |
| 6918 | } |
| 6919 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6920 | static PyObject * |
| 6921 | decode_code_page_stateful(int code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6922 | const char *s, Py_ssize_t size, |
| 6923 | const char *errors, Py_ssize_t *consumed) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6924 | { |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6925 | PyObject *v = NULL; |
| 6926 | int chunk_size, final, converted, done; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6927 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6928 | if (code_page < 0) { |
| 6929 | PyErr_SetString(PyExc_ValueError, "invalid code page number"); |
| 6930 | return NULL; |
| 6931 | } |
| 6932 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6933 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6934 | *consumed = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6935 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6936 | do |
| 6937 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6938 | #ifdef NEED_RETRY |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6939 | if (size > INT_MAX) { |
| 6940 | chunk_size = INT_MAX; |
| 6941 | final = 0; |
| 6942 | done = 0; |
| 6943 | } |
| 6944 | else |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6945 | #endif |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6946 | { |
| 6947 | chunk_size = (int)size; |
| 6948 | final = (consumed == NULL); |
| 6949 | done = 1; |
| 6950 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6951 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6952 | /* Skip trailing lead-byte unless 'final' is set */ |
| 6953 | if (!final && is_dbcs_lead_byte(code_page, s, chunk_size - 1)) |
| 6954 | --chunk_size; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6955 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6956 | if (chunk_size == 0 && done) { |
| 6957 | if (v != NULL) |
| 6958 | break; |
| 6959 | Py_INCREF(unicode_empty); |
| 6960 | return unicode_empty; |
| 6961 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6962 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6963 | |
| 6964 | converted = decode_code_page_strict(code_page, &v, |
| 6965 | s, chunk_size); |
| 6966 | if (converted == -2) |
| 6967 | converted = decode_code_page_errors(code_page, &v, |
| 6968 | s, chunk_size, |
| 6969 | errors); |
| 6970 | assert(converted != 0); |
| 6971 | |
| 6972 | if (converted < 0) { |
| 6973 | Py_XDECREF(v); |
| 6974 | return NULL; |
| 6975 | } |
| 6976 | |
| 6977 | if (consumed) |
| 6978 | *consumed += converted; |
| 6979 | |
| 6980 | s += converted; |
| 6981 | size -= converted; |
| 6982 | } while (!done); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6983 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 6984 | return unicode_result(v); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6985 | } |
| 6986 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6987 | PyObject * |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6988 | PyUnicode_DecodeCodePageStateful(int code_page, |
| 6989 | const char *s, |
| 6990 | Py_ssize_t size, |
| 6991 | const char *errors, |
| 6992 | Py_ssize_t *consumed) |
| 6993 | { |
| 6994 | return decode_code_page_stateful(code_page, s, size, errors, consumed); |
| 6995 | } |
| 6996 | |
| 6997 | PyObject * |
| 6998 | PyUnicode_DecodeMBCSStateful(const char *s, |
| 6999 | Py_ssize_t size, |
| 7000 | const char *errors, |
| 7001 | Py_ssize_t *consumed) |
| 7002 | { |
| 7003 | return decode_code_page_stateful(CP_ACP, s, size, errors, consumed); |
| 7004 | } |
| 7005 | |
| 7006 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7007 | PyUnicode_DecodeMBCS(const char *s, |
| 7008 | Py_ssize_t size, |
| 7009 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7010 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7011 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 7012 | } |
| 7013 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7014 | static DWORD |
| 7015 | encode_code_page_flags(UINT code_page, const char *errors) |
| 7016 | { |
| 7017 | if (code_page == CP_UTF8) { |
| 7018 | if (winver.dwMajorVersion >= 6) |
| 7019 | /* CP_UTF8 supports WC_ERR_INVALID_CHARS on Windows Vista |
| 7020 | and later */ |
| 7021 | return WC_ERR_INVALID_CHARS; |
| 7022 | else |
| 7023 | /* CP_UTF8 only supports flags=0 on Windows older than Vista */ |
| 7024 | return 0; |
| 7025 | } |
| 7026 | else if (code_page == CP_UTF7) { |
| 7027 | /* CP_UTF7 only supports flags=0 */ |
| 7028 | return 0; |
| 7029 | } |
| 7030 | else { |
| 7031 | if (errors != NULL && strcmp(errors, "replace") == 0) |
| 7032 | return 0; |
| 7033 | else |
| 7034 | return WC_NO_BEST_FIT_CHARS; |
| 7035 | } |
| 7036 | } |
| 7037 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7038 | /* |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7039 | * Encode a Unicode string to a Windows code page into a byte string in strict |
| 7040 | * mode. |
| 7041 | * |
| 7042 | * Returns consumed characters if succeed, returns -2 on encode error, or raise |
| 7043 | * a WindowsError and returns -1 on other error. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7044 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7045 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7046 | encode_code_page_strict(UINT code_page, PyObject **outbytes, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7047 | PyObject *unicode, Py_ssize_t offset, int len, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7048 | const char* errors) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7049 | { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7050 | BOOL usedDefaultChar = FALSE; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7051 | BOOL *pusedDefaultChar = &usedDefaultChar; |
| 7052 | int outsize; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7053 | PyObject *exc = NULL; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 7054 | wchar_t *p; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7055 | Py_ssize_t size; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7056 | const DWORD flags = encode_code_page_flags(code_page, NULL); |
| 7057 | char *out; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7058 | /* Create a substring so that we can get the UTF-16 representation |
| 7059 | of just the slice under consideration. */ |
| 7060 | PyObject *substring; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7061 | |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7062 | assert(len > 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7063 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7064 | if (code_page != CP_UTF8 && code_page != CP_UTF7) |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7065 | pusedDefaultChar = &usedDefaultChar; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7066 | else |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7067 | pusedDefaultChar = NULL; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7068 | |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7069 | substring = PyUnicode_Substring(unicode, offset, offset+len); |
| 7070 | if (substring == NULL) |
| 7071 | return -1; |
| 7072 | p = PyUnicode_AsUnicodeAndSize(substring, &size); |
| 7073 | if (p == NULL) { |
| 7074 | Py_DECREF(substring); |
| 7075 | return -1; |
| 7076 | } |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7077 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7078 | /* First get the size of the result */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7079 | outsize = WideCharToMultiByte(code_page, flags, |
| 7080 | p, size, |
| 7081 | NULL, 0, |
| 7082 | NULL, pusedDefaultChar); |
| 7083 | if (outsize <= 0) |
| 7084 | goto error; |
| 7085 | /* If we used a default char, then we failed! */ |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7086 | if (pusedDefaultChar && *pusedDefaultChar) { |
| 7087 | Py_DECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7088 | return -2; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7089 | } |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7090 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7091 | if (*outbytes == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7092 | /* Create string object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7093 | *outbytes = PyBytes_FromStringAndSize(NULL, outsize); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7094 | if (*outbytes == NULL) { |
| 7095 | Py_DECREF(substring); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7096 | return -1; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7097 | } |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7098 | out = PyBytes_AS_STRING(*outbytes); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7099 | } |
| 7100 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7101 | /* Extend string object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7102 | const Py_ssize_t n = PyBytes_Size(*outbytes); |
| 7103 | if (outsize > PY_SSIZE_T_MAX - n) { |
| 7104 | PyErr_NoMemory(); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7105 | Py_DECREF(substring); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7106 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7107 | } |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7108 | if (_PyBytes_Resize(outbytes, n + outsize) < 0) { |
| 7109 | Py_DECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7110 | return -1; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7111 | } |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7112 | out = PyBytes_AS_STRING(*outbytes) + n; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7113 | } |
| 7114 | |
| 7115 | /* Do the conversion */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7116 | outsize = WideCharToMultiByte(code_page, flags, |
| 7117 | p, size, |
| 7118 | out, outsize, |
| 7119 | NULL, pusedDefaultChar); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7120 | Py_CLEAR(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7121 | if (outsize <= 0) |
| 7122 | goto error; |
| 7123 | if (pusedDefaultChar && *pusedDefaultChar) |
| 7124 | return -2; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7125 | return 0; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7126 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7127 | error: |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7128 | Py_XDECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7129 | if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION) |
| 7130 | return -2; |
| 7131 | PyErr_SetFromWindowsErr(0); |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7132 | return -1; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7133 | } |
| 7134 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7135 | /* |
| 7136 | * Encode a Unicode string to a Windows code page into a byte string using a |
| 7137 | * error handler. |
| 7138 | * |
| 7139 | * Returns consumed characters if succeed, or raise a WindowsError and returns |
| 7140 | * -1 on other error. |
| 7141 | */ |
| 7142 | static int |
| 7143 | encode_code_page_errors(UINT code_page, PyObject **outbytes, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7144 | PyObject *unicode, Py_ssize_t unicode_offset, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7145 | Py_ssize_t insize, const char* errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7146 | { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7147 | const DWORD flags = encode_code_page_flags(code_page, errors); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7148 | Py_ssize_t pos = unicode_offset; |
| 7149 | Py_ssize_t endin = unicode_offset + insize; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7150 | /* Ideally, we should get reason from FormatMessage. This is the Windows |
| 7151 | 2000 English version of the message. */ |
| 7152 | const char *reason = "invalid character"; |
| 7153 | /* 4=maximum length of a UTF-8 sequence */ |
| 7154 | char buffer[4]; |
| 7155 | BOOL usedDefaultChar = FALSE, *pusedDefaultChar; |
| 7156 | Py_ssize_t outsize; |
| 7157 | char *out; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7158 | PyObject *errorHandler = NULL; |
| 7159 | PyObject *exc = NULL; |
| 7160 | PyObject *encoding_obj = NULL; |
| 7161 | char *encoding; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7162 | Py_ssize_t newpos, newoutsize; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7163 | PyObject *rep; |
| 7164 | int ret = -1; |
| 7165 | |
| 7166 | assert(insize > 0); |
| 7167 | |
| 7168 | encoding = code_page_name(code_page, &encoding_obj); |
| 7169 | if (encoding == NULL) |
| 7170 | return -1; |
| 7171 | |
| 7172 | if (errors == NULL || strcmp(errors, "strict") == 0) { |
| 7173 | /* The last error was ERROR_NO_UNICODE_TRANSLATION, |
| 7174 | then we raise a UnicodeEncodeError. */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7175 | make_encode_exception(&exc, encoding, unicode, 0, 0, reason); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7176 | if (exc != NULL) { |
| 7177 | PyCodec_StrictErrors(exc); |
| 7178 | Py_DECREF(exc); |
| 7179 | } |
| 7180 | Py_XDECREF(encoding_obj); |
| 7181 | return -1; |
| 7182 | } |
| 7183 | |
| 7184 | if (code_page != CP_UTF8 && code_page != CP_UTF7) |
| 7185 | pusedDefaultChar = &usedDefaultChar; |
| 7186 | else |
| 7187 | pusedDefaultChar = NULL; |
| 7188 | |
| 7189 | if (Py_ARRAY_LENGTH(buffer) > PY_SSIZE_T_MAX / insize) { |
| 7190 | PyErr_NoMemory(); |
| 7191 | goto error; |
| 7192 | } |
| 7193 | outsize = insize * Py_ARRAY_LENGTH(buffer); |
| 7194 | |
| 7195 | if (*outbytes == NULL) { |
| 7196 | /* Create string object */ |
| 7197 | *outbytes = PyBytes_FromStringAndSize(NULL, outsize); |
| 7198 | if (*outbytes == NULL) |
| 7199 | goto error; |
| 7200 | out = PyBytes_AS_STRING(*outbytes); |
| 7201 | } |
| 7202 | else { |
| 7203 | /* Extend string object */ |
| 7204 | Py_ssize_t n = PyBytes_Size(*outbytes); |
| 7205 | if (n > PY_SSIZE_T_MAX - outsize) { |
| 7206 | PyErr_NoMemory(); |
| 7207 | goto error; |
| 7208 | } |
| 7209 | if (_PyBytes_Resize(outbytes, n + outsize) < 0) |
| 7210 | goto error; |
| 7211 | out = PyBytes_AS_STRING(*outbytes) + n; |
| 7212 | } |
| 7213 | |
| 7214 | /* Encode the string character per character */ |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7215 | while (pos < endin) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7216 | { |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7217 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, pos); |
| 7218 | wchar_t chars[2]; |
| 7219 | int charsize; |
| 7220 | if (ch < 0x10000) { |
| 7221 | chars[0] = (wchar_t)ch; |
| 7222 | charsize = 1; |
| 7223 | } |
| 7224 | else { |
| 7225 | ch -= 0x10000; |
| 7226 | chars[0] = 0xd800 + (ch >> 10); |
| 7227 | chars[1] = 0xdc00 + (ch & 0x3ff); |
| 7228 | charsize = 2; |
| 7229 | } |
| 7230 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7231 | outsize = WideCharToMultiByte(code_page, flags, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7232 | chars, charsize, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7233 | buffer, Py_ARRAY_LENGTH(buffer), |
| 7234 | NULL, pusedDefaultChar); |
| 7235 | if (outsize > 0) { |
| 7236 | if (pusedDefaultChar == NULL || !(*pusedDefaultChar)) |
| 7237 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7238 | pos++; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7239 | memcpy(out, buffer, outsize); |
| 7240 | out += outsize; |
| 7241 | continue; |
| 7242 | } |
| 7243 | } |
| 7244 | else if (GetLastError() != ERROR_NO_UNICODE_TRANSLATION) { |
| 7245 | PyErr_SetFromWindowsErr(0); |
| 7246 | goto error; |
| 7247 | } |
| 7248 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7249 | rep = unicode_encode_call_errorhandler( |
| 7250 | errors, &errorHandler, encoding, reason, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7251 | unicode, &exc, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7252 | pos, pos + 1, &newpos); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7253 | if (rep == NULL) |
| 7254 | goto error; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7255 | pos = newpos; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7256 | |
| 7257 | if (PyBytes_Check(rep)) { |
| 7258 | outsize = PyBytes_GET_SIZE(rep); |
| 7259 | if (outsize != 1) { |
| 7260 | Py_ssize_t offset = out - PyBytes_AS_STRING(*outbytes); |
| 7261 | newoutsize = PyBytes_GET_SIZE(*outbytes) + (outsize - 1); |
| 7262 | if (_PyBytes_Resize(outbytes, newoutsize) < 0) { |
| 7263 | Py_DECREF(rep); |
| 7264 | goto error; |
| 7265 | } |
| 7266 | out = PyBytes_AS_STRING(*outbytes) + offset; |
| 7267 | } |
| 7268 | memcpy(out, PyBytes_AS_STRING(rep), outsize); |
| 7269 | out += outsize; |
| 7270 | } |
| 7271 | else { |
| 7272 | Py_ssize_t i; |
| 7273 | enum PyUnicode_Kind kind; |
| 7274 | void *data; |
| 7275 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7276 | if (PyUnicode_READY(rep) == -1) { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7277 | Py_DECREF(rep); |
| 7278 | goto error; |
| 7279 | } |
| 7280 | |
| 7281 | outsize = PyUnicode_GET_LENGTH(rep); |
| 7282 | if (outsize != 1) { |
| 7283 | Py_ssize_t offset = out - PyBytes_AS_STRING(*outbytes); |
| 7284 | newoutsize = PyBytes_GET_SIZE(*outbytes) + (outsize - 1); |
| 7285 | if (_PyBytes_Resize(outbytes, newoutsize) < 0) { |
| 7286 | Py_DECREF(rep); |
| 7287 | goto error; |
| 7288 | } |
| 7289 | out = PyBytes_AS_STRING(*outbytes) + offset; |
| 7290 | } |
| 7291 | kind = PyUnicode_KIND(rep); |
| 7292 | data = PyUnicode_DATA(rep); |
| 7293 | for (i=0; i < outsize; i++) { |
| 7294 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 7295 | if (ch > 127) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7296 | raise_encode_exception(&exc, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7297 | encoding, unicode, |
| 7298 | pos, pos + 1, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7299 | "unable to encode error handler result to ASCII"); |
| 7300 | Py_DECREF(rep); |
| 7301 | goto error; |
| 7302 | } |
| 7303 | *out = (unsigned char)ch; |
| 7304 | out++; |
| 7305 | } |
| 7306 | } |
| 7307 | Py_DECREF(rep); |
| 7308 | } |
| 7309 | /* write a NUL byte */ |
| 7310 | *out = 0; |
| 7311 | outsize = out - PyBytes_AS_STRING(*outbytes); |
| 7312 | assert(outsize <= PyBytes_GET_SIZE(*outbytes)); |
| 7313 | if (_PyBytes_Resize(outbytes, outsize) < 0) |
| 7314 | goto error; |
| 7315 | ret = 0; |
| 7316 | |
| 7317 | error: |
| 7318 | Py_XDECREF(encoding_obj); |
| 7319 | Py_XDECREF(errorHandler); |
| 7320 | Py_XDECREF(exc); |
| 7321 | return ret; |
| 7322 | } |
| 7323 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7324 | static PyObject * |
| 7325 | encode_code_page(int code_page, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7326 | PyObject *unicode, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7327 | const char *errors) |
| 7328 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7329 | Py_ssize_t len; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7330 | PyObject *outbytes = NULL; |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7331 | Py_ssize_t offset; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7332 | int chunk_len, ret, done; |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7333 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7334 | if (PyUnicode_READY(unicode) == -1) |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7335 | return NULL; |
| 7336 | len = PyUnicode_GET_LENGTH(unicode); |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 7337 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7338 | if (code_page < 0) { |
| 7339 | PyErr_SetString(PyExc_ValueError, "invalid code page number"); |
| 7340 | return NULL; |
| 7341 | } |
| 7342 | |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7343 | if (len == 0) |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7344 | return PyBytes_FromStringAndSize(NULL, 0); |
| 7345 | |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7346 | offset = 0; |
| 7347 | do |
| 7348 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7349 | #ifdef NEED_RETRY |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7350 | /* 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] | 7351 | chunks. */ |
| 7352 | if (len > INT_MAX/2) { |
| 7353 | chunk_len = INT_MAX/2; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7354 | done = 0; |
| 7355 | } |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7356 | else |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7357 | #endif |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7358 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7359 | chunk_len = (int)len; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7360 | done = 1; |
| 7361 | } |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7362 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7363 | ret = encode_code_page_strict(code_page, &outbytes, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7364 | unicode, offset, chunk_len, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7365 | errors); |
| 7366 | if (ret == -2) |
| 7367 | ret = encode_code_page_errors(code_page, &outbytes, |
| 7368 | unicode, offset, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7369 | chunk_len, errors); |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7370 | if (ret < 0) { |
| 7371 | Py_XDECREF(outbytes); |
| 7372 | return NULL; |
| 7373 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7374 | |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7375 | offset += chunk_len; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7376 | len -= chunk_len; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7377 | } while (!done); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7378 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7379 | return outbytes; |
| 7380 | } |
| 7381 | |
| 7382 | PyObject * |
| 7383 | PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
| 7384 | Py_ssize_t size, |
| 7385 | const char *errors) |
| 7386 | { |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7387 | PyObject *unicode, *res; |
| 7388 | unicode = PyUnicode_FromUnicode(p, size); |
| 7389 | if (unicode == NULL) |
| 7390 | return NULL; |
| 7391 | res = encode_code_page(CP_ACP, unicode, errors); |
| 7392 | Py_DECREF(unicode); |
| 7393 | return res; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7394 | } |
| 7395 | |
| 7396 | PyObject * |
| 7397 | PyUnicode_EncodeCodePage(int code_page, |
| 7398 | PyObject *unicode, |
| 7399 | const char *errors) |
| 7400 | { |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7401 | return encode_code_page(code_page, unicode, errors); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7402 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 7403 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7404 | PyObject * |
| 7405 | PyUnicode_AsMBCSString(PyObject *unicode) |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7406 | { |
| 7407 | if (!PyUnicode_Check(unicode)) { |
| 7408 | PyErr_BadArgument(); |
| 7409 | return NULL; |
| 7410 | } |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7411 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, NULL); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7412 | } |
| 7413 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7414 | #undef NEED_RETRY |
| 7415 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 7416 | #endif /* HAVE_MBCS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7417 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7418 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 7419 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7420 | PyObject * |
| 7421 | PyUnicode_DecodeCharmap(const char *s, |
| 7422 | Py_ssize_t size, |
| 7423 | PyObject *mapping, |
| 7424 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7425 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7426 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7427 | Py_ssize_t startinpos; |
| 7428 | Py_ssize_t endinpos; |
| 7429 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7430 | const char *e; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 7431 | PyObject *v; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7432 | Py_ssize_t extrachars = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7433 | PyObject *errorHandler = NULL; |
| 7434 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7435 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7436 | /* Default to Latin-1 */ |
| 7437 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7438 | return PyUnicode_DecodeLatin1(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7439 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7440 | v = PyUnicode_New(size, 127); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7441 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7442 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7443 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 7444 | return v; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7445 | outpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7446 | e = s + size; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7447 | if (PyUnicode_CheckExact(mapping)) { |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7448 | Py_ssize_t maplen; |
| 7449 | enum PyUnicode_Kind kind; |
| 7450 | void *data; |
| 7451 | Py_UCS4 x; |
| 7452 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7453 | if (PyUnicode_READY(mapping) == -1) |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7454 | return NULL; |
| 7455 | |
| 7456 | maplen = PyUnicode_GET_LENGTH(mapping); |
| 7457 | data = PyUnicode_DATA(mapping); |
| 7458 | kind = PyUnicode_KIND(mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7459 | while (s < e) { |
| 7460 | unsigned char ch = *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7461 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7462 | if (ch < maplen) |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7463 | x = PyUnicode_READ(kind, data, ch); |
| 7464 | else |
| 7465 | x = 0xfffe; /* invalid value */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7466 | |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7467 | if (x == 0xfffe) |
| 7468 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7469 | /* undefined mapping */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7470 | startinpos = s-starts; |
| 7471 | endinpos = startinpos+1; |
| 7472 | if (unicode_decode_call_errorhandler( |
| 7473 | errors, &errorHandler, |
| 7474 | "charmap", "character maps to <undefined>", |
| 7475 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7476 | &v, &outpos)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7477 | goto onError; |
| 7478 | } |
| 7479 | continue; |
| 7480 | } |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7481 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7482 | if (unicode_putchar(&v, &outpos, x) < 0) |
| 7483 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7484 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7485 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7486 | } |
| 7487 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7488 | while (s < e) { |
| 7489 | unsigned char ch = *s; |
| 7490 | PyObject *w, *x; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7491 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7492 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 7493 | w = PyLong_FromLong((long)ch); |
| 7494 | if (w == NULL) |
| 7495 | goto onError; |
| 7496 | x = PyObject_GetItem(mapping, w); |
| 7497 | Py_DECREF(w); |
| 7498 | if (x == NULL) { |
| 7499 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 7500 | /* No mapping found means: mapping is undefined. */ |
| 7501 | PyErr_Clear(); |
| 7502 | x = Py_None; |
| 7503 | Py_INCREF(x); |
| 7504 | } else |
| 7505 | goto onError; |
| 7506 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7507 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7508 | /* Apply mapping */ |
| 7509 | if (PyLong_Check(x)) { |
| 7510 | long value = PyLong_AS_LONG(x); |
| 7511 | if (value < 0 || value > 65535) { |
| 7512 | PyErr_SetString(PyExc_TypeError, |
| 7513 | "character mapping must be in range(65536)"); |
| 7514 | Py_DECREF(x); |
| 7515 | goto onError; |
| 7516 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7517 | if (unicode_putchar(&v, &outpos, value) < 0) |
| 7518 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7519 | } |
| 7520 | else if (x == Py_None) { |
| 7521 | /* undefined mapping */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7522 | startinpos = s-starts; |
| 7523 | endinpos = startinpos+1; |
| 7524 | if (unicode_decode_call_errorhandler( |
| 7525 | errors, &errorHandler, |
| 7526 | "charmap", "character maps to <undefined>", |
| 7527 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7528 | &v, &outpos)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7529 | Py_DECREF(x); |
| 7530 | goto onError; |
| 7531 | } |
| 7532 | Py_DECREF(x); |
| 7533 | continue; |
| 7534 | } |
| 7535 | else if (PyUnicode_Check(x)) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7536 | Py_ssize_t targetsize; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7537 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7538 | if (PyUnicode_READY(x) == -1) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7539 | goto onError; |
| 7540 | targetsize = PyUnicode_GET_LENGTH(x); |
| 7541 | |
| 7542 | if (targetsize == 1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7543 | /* 1-1 mapping */ |
Victor Stinner | 62aa4d0 | 2011-11-09 00:03:45 +0100 | [diff] [blame] | 7544 | if (unicode_putchar(&v, &outpos, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7545 | PyUnicode_READ_CHAR(x, 0)) < 0) |
| 7546 | goto onError; |
| 7547 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7548 | else if (targetsize > 1) { |
| 7549 | /* 1-n mapping */ |
| 7550 | if (targetsize > extrachars) { |
| 7551 | /* resize first */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7552 | Py_ssize_t needed = (targetsize - extrachars) + \ |
| 7553 | (targetsize << 2); |
| 7554 | extrachars += needed; |
| 7555 | /* XXX overflow detection missing */ |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 7556 | if (unicode_resize(&v, |
| 7557 | PyUnicode_GET_LENGTH(v) + needed) < 0) |
| 7558 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7559 | Py_DECREF(x); |
| 7560 | goto onError; |
| 7561 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7562 | } |
Victor Stinner | 1b487b4 | 2012-05-03 12:29:04 +0200 | [diff] [blame] | 7563 | 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] | 7564 | goto onError; |
| 7565 | PyUnicode_CopyCharacters(v, outpos, x, 0, targetsize); |
| 7566 | outpos += targetsize; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7567 | extrachars -= targetsize; |
| 7568 | } |
| 7569 | /* 1-0 mapping: skip the character */ |
| 7570 | } |
| 7571 | else { |
| 7572 | /* wrong return value */ |
| 7573 | PyErr_SetString(PyExc_TypeError, |
| 7574 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7575 | Py_DECREF(x); |
| 7576 | goto onError; |
| 7577 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7578 | Py_DECREF(x); |
| 7579 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7580 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7581 | } |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 7582 | if (unicode_resize(&v, outpos) < 0) |
Antoine Pitrou | a8f63c0 | 2011-11-08 18:37:16 +0100 | [diff] [blame] | 7583 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7584 | Py_XDECREF(errorHandler); |
| 7585 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 7586 | return unicode_result(v); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7587 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7588 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7589 | Py_XDECREF(errorHandler); |
| 7590 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7591 | Py_XDECREF(v); |
| 7592 | return NULL; |
| 7593 | } |
| 7594 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7595 | /* Charmap encoding: the lookup table */ |
| 7596 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7597 | struct encoding_map { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7598 | PyObject_HEAD |
| 7599 | unsigned char level1[32]; |
| 7600 | int count2, count3; |
| 7601 | unsigned char level23[1]; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7602 | }; |
| 7603 | |
| 7604 | static PyObject* |
| 7605 | encoding_map_size(PyObject *obj, PyObject* args) |
| 7606 | { |
| 7607 | struct encoding_map *map = (struct encoding_map*)obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7608 | return PyLong_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7609 | 128*map->count3); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7610 | } |
| 7611 | |
| 7612 | static PyMethodDef encoding_map_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7613 | {"size", encoding_map_size, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7614 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 7615 | { 0 } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7616 | }; |
| 7617 | |
| 7618 | static void |
| 7619 | encoding_map_dealloc(PyObject* o) |
| 7620 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7621 | PyObject_FREE(o); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7622 | } |
| 7623 | |
| 7624 | static PyTypeObject EncodingMapType = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7625 | PyVarObject_HEAD_INIT(NULL, 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7626 | "EncodingMap", /*tp_name*/ |
| 7627 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 7628 | 0, /*tp_itemsize*/ |
| 7629 | /* methods */ |
| 7630 | encoding_map_dealloc, /*tp_dealloc*/ |
| 7631 | 0, /*tp_print*/ |
| 7632 | 0, /*tp_getattr*/ |
| 7633 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 7634 | 0, /*tp_reserved*/ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7635 | 0, /*tp_repr*/ |
| 7636 | 0, /*tp_as_number*/ |
| 7637 | 0, /*tp_as_sequence*/ |
| 7638 | 0, /*tp_as_mapping*/ |
| 7639 | 0, /*tp_hash*/ |
| 7640 | 0, /*tp_call*/ |
| 7641 | 0, /*tp_str*/ |
| 7642 | 0, /*tp_getattro*/ |
| 7643 | 0, /*tp_setattro*/ |
| 7644 | 0, /*tp_as_buffer*/ |
| 7645 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 7646 | 0, /*tp_doc*/ |
| 7647 | 0, /*tp_traverse*/ |
| 7648 | 0, /*tp_clear*/ |
| 7649 | 0, /*tp_richcompare*/ |
| 7650 | 0, /*tp_weaklistoffset*/ |
| 7651 | 0, /*tp_iter*/ |
| 7652 | 0, /*tp_iternext*/ |
| 7653 | encoding_map_methods, /*tp_methods*/ |
| 7654 | 0, /*tp_members*/ |
| 7655 | 0, /*tp_getset*/ |
| 7656 | 0, /*tp_base*/ |
| 7657 | 0, /*tp_dict*/ |
| 7658 | 0, /*tp_descr_get*/ |
| 7659 | 0, /*tp_descr_set*/ |
| 7660 | 0, /*tp_dictoffset*/ |
| 7661 | 0, /*tp_init*/ |
| 7662 | 0, /*tp_alloc*/ |
| 7663 | 0, /*tp_new*/ |
| 7664 | 0, /*tp_free*/ |
| 7665 | 0, /*tp_is_gc*/ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7666 | }; |
| 7667 | |
| 7668 | PyObject* |
| 7669 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 7670 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7671 | PyObject *result; |
| 7672 | struct encoding_map *mresult; |
| 7673 | int i; |
| 7674 | int need_dict = 0; |
| 7675 | unsigned char level1[32]; |
| 7676 | unsigned char level2[512]; |
| 7677 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 7678 | int count2 = 0, count3 = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7679 | int kind; |
| 7680 | void *data; |
| 7681 | Py_UCS4 ch; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7682 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7683 | if (!PyUnicode_Check(string) || PyUnicode_GET_LENGTH(string) != 256) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7684 | PyErr_BadArgument(); |
| 7685 | return NULL; |
| 7686 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7687 | kind = PyUnicode_KIND(string); |
| 7688 | data = PyUnicode_DATA(string); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7689 | memset(level1, 0xFF, sizeof level1); |
| 7690 | memset(level2, 0xFF, sizeof level2); |
| 7691 | |
| 7692 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 7693 | or if there are non-BMP characters, we need to use |
| 7694 | a mapping dictionary. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7695 | if (PyUnicode_READ(kind, data, 0) != 0) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7696 | need_dict = 1; |
| 7697 | for (i = 1; i < 256; i++) { |
| 7698 | int l1, l2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7699 | ch = PyUnicode_READ(kind, data, i); |
| 7700 | if (ch == 0 || ch > 0xFFFF) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7701 | need_dict = 1; |
| 7702 | break; |
| 7703 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7704 | if (ch == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7705 | /* unmapped character */ |
| 7706 | continue; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7707 | l1 = ch >> 11; |
| 7708 | l2 = ch >> 7; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7709 | if (level1[l1] == 0xFF) |
| 7710 | level1[l1] = count2++; |
| 7711 | if (level2[l2] == 0xFF) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7712 | level2[l2] = count3++; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7713 | } |
| 7714 | |
| 7715 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 7716 | need_dict = 1; |
| 7717 | |
| 7718 | if (need_dict) { |
| 7719 | PyObject *result = PyDict_New(); |
| 7720 | PyObject *key, *value; |
| 7721 | if (!result) |
| 7722 | return NULL; |
| 7723 | for (i = 0; i < 256; i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7724 | key = PyLong_FromLong(PyUnicode_READ(kind, data, i)); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7725 | value = PyLong_FromLong(i); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7726 | if (!key || !value) |
| 7727 | goto failed1; |
| 7728 | if (PyDict_SetItem(result, key, value) == -1) |
| 7729 | goto failed1; |
| 7730 | Py_DECREF(key); |
| 7731 | Py_DECREF(value); |
| 7732 | } |
| 7733 | return result; |
| 7734 | failed1: |
| 7735 | Py_XDECREF(key); |
| 7736 | Py_XDECREF(value); |
| 7737 | Py_DECREF(result); |
| 7738 | return NULL; |
| 7739 | } |
| 7740 | |
| 7741 | /* Create a three-level trie */ |
| 7742 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 7743 | 16*count2 + 128*count3 - 1); |
| 7744 | if (!result) |
| 7745 | return PyErr_NoMemory(); |
| 7746 | PyObject_Init(result, &EncodingMapType); |
| 7747 | mresult = (struct encoding_map*)result; |
| 7748 | mresult->count2 = count2; |
| 7749 | mresult->count3 = count3; |
| 7750 | mlevel1 = mresult->level1; |
| 7751 | mlevel2 = mresult->level23; |
| 7752 | mlevel3 = mresult->level23 + 16*count2; |
| 7753 | memcpy(mlevel1, level1, 32); |
| 7754 | memset(mlevel2, 0xFF, 16*count2); |
| 7755 | memset(mlevel3, 0, 128*count3); |
| 7756 | count3 = 0; |
| 7757 | for (i = 1; i < 256; i++) { |
| 7758 | int o1, o2, o3, i2, i3; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7759 | if (PyUnicode_READ(kind, data, i) == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7760 | /* unmapped character */ |
| 7761 | continue; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7762 | o1 = PyUnicode_READ(kind, data, i)>>11; |
| 7763 | o2 = (PyUnicode_READ(kind, data, i)>>7) & 0xF; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7764 | i2 = 16*mlevel1[o1] + o2; |
| 7765 | if (mlevel2[i2] == 0xFF) |
| 7766 | mlevel2[i2] = count3++; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7767 | o3 = PyUnicode_READ(kind, data, i) & 0x7F; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7768 | i3 = 128*mlevel2[i2] + o3; |
| 7769 | mlevel3[i3] = i; |
| 7770 | } |
| 7771 | return result; |
| 7772 | } |
| 7773 | |
| 7774 | static int |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 7775 | encoding_map_lookup(Py_UCS4 c, PyObject *mapping) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7776 | { |
| 7777 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 7778 | int l1 = c>>11; |
| 7779 | int l2 = (c>>7) & 0xF; |
| 7780 | int l3 = c & 0x7F; |
| 7781 | int i; |
| 7782 | |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 7783 | if (c > 0xFFFF) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7784 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7785 | if (c == 0) |
| 7786 | return 0; |
| 7787 | /* level 1*/ |
| 7788 | i = map->level1[l1]; |
| 7789 | if (i == 0xFF) { |
| 7790 | return -1; |
| 7791 | } |
| 7792 | /* level 2*/ |
| 7793 | i = map->level23[16*i+l2]; |
| 7794 | if (i == 0xFF) { |
| 7795 | return -1; |
| 7796 | } |
| 7797 | /* level 3 */ |
| 7798 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 7799 | if (i == 0) { |
| 7800 | return -1; |
| 7801 | } |
| 7802 | return i; |
| 7803 | } |
| 7804 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7805 | /* Lookup the character ch in the mapping. If the character |
| 7806 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 7807 | error occurred). */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7808 | static PyObject * |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 7809 | charmapencode_lookup(Py_UCS4 c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7810 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7811 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7812 | PyObject *x; |
| 7813 | |
| 7814 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7815 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7816 | x = PyObject_GetItem(mapping, w); |
| 7817 | Py_DECREF(w); |
| 7818 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7819 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 7820 | /* No mapping found means: mapping is undefined. */ |
| 7821 | PyErr_Clear(); |
| 7822 | x = Py_None; |
| 7823 | Py_INCREF(x); |
| 7824 | return x; |
| 7825 | } else |
| 7826 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7827 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 7828 | else if (x == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7829 | return x; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7830 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7831 | long value = PyLong_AS_LONG(x); |
| 7832 | if (value < 0 || value > 255) { |
| 7833 | PyErr_SetString(PyExc_TypeError, |
| 7834 | "character mapping must be in range(256)"); |
| 7835 | Py_DECREF(x); |
| 7836 | return NULL; |
| 7837 | } |
| 7838 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7839 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7840 | else if (PyBytes_Check(x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7841 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7842 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7843 | /* wrong return value */ |
| 7844 | PyErr_Format(PyExc_TypeError, |
| 7845 | "character mapping must return integer, bytes or None, not %.400s", |
| 7846 | x->ob_type->tp_name); |
| 7847 | Py_DECREF(x); |
| 7848 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7849 | } |
| 7850 | } |
| 7851 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7852 | static int |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 7853 | charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7854 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7855 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
| 7856 | /* exponentially overallocate to minimize reallocations */ |
| 7857 | if (requiredsize < 2*outsize) |
| 7858 | requiredsize = 2*outsize; |
| 7859 | if (_PyBytes_Resize(outobj, requiredsize)) |
| 7860 | return -1; |
| 7861 | return 0; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7862 | } |
| 7863 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7864 | typedef enum charmapencode_result { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7865 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7866 | } charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7867 | /* 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] | 7868 | various state variables. Resize the output bytes object if not enough |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7869 | space is available. Return a new reference to the object that |
| 7870 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 7871 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 7872 | reallocation error occurred. The caller must decref the result */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7873 | static charmapencode_result |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 7874 | charmapencode_output(Py_UCS4 c, PyObject *mapping, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7875 | PyObject **outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7876 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7877 | PyObject *rep; |
| 7878 | char *outstart; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7879 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7880 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7881 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7882 | int res = encoding_map_lookup(c, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7883 | Py_ssize_t requiredsize = *outpos+1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7884 | if (res == -1) |
| 7885 | return enc_FAILED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7886 | if (outsize<requiredsize) |
| 7887 | if (charmapencode_resize(outobj, outpos, requiredsize)) |
| 7888 | return enc_EXCEPTION; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7889 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7890 | outstart[(*outpos)++] = (char)res; |
| 7891 | return enc_SUCCESS; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7892 | } |
| 7893 | |
| 7894 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7895 | if (rep==NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7896 | return enc_EXCEPTION; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7897 | else if (rep==Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7898 | Py_DECREF(rep); |
| 7899 | return enc_FAILED; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7900 | } else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7901 | if (PyLong_Check(rep)) { |
| 7902 | Py_ssize_t requiredsize = *outpos+1; |
| 7903 | if (outsize<requiredsize) |
| 7904 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 7905 | Py_DECREF(rep); |
| 7906 | return enc_EXCEPTION; |
| 7907 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7908 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7909 | outstart[(*outpos)++] = (char)PyLong_AS_LONG(rep); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7910 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7911 | else { |
| 7912 | const char *repchars = PyBytes_AS_STRING(rep); |
| 7913 | Py_ssize_t repsize = PyBytes_GET_SIZE(rep); |
| 7914 | Py_ssize_t requiredsize = *outpos+repsize; |
| 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 | memcpy(outstart + *outpos, repchars, repsize); |
| 7922 | *outpos += repsize; |
| 7923 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7924 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7925 | Py_DECREF(rep); |
| 7926 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7927 | } |
| 7928 | |
| 7929 | /* handle an error in PyUnicode_EncodeCharmap |
| 7930 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7931 | static int |
| 7932 | charmap_encoding_error( |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7933 | PyObject *unicode, Py_ssize_t *inpos, PyObject *mapping, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7934 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 7935 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 7936 | PyObject **res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7937 | { |
| 7938 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7939 | Py_ssize_t size, repsize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7940 | Py_ssize_t newpos; |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 7941 | enum PyUnicode_Kind kind; |
| 7942 | void *data; |
| 7943 | Py_ssize_t index; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7944 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7945 | Py_ssize_t collstartpos = *inpos; |
| 7946 | Py_ssize_t collendpos = *inpos+1; |
| 7947 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7948 | char *encoding = "charmap"; |
| 7949 | char *reason = "character maps to <undefined>"; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7950 | charmapencode_result x; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7951 | Py_UCS4 ch; |
Brian Curtin | 2787ea4 | 2011-11-02 15:09:37 -0500 | [diff] [blame] | 7952 | int val; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7953 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7954 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7955 | return -1; |
| 7956 | size = PyUnicode_GET_LENGTH(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7957 | /* find all unencodable characters */ |
| 7958 | while (collendpos < size) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7959 | PyObject *rep; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7960 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7961 | ch = PyUnicode_READ_CHAR(unicode, collendpos); |
Brian Curtin | 2787ea4 | 2011-11-02 15:09:37 -0500 | [diff] [blame] | 7962 | val = encoding_map_lookup(ch, mapping); |
| 7963 | if (val != -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7964 | break; |
| 7965 | ++collendpos; |
| 7966 | continue; |
| 7967 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7968 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7969 | ch = PyUnicode_READ_CHAR(unicode, collendpos); |
| 7970 | rep = charmapencode_lookup(ch, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7971 | if (rep==NULL) |
| 7972 | return -1; |
| 7973 | else if (rep!=Py_None) { |
| 7974 | Py_DECREF(rep); |
| 7975 | break; |
| 7976 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7977 | Py_DECREF(rep); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7978 | ++collendpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7979 | } |
| 7980 | /* cache callback name lookup |
| 7981 | * (if not done yet, i.e. it's the first error) */ |
| 7982 | if (*known_errorHandler==-1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7983 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 7984 | *known_errorHandler = 1; |
| 7985 | else if (!strcmp(errors, "replace")) |
| 7986 | *known_errorHandler = 2; |
| 7987 | else if (!strcmp(errors, "ignore")) |
| 7988 | *known_errorHandler = 3; |
| 7989 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 7990 | *known_errorHandler = 4; |
| 7991 | else |
| 7992 | *known_errorHandler = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7993 | } |
| 7994 | switch (*known_errorHandler) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7995 | case 1: /* strict */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7996 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7997 | return -1; |
| 7998 | case 2: /* replace */ |
| 7999 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8000 | x = charmapencode_output('?', mapping, res, respos); |
| 8001 | if (x==enc_EXCEPTION) { |
| 8002 | return -1; |
| 8003 | } |
| 8004 | else if (x==enc_FAILED) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8005 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8006 | return -1; |
| 8007 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8008 | } |
| 8009 | /* fall through */ |
| 8010 | case 3: /* ignore */ |
| 8011 | *inpos = collendpos; |
| 8012 | break; |
| 8013 | case 4: /* xmlcharrefreplace */ |
| 8014 | /* generate replacement (temporarily (mis)uses p) */ |
| 8015 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8016 | char buffer[2+29+1+1]; |
| 8017 | char *cp; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8018 | sprintf(buffer, "&#%d;", (int)PyUnicode_READ_CHAR(unicode, collpos)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8019 | for (cp = buffer; *cp; ++cp) { |
| 8020 | x = charmapencode_output(*cp, mapping, res, respos); |
| 8021 | if (x==enc_EXCEPTION) |
| 8022 | return -1; |
| 8023 | else if (x==enc_FAILED) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8024 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8025 | return -1; |
| 8026 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8027 | } |
| 8028 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8029 | *inpos = collendpos; |
| 8030 | break; |
| 8031 | default: |
| 8032 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8033 | encoding, reason, unicode, exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8034 | collstartpos, collendpos, &newpos); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8035 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8036 | return -1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8037 | if (PyBytes_Check(repunicode)) { |
| 8038 | /* Directly copy bytes result to output. */ |
| 8039 | Py_ssize_t outsize = PyBytes_Size(*res); |
| 8040 | Py_ssize_t requiredsize; |
| 8041 | repsize = PyBytes_Size(repunicode); |
| 8042 | requiredsize = *respos + repsize; |
| 8043 | if (requiredsize > outsize) |
| 8044 | /* Make room for all additional bytes. */ |
| 8045 | if (charmapencode_resize(res, respos, requiredsize)) { |
| 8046 | Py_DECREF(repunicode); |
| 8047 | return -1; |
| 8048 | } |
| 8049 | memcpy(PyBytes_AsString(*res) + *respos, |
| 8050 | PyBytes_AsString(repunicode), repsize); |
| 8051 | *respos += repsize; |
| 8052 | *inpos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8053 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8054 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8055 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8056 | /* generate replacement */ |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 8057 | if (PyUnicode_READY(repunicode) == -1) { |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8058 | Py_DECREF(repunicode); |
| 8059 | return -1; |
| 8060 | } |
Victor Stinner | 9e30aa5 | 2011-11-21 02:49:52 +0100 | [diff] [blame] | 8061 | repsize = PyUnicode_GET_LENGTH(repunicode); |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8062 | data = PyUnicode_DATA(repunicode); |
| 8063 | kind = PyUnicode_KIND(repunicode); |
| 8064 | for (index = 0; index < repsize; index++) { |
| 8065 | Py_UCS4 repch = PyUnicode_READ(kind, data, index); |
| 8066 | x = charmapencode_output(repch, mapping, res, respos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8067 | if (x==enc_EXCEPTION) { |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8068 | Py_DECREF(repunicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8069 | return -1; |
| 8070 | } |
| 8071 | else if (x==enc_FAILED) { |
| 8072 | Py_DECREF(repunicode); |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8073 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8074 | return -1; |
| 8075 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8076 | } |
| 8077 | *inpos = newpos; |
| 8078 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8079 | } |
| 8080 | return 0; |
| 8081 | } |
| 8082 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8083 | PyObject * |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8084 | _PyUnicode_EncodeCharmap(PyObject *unicode, |
| 8085 | PyObject *mapping, |
| 8086 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8087 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8088 | /* output object */ |
| 8089 | PyObject *res = NULL; |
| 8090 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8091 | Py_ssize_t inpos = 0; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8092 | Py_ssize_t size; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8093 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8094 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8095 | PyObject *errorHandler = NULL; |
| 8096 | PyObject *exc = NULL; |
| 8097 | /* the following variable is used for caching string comparisons |
| 8098 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 8099 | * 3=ignore, 4=xmlcharrefreplace */ |
| 8100 | int known_errorHandler = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8101 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 8102 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8103 | return NULL; |
| 8104 | size = PyUnicode_GET_LENGTH(unicode); |
| 8105 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8106 | /* Default to Latin-1 */ |
| 8107 | if (mapping == NULL) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8108 | return unicode_encode_ucs1(unicode, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8109 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8110 | /* allocate enough for a simple encoding without |
| 8111 | replacements, if we need more, we'll resize */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8112 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8113 | if (res == NULL) |
| 8114 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 8115 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8116 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8117 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8118 | while (inpos<size) { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8119 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, inpos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8120 | /* try to encode it */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8121 | charmapencode_result x = charmapencode_output(ch, mapping, &res, &respos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8122 | if (x==enc_EXCEPTION) /* error */ |
| 8123 | goto onError; |
| 8124 | if (x==enc_FAILED) { /* unencodable character */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8125 | if (charmap_encoding_error(unicode, &inpos, mapping, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8126 | &exc, |
| 8127 | &known_errorHandler, &errorHandler, errors, |
| 8128 | &res, &respos)) { |
| 8129 | goto onError; |
| 8130 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8131 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8132 | else |
| 8133 | /* done with this character => adjust input position */ |
| 8134 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8135 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8136 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8137 | /* Resize if we allocated to much */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8138 | if (respos<PyBytes_GET_SIZE(res)) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 8139 | if (_PyBytes_Resize(&res, respos) < 0) |
| 8140 | goto onError; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8141 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8142 | Py_XDECREF(exc); |
| 8143 | Py_XDECREF(errorHandler); |
| 8144 | return res; |
| 8145 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8146 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8147 | Py_XDECREF(res); |
| 8148 | Py_XDECREF(exc); |
| 8149 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8150 | return NULL; |
| 8151 | } |
| 8152 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8153 | /* Deprecated */ |
| 8154 | PyObject * |
| 8155 | PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
| 8156 | Py_ssize_t size, |
| 8157 | PyObject *mapping, |
| 8158 | const char *errors) |
| 8159 | { |
| 8160 | PyObject *result; |
| 8161 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 8162 | if (unicode == NULL) |
| 8163 | return NULL; |
| 8164 | result = _PyUnicode_EncodeCharmap(unicode, mapping, errors); |
| 8165 | Py_DECREF(unicode); |
Victor Stinner | fc026c9 | 2011-11-04 00:24:51 +0100 | [diff] [blame] | 8166 | return result; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8167 | } |
| 8168 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8169 | PyObject * |
| 8170 | PyUnicode_AsCharmapString(PyObject *unicode, |
| 8171 | PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8172 | { |
| 8173 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8174 | PyErr_BadArgument(); |
| 8175 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8176 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8177 | return _PyUnicode_EncodeCharmap(unicode, mapping, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8178 | } |
| 8179 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8180 | /* create or adjust a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8181 | static void |
| 8182 | make_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8183 | PyObject *unicode, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8184 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8185 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8186 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8187 | if (*exceptionObject == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8188 | *exceptionObject = _PyUnicodeTranslateError_Create( |
| 8189 | unicode, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8190 | } |
| 8191 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8192 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 8193 | goto onError; |
| 8194 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 8195 | goto onError; |
| 8196 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 8197 | goto onError; |
| 8198 | return; |
| 8199 | onError: |
| 8200 | Py_DECREF(*exceptionObject); |
| 8201 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8202 | } |
| 8203 | } |
| 8204 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8205 | /* raises a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8206 | static void |
| 8207 | raise_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8208 | PyObject *unicode, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8209 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8210 | const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8211 | { |
| 8212 | make_translate_exception(exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8213 | unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8214 | if (*exceptionObject != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8215 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8216 | } |
| 8217 | |
| 8218 | /* error handling callback helper: |
| 8219 | build arguments, call the callback and check the arguments, |
| 8220 | put the result into newpos and return the replacement string, which |
| 8221 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8222 | static PyObject * |
| 8223 | unicode_translate_call_errorhandler(const char *errors, |
| 8224 | PyObject **errorHandler, |
| 8225 | const char *reason, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8226 | PyObject *unicode, PyObject **exceptionObject, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8227 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8228 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8229 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8230 | 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] | 8231 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8232 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8233 | PyObject *restuple; |
| 8234 | PyObject *resunicode; |
| 8235 | |
| 8236 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8237 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8238 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8239 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8240 | } |
| 8241 | |
| 8242 | make_translate_exception(exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8243 | unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8244 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8245 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8246 | |
| 8247 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8248 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8249 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8250 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8251 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 8252 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8253 | Py_DECREF(restuple); |
| 8254 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8255 | } |
| 8256 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8257 | &resunicode, &i_newpos)) { |
| 8258 | Py_DECREF(restuple); |
| 8259 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8260 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8261 | if (i_newpos<0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8262 | *newpos = PyUnicode_GET_LENGTH(unicode)+i_newpos; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8263 | else |
| 8264 | *newpos = i_newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8265 | if (*newpos<0 || *newpos>PyUnicode_GET_LENGTH(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8266 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 8267 | Py_DECREF(restuple); |
| 8268 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 8269 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8270 | Py_INCREF(resunicode); |
| 8271 | Py_DECREF(restuple); |
| 8272 | return resunicode; |
| 8273 | } |
| 8274 | |
| 8275 | /* Lookup the character ch in the mapping and put the result in result, |
| 8276 | which must be decrefed by the caller. |
| 8277 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8278 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8279 | charmaptranslate_lookup(Py_UCS4 c, PyObject *mapping, PyObject **result) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8280 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8281 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8282 | PyObject *x; |
| 8283 | |
| 8284 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8285 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8286 | x = PyObject_GetItem(mapping, w); |
| 8287 | Py_DECREF(w); |
| 8288 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8289 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 8290 | /* No mapping found means: use 1:1 mapping. */ |
| 8291 | PyErr_Clear(); |
| 8292 | *result = NULL; |
| 8293 | return 0; |
| 8294 | } else |
| 8295 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8296 | } |
| 8297 | else if (x == Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8298 | *result = x; |
| 8299 | return 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8300 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8301 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8302 | long value = PyLong_AS_LONG(x); |
| 8303 | long max = PyUnicode_GetMax(); |
| 8304 | if (value < 0 || value > max) { |
| 8305 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5a2f7e60 | 2007-10-24 21:13:09 +0000 | [diff] [blame] | 8306 | "character mapping must be in range(0x%x)", max+1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8307 | Py_DECREF(x); |
| 8308 | return -1; |
| 8309 | } |
| 8310 | *result = x; |
| 8311 | return 0; |
| 8312 | } |
| 8313 | else if (PyUnicode_Check(x)) { |
| 8314 | *result = x; |
| 8315 | return 0; |
| 8316 | } |
| 8317 | else { |
| 8318 | /* wrong return value */ |
| 8319 | PyErr_SetString(PyExc_TypeError, |
| 8320 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8321 | Py_DECREF(x); |
| 8322 | return -1; |
| 8323 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8324 | } |
| 8325 | /* ensure that *outobj is at least requiredsize characters long, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8326 | if not reallocate and adjust various state variables. |
| 8327 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8328 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8329 | charmaptranslate_makespace(Py_UCS4 **outobj, Py_ssize_t *psize, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8330 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8331 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8332 | Py_ssize_t oldsize = *psize; |
Kristjan Valur Jonsson | 85634d7 | 2012-05-31 09:37:31 +0000 | [diff] [blame] | 8333 | Py_UCS4 *new_outobj; |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 8334 | if (requiredsize > oldsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8335 | /* exponentially overallocate to minimize reallocations */ |
| 8336 | if (requiredsize < 2 * oldsize) |
| 8337 | requiredsize = 2 * oldsize; |
Kristjan Valur Jonsson | 85634d7 | 2012-05-31 09:37:31 +0000 | [diff] [blame] | 8338 | new_outobj = PyMem_Realloc(*outobj, requiredsize * sizeof(Py_UCS4)); |
| 8339 | if (new_outobj == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8340 | return -1; |
Kristjan Valur Jonsson | 85634d7 | 2012-05-31 09:37:31 +0000 | [diff] [blame] | 8341 | *outobj = new_outobj; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8342 | *psize = requiredsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8343 | } |
| 8344 | return 0; |
| 8345 | } |
| 8346 | /* lookup the character, put the result in the output string and adjust |
| 8347 | various state variables. Return a new reference to the object that |
| 8348 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 8349 | undefined (in which case no character was written). |
| 8350 | The called must decref result. |
| 8351 | Return 0 on success, -1 on error. */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8352 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8353 | charmaptranslate_output(PyObject *input, Py_ssize_t ipos, |
| 8354 | PyObject *mapping, Py_UCS4 **output, |
| 8355 | Py_ssize_t *osize, Py_ssize_t *opos, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8356 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8357 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8358 | Py_UCS4 curinp = PyUnicode_READ_CHAR(input, ipos); |
| 8359 | if (charmaptranslate_lookup(curinp, mapping, res)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8360 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8361 | if (*res==NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8362 | /* not found => default to 1:1 mapping */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8363 | (*output)[(*opos)++] = curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8364 | } |
| 8365 | else if (*res==Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8366 | ; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8367 | else if (PyLong_Check(*res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8368 | /* 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] | 8369 | (*output)[(*opos)++] = (Py_UCS4)PyLong_AS_LONG(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8370 | } |
| 8371 | else if (PyUnicode_Check(*res)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8372 | Py_ssize_t repsize; |
| 8373 | if (PyUnicode_READY(*res) == -1) |
| 8374 | return -1; |
| 8375 | repsize = PyUnicode_GET_LENGTH(*res); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8376 | if (repsize==1) { |
| 8377 | /* 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] | 8378 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8379 | } |
| 8380 | else if (repsize!=0) { |
| 8381 | /* more than one character */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8382 | Py_ssize_t requiredsize = *opos + |
| 8383 | (PyUnicode_GET_LENGTH(input) - ipos) + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8384 | repsize - 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8385 | Py_ssize_t i; |
| 8386 | if (charmaptranslate_makespace(output, osize, requiredsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8387 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8388 | for(i = 0; i < repsize; i++) |
| 8389 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, i); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8390 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8391 | } |
| 8392 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8393 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8394 | return 0; |
| 8395 | } |
| 8396 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8397 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8398 | _PyUnicode_TranslateCharmap(PyObject *input, |
| 8399 | PyObject *mapping, |
| 8400 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8401 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8402 | /* input object */ |
| 8403 | char *idata; |
| 8404 | Py_ssize_t size, i; |
| 8405 | int kind; |
| 8406 | /* output buffer */ |
| 8407 | Py_UCS4 *output = NULL; |
| 8408 | Py_ssize_t osize; |
| 8409 | PyObject *res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8410 | /* current output position */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8411 | Py_ssize_t opos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8412 | char *reason = "character maps to <undefined>"; |
| 8413 | PyObject *errorHandler = NULL; |
| 8414 | PyObject *exc = NULL; |
| 8415 | /* the following variable is used for caching string comparisons |
| 8416 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 8417 | * 3=ignore, 4=xmlcharrefreplace */ |
| 8418 | int known_errorHandler = -1; |
| 8419 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8420 | if (mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8421 | PyErr_BadArgument(); |
| 8422 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8423 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8424 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8425 | if (PyUnicode_READY(input) == -1) |
| 8426 | return NULL; |
| 8427 | idata = (char*)PyUnicode_DATA(input); |
| 8428 | kind = PyUnicode_KIND(input); |
| 8429 | size = PyUnicode_GET_LENGTH(input); |
| 8430 | i = 0; |
| 8431 | |
| 8432 | if (size == 0) { |
| 8433 | Py_INCREF(input); |
| 8434 | return input; |
| 8435 | } |
| 8436 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8437 | /* allocate enough for a simple 1:1 translation without |
| 8438 | replacements, if we need more, we'll resize */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8439 | osize = size; |
| 8440 | output = PyMem_Malloc(osize * sizeof(Py_UCS4)); |
| 8441 | opos = 0; |
| 8442 | if (output == NULL) { |
| 8443 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8444 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8445 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8446 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8447 | while (i<size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8448 | /* try to encode it */ |
| 8449 | PyObject *x = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8450 | if (charmaptranslate_output(input, i, mapping, |
| 8451 | &output, &osize, &opos, &x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8452 | Py_XDECREF(x); |
| 8453 | goto onError; |
| 8454 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8455 | Py_XDECREF(x); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8456 | if (x!=Py_None) /* it worked => adjust input pointer */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8457 | ++i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8458 | else { /* untranslatable character */ |
| 8459 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
| 8460 | Py_ssize_t repsize; |
| 8461 | Py_ssize_t newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8462 | Py_ssize_t uni2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8463 | /* startpos for collecting untranslatable chars */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8464 | Py_ssize_t collstart = i; |
| 8465 | Py_ssize_t collend = i+1; |
| 8466 | Py_ssize_t coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8467 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8468 | /* find all untranslatable characters */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8469 | while (collend < size) { |
| 8470 | if (charmaptranslate_lookup(PyUnicode_READ(kind,idata, collend), mapping, &x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8471 | goto onError; |
| 8472 | Py_XDECREF(x); |
| 8473 | if (x!=Py_None) |
| 8474 | break; |
| 8475 | ++collend; |
| 8476 | } |
| 8477 | /* cache callback name lookup |
| 8478 | * (if not done yet, i.e. it's the first error) */ |
| 8479 | if (known_errorHandler==-1) { |
| 8480 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 8481 | known_errorHandler = 1; |
| 8482 | else if (!strcmp(errors, "replace")) |
| 8483 | known_errorHandler = 2; |
| 8484 | else if (!strcmp(errors, "ignore")) |
| 8485 | known_errorHandler = 3; |
| 8486 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 8487 | known_errorHandler = 4; |
| 8488 | else |
| 8489 | known_errorHandler = 0; |
| 8490 | } |
| 8491 | switch (known_errorHandler) { |
| 8492 | case 1: /* strict */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8493 | raise_translate_exception(&exc, input, collstart, |
| 8494 | collend, reason); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8495 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8496 | case 2: /* replace */ |
| 8497 | /* 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] | 8498 | for (coll = collstart; coll<collend; coll++) |
| 8499 | output[opos++] = '?'; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8500 | /* fall through */ |
| 8501 | case 3: /* ignore */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8502 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8503 | break; |
| 8504 | case 4: /* xmlcharrefreplace */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8505 | /* generate replacement (temporarily (mis)uses i) */ |
| 8506 | for (i = collstart; i < collend; ++i) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8507 | char buffer[2+29+1+1]; |
| 8508 | char *cp; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8509 | sprintf(buffer, "&#%d;", PyUnicode_READ(kind, idata, i)); |
| 8510 | if (charmaptranslate_makespace(&output, &osize, |
| 8511 | opos+strlen(buffer)+(size-collend))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8512 | goto onError; |
| 8513 | for (cp = buffer; *cp; ++cp) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8514 | output[opos++] = *cp; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8515 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8516 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8517 | break; |
| 8518 | default: |
| 8519 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8520 | reason, input, &exc, |
| 8521 | collstart, collend, &newpos); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 8522 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8523 | goto onError; |
Benjamin Peterson | 9ca3ffa | 2012-01-01 16:04:29 -0600 | [diff] [blame] | 8524 | if (PyUnicode_READY(repunicode) == -1) { |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 8525 | Py_DECREF(repunicode); |
| 8526 | goto onError; |
| 8527 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8528 | /* generate replacement */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8529 | repsize = PyUnicode_GET_LENGTH(repunicode); |
| 8530 | if (charmaptranslate_makespace(&output, &osize, |
| 8531 | opos+repsize+(size-collend))) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8532 | Py_DECREF(repunicode); |
| 8533 | goto onError; |
| 8534 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8535 | for (uni2 = 0; repsize-->0; ++uni2) |
| 8536 | output[opos++] = PyUnicode_READ_CHAR(repunicode, uni2); |
| 8537 | i = newpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8538 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8539 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8540 | } |
| 8541 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8542 | res = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, output, opos); |
| 8543 | if (!res) |
| 8544 | goto onError; |
| 8545 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8546 | Py_XDECREF(exc); |
| 8547 | Py_XDECREF(errorHandler); |
| 8548 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8549 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8550 | onError: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8551 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8552 | Py_XDECREF(exc); |
| 8553 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8554 | return NULL; |
| 8555 | } |
| 8556 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8557 | /* Deprecated. Use PyUnicode_Translate instead. */ |
| 8558 | PyObject * |
| 8559 | PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
| 8560 | Py_ssize_t size, |
| 8561 | PyObject *mapping, |
| 8562 | const char *errors) |
| 8563 | { |
| 8564 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 8565 | if (!unicode) |
| 8566 | return NULL; |
| 8567 | return _PyUnicode_TranslateCharmap(unicode, mapping, errors); |
| 8568 | } |
| 8569 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8570 | PyObject * |
| 8571 | PyUnicode_Translate(PyObject *str, |
| 8572 | PyObject *mapping, |
| 8573 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8574 | { |
| 8575 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8576 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8577 | str = PyUnicode_FromObject(str); |
| 8578 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8579 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8580 | result = _PyUnicode_TranslateCharmap(str, mapping, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8581 | Py_DECREF(str); |
| 8582 | return result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8583 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8584 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8585 | Py_XDECREF(str); |
| 8586 | return NULL; |
| 8587 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8588 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8589 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8590 | fix_decimal_and_space_to_ascii(PyObject *self) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8591 | { |
| 8592 | /* No need to call PyUnicode_READY(self) because this function is only |
| 8593 | called as a callback from fixup() which does it already. */ |
| 8594 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8595 | const int kind = PyUnicode_KIND(self); |
| 8596 | void *data = PyUnicode_DATA(self); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 8597 | Py_UCS4 maxchar = 127, ch, fixed; |
Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame] | 8598 | int modified = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8599 | Py_ssize_t i; |
| 8600 | |
| 8601 | for (i = 0; i < len; ++i) { |
| 8602 | ch = PyUnicode_READ(kind, data, i); |
| 8603 | fixed = 0; |
| 8604 | if (ch > 127) { |
| 8605 | if (Py_UNICODE_ISSPACE(ch)) |
| 8606 | fixed = ' '; |
| 8607 | else { |
| 8608 | const int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8609 | if (decimal >= 0) |
| 8610 | fixed = '0' + decimal; |
| 8611 | } |
| 8612 | if (fixed != 0) { |
Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame] | 8613 | modified = 1; |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 8614 | maxchar = MAX_MAXCHAR(maxchar, fixed); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8615 | PyUnicode_WRITE(kind, data, i, fixed); |
| 8616 | } |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 8617 | else |
| 8618 | maxchar = MAX_MAXCHAR(maxchar, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8619 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8620 | } |
| 8621 | |
Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame] | 8622 | return (modified) ? maxchar : 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8623 | } |
| 8624 | |
| 8625 | PyObject * |
| 8626 | _PyUnicode_TransformDecimalAndSpaceToASCII(PyObject *unicode) |
| 8627 | { |
| 8628 | if (!PyUnicode_Check(unicode)) { |
| 8629 | PyErr_BadInternalCall(); |
| 8630 | return NULL; |
| 8631 | } |
| 8632 | if (PyUnicode_READY(unicode) == -1) |
| 8633 | return NULL; |
| 8634 | if (PyUnicode_MAX_CHAR_VALUE(unicode) <= 127) { |
| 8635 | /* If the string is already ASCII, just return the same string */ |
| 8636 | Py_INCREF(unicode); |
| 8637 | return unicode; |
| 8638 | } |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8639 | return fixup(unicode, fix_decimal_and_space_to_ascii); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8640 | } |
| 8641 | |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8642 | PyObject * |
| 8643 | PyUnicode_TransformDecimalToASCII(Py_UNICODE *s, |
| 8644 | Py_ssize_t length) |
| 8645 | { |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8646 | PyObject *decimal; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8647 | Py_ssize_t i; |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8648 | Py_UCS4 maxchar; |
| 8649 | enum PyUnicode_Kind kind; |
| 8650 | void *data; |
| 8651 | |
Victor Stinner | 99d7ad0 | 2012-02-22 13:37:39 +0100 | [diff] [blame] | 8652 | maxchar = 127; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8653 | for (i = 0; i < length; i++) { |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8654 | Py_UNICODE ch = s[i]; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8655 | if (ch > 127) { |
| 8656 | int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8657 | if (decimal >= 0) |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8658 | ch = '0' + decimal; |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 8659 | maxchar = MAX_MAXCHAR(maxchar, ch); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8660 | } |
| 8661 | } |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8662 | |
| 8663 | /* Copy to a new string */ |
| 8664 | decimal = PyUnicode_New(length, maxchar); |
| 8665 | if (decimal == NULL) |
| 8666 | return decimal; |
| 8667 | kind = PyUnicode_KIND(decimal); |
| 8668 | data = PyUnicode_DATA(decimal); |
| 8669 | /* Iterate over code points */ |
| 8670 | for (i = 0; i < length; i++) { |
| 8671 | Py_UNICODE ch = s[i]; |
| 8672 | if (ch > 127) { |
| 8673 | int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8674 | if (decimal >= 0) |
| 8675 | ch = '0' + decimal; |
| 8676 | } |
| 8677 | PyUnicode_WRITE(kind, data, i, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8678 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 8679 | return unicode_result(decimal); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8680 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8681 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 8682 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8683 | int |
| 8684 | PyUnicode_EncodeDecimal(Py_UNICODE *s, |
| 8685 | Py_ssize_t length, |
| 8686 | char *output, |
| 8687 | const char *errors) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8688 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8689 | PyObject *unicode; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8690 | Py_ssize_t i; |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8691 | enum PyUnicode_Kind kind; |
| 8692 | void *data; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8693 | |
| 8694 | if (output == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8695 | PyErr_BadArgument(); |
| 8696 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8697 | } |
| 8698 | |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8699 | unicode = PyUnicode_FromUnicode(s, length); |
| 8700 | if (unicode == NULL) |
| 8701 | return -1; |
| 8702 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 8703 | if (PyUnicode_READY(unicode) == -1) { |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8704 | Py_DECREF(unicode); |
| 8705 | return -1; |
| 8706 | } |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8707 | kind = PyUnicode_KIND(unicode); |
| 8708 | data = PyUnicode_DATA(unicode); |
| 8709 | |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8710 | for (i=0; i < length; ) { |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8711 | PyObject *exc; |
| 8712 | Py_UCS4 ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8713 | int decimal; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8714 | Py_ssize_t startpos; |
| 8715 | |
| 8716 | ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8717 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8718 | if (Py_UNICODE_ISSPACE(ch)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8719 | *output++ = ' '; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8720 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8721 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8722 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8723 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 8724 | if (decimal >= 0) { |
| 8725 | *output++ = '0' + decimal; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8726 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8727 | continue; |
| 8728 | } |
| 8729 | if (0 < ch && ch < 256) { |
| 8730 | *output++ = (char)ch; |
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; |
| 8733 | } |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8734 | |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8735 | startpos = i; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8736 | exc = NULL; |
| 8737 | raise_encode_exception(&exc, "decimal", unicode, |
| 8738 | startpos, startpos+1, |
| 8739 | "invalid decimal Unicode string"); |
| 8740 | Py_XDECREF(exc); |
| 8741 | Py_DECREF(unicode); |
| 8742 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8743 | } |
| 8744 | /* 0-terminate the output string */ |
| 8745 | *output++ = '\0'; |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8746 | Py_DECREF(unicode); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8747 | return 0; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8748 | } |
| 8749 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8750 | /* --- Helpers ------------------------------------------------------------ */ |
| 8751 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8752 | static Py_ssize_t |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 8753 | any_find_slice(int direction, PyObject* s1, PyObject* s2, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8754 | Py_ssize_t start, |
| 8755 | Py_ssize_t end) |
| 8756 | { |
| 8757 | int kind1, kind2, kind; |
| 8758 | void *buf1, *buf2; |
| 8759 | Py_ssize_t len1, len2, result; |
| 8760 | |
| 8761 | kind1 = PyUnicode_KIND(s1); |
| 8762 | kind2 = PyUnicode_KIND(s2); |
| 8763 | kind = kind1 > kind2 ? kind1 : kind2; |
| 8764 | buf1 = PyUnicode_DATA(s1); |
| 8765 | buf2 = PyUnicode_DATA(s2); |
| 8766 | if (kind1 != kind) |
| 8767 | buf1 = _PyUnicode_AsKind(s1, kind); |
| 8768 | if (!buf1) |
| 8769 | return -2; |
| 8770 | if (kind2 != kind) |
| 8771 | buf2 = _PyUnicode_AsKind(s2, kind); |
| 8772 | if (!buf2) { |
| 8773 | if (kind1 != kind) PyMem_Free(buf1); |
| 8774 | return -2; |
| 8775 | } |
| 8776 | len1 = PyUnicode_GET_LENGTH(s1); |
| 8777 | len2 = PyUnicode_GET_LENGTH(s2); |
| 8778 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 8779 | if (direction > 0) { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 8780 | switch (kind) { |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 8781 | case PyUnicode_1BYTE_KIND: |
| 8782 | if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2)) |
| 8783 | result = asciilib_find_slice(buf1, len1, buf2, len2, start, end); |
| 8784 | else |
| 8785 | result = ucs1lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 8786 | break; |
| 8787 | case PyUnicode_2BYTE_KIND: |
| 8788 | result = ucs2lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 8789 | break; |
| 8790 | case PyUnicode_4BYTE_KIND: |
| 8791 | result = ucs4lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 8792 | break; |
| 8793 | default: |
| 8794 | assert(0); result = -2; |
| 8795 | } |
| 8796 | } |
| 8797 | else { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 8798 | switch (kind) { |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 8799 | case PyUnicode_1BYTE_KIND: |
| 8800 | if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2)) |
| 8801 | result = asciilib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 8802 | else |
| 8803 | result = ucs1lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 8804 | break; |
| 8805 | case PyUnicode_2BYTE_KIND: |
| 8806 | result = ucs2lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 8807 | break; |
| 8808 | case PyUnicode_4BYTE_KIND: |
| 8809 | result = ucs4lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 8810 | break; |
| 8811 | default: |
| 8812 | assert(0); result = -2; |
| 8813 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8814 | } |
| 8815 | |
| 8816 | if (kind1 != kind) |
| 8817 | PyMem_Free(buf1); |
| 8818 | if (kind2 != kind) |
| 8819 | PyMem_Free(buf2); |
| 8820 | |
| 8821 | return result; |
| 8822 | } |
| 8823 | |
| 8824 | Py_ssize_t |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8825 | _PyUnicode_InsertThousandsGrouping( |
| 8826 | PyObject *unicode, Py_ssize_t index, |
| 8827 | Py_ssize_t n_buffer, |
| 8828 | void *digits, Py_ssize_t n_digits, |
| 8829 | Py_ssize_t min_width, |
| 8830 | const char *grouping, PyObject *thousands_sep, |
| 8831 | Py_UCS4 *maxchar) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8832 | { |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8833 | unsigned int kind, thousands_sep_kind; |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8834 | char *data, *thousands_sep_data; |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8835 | Py_ssize_t thousands_sep_len; |
| 8836 | Py_ssize_t len; |
| 8837 | |
| 8838 | if (unicode != NULL) { |
| 8839 | kind = PyUnicode_KIND(unicode); |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8840 | data = (char *) PyUnicode_DATA(unicode) + index * kind; |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8841 | } |
| 8842 | else { |
| 8843 | kind = PyUnicode_1BYTE_KIND; |
| 8844 | data = NULL; |
| 8845 | } |
| 8846 | thousands_sep_kind = PyUnicode_KIND(thousands_sep); |
| 8847 | thousands_sep_data = PyUnicode_DATA(thousands_sep); |
| 8848 | thousands_sep_len = PyUnicode_GET_LENGTH(thousands_sep); |
| 8849 | if (unicode != NULL && thousands_sep_kind != kind) { |
Victor Stinner | 90f50d4 | 2012-02-24 01:44:47 +0100 | [diff] [blame] | 8850 | if (thousands_sep_kind < kind) { |
| 8851 | thousands_sep_data = _PyUnicode_AsKind(thousands_sep, kind); |
| 8852 | if (!thousands_sep_data) |
| 8853 | return -1; |
| 8854 | } |
| 8855 | else { |
| 8856 | data = _PyUnicode_AsKind(unicode, thousands_sep_kind); |
| 8857 | if (!data) |
| 8858 | return -1; |
| 8859 | } |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8860 | } |
| 8861 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 8862 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8863 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8864 | if (unicode != NULL && PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8865 | len = asciilib_InsertThousandsGrouping( |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8866 | (Py_UCS1 *) data, n_buffer, (Py_UCS1 *) digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8867 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8868 | (Py_UCS1 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8869 | else |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8870 | len = ucs1lib_InsertThousandsGrouping( |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8871 | (Py_UCS1*)data, n_buffer, (Py_UCS1*)digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8872 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8873 | (Py_UCS1 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8874 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8875 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8876 | len = ucs2lib_InsertThousandsGrouping( |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8877 | (Py_UCS2 *) data, n_buffer, (Py_UCS2 *) 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_UCS2 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8880 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8881 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8882 | len = ucs4lib_InsertThousandsGrouping( |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8883 | (Py_UCS4 *) data, n_buffer, (Py_UCS4 *) digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8884 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8885 | (Py_UCS4 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8886 | break; |
| 8887 | default: |
| 8888 | assert(0); |
| 8889 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8890 | } |
Victor Stinner | 90f50d4 | 2012-02-24 01:44:47 +0100 | [diff] [blame] | 8891 | if (unicode != NULL && thousands_sep_kind != kind) { |
| 8892 | if (thousands_sep_kind < kind) |
| 8893 | PyMem_Free(thousands_sep_data); |
| 8894 | else |
| 8895 | PyMem_Free(data); |
| 8896 | } |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8897 | if (unicode == NULL) { |
| 8898 | *maxchar = 127; |
| 8899 | if (len != n_digits) { |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 8900 | *maxchar = MAX_MAXCHAR(*maxchar, |
| 8901 | PyUnicode_MAX_CHAR_VALUE(thousands_sep)); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8902 | } |
| 8903 | } |
| 8904 | return len; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8905 | } |
| 8906 | |
| 8907 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8908 | /* helper macro to fixup start/end slice values */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 8909 | #define ADJUST_INDICES(start, end, len) \ |
| 8910 | if (end > len) \ |
| 8911 | end = len; \ |
| 8912 | else if (end < 0) { \ |
| 8913 | end += len; \ |
| 8914 | if (end < 0) \ |
| 8915 | end = 0; \ |
| 8916 | } \ |
| 8917 | if (start < 0) { \ |
| 8918 | start += len; \ |
| 8919 | if (start < 0) \ |
| 8920 | start = 0; \ |
| 8921 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8922 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8923 | Py_ssize_t |
| 8924 | PyUnicode_Count(PyObject *str, |
| 8925 | PyObject *substr, |
| 8926 | Py_ssize_t start, |
| 8927 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8928 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8929 | Py_ssize_t result; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 8930 | PyObject* str_obj; |
| 8931 | PyObject* sub_obj; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8932 | int kind1, kind2, kind; |
| 8933 | void *buf1 = NULL, *buf2 = NULL; |
| 8934 | Py_ssize_t len1, len2; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8935 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 8936 | str_obj = PyUnicode_FromObject(str); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 8937 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8938 | return -1; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 8939 | sub_obj = PyUnicode_FromObject(substr); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 8940 | if (!sub_obj) { |
| 8941 | Py_DECREF(str_obj); |
| 8942 | return -1; |
| 8943 | } |
Benjamin Peterson | 4c13a4a | 2012-01-02 09:07:38 -0600 | [diff] [blame] | 8944 | if (PyUnicode_READY(sub_obj) == -1 || PyUnicode_READY(str_obj) == -1) { |
Benjamin Peterson | 5e458f5 | 2012-01-02 10:12:13 -0600 | [diff] [blame] | 8945 | Py_DECREF(sub_obj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8946 | Py_DECREF(str_obj); |
| 8947 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8948 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8949 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8950 | kind1 = PyUnicode_KIND(str_obj); |
| 8951 | kind2 = PyUnicode_KIND(sub_obj); |
Antoine Pitrou | e45c0c5 | 2012-05-12 15:49:07 +0200 | [diff] [blame] | 8952 | kind = kind1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8953 | buf1 = PyUnicode_DATA(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8954 | buf2 = PyUnicode_DATA(sub_obj); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 8955 | if (kind2 != kind) { |
Antoine Pitrou | 758153b | 2012-05-12 15:51:51 +0200 | [diff] [blame] | 8956 | if (kind2 > kind) { |
| 8957 | Py_DECREF(sub_obj); |
| 8958 | Py_DECREF(str_obj); |
Antoine Pitrou | e45c0c5 | 2012-05-12 15:49:07 +0200 | [diff] [blame] | 8959 | return 0; |
Antoine Pitrou | 758153b | 2012-05-12 15:51:51 +0200 | [diff] [blame] | 8960 | } |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 8961 | buf2 = _PyUnicode_AsKind(sub_obj, kind); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 8962 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8963 | if (!buf2) |
| 8964 | goto onError; |
| 8965 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 8966 | len2 = PyUnicode_GET_LENGTH(sub_obj); |
| 8967 | |
| 8968 | ADJUST_INDICES(start, end, len1); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 8969 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8970 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8971 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sub_obj)) |
| 8972 | result = asciilib_count( |
| 8973 | ((Py_UCS1*)buf1) + start, end - start, |
| 8974 | buf2, len2, PY_SSIZE_T_MAX |
| 8975 | ); |
| 8976 | else |
| 8977 | result = ucs1lib_count( |
| 8978 | ((Py_UCS1*)buf1) + start, end - start, |
| 8979 | buf2, len2, PY_SSIZE_T_MAX |
| 8980 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8981 | break; |
| 8982 | case PyUnicode_2BYTE_KIND: |
| 8983 | result = ucs2lib_count( |
| 8984 | ((Py_UCS2*)buf1) + start, end - start, |
| 8985 | buf2, len2, PY_SSIZE_T_MAX |
| 8986 | ); |
| 8987 | break; |
| 8988 | case PyUnicode_4BYTE_KIND: |
| 8989 | result = ucs4lib_count( |
| 8990 | ((Py_UCS4*)buf1) + start, end - start, |
| 8991 | buf2, len2, PY_SSIZE_T_MAX |
| 8992 | ); |
| 8993 | break; |
| 8994 | default: |
| 8995 | assert(0); result = 0; |
| 8996 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8997 | |
| 8998 | Py_DECREF(sub_obj); |
| 8999 | Py_DECREF(str_obj); |
| 9000 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9001 | if (kind2 != kind) |
| 9002 | PyMem_Free(buf2); |
| 9003 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9004 | return result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9005 | onError: |
| 9006 | Py_DECREF(sub_obj); |
| 9007 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9008 | if (kind2 != kind && buf2) |
| 9009 | PyMem_Free(buf2); |
| 9010 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9011 | } |
| 9012 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9013 | Py_ssize_t |
| 9014 | PyUnicode_Find(PyObject *str, |
| 9015 | PyObject *sub, |
| 9016 | Py_ssize_t start, |
| 9017 | Py_ssize_t end, |
| 9018 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9019 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9020 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9021 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9022 | str = PyUnicode_FromObject(str); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9023 | if (!str) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9024 | return -2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9025 | sub = PyUnicode_FromObject(sub); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9026 | if (!sub) { |
| 9027 | Py_DECREF(str); |
| 9028 | return -2; |
| 9029 | } |
| 9030 | if (PyUnicode_READY(sub) == -1 || PyUnicode_READY(str) == -1) { |
| 9031 | Py_DECREF(sub); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9032 | Py_DECREF(str); |
| 9033 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9034 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9035 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9036 | result = any_find_slice(direction, |
| 9037 | str, sub, start, end |
| 9038 | ); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9039 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9040 | Py_DECREF(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9041 | Py_DECREF(sub); |
| 9042 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9043 | return result; |
| 9044 | } |
| 9045 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9046 | Py_ssize_t |
| 9047 | PyUnicode_FindChar(PyObject *str, Py_UCS4 ch, |
| 9048 | Py_ssize_t start, Py_ssize_t end, |
| 9049 | int direction) |
| 9050 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9051 | int kind; |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9052 | Py_ssize_t result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9053 | if (PyUnicode_READY(str) == -1) |
| 9054 | return -2; |
Victor Stinner | 267aa24 | 2011-10-02 01:08:37 +0200 | [diff] [blame] | 9055 | if (start < 0 || end < 0) { |
| 9056 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 9057 | return -2; |
| 9058 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9059 | if (end > PyUnicode_GET_LENGTH(str)) |
| 9060 | end = PyUnicode_GET_LENGTH(str); |
| 9061 | kind = PyUnicode_KIND(str); |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9062 | result = findchar(PyUnicode_1BYTE_DATA(str) + kind*start, |
| 9063 | kind, end-start, ch, direction); |
| 9064 | if (result == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9065 | return -1; |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9066 | else |
| 9067 | return start + result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9068 | } |
| 9069 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9070 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9071 | tailmatch(PyObject *self, |
| 9072 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9073 | Py_ssize_t start, |
| 9074 | Py_ssize_t end, |
| 9075 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9076 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9077 | int kind_self; |
| 9078 | int kind_sub; |
| 9079 | void *data_self; |
| 9080 | void *data_sub; |
| 9081 | Py_ssize_t offset; |
| 9082 | Py_ssize_t i; |
| 9083 | Py_ssize_t end_sub; |
| 9084 | |
| 9085 | if (PyUnicode_READY(self) == -1 || |
| 9086 | PyUnicode_READY(substring) == -1) |
| 9087 | return 0; |
| 9088 | |
| 9089 | if (PyUnicode_GET_LENGTH(substring) == 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9090 | return 1; |
| 9091 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9092 | ADJUST_INDICES(start, end, PyUnicode_GET_LENGTH(self)); |
| 9093 | end -= PyUnicode_GET_LENGTH(substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9094 | if (end < start) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9095 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9096 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9097 | kind_self = PyUnicode_KIND(self); |
| 9098 | data_self = PyUnicode_DATA(self); |
| 9099 | kind_sub = PyUnicode_KIND(substring); |
| 9100 | data_sub = PyUnicode_DATA(substring); |
| 9101 | end_sub = PyUnicode_GET_LENGTH(substring) - 1; |
| 9102 | |
| 9103 | if (direction > 0) |
| 9104 | offset = end; |
| 9105 | else |
| 9106 | offset = start; |
| 9107 | |
| 9108 | if (PyUnicode_READ(kind_self, data_self, offset) == |
| 9109 | PyUnicode_READ(kind_sub, data_sub, 0) && |
| 9110 | PyUnicode_READ(kind_self, data_self, offset + end_sub) == |
| 9111 | PyUnicode_READ(kind_sub, data_sub, end_sub)) { |
| 9112 | /* If both are of the same kind, memcmp is sufficient */ |
| 9113 | if (kind_self == kind_sub) { |
| 9114 | return ! memcmp((char *)data_self + |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9115 | (offset * PyUnicode_KIND(substring)), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9116 | data_sub, |
| 9117 | PyUnicode_GET_LENGTH(substring) * |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9118 | PyUnicode_KIND(substring)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9119 | } |
| 9120 | /* otherwise we have to compare each character by first accesing it */ |
| 9121 | else { |
| 9122 | /* We do not need to compare 0 and len(substring)-1 because |
| 9123 | the if statement above ensured already that they are equal |
| 9124 | when we end up here. */ |
| 9125 | // TODO: honor direction and do a forward or backwards search |
| 9126 | for (i = 1; i < end_sub; ++i) { |
| 9127 | if (PyUnicode_READ(kind_self, data_self, offset + i) != |
| 9128 | PyUnicode_READ(kind_sub, data_sub, i)) |
| 9129 | return 0; |
| 9130 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9131 | return 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9132 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9133 | } |
| 9134 | |
| 9135 | return 0; |
| 9136 | } |
| 9137 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9138 | Py_ssize_t |
| 9139 | PyUnicode_Tailmatch(PyObject *str, |
| 9140 | PyObject *substr, |
| 9141 | Py_ssize_t start, |
| 9142 | Py_ssize_t end, |
| 9143 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9144 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9145 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9146 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9147 | str = PyUnicode_FromObject(str); |
| 9148 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9149 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9150 | substr = PyUnicode_FromObject(substr); |
| 9151 | if (substr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9152 | Py_DECREF(str); |
| 9153 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9154 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9155 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9156 | result = tailmatch(str, substr, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9157 | start, end, direction); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9158 | Py_DECREF(str); |
| 9159 | Py_DECREF(substr); |
| 9160 | return result; |
| 9161 | } |
| 9162 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9163 | /* Apply fixfct filter to the Unicode object self and return a |
| 9164 | reference to the modified object */ |
| 9165 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9166 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9167 | fixup(PyObject *self, |
| 9168 | Py_UCS4 (*fixfct)(PyObject *s)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9169 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9170 | PyObject *u; |
| 9171 | Py_UCS4 maxchar_old, maxchar_new = 0; |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9172 | PyObject *v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9173 | |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 9174 | u = _PyUnicode_Copy(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9175 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9176 | return NULL; |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 9177 | maxchar_old = PyUnicode_MAX_CHAR_VALUE(u); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9178 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9179 | /* fix functions return the new maximum character in a string, |
| 9180 | if the kind of the resulting unicode object does not change, |
| 9181 | everything is fine. Otherwise we need to change the string kind |
| 9182 | and re-run the fix function. */ |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9183 | maxchar_new = fixfct(u); |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9184 | |
| 9185 | if (maxchar_new == 0) { |
| 9186 | /* no changes */; |
| 9187 | if (PyUnicode_CheckExact(self)) { |
| 9188 | Py_DECREF(u); |
| 9189 | Py_INCREF(self); |
| 9190 | return self; |
| 9191 | } |
| 9192 | else |
| 9193 | return u; |
| 9194 | } |
| 9195 | |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9196 | maxchar_new = align_maxchar(maxchar_new); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9197 | |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9198 | if (maxchar_new == maxchar_old) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9199 | return u; |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9200 | |
| 9201 | /* In case the maximum character changed, we need to |
| 9202 | convert the string to the new category. */ |
| 9203 | v = PyUnicode_New(PyUnicode_GET_LENGTH(self), maxchar_new); |
| 9204 | if (v == NULL) { |
| 9205 | Py_DECREF(u); |
| 9206 | return NULL; |
| 9207 | } |
| 9208 | if (maxchar_new > maxchar_old) { |
| 9209 | /* If the maxchar increased so that the kind changed, not all |
| 9210 | characters are representable anymore and we need to fix the |
| 9211 | string again. This only happens in very few cases. */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9212 | _PyUnicode_FastCopyCharacters(v, 0, |
| 9213 | self, 0, PyUnicode_GET_LENGTH(self)); |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9214 | maxchar_old = fixfct(v); |
| 9215 | assert(maxchar_old > 0 && maxchar_old <= maxchar_new); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9216 | } |
| 9217 | else { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9218 | _PyUnicode_FastCopyCharacters(v, 0, |
| 9219 | u, 0, PyUnicode_GET_LENGTH(self)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9220 | } |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9221 | Py_DECREF(u); |
| 9222 | assert(_PyUnicode_CheckConsistency(v, 1)); |
| 9223 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9224 | } |
| 9225 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9226 | static PyObject * |
| 9227 | ascii_upper_or_lower(PyObject *self, int lower) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9228 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9229 | Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9230 | char *resdata, *data = PyUnicode_DATA(self); |
| 9231 | PyObject *res; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9232 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9233 | res = PyUnicode_New(len, 127); |
| 9234 | if (res == NULL) |
| 9235 | return NULL; |
| 9236 | resdata = PyUnicode_DATA(res); |
| 9237 | if (lower) |
| 9238 | _Py_bytes_lower(resdata, data, len); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9239 | else |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9240 | _Py_bytes_upper(resdata, data, len); |
| 9241 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9242 | } |
| 9243 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9244 | static Py_UCS4 |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9245 | 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] | 9246 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9247 | Py_ssize_t j; |
| 9248 | int final_sigma; |
| 9249 | Py_UCS4 c; |
| 9250 | /* 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] | 9251 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9252 | \p{cased}\p{case-ignorable}*U+03A3!(\p{case-ignorable}*\p{cased}) |
| 9253 | |
| 9254 | where ! is a negation and \p{xxx} is a character with property xxx. |
| 9255 | */ |
| 9256 | for (j = i - 1; j >= 0; j--) { |
| 9257 | c = PyUnicode_READ(kind, data, j); |
| 9258 | if (!_PyUnicode_IsCaseIgnorable(c)) |
| 9259 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9260 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9261 | final_sigma = j >= 0 && _PyUnicode_IsCased(c); |
| 9262 | if (final_sigma) { |
| 9263 | for (j = i + 1; j < length; j++) { |
| 9264 | c = PyUnicode_READ(kind, data, j); |
| 9265 | if (!_PyUnicode_IsCaseIgnorable(c)) |
| 9266 | break; |
| 9267 | } |
| 9268 | final_sigma = j == length || !_PyUnicode_IsCased(c); |
| 9269 | } |
| 9270 | return (final_sigma) ? 0x3C2 : 0x3C3; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9271 | } |
| 9272 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9273 | static int |
| 9274 | lower_ucs4(int kind, void *data, Py_ssize_t length, Py_ssize_t i, |
| 9275 | Py_UCS4 c, Py_UCS4 *mapped) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9276 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9277 | /* Obscure special case. */ |
| 9278 | if (c == 0x3A3) { |
| 9279 | mapped[0] = handle_capital_sigma(kind, data, length, i); |
| 9280 | return 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9281 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9282 | return _PyUnicode_ToLowerFull(c, mapped); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9283 | } |
| 9284 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9285 | static Py_ssize_t |
| 9286 | 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] | 9287 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9288 | Py_ssize_t i, k = 0; |
| 9289 | int n_res, j; |
| 9290 | Py_UCS4 c, mapped[3]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9291 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9292 | c = PyUnicode_READ(kind, data, 0); |
| 9293 | n_res = _PyUnicode_ToUpperFull(c, mapped); |
| 9294 | for (j = 0; j < n_res; j++) { |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9295 | *maxchar = MAX_MAXCHAR(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9296 | res[k++] = mapped[j]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9297 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9298 | for (i = 1; i < length; i++) { |
| 9299 | c = PyUnicode_READ(kind, data, i); |
| 9300 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9301 | for (j = 0; j < n_res; j++) { |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9302 | *maxchar = MAX_MAXCHAR(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9303 | res[k++] = mapped[j]; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9304 | } |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9305 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9306 | return k; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9307 | } |
| 9308 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9309 | static Py_ssize_t |
| 9310 | do_swapcase(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) { |
| 9311 | Py_ssize_t i, k = 0; |
| 9312 | |
| 9313 | for (i = 0; i < length; i++) { |
| 9314 | Py_UCS4 c = PyUnicode_READ(kind, data, i), mapped[3]; |
| 9315 | int n_res, j; |
| 9316 | if (Py_UNICODE_ISUPPER(c)) { |
| 9317 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9318 | } |
| 9319 | else if (Py_UNICODE_ISLOWER(c)) { |
| 9320 | n_res = _PyUnicode_ToUpperFull(c, mapped); |
| 9321 | } |
| 9322 | else { |
| 9323 | n_res = 1; |
| 9324 | mapped[0] = c; |
| 9325 | } |
| 9326 | for (j = 0; j < n_res; j++) { |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9327 | *maxchar = MAX_MAXCHAR(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9328 | res[k++] = mapped[j]; |
| 9329 | } |
| 9330 | } |
| 9331 | return k; |
| 9332 | } |
| 9333 | |
| 9334 | static Py_ssize_t |
| 9335 | do_upper_or_lower(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, |
| 9336 | Py_UCS4 *maxchar, int lower) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9337 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9338 | Py_ssize_t i, k = 0; |
| 9339 | |
| 9340 | for (i = 0; i < length; i++) { |
| 9341 | Py_UCS4 c = PyUnicode_READ(kind, data, i), mapped[3]; |
| 9342 | int n_res, j; |
| 9343 | if (lower) |
| 9344 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9345 | else |
| 9346 | n_res = _PyUnicode_ToUpperFull(c, mapped); |
| 9347 | for (j = 0; j < n_res; j++) { |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9348 | *maxchar = MAX_MAXCHAR(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9349 | res[k++] = mapped[j]; |
| 9350 | } |
| 9351 | } |
| 9352 | return k; |
| 9353 | } |
| 9354 | |
| 9355 | static Py_ssize_t |
| 9356 | do_upper(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9357 | { |
| 9358 | return do_upper_or_lower(kind, data, length, res, maxchar, 0); |
| 9359 | } |
| 9360 | |
| 9361 | static Py_ssize_t |
| 9362 | do_lower(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9363 | { |
| 9364 | return do_upper_or_lower(kind, data, length, res, maxchar, 1); |
| 9365 | } |
| 9366 | |
Benjamin Peterson | e51757f | 2012-01-12 21:10:29 -0500 | [diff] [blame] | 9367 | static Py_ssize_t |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 9368 | do_casefold(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9369 | { |
| 9370 | Py_ssize_t i, k = 0; |
| 9371 | |
| 9372 | for (i = 0; i < length; i++) { |
| 9373 | Py_UCS4 c = PyUnicode_READ(kind, data, i); |
| 9374 | Py_UCS4 mapped[3]; |
| 9375 | int j, n_res = _PyUnicode_ToFoldedFull(c, mapped); |
| 9376 | for (j = 0; j < n_res; j++) { |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9377 | *maxchar = MAX_MAXCHAR(*maxchar, mapped[j]); |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 9378 | res[k++] = mapped[j]; |
| 9379 | } |
| 9380 | } |
| 9381 | return k; |
| 9382 | } |
| 9383 | |
| 9384 | static Py_ssize_t |
Benjamin Peterson | e51757f | 2012-01-12 21:10:29 -0500 | [diff] [blame] | 9385 | do_title(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9386 | { |
| 9387 | Py_ssize_t i, k = 0; |
| 9388 | int previous_is_cased; |
| 9389 | |
| 9390 | previous_is_cased = 0; |
| 9391 | for (i = 0; i < length; i++) { |
| 9392 | const Py_UCS4 c = PyUnicode_READ(kind, data, i); |
| 9393 | Py_UCS4 mapped[3]; |
| 9394 | int n_res, j; |
| 9395 | |
| 9396 | if (previous_is_cased) |
| 9397 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9398 | else |
| 9399 | n_res = _PyUnicode_ToTitleFull(c, mapped); |
| 9400 | |
| 9401 | for (j = 0; j < n_res; j++) { |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9402 | *maxchar = MAX_MAXCHAR(*maxchar, mapped[j]); |
Benjamin Peterson | e51757f | 2012-01-12 21:10:29 -0500 | [diff] [blame] | 9403 | res[k++] = mapped[j]; |
| 9404 | } |
| 9405 | |
| 9406 | previous_is_cased = _PyUnicode_IsCased(c); |
| 9407 | } |
| 9408 | return k; |
| 9409 | } |
| 9410 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9411 | static PyObject * |
| 9412 | case_operation(PyObject *self, |
| 9413 | Py_ssize_t (*perform)(int, void *, Py_ssize_t, Py_UCS4 *, Py_UCS4 *)) |
| 9414 | { |
| 9415 | PyObject *res = NULL; |
| 9416 | Py_ssize_t length, newlength = 0; |
| 9417 | int kind, outkind; |
| 9418 | void *data, *outdata; |
| 9419 | Py_UCS4 maxchar = 0, *tmp, *tmpend; |
| 9420 | |
Benjamin Peterson | eea4846 | 2012-01-16 14:28:50 -0500 | [diff] [blame] | 9421 | assert(PyUnicode_IS_READY(self)); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9422 | |
| 9423 | kind = PyUnicode_KIND(self); |
| 9424 | data = PyUnicode_DATA(self); |
| 9425 | length = PyUnicode_GET_LENGTH(self); |
| 9426 | tmp = PyMem_MALLOC(sizeof(Py_UCS4) * 3 * length); |
| 9427 | if (tmp == NULL) |
| 9428 | return PyErr_NoMemory(); |
| 9429 | newlength = perform(kind, data, length, tmp, &maxchar); |
| 9430 | res = PyUnicode_New(newlength, maxchar); |
| 9431 | if (res == NULL) |
| 9432 | goto leave; |
| 9433 | tmpend = tmp + newlength; |
| 9434 | outdata = PyUnicode_DATA(res); |
| 9435 | outkind = PyUnicode_KIND(res); |
| 9436 | switch (outkind) { |
| 9437 | case PyUnicode_1BYTE_KIND: |
| 9438 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS1, tmp, tmpend, outdata); |
| 9439 | break; |
| 9440 | case PyUnicode_2BYTE_KIND: |
| 9441 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS2, tmp, tmpend, outdata); |
| 9442 | break; |
| 9443 | case PyUnicode_4BYTE_KIND: |
| 9444 | memcpy(outdata, tmp, sizeof(Py_UCS4) * newlength); |
| 9445 | break; |
| 9446 | default: |
| 9447 | assert(0); |
| 9448 | break; |
| 9449 | } |
| 9450 | leave: |
| 9451 | PyMem_FREE(tmp); |
| 9452 | return res; |
| 9453 | } |
| 9454 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9455 | PyObject * |
| 9456 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9457 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9458 | PyObject *sep = NULL; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9459 | Py_ssize_t seplen; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9460 | PyObject *res = NULL; /* the result */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9461 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9462 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
| 9463 | PyObject **items; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9464 | PyObject *item; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9465 | Py_ssize_t sz, i, res_offset; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9466 | Py_UCS4 maxchar; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9467 | Py_UCS4 item_maxchar; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9468 | int use_memcpy; |
| 9469 | unsigned char *res_data = NULL, *sep_data = NULL; |
| 9470 | PyObject *last_obj; |
| 9471 | unsigned int kind = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9472 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9473 | fseq = PySequence_Fast(seq, ""); |
| 9474 | if (fseq == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9475 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9476 | } |
| 9477 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9478 | /* NOTE: the following code can't call back into Python code, |
| 9479 | * so we are sure that fseq won't be mutated. |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9480 | */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9481 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9482 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 9483 | /* If empty sequence, return u"". */ |
| 9484 | if (seqlen == 0) { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9485 | Py_DECREF(fseq); |
| 9486 | Py_INCREF(unicode_empty); |
| 9487 | res = unicode_empty; |
| 9488 | return res; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9489 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9490 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9491 | /* If singleton sequence with an exact Unicode, return that. */ |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9492 | last_obj = NULL; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9493 | items = PySequence_Fast_ITEMS(fseq); |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9494 | if (seqlen == 1) { |
| 9495 | if (PyUnicode_CheckExact(items[0])) { |
| 9496 | res = items[0]; |
| 9497 | Py_INCREF(res); |
| 9498 | Py_DECREF(fseq); |
| 9499 | return res; |
| 9500 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9501 | seplen = 0; |
Victor Stinner | c6f0df7 | 2011-10-06 15:58:54 +0200 | [diff] [blame] | 9502 | maxchar = 0; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9503 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9504 | else { |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9505 | /* Set up sep and seplen */ |
| 9506 | if (separator == NULL) { |
| 9507 | /* fall back to a blank space separator */ |
| 9508 | sep = PyUnicode_FromOrdinal(' '); |
| 9509 | if (!sep) |
| 9510 | goto onError; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9511 | seplen = 1; |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9512 | maxchar = 32; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9513 | } |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9514 | else { |
| 9515 | if (!PyUnicode_Check(separator)) { |
| 9516 | PyErr_Format(PyExc_TypeError, |
| 9517 | "separator: expected str instance," |
| 9518 | " %.80s found", |
| 9519 | Py_TYPE(separator)->tp_name); |
| 9520 | goto onError; |
| 9521 | } |
| 9522 | if (PyUnicode_READY(separator)) |
| 9523 | goto onError; |
| 9524 | sep = separator; |
| 9525 | seplen = PyUnicode_GET_LENGTH(separator); |
| 9526 | maxchar = PyUnicode_MAX_CHAR_VALUE(separator); |
| 9527 | /* inc refcount to keep this code path symmetric with the |
| 9528 | above case of a blank separator */ |
| 9529 | Py_INCREF(sep); |
| 9530 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9531 | last_obj = sep; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9532 | } |
| 9533 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9534 | /* There are at least two things to join, or else we have a subclass |
| 9535 | * of str in the sequence. |
| 9536 | * Do a pre-pass to figure out the total amount of space we'll |
| 9537 | * need (sz), and see whether all argument are strings. |
| 9538 | */ |
| 9539 | sz = 0; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9540 | #ifdef Py_DEBUG |
| 9541 | use_memcpy = 0; |
| 9542 | #else |
| 9543 | use_memcpy = 1; |
| 9544 | #endif |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9545 | for (i = 0; i < seqlen; i++) { |
| 9546 | const Py_ssize_t old_sz = sz; |
| 9547 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9548 | if (!PyUnicode_Check(item)) { |
| 9549 | PyErr_Format(PyExc_TypeError, |
| 9550 | "sequence item %zd: expected str instance," |
| 9551 | " %.80s found", |
| 9552 | i, Py_TYPE(item)->tp_name); |
| 9553 | goto onError; |
| 9554 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9555 | if (PyUnicode_READY(item) == -1) |
| 9556 | goto onError; |
| 9557 | sz += PyUnicode_GET_LENGTH(item); |
| 9558 | item_maxchar = PyUnicode_MAX_CHAR_VALUE(item); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9559 | maxchar = MAX_MAXCHAR(maxchar, item_maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9560 | if (i != 0) |
| 9561 | sz += seplen; |
| 9562 | if (sz < old_sz || sz > PY_SSIZE_T_MAX) { |
| 9563 | PyErr_SetString(PyExc_OverflowError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9564 | "join() result is too long for a Python string"); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9565 | goto onError; |
| 9566 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9567 | if (use_memcpy && last_obj != NULL) { |
| 9568 | if (PyUnicode_KIND(last_obj) != PyUnicode_KIND(item)) |
| 9569 | use_memcpy = 0; |
| 9570 | } |
| 9571 | last_obj = item; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9572 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9573 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9574 | res = PyUnicode_New(sz, maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9575 | if (res == NULL) |
| 9576 | goto onError; |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9577 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9578 | /* Catenate everything. */ |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9579 | #ifdef Py_DEBUG |
| 9580 | use_memcpy = 0; |
| 9581 | #else |
| 9582 | if (use_memcpy) { |
| 9583 | res_data = PyUnicode_1BYTE_DATA(res); |
| 9584 | kind = PyUnicode_KIND(res); |
| 9585 | if (seplen != 0) |
| 9586 | sep_data = PyUnicode_1BYTE_DATA(sep); |
| 9587 | } |
| 9588 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9589 | for (i = 0, res_offset = 0; i < seqlen; ++i) { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9590 | Py_ssize_t itemlen; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9591 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9592 | /* Copy item, and maybe the separator. */ |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9593 | if (i && seplen != 0) { |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9594 | if (use_memcpy) { |
| 9595 | Py_MEMCPY(res_data, |
| 9596 | sep_data, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9597 | kind * seplen); |
| 9598 | res_data += kind * seplen; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9599 | } |
| 9600 | else { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9601 | _PyUnicode_FastCopyCharacters(res, res_offset, sep, 0, seplen); |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9602 | res_offset += seplen; |
| 9603 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9604 | } |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9605 | itemlen = PyUnicode_GET_LENGTH(item); |
| 9606 | if (itemlen != 0) { |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9607 | if (use_memcpy) { |
| 9608 | Py_MEMCPY(res_data, |
| 9609 | PyUnicode_DATA(item), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9610 | kind * itemlen); |
| 9611 | res_data += kind * itemlen; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9612 | } |
| 9613 | else { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9614 | _PyUnicode_FastCopyCharacters(res, res_offset, item, 0, itemlen); |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9615 | res_offset += itemlen; |
| 9616 | } |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9617 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9618 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9619 | if (use_memcpy) |
| 9620 | assert(res_data == PyUnicode_1BYTE_DATA(res) |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9621 | + kind * PyUnicode_GET_LENGTH(res)); |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9622 | else |
| 9623 | assert(res_offset == PyUnicode_GET_LENGTH(res)); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9624 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9625 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9626 | Py_XDECREF(sep); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9627 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9628 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9629 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9630 | onError: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9631 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9632 | Py_XDECREF(sep); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9633 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9634 | return NULL; |
| 9635 | } |
| 9636 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9637 | #define FILL(kind, data, value, start, length) \ |
| 9638 | do { \ |
| 9639 | Py_ssize_t i_ = 0; \ |
| 9640 | assert(kind != PyUnicode_WCHAR_KIND); \ |
| 9641 | switch ((kind)) { \ |
| 9642 | case PyUnicode_1BYTE_KIND: { \ |
| 9643 | unsigned char * to_ = (unsigned char *)((data)) + (start); \ |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 9644 | memset(to_, (unsigned char)value, (length)); \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9645 | break; \ |
| 9646 | } \ |
| 9647 | case PyUnicode_2BYTE_KIND: { \ |
| 9648 | Py_UCS2 * to_ = (Py_UCS2 *)((data)) + (start); \ |
| 9649 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 9650 | break; \ |
| 9651 | } \ |
Benjamin Peterson | e157cf1 | 2012-01-01 15:56:20 -0600 | [diff] [blame] | 9652 | case PyUnicode_4BYTE_KIND: { \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9653 | Py_UCS4 * to_ = (Py_UCS4 *)((data)) + (start); \ |
| 9654 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 9655 | break; \ |
Benjamin Peterson | e157cf1 | 2012-01-01 15:56:20 -0600 | [diff] [blame] | 9656 | default: assert(0); \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9657 | } \ |
| 9658 | } \ |
| 9659 | } while (0) |
| 9660 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9661 | void |
| 9662 | _PyUnicode_FastFill(PyObject *unicode, Py_ssize_t start, Py_ssize_t length, |
| 9663 | Py_UCS4 fill_char) |
| 9664 | { |
| 9665 | const enum PyUnicode_Kind kind = PyUnicode_KIND(unicode); |
| 9666 | const void *data = PyUnicode_DATA(unicode); |
| 9667 | assert(PyUnicode_IS_READY(unicode)); |
| 9668 | assert(unicode_modifiable(unicode)); |
| 9669 | assert(fill_char <= PyUnicode_MAX_CHAR_VALUE(unicode)); |
| 9670 | assert(start >= 0); |
| 9671 | assert(start + length <= PyUnicode_GET_LENGTH(unicode)); |
| 9672 | FILL(kind, data, fill_char, start, length); |
| 9673 | } |
| 9674 | |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 9675 | Py_ssize_t |
| 9676 | PyUnicode_Fill(PyObject *unicode, Py_ssize_t start, Py_ssize_t length, |
| 9677 | Py_UCS4 fill_char) |
| 9678 | { |
| 9679 | Py_ssize_t maxlen; |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 9680 | |
| 9681 | if (!PyUnicode_Check(unicode)) { |
| 9682 | PyErr_BadInternalCall(); |
| 9683 | return -1; |
| 9684 | } |
| 9685 | if (PyUnicode_READY(unicode) == -1) |
| 9686 | return -1; |
| 9687 | if (unicode_check_modifiable(unicode)) |
| 9688 | return -1; |
| 9689 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9690 | if (start < 0) { |
| 9691 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 9692 | return -1; |
| 9693 | } |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 9694 | if (fill_char > PyUnicode_MAX_CHAR_VALUE(unicode)) { |
| 9695 | PyErr_SetString(PyExc_ValueError, |
| 9696 | "fill character is bigger than " |
| 9697 | "the string maximum character"); |
| 9698 | return -1; |
| 9699 | } |
| 9700 | |
| 9701 | maxlen = PyUnicode_GET_LENGTH(unicode) - start; |
| 9702 | length = Py_MIN(maxlen, length); |
| 9703 | if (length <= 0) |
| 9704 | return 0; |
| 9705 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9706 | _PyUnicode_FastFill(unicode, start, length, fill_char); |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 9707 | return length; |
| 9708 | } |
| 9709 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9710 | static PyObject * |
| 9711 | pad(PyObject *self, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9712 | Py_ssize_t left, |
| 9713 | Py_ssize_t right, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9714 | Py_UCS4 fill) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9715 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9716 | PyObject *u; |
| 9717 | Py_UCS4 maxchar; |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9718 | int kind; |
| 9719 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9720 | |
| 9721 | if (left < 0) |
| 9722 | left = 0; |
| 9723 | if (right < 0) |
| 9724 | right = 0; |
| 9725 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 9726 | if (left == 0 && right == 0) |
| 9727 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9728 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9729 | if (left > PY_SSIZE_T_MAX - _PyUnicode_LENGTH(self) || |
| 9730 | right > PY_SSIZE_T_MAX - (left + _PyUnicode_LENGTH(self))) { |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 9731 | PyErr_SetString(PyExc_OverflowError, "padded string is too long"); |
| 9732 | return NULL; |
| 9733 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9734 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9735 | maxchar = MAX_MAXCHAR(maxchar, fill); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9736 | u = PyUnicode_New(left + _PyUnicode_LENGTH(self) + right, maxchar); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9737 | if (!u) |
| 9738 | return NULL; |
| 9739 | |
| 9740 | kind = PyUnicode_KIND(u); |
| 9741 | data = PyUnicode_DATA(u); |
| 9742 | if (left) |
| 9743 | FILL(kind, data, fill, 0, left); |
| 9744 | if (right) |
| 9745 | FILL(kind, data, fill, left + _PyUnicode_LENGTH(self), right); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9746 | _PyUnicode_FastCopyCharacters(u, left, self, 0, _PyUnicode_LENGTH(self)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9747 | assert(_PyUnicode_CheckConsistency(u, 1)); |
| 9748 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9749 | } |
| 9750 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9751 | PyObject * |
| 9752 | PyUnicode_Splitlines(PyObject *string, int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9753 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9754 | PyObject *list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9755 | |
| 9756 | string = PyUnicode_FromObject(string); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9757 | if (string == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9758 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9759 | if (PyUnicode_READY(string) == -1) { |
| 9760 | Py_DECREF(string); |
| 9761 | return NULL; |
| 9762 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9763 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9764 | switch (PyUnicode_KIND(string)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9765 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9766 | if (PyUnicode_IS_ASCII(string)) |
| 9767 | list = asciilib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9768 | string, PyUnicode_1BYTE_DATA(string), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9769 | PyUnicode_GET_LENGTH(string), keepends); |
| 9770 | else |
| 9771 | list = ucs1lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9772 | string, PyUnicode_1BYTE_DATA(string), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9773 | PyUnicode_GET_LENGTH(string), keepends); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9774 | break; |
| 9775 | case PyUnicode_2BYTE_KIND: |
| 9776 | list = ucs2lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9777 | string, PyUnicode_2BYTE_DATA(string), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9778 | PyUnicode_GET_LENGTH(string), keepends); |
| 9779 | break; |
| 9780 | case PyUnicode_4BYTE_KIND: |
| 9781 | list = ucs4lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9782 | string, PyUnicode_4BYTE_DATA(string), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9783 | PyUnicode_GET_LENGTH(string), keepends); |
| 9784 | break; |
| 9785 | default: |
| 9786 | assert(0); |
| 9787 | list = 0; |
| 9788 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9789 | Py_DECREF(string); |
| 9790 | return list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9791 | } |
| 9792 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9793 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9794 | split(PyObject *self, |
| 9795 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9796 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9797 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9798 | int kind1, kind2, kind; |
| 9799 | void *buf1, *buf2; |
| 9800 | Py_ssize_t len1, len2; |
| 9801 | PyObject* out; |
| 9802 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9803 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9804 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9805 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9806 | if (PyUnicode_READY(self) == -1) |
| 9807 | return NULL; |
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 | if (substring == NULL) |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9810 | switch (PyUnicode_KIND(self)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9811 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9812 | if (PyUnicode_IS_ASCII(self)) |
| 9813 | return asciilib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9814 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9815 | PyUnicode_GET_LENGTH(self), maxcount |
| 9816 | ); |
| 9817 | else |
| 9818 | return ucs1lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9819 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9820 | PyUnicode_GET_LENGTH(self), maxcount |
| 9821 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9822 | case PyUnicode_2BYTE_KIND: |
| 9823 | return ucs2lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9824 | self, PyUnicode_2BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9825 | PyUnicode_GET_LENGTH(self), maxcount |
| 9826 | ); |
| 9827 | case PyUnicode_4BYTE_KIND: |
| 9828 | return ucs4lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9829 | self, PyUnicode_4BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9830 | PyUnicode_GET_LENGTH(self), maxcount |
| 9831 | ); |
| 9832 | default: |
| 9833 | assert(0); |
| 9834 | return NULL; |
| 9835 | } |
| 9836 | |
| 9837 | if (PyUnicode_READY(substring) == -1) |
| 9838 | return NULL; |
| 9839 | |
| 9840 | kind1 = PyUnicode_KIND(self); |
| 9841 | kind2 = PyUnicode_KIND(substring); |
| 9842 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9843 | buf1 = PyUnicode_DATA(self); |
| 9844 | buf2 = PyUnicode_DATA(substring); |
| 9845 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9846 | buf1 = _PyUnicode_AsKind(self, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9847 | if (!buf1) |
| 9848 | return NULL; |
| 9849 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9850 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9851 | if (!buf2) { |
| 9852 | if (kind1 != kind) PyMem_Free(buf1); |
| 9853 | return NULL; |
| 9854 | } |
| 9855 | len1 = PyUnicode_GET_LENGTH(self); |
| 9856 | len2 = PyUnicode_GET_LENGTH(substring); |
| 9857 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9858 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9859 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9860 | if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) |
| 9861 | out = asciilib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9862 | self, buf1, len1, buf2, len2, maxcount); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9863 | else |
| 9864 | out = ucs1lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9865 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9866 | break; |
| 9867 | case PyUnicode_2BYTE_KIND: |
| 9868 | out = ucs2lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9869 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9870 | break; |
| 9871 | case PyUnicode_4BYTE_KIND: |
| 9872 | out = ucs4lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9873 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9874 | break; |
| 9875 | default: |
| 9876 | out = NULL; |
| 9877 | } |
| 9878 | if (kind1 != kind) |
| 9879 | PyMem_Free(buf1); |
| 9880 | if (kind2 != kind) |
| 9881 | PyMem_Free(buf2); |
| 9882 | return out; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9883 | } |
| 9884 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9885 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9886 | rsplit(PyObject *self, |
| 9887 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9888 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9889 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9890 | int kind1, kind2, kind; |
| 9891 | void *buf1, *buf2; |
| 9892 | Py_ssize_t len1, len2; |
| 9893 | PyObject* out; |
| 9894 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9895 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9896 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9897 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9898 | if (PyUnicode_READY(self) == -1) |
| 9899 | return NULL; |
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 | if (substring == NULL) |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9902 | switch (PyUnicode_KIND(self)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9903 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9904 | if (PyUnicode_IS_ASCII(self)) |
| 9905 | return asciilib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9906 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9907 | PyUnicode_GET_LENGTH(self), maxcount |
| 9908 | ); |
| 9909 | else |
| 9910 | return ucs1lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9911 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9912 | PyUnicode_GET_LENGTH(self), maxcount |
| 9913 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9914 | case PyUnicode_2BYTE_KIND: |
| 9915 | return ucs2lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9916 | self, PyUnicode_2BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9917 | PyUnicode_GET_LENGTH(self), maxcount |
| 9918 | ); |
| 9919 | case PyUnicode_4BYTE_KIND: |
| 9920 | return ucs4lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9921 | self, PyUnicode_4BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9922 | PyUnicode_GET_LENGTH(self), maxcount |
| 9923 | ); |
| 9924 | default: |
| 9925 | assert(0); |
| 9926 | return NULL; |
| 9927 | } |
| 9928 | |
| 9929 | if (PyUnicode_READY(substring) == -1) |
| 9930 | return NULL; |
| 9931 | |
| 9932 | kind1 = PyUnicode_KIND(self); |
| 9933 | kind2 = PyUnicode_KIND(substring); |
| 9934 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9935 | buf1 = PyUnicode_DATA(self); |
| 9936 | buf2 = PyUnicode_DATA(substring); |
| 9937 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9938 | buf1 = _PyUnicode_AsKind(self, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9939 | if (!buf1) |
| 9940 | return NULL; |
| 9941 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9942 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9943 | if (!buf2) { |
| 9944 | if (kind1 != kind) PyMem_Free(buf1); |
| 9945 | return NULL; |
| 9946 | } |
| 9947 | len1 = PyUnicode_GET_LENGTH(self); |
| 9948 | len2 = PyUnicode_GET_LENGTH(substring); |
| 9949 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9950 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9951 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9952 | if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) |
| 9953 | out = asciilib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9954 | self, buf1, len1, buf2, len2, maxcount); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9955 | else |
| 9956 | out = ucs1lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9957 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9958 | break; |
| 9959 | case PyUnicode_2BYTE_KIND: |
| 9960 | out = ucs2lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9961 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9962 | break; |
| 9963 | case PyUnicode_4BYTE_KIND: |
| 9964 | out = ucs4lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9965 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9966 | break; |
| 9967 | default: |
| 9968 | out = NULL; |
| 9969 | } |
| 9970 | if (kind1 != kind) |
| 9971 | PyMem_Free(buf1); |
| 9972 | if (kind2 != kind) |
| 9973 | PyMem_Free(buf2); |
| 9974 | return out; |
| 9975 | } |
| 9976 | |
| 9977 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9978 | anylib_find(int kind, PyObject *str1, void *buf1, Py_ssize_t len1, |
| 9979 | 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] | 9980 | { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9981 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9982 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9983 | if (PyUnicode_IS_ASCII(str1) && PyUnicode_IS_ASCII(str2)) |
| 9984 | return asciilib_find(buf1, len1, buf2, len2, offset); |
| 9985 | else |
| 9986 | return ucs1lib_find(buf1, len1, buf2, len2, offset); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9987 | case PyUnicode_2BYTE_KIND: |
| 9988 | return ucs2lib_find(buf1, len1, buf2, len2, offset); |
| 9989 | case PyUnicode_4BYTE_KIND: |
| 9990 | return ucs4lib_find(buf1, len1, buf2, len2, offset); |
| 9991 | } |
| 9992 | assert(0); |
| 9993 | return -1; |
| 9994 | } |
| 9995 | |
| 9996 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9997 | anylib_count(int kind, PyObject *sstr, void* sbuf, Py_ssize_t slen, |
| 9998 | 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] | 9999 | { |
Benjamin Peterson | c0b95d1 | 2011-12-20 17:24:05 -0600 | [diff] [blame] | 10000 | switch (kind) { |
| 10001 | case PyUnicode_1BYTE_KIND: |
| 10002 | if (PyUnicode_IS_ASCII(sstr) && PyUnicode_IS_ASCII(str1)) |
| 10003 | return asciilib_count(sbuf, slen, buf1, len1, maxcount); |
| 10004 | else |
| 10005 | return ucs1lib_count(sbuf, slen, buf1, len1, maxcount); |
| 10006 | case PyUnicode_2BYTE_KIND: |
| 10007 | return ucs2lib_count(sbuf, slen, buf1, len1, maxcount); |
| 10008 | case PyUnicode_4BYTE_KIND: |
| 10009 | return ucs4lib_count(sbuf, slen, buf1, len1, maxcount); |
| 10010 | } |
| 10011 | assert(0); |
| 10012 | return 0; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10013 | } |
| 10014 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10015 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10016 | replace(PyObject *self, PyObject *str1, |
| 10017 | PyObject *str2, Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10018 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10019 | PyObject *u; |
| 10020 | char *sbuf = PyUnicode_DATA(self); |
| 10021 | char *buf1 = PyUnicode_DATA(str1); |
| 10022 | char *buf2 = PyUnicode_DATA(str2); |
| 10023 | int srelease = 0, release1 = 0, release2 = 0; |
| 10024 | int skind = PyUnicode_KIND(self); |
| 10025 | int kind1 = PyUnicode_KIND(str1); |
| 10026 | int kind2 = PyUnicode_KIND(str2); |
| 10027 | Py_ssize_t slen = PyUnicode_GET_LENGTH(self); |
| 10028 | Py_ssize_t len1 = PyUnicode_GET_LENGTH(str1); |
| 10029 | Py_ssize_t len2 = PyUnicode_GET_LENGTH(str2); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10030 | int mayshrink; |
| 10031 | Py_UCS4 maxchar, maxchar_str2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10032 | |
| 10033 | if (maxcount < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10034 | maxcount = PY_SSIZE_T_MAX; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10035 | else if (maxcount == 0 || slen == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10036 | goto nothing; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10037 | |
Victor Stinner | 59de0ee | 2011-10-07 10:01:28 +0200 | [diff] [blame] | 10038 | if (str1 == str2) |
| 10039 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10040 | if (skind < kind1) |
| 10041 | /* substring too wide to be present */ |
| 10042 | goto nothing; |
| 10043 | |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10044 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
| 10045 | maxchar_str2 = PyUnicode_MAX_CHAR_VALUE(str2); |
| 10046 | /* Replacing str1 with str2 may cause a maxchar reduction in the |
| 10047 | result string. */ |
| 10048 | mayshrink = (maxchar_str2 < maxchar); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 10049 | maxchar = MAX_MAXCHAR(maxchar, maxchar_str2); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10050 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10051 | if (len1 == len2) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10052 | /* same length */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10053 | if (len1 == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10054 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10055 | if (len1 == 1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10056 | /* replace characters */ |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10057 | Py_UCS4 u1, u2; |
| 10058 | int rkind; |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10059 | Py_ssize_t index, pos; |
| 10060 | char *src; |
| 10061 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10062 | u1 = PyUnicode_READ_CHAR(str1, 0); |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10063 | pos = findchar(sbuf, PyUnicode_KIND(self), slen, u1, 1); |
| 10064 | if (pos < 0) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10065 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10066 | u2 = PyUnicode_READ_CHAR(str2, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10067 | u = PyUnicode_New(slen, maxchar); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10068 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10069 | goto error; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 10070 | _PyUnicode_FastCopyCharacters(u, 0, self, 0, slen); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10071 | rkind = PyUnicode_KIND(u); |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10072 | |
| 10073 | PyUnicode_WRITE(rkind, PyUnicode_DATA(u), pos, u2); |
| 10074 | index = 0; |
| 10075 | src = sbuf; |
| 10076 | while (--maxcount) |
| 10077 | { |
| 10078 | pos++; |
| 10079 | src += pos * PyUnicode_KIND(self); |
| 10080 | slen -= pos; |
| 10081 | index += pos; |
| 10082 | pos = findchar(src, PyUnicode_KIND(self), slen, u1, 1); |
| 10083 | if (pos < 0) |
| 10084 | break; |
| 10085 | PyUnicode_WRITE(rkind, PyUnicode_DATA(u), index + pos, u2); |
| 10086 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10087 | } |
| 10088 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10089 | int rkind = skind; |
| 10090 | char *res; |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10091 | Py_ssize_t i; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10092 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10093 | if (kind1 < rkind) { |
| 10094 | /* widen substring */ |
| 10095 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10096 | if (!buf1) goto error; |
| 10097 | release1 = 1; |
| 10098 | } |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10099 | i = anylib_find(rkind, self, sbuf, slen, str1, buf1, len1, 0); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10100 | if (i < 0) |
| 10101 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10102 | if (rkind > kind2) { |
| 10103 | /* widen replacement */ |
| 10104 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 10105 | if (!buf2) goto error; |
| 10106 | release2 = 1; |
| 10107 | } |
| 10108 | else if (rkind < kind2) { |
| 10109 | /* widen self and buf1 */ |
| 10110 | rkind = kind2; |
| 10111 | if (release1) PyMem_Free(buf1); |
| 10112 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 10113 | if (!sbuf) goto error; |
| 10114 | srelease = 1; |
| 10115 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10116 | if (!buf1) goto error; |
| 10117 | release1 = 1; |
| 10118 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10119 | u = PyUnicode_New(slen, maxchar); |
| 10120 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10121 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10122 | assert(PyUnicode_KIND(u) == rkind); |
| 10123 | res = PyUnicode_DATA(u); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10124 | |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10125 | memcpy(res, sbuf, rkind * slen); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10126 | /* change everything in-place, starting with this one */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10127 | memcpy(res + rkind * i, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10128 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10129 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10130 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10131 | |
| 10132 | while ( --maxcount > 0) { |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10133 | i = anylib_find(rkind, self, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10134 | sbuf+rkind*i, slen-i, |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10135 | str1, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10136 | if (i == -1) |
| 10137 | break; |
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 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10143 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10144 | } |
| 10145 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10146 | Py_ssize_t n, i, j, ires; |
| 10147 | Py_ssize_t product, new_size; |
| 10148 | int rkind = skind; |
| 10149 | char *res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10150 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10151 | if (kind1 < rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10152 | /* widen substring */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10153 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10154 | if (!buf1) goto error; |
| 10155 | release1 = 1; |
| 10156 | } |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10157 | n = anylib_count(rkind, self, sbuf, slen, str1, buf1, len1, maxcount); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10158 | if (n == 0) |
| 10159 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10160 | if (kind2 < rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10161 | /* widen replacement */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10162 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 10163 | if (!buf2) goto error; |
| 10164 | release2 = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10165 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10166 | else if (kind2 > rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10167 | /* widen self and buf1 */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10168 | rkind = kind2; |
| 10169 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 10170 | if (!sbuf) goto error; |
| 10171 | srelease = 1; |
| 10172 | if (release1) PyMem_Free(buf1); |
| 10173 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10174 | if (!buf1) goto error; |
| 10175 | release1 = 1; |
| 10176 | } |
| 10177 | /* new_size = PyUnicode_GET_LENGTH(self) + n * (PyUnicode_GET_LENGTH(str2) - |
| 10178 | PyUnicode_GET_LENGTH(str1))); */ |
| 10179 | product = n * (len2-len1); |
| 10180 | if ((product / (len2-len1)) != n) { |
| 10181 | PyErr_SetString(PyExc_OverflowError, |
| 10182 | "replace string is too long"); |
| 10183 | goto error; |
| 10184 | } |
| 10185 | new_size = slen + product; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10186 | if (new_size == 0) { |
| 10187 | Py_INCREF(unicode_empty); |
| 10188 | u = unicode_empty; |
| 10189 | goto done; |
| 10190 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10191 | if (new_size < 0 || new_size > (PY_SSIZE_T_MAX >> (rkind-1))) { |
| 10192 | PyErr_SetString(PyExc_OverflowError, |
| 10193 | "replace string is too long"); |
| 10194 | goto error; |
| 10195 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10196 | u = PyUnicode_New(new_size, maxchar); |
| 10197 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10198 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10199 | assert(PyUnicode_KIND(u) == rkind); |
| 10200 | res = PyUnicode_DATA(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10201 | ires = i = 0; |
| 10202 | if (len1 > 0) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10203 | while (n-- > 0) { |
| 10204 | /* look for next match */ |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10205 | j = anylib_find(rkind, self, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10206 | sbuf + rkind * i, slen-i, |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10207 | str1, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10208 | if (j == -1) |
| 10209 | break; |
| 10210 | else if (j > i) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10211 | /* copy unchanged part [i:j] */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10212 | memcpy(res + rkind * ires, |
| 10213 | sbuf + rkind * i, |
| 10214 | rkind * (j-i)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10215 | ires += j - i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10216 | } |
| 10217 | /* copy substitution string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10218 | if (len2 > 0) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10219 | memcpy(res + rkind * ires, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10220 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10221 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10222 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10223 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10224 | i = j + len1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10225 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10226 | if (i < slen) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10227 | /* copy tail [i:] */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10228 | memcpy(res + rkind * ires, |
| 10229 | sbuf + rkind * i, |
| 10230 | rkind * (slen-i)); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10231 | } |
| 10232 | else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10233 | /* interleave */ |
| 10234 | while (n > 0) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10235 | memcpy(res + rkind * ires, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10236 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10237 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10238 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10239 | if (--n <= 0) |
| 10240 | break; |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10241 | memcpy(res + rkind * ires, |
| 10242 | sbuf + rkind * i, |
| 10243 | rkind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10244 | ires++; |
| 10245 | i++; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10246 | } |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10247 | memcpy(res + rkind * ires, |
| 10248 | sbuf + rkind * i, |
| 10249 | rkind * (slen-i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10250 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10251 | } |
| 10252 | |
| 10253 | if (mayshrink) { |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10254 | unicode_adjust_maxchar(&u); |
| 10255 | if (u == NULL) |
| 10256 | goto error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10257 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10258 | |
| 10259 | done: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10260 | if (srelease) |
| 10261 | PyMem_FREE(sbuf); |
| 10262 | if (release1) |
| 10263 | PyMem_FREE(buf1); |
| 10264 | if (release2) |
| 10265 | PyMem_FREE(buf2); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10266 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10267 | return u; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10268 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10269 | nothing: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10270 | /* nothing to replace; return original string (when possible) */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10271 | if (srelease) |
| 10272 | PyMem_FREE(sbuf); |
| 10273 | if (release1) |
| 10274 | PyMem_FREE(buf1); |
| 10275 | if (release2) |
| 10276 | PyMem_FREE(buf2); |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10277 | return unicode_result_unchanged(self); |
| 10278 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10279 | error: |
| 10280 | if (srelease && sbuf) |
| 10281 | PyMem_FREE(sbuf); |
| 10282 | if (release1 && buf1) |
| 10283 | PyMem_FREE(buf1); |
| 10284 | if (release2 && buf2) |
| 10285 | PyMem_FREE(buf2); |
| 10286 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10287 | } |
| 10288 | |
| 10289 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 10290 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10291 | PyDoc_STRVAR(title__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10292 | "S.title() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10293 | \n\ |
| 10294 | 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] | 10295 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10296 | |
| 10297 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10298 | unicode_title(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10299 | { |
Benjamin Peterson | eea4846 | 2012-01-16 14:28:50 -0500 | [diff] [blame] | 10300 | if (PyUnicode_READY(self) == -1) |
| 10301 | return NULL; |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 10302 | return case_operation(self, do_title); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10303 | } |
| 10304 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10305 | PyDoc_STRVAR(capitalize__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10306 | "S.capitalize() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10307 | \n\ |
| 10308 | 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] | 10309 | have upper case and the rest lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10310 | |
| 10311 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10312 | unicode_capitalize(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10313 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 10314 | if (PyUnicode_READY(self) == -1) |
| 10315 | return NULL; |
| 10316 | if (PyUnicode_GET_LENGTH(self) == 0) |
| 10317 | return unicode_result_unchanged(self); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 10318 | return case_operation(self, do_capitalize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10319 | } |
| 10320 | |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 10321 | PyDoc_STRVAR(casefold__doc__, |
| 10322 | "S.casefold() -> str\n\ |
| 10323 | \n\ |
| 10324 | Return a version of S suitable for caseless comparisons."); |
| 10325 | |
| 10326 | static PyObject * |
| 10327 | unicode_casefold(PyObject *self) |
| 10328 | { |
| 10329 | if (PyUnicode_READY(self) == -1) |
| 10330 | return NULL; |
| 10331 | if (PyUnicode_IS_ASCII(self)) |
| 10332 | return ascii_upper_or_lower(self, 1); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 10333 | return case_operation(self, do_casefold); |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 10334 | } |
| 10335 | |
| 10336 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10337 | /* Argument converter. Coerces to a single unicode character */ |
| 10338 | |
| 10339 | static int |
| 10340 | convert_uc(PyObject *obj, void *addr) |
| 10341 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10342 | Py_UCS4 *fillcharloc = (Py_UCS4 *)addr; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10343 | PyObject *uniobj; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10344 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10345 | uniobj = PyUnicode_FromObject(obj); |
| 10346 | if (uniobj == NULL) { |
| 10347 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10348 | "The fill character cannot be converted to Unicode"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10349 | return 0; |
| 10350 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10351 | if (PyUnicode_GET_LENGTH(uniobj) != 1) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10352 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10353 | "The fill character must be exactly one character long"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10354 | Py_DECREF(uniobj); |
| 10355 | return 0; |
| 10356 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10357 | *fillcharloc = PyUnicode_READ_CHAR(uniobj, 0); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10358 | Py_DECREF(uniobj); |
| 10359 | return 1; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10360 | } |
| 10361 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10362 | PyDoc_STRVAR(center__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10363 | "S.center(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10364 | \n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10365 | Return S centered in a string of length width. Padding is\n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10366 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10367 | |
| 10368 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10369 | unicode_center(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10370 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10371 | Py_ssize_t marg, left; |
| 10372 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10373 | Py_UCS4 fillchar = ' '; |
| 10374 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 10375 | 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] | 10376 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10377 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 10378 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10379 | return NULL; |
| 10380 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10381 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 10382 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10383 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10384 | marg = width - PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10385 | left = marg / 2 + (marg & width & 1); |
| 10386 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10387 | return pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10388 | } |
| 10389 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10390 | /* This function assumes that str1 and str2 are readied by the caller. */ |
| 10391 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10392 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10393 | unicode_compare(PyObject *str1, PyObject *str2) |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10394 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10395 | int kind1, kind2; |
| 10396 | void *data1, *data2; |
| 10397 | Py_ssize_t len1, len2, i; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10398 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10399 | kind1 = PyUnicode_KIND(str1); |
| 10400 | kind2 = PyUnicode_KIND(str2); |
| 10401 | data1 = PyUnicode_DATA(str1); |
| 10402 | data2 = PyUnicode_DATA(str2); |
| 10403 | len1 = PyUnicode_GET_LENGTH(str1); |
| 10404 | len2 = PyUnicode_GET_LENGTH(str2); |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10405 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10406 | for (i = 0; i < len1 && i < len2; ++i) { |
| 10407 | Py_UCS4 c1, c2; |
| 10408 | c1 = PyUnicode_READ(kind1, data1, i); |
| 10409 | c2 = PyUnicode_READ(kind2, data2, i); |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 10410 | |
| 10411 | if (c1 != c2) |
| 10412 | return (c1 < c2) ? -1 : 1; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10413 | } |
| 10414 | |
| 10415 | return (len1 < len2) ? -1 : (len1 != len2); |
| 10416 | } |
| 10417 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10418 | int |
| 10419 | PyUnicode_Compare(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10420 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10421 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 10422 | if (PyUnicode_READY(left) == -1 || |
| 10423 | PyUnicode_READY(right) == -1) |
| 10424 | return -1; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10425 | return unicode_compare(left, right); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10426 | } |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 10427 | PyErr_Format(PyExc_TypeError, |
| 10428 | "Can't compare %.100s and %.100s", |
| 10429 | left->ob_type->tp_name, |
| 10430 | right->ob_type->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10431 | return -1; |
| 10432 | } |
| 10433 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10434 | int |
| 10435 | PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str) |
| 10436 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10437 | Py_ssize_t i; |
| 10438 | int kind; |
| 10439 | void *data; |
| 10440 | Py_UCS4 chr; |
| 10441 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 10442 | assert(_PyUnicode_CHECK(uni)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10443 | if (PyUnicode_READY(uni) == -1) |
| 10444 | return -1; |
| 10445 | kind = PyUnicode_KIND(uni); |
| 10446 | data = PyUnicode_DATA(uni); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10447 | /* Compare Unicode string and source character set string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10448 | for (i = 0; (chr = PyUnicode_READ(kind, data, i)) && str[i]; i++) |
| 10449 | if (chr != str[i]) |
| 10450 | return (chr < (unsigned char)(str[i])) ? -1 : 1; |
Benjamin Peterson | 8667a9b | 2010-01-09 21:45:28 +0000 | [diff] [blame] | 10451 | /* This check keeps Python strings that end in '\0' from comparing equal |
| 10452 | to C strings identical up to that point. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10453 | if (PyUnicode_GET_LENGTH(uni) != i || chr) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10454 | return 1; /* uni is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10455 | if (str[i]) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10456 | return -1; /* str is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10457 | return 0; |
| 10458 | } |
| 10459 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10460 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10461 | #define TEST_COND(cond) \ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10462 | ((cond) ? Py_True : Py_False) |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10463 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10464 | PyObject * |
| 10465 | PyUnicode_RichCompare(PyObject *left, PyObject *right, int op) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10466 | { |
| 10467 | int result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10468 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10469 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 10470 | PyObject *v; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10471 | if (PyUnicode_READY(left) == -1 || |
| 10472 | PyUnicode_READY(right) == -1) |
| 10473 | return NULL; |
| 10474 | if (PyUnicode_GET_LENGTH(left) != PyUnicode_GET_LENGTH(right) || |
| 10475 | PyUnicode_KIND(left) != PyUnicode_KIND(right)) { |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10476 | if (op == Py_EQ) { |
| 10477 | Py_INCREF(Py_False); |
| 10478 | return Py_False; |
| 10479 | } |
| 10480 | if (op == Py_NE) { |
| 10481 | Py_INCREF(Py_True); |
| 10482 | return Py_True; |
| 10483 | } |
| 10484 | } |
| 10485 | if (left == right) |
| 10486 | result = 0; |
| 10487 | else |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10488 | result = unicode_compare(left, right); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10489 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10490 | /* Convert the return value to a Boolean */ |
| 10491 | switch (op) { |
| 10492 | case Py_EQ: |
| 10493 | v = TEST_COND(result == 0); |
| 10494 | break; |
| 10495 | case Py_NE: |
| 10496 | v = TEST_COND(result != 0); |
| 10497 | break; |
| 10498 | case Py_LE: |
| 10499 | v = TEST_COND(result <= 0); |
| 10500 | break; |
| 10501 | case Py_GE: |
| 10502 | v = TEST_COND(result >= 0); |
| 10503 | break; |
| 10504 | case Py_LT: |
| 10505 | v = TEST_COND(result == -1); |
| 10506 | break; |
| 10507 | case Py_GT: |
| 10508 | v = TEST_COND(result == 1); |
| 10509 | break; |
| 10510 | default: |
| 10511 | PyErr_BadArgument(); |
| 10512 | return NULL; |
| 10513 | } |
| 10514 | Py_INCREF(v); |
| 10515 | return v; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10516 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10517 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 10518 | Py_RETURN_NOTIMPLEMENTED; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10519 | } |
| 10520 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10521 | int |
| 10522 | PyUnicode_Contains(PyObject *container, PyObject *element) |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10523 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10524 | PyObject *str, *sub; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10525 | int kind1, kind2, kind; |
| 10526 | void *buf1, *buf2; |
| 10527 | Py_ssize_t len1, len2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10528 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10529 | |
| 10530 | /* Coerce the two arguments */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10531 | sub = PyUnicode_FromObject(element); |
| 10532 | if (!sub) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10533 | PyErr_Format(PyExc_TypeError, |
| 10534 | "'in <string>' requires string as left operand, not %s", |
| 10535 | element->ob_type->tp_name); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10536 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10537 | } |
| 10538 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10539 | str = PyUnicode_FromObject(container); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 10540 | if (!str) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10541 | Py_DECREF(sub); |
| 10542 | return -1; |
| 10543 | } |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 10544 | if (PyUnicode_READY(sub) == -1 || PyUnicode_READY(str) == -1) { |
| 10545 | Py_DECREF(sub); |
| 10546 | Py_DECREF(str); |
| 10547 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10548 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10549 | kind1 = PyUnicode_KIND(str); |
| 10550 | kind2 = PyUnicode_KIND(sub); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 10551 | kind = kind1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10552 | buf1 = PyUnicode_DATA(str); |
| 10553 | buf2 = PyUnicode_DATA(sub); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 10554 | if (kind2 != kind) { |
Antoine Pitrou | 758153b | 2012-05-12 15:51:51 +0200 | [diff] [blame] | 10555 | if (kind2 > kind) { |
| 10556 | Py_DECREF(sub); |
| 10557 | Py_DECREF(str); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 10558 | return 0; |
Antoine Pitrou | 758153b | 2012-05-12 15:51:51 +0200 | [diff] [blame] | 10559 | } |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10560 | buf2 = _PyUnicode_AsKind(sub, kind); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 10561 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10562 | if (!buf2) { |
| 10563 | Py_DECREF(sub); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 10564 | Py_DECREF(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10565 | return -1; |
| 10566 | } |
| 10567 | len1 = PyUnicode_GET_LENGTH(str); |
| 10568 | len2 = PyUnicode_GET_LENGTH(sub); |
| 10569 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10570 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10571 | case PyUnicode_1BYTE_KIND: |
| 10572 | result = ucs1lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10573 | break; |
| 10574 | case PyUnicode_2BYTE_KIND: |
| 10575 | result = ucs2lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10576 | break; |
| 10577 | case PyUnicode_4BYTE_KIND: |
| 10578 | result = ucs4lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10579 | break; |
| 10580 | default: |
| 10581 | result = -1; |
| 10582 | assert(0); |
| 10583 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10584 | |
| 10585 | Py_DECREF(str); |
| 10586 | Py_DECREF(sub); |
| 10587 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10588 | if (kind2 != kind) |
| 10589 | PyMem_Free(buf2); |
| 10590 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10591 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10592 | } |
| 10593 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10594 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 10595 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10596 | PyObject * |
| 10597 | PyUnicode_Concat(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10598 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10599 | PyObject *u = NULL, *v = NULL, *w; |
Victor Stinner | 127226b | 2011-10-13 01:12:34 +0200 | [diff] [blame] | 10600 | Py_UCS4 maxchar, maxchar2; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10601 | Py_ssize_t u_len, v_len, new_len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10602 | |
| 10603 | /* Coerce the two arguments */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10604 | u = PyUnicode_FromObject(left); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10605 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10606 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10607 | v = PyUnicode_FromObject(right); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10608 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10609 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10610 | |
| 10611 | /* Shortcuts */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 10612 | if (v == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10613 | Py_DECREF(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10614 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10615 | } |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 10616 | if (u == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10617 | Py_DECREF(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10618 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10619 | } |
| 10620 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10621 | u_len = PyUnicode_GET_LENGTH(u); |
| 10622 | v_len = PyUnicode_GET_LENGTH(v); |
| 10623 | if (u_len > PY_SSIZE_T_MAX - v_len) { |
| 10624 | PyErr_SetString(PyExc_OverflowError, |
| 10625 | "strings are too large to concat"); |
| 10626 | goto onError; |
| 10627 | } |
| 10628 | new_len = u_len + v_len; |
| 10629 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10630 | maxchar = PyUnicode_MAX_CHAR_VALUE(u); |
Victor Stinner | 127226b | 2011-10-13 01:12:34 +0200 | [diff] [blame] | 10631 | maxchar2 = PyUnicode_MAX_CHAR_VALUE(v); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 10632 | maxchar = MAX_MAXCHAR(maxchar, maxchar2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10633 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10634 | /* Concat the two Unicode strings */ |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10635 | w = PyUnicode_New(new_len, maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10636 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10637 | goto onError; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 10638 | _PyUnicode_FastCopyCharacters(w, 0, u, 0, u_len); |
| 10639 | _PyUnicode_FastCopyCharacters(w, u_len, v, 0, v_len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10640 | Py_DECREF(u); |
| 10641 | Py_DECREF(v); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10642 | assert(_PyUnicode_CheckConsistency(w, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10643 | return w; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10644 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10645 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10646 | Py_XDECREF(u); |
| 10647 | Py_XDECREF(v); |
| 10648 | return NULL; |
| 10649 | } |
| 10650 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10651 | void |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10652 | PyUnicode_Append(PyObject **p_left, PyObject *right) |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10653 | { |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10654 | PyObject *left, *res; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10655 | Py_UCS4 maxchar, maxchar2; |
| 10656 | Py_ssize_t left_len, right_len, new_len; |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10657 | |
| 10658 | if (p_left == NULL) { |
| 10659 | if (!PyErr_Occurred()) |
| 10660 | PyErr_BadInternalCall(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10661 | return; |
| 10662 | } |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10663 | left = *p_left; |
| 10664 | if (right == NULL || !PyUnicode_Check(left)) { |
| 10665 | if (!PyErr_Occurred()) |
| 10666 | PyErr_BadInternalCall(); |
| 10667 | goto error; |
| 10668 | } |
| 10669 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 10670 | if (PyUnicode_READY(left) == -1) |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 10671 | goto error; |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 10672 | if (PyUnicode_READY(right) == -1) |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 10673 | goto error; |
| 10674 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10675 | /* Shortcuts */ |
| 10676 | if (left == unicode_empty) { |
| 10677 | Py_DECREF(left); |
| 10678 | Py_INCREF(right); |
| 10679 | *p_left = right; |
| 10680 | return; |
| 10681 | } |
| 10682 | if (right == unicode_empty) |
| 10683 | return; |
| 10684 | |
| 10685 | left_len = PyUnicode_GET_LENGTH(left); |
| 10686 | right_len = PyUnicode_GET_LENGTH(right); |
| 10687 | if (left_len > PY_SSIZE_T_MAX - right_len) { |
| 10688 | PyErr_SetString(PyExc_OverflowError, |
| 10689 | "strings are too large to concat"); |
| 10690 | goto error; |
| 10691 | } |
| 10692 | new_len = left_len + right_len; |
| 10693 | |
| 10694 | if (unicode_modifiable(left) |
| 10695 | && PyUnicode_CheckExact(right) |
| 10696 | && PyUnicode_KIND(right) <= PyUnicode_KIND(left) |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10697 | /* Don't resize for ascii += latin1. Convert ascii to latin1 requires |
| 10698 | to change the structure size, but characters are stored just after |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 10699 | the structure, and so it requires to move all characters which is |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10700 | not so different than duplicating the string. */ |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10701 | && !(PyUnicode_IS_ASCII(left) && !PyUnicode_IS_ASCII(right))) |
| 10702 | { |
| 10703 | /* append inplace */ |
| 10704 | if (unicode_resize(p_left, new_len) != 0) { |
| 10705 | /* XXX if _PyUnicode_Resize() fails, 'left' has been |
| 10706 | * deallocated so it cannot be put back into |
| 10707 | * 'variable'. The MemoryError is raised when there |
| 10708 | * is no value in 'variable', which might (very |
| 10709 | * remotely) be a cause of incompatibilities. |
| 10710 | */ |
| 10711 | goto error; |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10712 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10713 | /* copy 'right' into the newly allocated area of 'left' */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 10714 | _PyUnicode_FastCopyCharacters(*p_left, left_len, right, 0, right_len); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10715 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10716 | else { |
| 10717 | maxchar = PyUnicode_MAX_CHAR_VALUE(left); |
| 10718 | maxchar2 = PyUnicode_MAX_CHAR_VALUE(right); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 10719 | maxchar = MAX_MAXCHAR(maxchar, maxchar2); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10720 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10721 | /* Concat the two Unicode strings */ |
| 10722 | res = PyUnicode_New(new_len, maxchar); |
| 10723 | if (res == NULL) |
| 10724 | goto error; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 10725 | _PyUnicode_FastCopyCharacters(res, 0, left, 0, left_len); |
| 10726 | _PyUnicode_FastCopyCharacters(res, left_len, right, 0, right_len); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10727 | Py_DECREF(left); |
| 10728 | *p_left = res; |
| 10729 | } |
| 10730 | assert(_PyUnicode_CheckConsistency(*p_left, 1)); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10731 | return; |
| 10732 | |
| 10733 | error: |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10734 | Py_CLEAR(*p_left); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10735 | } |
| 10736 | |
| 10737 | void |
| 10738 | PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right) |
| 10739 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10740 | PyUnicode_Append(pleft, right); |
| 10741 | Py_XDECREF(right); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10742 | } |
| 10743 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10744 | PyDoc_STRVAR(count__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10745 | "S.count(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10746 | \n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10747 | Return the number of non-overlapping occurrences of substring sub in\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10748 | 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] | 10749 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10750 | |
| 10751 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10752 | unicode_count(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10753 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10754 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10755 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10756 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10757 | PyObject *result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10758 | int kind1, kind2, kind; |
| 10759 | void *buf1, *buf2; |
| 10760 | Py_ssize_t len1, len2, iresult; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10761 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10762 | if (!stringlib_parse_args_finds_unicode("count", args, &substring, |
| 10763 | &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10764 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10765 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10766 | kind1 = PyUnicode_KIND(self); |
| 10767 | kind2 = PyUnicode_KIND(substring); |
Benjamin Peterson | b63f49f | 2012-05-03 18:31:07 -0400 | [diff] [blame] | 10768 | if (kind2 > kind1) |
| 10769 | return PyLong_FromLong(0); |
| 10770 | kind = kind1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10771 | buf1 = PyUnicode_DATA(self); |
| 10772 | buf2 = PyUnicode_DATA(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10773 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10774 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10775 | if (!buf2) { |
| 10776 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10777 | return NULL; |
| 10778 | } |
| 10779 | len1 = PyUnicode_GET_LENGTH(self); |
| 10780 | len2 = PyUnicode_GET_LENGTH(substring); |
| 10781 | |
| 10782 | ADJUST_INDICES(start, end, len1); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10783 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10784 | case PyUnicode_1BYTE_KIND: |
| 10785 | iresult = ucs1lib_count( |
| 10786 | ((Py_UCS1*)buf1) + start, end - start, |
| 10787 | buf2, len2, PY_SSIZE_T_MAX |
| 10788 | ); |
| 10789 | break; |
| 10790 | case PyUnicode_2BYTE_KIND: |
| 10791 | iresult = ucs2lib_count( |
| 10792 | ((Py_UCS2*)buf1) + start, end - start, |
| 10793 | buf2, len2, PY_SSIZE_T_MAX |
| 10794 | ); |
| 10795 | break; |
| 10796 | case PyUnicode_4BYTE_KIND: |
| 10797 | iresult = ucs4lib_count( |
| 10798 | ((Py_UCS4*)buf1) + start, end - start, |
| 10799 | buf2, len2, PY_SSIZE_T_MAX |
| 10800 | ); |
| 10801 | break; |
| 10802 | default: |
| 10803 | assert(0); iresult = 0; |
| 10804 | } |
| 10805 | |
| 10806 | result = PyLong_FromSsize_t(iresult); |
| 10807 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10808 | if (kind2 != kind) |
| 10809 | PyMem_Free(buf2); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10810 | |
| 10811 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10812 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10813 | return result; |
| 10814 | } |
| 10815 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10816 | PyDoc_STRVAR(encode__doc__, |
Victor Stinner | c911bbf | 2010-11-07 19:04:46 +0000 | [diff] [blame] | 10817 | "S.encode(encoding='utf-8', errors='strict') -> bytes\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10818 | \n\ |
Victor Stinner | e14e212 | 2010-11-07 18:41:46 +0000 | [diff] [blame] | 10819 | Encode S using the codec registered for encoding. Default encoding\n\ |
| 10820 | 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] | 10821 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 10822 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 10823 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 10824 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10825 | |
| 10826 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10827 | unicode_encode(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10828 | { |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 10829 | static char *kwlist[] = {"encoding", "errors", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10830 | char *encoding = NULL; |
| 10831 | char *errors = NULL; |
Guido van Rossum | 35d9428 | 2007-08-27 18:20:11 +0000 | [diff] [blame] | 10832 | |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 10833 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:encode", |
| 10834 | kwlist, &encoding, &errors)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10835 | return NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10836 | return PyUnicode_AsEncodedString(self, encoding, errors); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 10837 | } |
| 10838 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10839 | PyDoc_STRVAR(expandtabs__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10840 | "S.expandtabs([tabsize]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10841 | \n\ |
| 10842 | 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] | 10843 | 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] | 10844 | |
| 10845 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10846 | unicode_expandtabs(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10847 | { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10848 | Py_ssize_t i, j, line_pos, src_len, incr; |
| 10849 | Py_UCS4 ch; |
| 10850 | PyObject *u; |
| 10851 | void *src_data, *dest_data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10852 | int tabsize = 8; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10853 | int kind; |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10854 | int found; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10855 | |
| 10856 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10857 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10858 | |
Antoine Pitrou | 2242522 | 2011-10-04 19:10:51 +0200 | [diff] [blame] | 10859 | if (PyUnicode_READY(self) == -1) |
| 10860 | return NULL; |
| 10861 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 10862 | /* First pass: determine size of output string */ |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10863 | src_len = PyUnicode_GET_LENGTH(self); |
| 10864 | i = j = line_pos = 0; |
| 10865 | kind = PyUnicode_KIND(self); |
| 10866 | src_data = PyUnicode_DATA(self); |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10867 | found = 0; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10868 | for (; i < src_len; i++) { |
| 10869 | ch = PyUnicode_READ(kind, src_data, i); |
| 10870 | if (ch == '\t') { |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10871 | found = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10872 | if (tabsize > 0) { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10873 | incr = tabsize - (line_pos % tabsize); /* cannot overflow */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10874 | if (j > PY_SSIZE_T_MAX - incr) |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10875 | goto overflow; |
| 10876 | line_pos += incr; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10877 | j += incr; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10878 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10879 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10880 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10881 | if (j > PY_SSIZE_T_MAX - 1) |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10882 | goto overflow; |
| 10883 | line_pos++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10884 | j++; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10885 | if (ch == '\n' || ch == '\r') |
| 10886 | line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10887 | } |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10888 | } |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10889 | if (!found) |
| 10890 | return unicode_result_unchanged(self); |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 10891 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10892 | /* Second pass: create output string and fill it */ |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10893 | u = PyUnicode_New(j, PyUnicode_MAX_CHAR_VALUE(self)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10894 | if (!u) |
| 10895 | return NULL; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10896 | dest_data = PyUnicode_DATA(u); |
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 | i = j = line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10899 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10900 | for (; i < src_len; i++) { |
| 10901 | ch = PyUnicode_READ(kind, src_data, i); |
| 10902 | if (ch == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10903 | if (tabsize > 0) { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10904 | incr = tabsize - (line_pos % tabsize); |
| 10905 | line_pos += incr; |
Victor Stinner | da79e63 | 2012-02-22 13:37:04 +0100 | [diff] [blame] | 10906 | FILL(kind, dest_data, ' ', j, incr); |
| 10907 | j += incr; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10908 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10909 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10910 | else { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10911 | line_pos++; |
| 10912 | PyUnicode_WRITE(kind, dest_data, j, ch); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10913 | j++; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10914 | if (ch == '\n' || ch == '\r') |
| 10915 | line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10916 | } |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10917 | } |
| 10918 | assert (j == PyUnicode_GET_LENGTH(u)); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 10919 | return unicode_result(u); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10920 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10921 | overflow: |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10922 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 10923 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10924 | } |
| 10925 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10926 | PyDoc_STRVAR(find__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10927 | "S.find(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10928 | \n\ |
| 10929 | Return the lowest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 10930 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10931 | arguments start and end are interpreted as in slice notation.\n\ |
| 10932 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10933 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10934 | |
| 10935 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10936 | unicode_find(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10937 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10938 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 10939 | Py_ssize_t start; |
| 10940 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10941 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10942 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10943 | if (!stringlib_parse_args_finds_unicode("find", args, &substring, |
| 10944 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10945 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10946 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10947 | if (PyUnicode_READY(self) == -1) |
| 10948 | return NULL; |
| 10949 | if (PyUnicode_READY(substring) == -1) |
| 10950 | return NULL; |
| 10951 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10952 | result = any_find_slice(1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10953 | |
| 10954 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10955 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10956 | if (result == -2) |
| 10957 | return NULL; |
| 10958 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 10959 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10960 | } |
| 10961 | |
| 10962 | static PyObject * |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 10963 | unicode_getitem(PyObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10964 | { |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 10965 | void *data; |
| 10966 | enum PyUnicode_Kind kind; |
| 10967 | Py_UCS4 ch; |
| 10968 | PyObject *res; |
| 10969 | |
| 10970 | if (!PyUnicode_Check(self) || PyUnicode_READY(self) == -1) { |
| 10971 | PyErr_BadArgument(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10972 | return NULL; |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 10973 | } |
| 10974 | if (index < 0 || index >= PyUnicode_GET_LENGTH(self)) { |
| 10975 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 10976 | return NULL; |
| 10977 | } |
| 10978 | kind = PyUnicode_KIND(self); |
| 10979 | data = PyUnicode_DATA(self); |
| 10980 | ch = PyUnicode_READ(kind, data, index); |
| 10981 | if (ch < 256) |
| 10982 | return get_latin1_char(ch); |
| 10983 | |
| 10984 | res = PyUnicode_New(1, ch); |
| 10985 | if (res == NULL) |
| 10986 | return NULL; |
| 10987 | kind = PyUnicode_KIND(res); |
| 10988 | data = PyUnicode_DATA(res); |
| 10989 | PyUnicode_WRITE(kind, data, 0, ch); |
| 10990 | assert(_PyUnicode_CheckConsistency(res, 1)); |
| 10991 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10992 | } |
| 10993 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 10994 | /* Believe it or not, this produces the same value for ASCII strings |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 10995 | as bytes_hash(). */ |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 10996 | static Py_hash_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10997 | unicode_hash(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10998 | { |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 10999 | Py_ssize_t len; |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 11000 | Py_uhash_t x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11001 | |
Benjamin Peterson | f6622c8 | 2012-04-09 14:53:07 -0400 | [diff] [blame] | 11002 | #ifdef Py_DEBUG |
Benjamin Peterson | 69e9727 | 2012-02-21 11:08:50 -0500 | [diff] [blame] | 11003 | assert(_Py_HashSecret_Initialized); |
Benjamin Peterson | f6622c8 | 2012-04-09 14:53:07 -0400 | [diff] [blame] | 11004 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11005 | if (_PyUnicode_HASH(self) != -1) |
| 11006 | return _PyUnicode_HASH(self); |
| 11007 | if (PyUnicode_READY(self) == -1) |
| 11008 | return -1; |
| 11009 | len = PyUnicode_GET_LENGTH(self); |
Georg Brandl | 16fa2a1 | 2012-02-21 00:50:13 +0100 | [diff] [blame] | 11010 | /* |
| 11011 | We make the hash of the empty string be 0, rather than using |
| 11012 | (prefix ^ suffix), since this slightly obfuscates the hash secret |
| 11013 | */ |
| 11014 | if (len == 0) { |
| 11015 | _PyUnicode_HASH(self) = 0; |
| 11016 | return 0; |
| 11017 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11018 | |
| 11019 | /* The hash function as a macro, gets expanded three times below. */ |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 11020 | #define HASH(P) \ |
| 11021 | x ^= (Py_uhash_t) *P << 7; \ |
| 11022 | while (--len >= 0) \ |
| 11023 | x = (_PyHASH_MULTIPLIER * x) ^ (Py_uhash_t) *P++; \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11024 | |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 11025 | x = (Py_uhash_t) _Py_HashSecret.prefix; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11026 | switch (PyUnicode_KIND(self)) { |
| 11027 | case PyUnicode_1BYTE_KIND: { |
| 11028 | const unsigned char *c = PyUnicode_1BYTE_DATA(self); |
| 11029 | HASH(c); |
| 11030 | break; |
| 11031 | } |
| 11032 | case PyUnicode_2BYTE_KIND: { |
| 11033 | const Py_UCS2 *s = PyUnicode_2BYTE_DATA(self); |
| 11034 | HASH(s); |
| 11035 | break; |
| 11036 | } |
| 11037 | default: { |
| 11038 | Py_UCS4 *l; |
| 11039 | assert(PyUnicode_KIND(self) == PyUnicode_4BYTE_KIND && |
| 11040 | "Impossible switch case in unicode_hash"); |
| 11041 | l = PyUnicode_4BYTE_DATA(self); |
| 11042 | HASH(l); |
| 11043 | break; |
| 11044 | } |
| 11045 | } |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 11046 | x ^= (Py_uhash_t) PyUnicode_GET_LENGTH(self); |
| 11047 | x ^= (Py_uhash_t) _Py_HashSecret.suffix; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11048 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11049 | if (x == -1) |
| 11050 | x = -2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11051 | _PyUnicode_HASH(self) = x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11052 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11053 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11054 | #undef HASH |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11055 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11056 | PyDoc_STRVAR(index__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11057 | "S.index(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11058 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11059 | 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] | 11060 | |
| 11061 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11062 | unicode_index(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11063 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11064 | Py_ssize_t result; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11065 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 11066 | Py_ssize_t start; |
| 11067 | Py_ssize_t end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11068 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11069 | if (!stringlib_parse_args_finds_unicode("index", args, &substring, |
| 11070 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11071 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11072 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11073 | if (PyUnicode_READY(self) == -1) |
| 11074 | return NULL; |
| 11075 | if (PyUnicode_READY(substring) == -1) |
| 11076 | return NULL; |
| 11077 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11078 | result = any_find_slice(1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11079 | |
| 11080 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11081 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11082 | if (result == -2) |
| 11083 | return NULL; |
| 11084 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11085 | if (result < 0) { |
| 11086 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 11087 | return NULL; |
| 11088 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11089 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11090 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11091 | } |
| 11092 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11093 | PyDoc_STRVAR(islower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11094 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11095 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11096 | 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] | 11097 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11098 | |
| 11099 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11100 | unicode_islower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11101 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11102 | Py_ssize_t i, length; |
| 11103 | int kind; |
| 11104 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11105 | int cased; |
| 11106 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11107 | if (PyUnicode_READY(self) == -1) |
| 11108 | return NULL; |
| 11109 | length = PyUnicode_GET_LENGTH(self); |
| 11110 | kind = PyUnicode_KIND(self); |
| 11111 | data = PyUnicode_DATA(self); |
| 11112 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11113 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11114 | if (length == 1) |
| 11115 | return PyBool_FromLong( |
| 11116 | Py_UNICODE_ISLOWER(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11117 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11118 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11119 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11120 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11121 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11122 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11123 | for (i = 0; i < length; i++) { |
| 11124 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11125 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11126 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 11127 | return PyBool_FromLong(0); |
| 11128 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 11129 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11130 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11131 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11132 | } |
| 11133 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11134 | PyDoc_STRVAR(isupper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11135 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11136 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11137 | 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] | 11138 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11139 | |
| 11140 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11141 | unicode_isupper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11142 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11143 | Py_ssize_t i, length; |
| 11144 | int kind; |
| 11145 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11146 | int cased; |
| 11147 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11148 | if (PyUnicode_READY(self) == -1) |
| 11149 | return NULL; |
| 11150 | length = PyUnicode_GET_LENGTH(self); |
| 11151 | kind = PyUnicode_KIND(self); |
| 11152 | data = PyUnicode_DATA(self); |
| 11153 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11154 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11155 | if (length == 1) |
| 11156 | return PyBool_FromLong( |
| 11157 | Py_UNICODE_ISUPPER(PyUnicode_READ(kind, data, 0)) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11158 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11159 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11160 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11161 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11162 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11163 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11164 | for (i = 0; i < length; i++) { |
| 11165 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11166 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11167 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 11168 | return PyBool_FromLong(0); |
| 11169 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 11170 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11171 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11172 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11173 | } |
| 11174 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11175 | PyDoc_STRVAR(istitle__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11176 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11177 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11178 | Return True if S is a titlecased string and there is at least one\n\ |
| 11179 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 11180 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 11181 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11182 | |
| 11183 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11184 | unicode_istitle(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11185 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11186 | Py_ssize_t i, length; |
| 11187 | int kind; |
| 11188 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11189 | int cased, previous_is_cased; |
| 11190 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11191 | if (PyUnicode_READY(self) == -1) |
| 11192 | return NULL; |
| 11193 | length = PyUnicode_GET_LENGTH(self); |
| 11194 | kind = PyUnicode_KIND(self); |
| 11195 | data = PyUnicode_DATA(self); |
| 11196 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11197 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11198 | if (length == 1) { |
| 11199 | Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11200 | return PyBool_FromLong((Py_UNICODE_ISTITLE(ch) != 0) || |
| 11201 | (Py_UNICODE_ISUPPER(ch) != 0)); |
| 11202 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11203 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11204 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11205 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11206 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11207 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11208 | cased = 0; |
| 11209 | previous_is_cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11210 | for (i = 0; i < length; i++) { |
| 11211 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11212 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11213 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 11214 | if (previous_is_cased) |
| 11215 | return PyBool_FromLong(0); |
| 11216 | previous_is_cased = 1; |
| 11217 | cased = 1; |
| 11218 | } |
| 11219 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 11220 | if (!previous_is_cased) |
| 11221 | return PyBool_FromLong(0); |
| 11222 | previous_is_cased = 1; |
| 11223 | cased = 1; |
| 11224 | } |
| 11225 | else |
| 11226 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11227 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11228 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11229 | } |
| 11230 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11231 | PyDoc_STRVAR(isspace__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11232 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11233 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11234 | Return True if all characters in S are whitespace\n\ |
| 11235 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11236 | |
| 11237 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11238 | unicode_isspace(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11239 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11240 | Py_ssize_t i, length; |
| 11241 | int kind; |
| 11242 | void *data; |
| 11243 | |
| 11244 | if (PyUnicode_READY(self) == -1) |
| 11245 | return NULL; |
| 11246 | length = PyUnicode_GET_LENGTH(self); |
| 11247 | kind = PyUnicode_KIND(self); |
| 11248 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11249 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11250 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11251 | if (length == 1) |
| 11252 | return PyBool_FromLong( |
| 11253 | Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11254 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11255 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11256 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11257 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11258 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11259 | for (i = 0; i < length; i++) { |
| 11260 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11261 | if (!Py_UNICODE_ISSPACE(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11262 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11263 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11264 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11265 | } |
| 11266 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11267 | PyDoc_STRVAR(isalpha__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11268 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11269 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11270 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11271 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11272 | |
| 11273 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11274 | unicode_isalpha(PyObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11275 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11276 | Py_ssize_t i, length; |
| 11277 | int kind; |
| 11278 | void *data; |
| 11279 | |
| 11280 | if (PyUnicode_READY(self) == -1) |
| 11281 | return NULL; |
| 11282 | length = PyUnicode_GET_LENGTH(self); |
| 11283 | kind = PyUnicode_KIND(self); |
| 11284 | data = PyUnicode_DATA(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11285 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11286 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11287 | if (length == 1) |
| 11288 | return PyBool_FromLong( |
| 11289 | Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, 0))); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11290 | |
| 11291 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11292 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11293 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11294 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11295 | for (i = 0; i < length; i++) { |
| 11296 | if (!Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11297 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11298 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11299 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11300 | } |
| 11301 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11302 | PyDoc_STRVAR(isalnum__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11303 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11304 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11305 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11306 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11307 | |
| 11308 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11309 | unicode_isalnum(PyObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11310 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11311 | int kind; |
| 11312 | void *data; |
| 11313 | Py_ssize_t len, i; |
| 11314 | |
| 11315 | if (PyUnicode_READY(self) == -1) |
| 11316 | return NULL; |
| 11317 | |
| 11318 | kind = PyUnicode_KIND(self); |
| 11319 | data = PyUnicode_DATA(self); |
| 11320 | len = PyUnicode_GET_LENGTH(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11321 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11322 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11323 | if (len == 1) { |
| 11324 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11325 | return PyBool_FromLong(Py_UNICODE_ISALNUM(ch)); |
| 11326 | } |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11327 | |
| 11328 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11329 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11330 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11331 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11332 | for (i = 0; i < len; i++) { |
| 11333 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11334 | if (!Py_UNICODE_ISALNUM(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11335 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11336 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11337 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11338 | } |
| 11339 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11340 | PyDoc_STRVAR(isdecimal__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11341 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11342 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11343 | 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] | 11344 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11345 | |
| 11346 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11347 | unicode_isdecimal(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11348 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11349 | Py_ssize_t i, length; |
| 11350 | int kind; |
| 11351 | void *data; |
| 11352 | |
| 11353 | if (PyUnicode_READY(self) == -1) |
| 11354 | return NULL; |
| 11355 | length = PyUnicode_GET_LENGTH(self); |
| 11356 | kind = PyUnicode_KIND(self); |
| 11357 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11358 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11359 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11360 | if (length == 1) |
| 11361 | return PyBool_FromLong( |
| 11362 | Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11363 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11364 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11365 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11366 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11367 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11368 | for (i = 0; i < length; i++) { |
| 11369 | if (!Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11370 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11371 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11372 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11373 | } |
| 11374 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11375 | PyDoc_STRVAR(isdigit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11376 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11377 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11378 | Return True if all characters in S are digits\n\ |
| 11379 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11380 | |
| 11381 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11382 | unicode_isdigit(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11383 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11384 | Py_ssize_t i, length; |
| 11385 | int kind; |
| 11386 | void *data; |
| 11387 | |
| 11388 | if (PyUnicode_READY(self) == -1) |
| 11389 | return NULL; |
| 11390 | length = PyUnicode_GET_LENGTH(self); |
| 11391 | kind = PyUnicode_KIND(self); |
| 11392 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11393 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11394 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11395 | if (length == 1) { |
| 11396 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11397 | return PyBool_FromLong(Py_UNICODE_ISDIGIT(ch)); |
| 11398 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11399 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11400 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11401 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11402 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11403 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11404 | for (i = 0; i < length; i++) { |
| 11405 | if (!Py_UNICODE_ISDIGIT(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11406 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11407 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11408 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11409 | } |
| 11410 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11411 | PyDoc_STRVAR(isnumeric__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11412 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11413 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11414 | 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] | 11415 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11416 | |
| 11417 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11418 | unicode_isnumeric(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11419 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11420 | Py_ssize_t i, length; |
| 11421 | int kind; |
| 11422 | void *data; |
| 11423 | |
| 11424 | if (PyUnicode_READY(self) == -1) |
| 11425 | return NULL; |
| 11426 | length = PyUnicode_GET_LENGTH(self); |
| 11427 | kind = PyUnicode_KIND(self); |
| 11428 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11429 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11430 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11431 | if (length == 1) |
| 11432 | return PyBool_FromLong( |
| 11433 | Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11434 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11435 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11436 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11437 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11438 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11439 | for (i = 0; i < length; i++) { |
| 11440 | if (!Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11441 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11442 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11443 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11444 | } |
| 11445 | |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11446 | int |
| 11447 | PyUnicode_IsIdentifier(PyObject *self) |
| 11448 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11449 | int kind; |
| 11450 | void *data; |
| 11451 | Py_ssize_t i; |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11452 | Py_UCS4 first; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11453 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11454 | if (PyUnicode_READY(self) == -1) { |
| 11455 | Py_FatalError("identifier not ready"); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11456 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11457 | } |
| 11458 | |
| 11459 | /* Special case for empty strings */ |
| 11460 | if (PyUnicode_GET_LENGTH(self) == 0) |
| 11461 | return 0; |
| 11462 | kind = PyUnicode_KIND(self); |
| 11463 | data = PyUnicode_DATA(self); |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11464 | |
| 11465 | /* PEP 3131 says that the first character must be in |
| 11466 | XID_Start and subsequent characters in XID_Continue, |
| 11467 | and for the ASCII range, the 2.x rules apply (i.e |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11468 | start with letters and underscore, continue with |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11469 | letters, digits, underscore). However, given the current |
| 11470 | definition of XID_Start and XID_Continue, it is sufficient |
| 11471 | to check just for these, except that _ must be allowed |
| 11472 | as starting an identifier. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11473 | first = PyUnicode_READ(kind, data, 0); |
Benjamin Peterson | f413b80 | 2011-08-12 22:17:18 -0500 | [diff] [blame] | 11474 | if (!_PyUnicode_IsXidStart(first) && first != 0x5F /* LOW LINE */) |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11475 | return 0; |
| 11476 | |
Benjamin Peterson | 9c6e6a0 | 2011-09-28 08:09:05 -0400 | [diff] [blame] | 11477 | for (i = 1; i < PyUnicode_GET_LENGTH(self); i++) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11478 | if (!_PyUnicode_IsXidContinue(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11479 | return 0; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11480 | return 1; |
| 11481 | } |
| 11482 | |
| 11483 | PyDoc_STRVAR(isidentifier__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11484 | "S.isidentifier() -> bool\n\ |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11485 | \n\ |
| 11486 | Return True if S is a valid identifier according\n\ |
| 11487 | to the language definition."); |
| 11488 | |
| 11489 | static PyObject* |
| 11490 | unicode_isidentifier(PyObject *self) |
| 11491 | { |
| 11492 | return PyBool_FromLong(PyUnicode_IsIdentifier(self)); |
| 11493 | } |
| 11494 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11495 | PyDoc_STRVAR(isprintable__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11496 | "S.isprintable() -> bool\n\ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11497 | \n\ |
| 11498 | Return True if all characters in S are considered\n\ |
| 11499 | printable in repr() or S is empty, False otherwise."); |
| 11500 | |
| 11501 | static PyObject* |
| 11502 | unicode_isprintable(PyObject *self) |
| 11503 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11504 | Py_ssize_t i, length; |
| 11505 | int kind; |
| 11506 | void *data; |
| 11507 | |
| 11508 | if (PyUnicode_READY(self) == -1) |
| 11509 | return NULL; |
| 11510 | length = PyUnicode_GET_LENGTH(self); |
| 11511 | kind = PyUnicode_KIND(self); |
| 11512 | data = PyUnicode_DATA(self); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11513 | |
| 11514 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11515 | if (length == 1) |
| 11516 | return PyBool_FromLong( |
| 11517 | Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, 0))); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11518 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11519 | for (i = 0; i < length; i++) { |
| 11520 | if (!Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, i))) { |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11521 | Py_RETURN_FALSE; |
| 11522 | } |
| 11523 | } |
| 11524 | Py_RETURN_TRUE; |
| 11525 | } |
| 11526 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11527 | PyDoc_STRVAR(join__doc__, |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 11528 | "S.join(iterable) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11529 | \n\ |
| 11530 | 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] | 11531 | iterable. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11532 | |
| 11533 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11534 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11535 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11536 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11537 | } |
| 11538 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11539 | static Py_ssize_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11540 | unicode_length(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11541 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11542 | if (PyUnicode_READY(self) == -1) |
| 11543 | return -1; |
| 11544 | return PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11545 | } |
| 11546 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11547 | PyDoc_STRVAR(ljust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11548 | "S.ljust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11549 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 11550 | 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] | 11551 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11552 | |
| 11553 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11554 | unicode_ljust(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11555 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11556 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11557 | Py_UCS4 fillchar = ' '; |
| 11558 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11559 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11560 | return NULL; |
| 11561 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 11562 | if (PyUnicode_READY(self) == -1) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11563 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11564 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11565 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 11566 | return unicode_result_unchanged(self); |
| 11567 | |
| 11568 | return pad(self, 0, width - PyUnicode_GET_LENGTH(self), fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11569 | } |
| 11570 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11571 | PyDoc_STRVAR(lower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11572 | "S.lower() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11573 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11574 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11575 | |
| 11576 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11577 | unicode_lower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11578 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 11579 | if (PyUnicode_READY(self) == -1) |
| 11580 | return NULL; |
| 11581 | if (PyUnicode_IS_ASCII(self)) |
| 11582 | return ascii_upper_or_lower(self, 1); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 11583 | return case_operation(self, do_lower); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11584 | } |
| 11585 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11586 | #define LEFTSTRIP 0 |
| 11587 | #define RIGHTSTRIP 1 |
| 11588 | #define BOTHSTRIP 2 |
| 11589 | |
| 11590 | /* Arrays indexed by above */ |
| 11591 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 11592 | |
| 11593 | #define STRIPNAME(i) (stripformat[i]+3) |
| 11594 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11595 | /* externally visible for str.strip(unicode) */ |
| 11596 | PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11597 | _PyUnicode_XStrip(PyObject *self, int striptype, PyObject *sepobj) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11598 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11599 | void *data; |
| 11600 | int kind; |
| 11601 | Py_ssize_t i, j, len; |
| 11602 | BLOOM_MASK sepmask; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11603 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11604 | if (PyUnicode_READY(self) == -1 || PyUnicode_READY(sepobj) == -1) |
| 11605 | return NULL; |
| 11606 | |
| 11607 | kind = PyUnicode_KIND(self); |
| 11608 | data = PyUnicode_DATA(self); |
| 11609 | len = PyUnicode_GET_LENGTH(self); |
| 11610 | sepmask = make_bloom_mask(PyUnicode_KIND(sepobj), |
| 11611 | PyUnicode_DATA(sepobj), |
| 11612 | PyUnicode_GET_LENGTH(sepobj)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11613 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11614 | i = 0; |
| 11615 | if (striptype != RIGHTSTRIP) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11616 | while (i < len && |
| 11617 | BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, i), sepobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11618 | i++; |
| 11619 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11620 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11621 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11622 | j = len; |
| 11623 | if (striptype != LEFTSTRIP) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11624 | do { |
| 11625 | j--; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11626 | } while (j >= i && |
| 11627 | BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, j), sepobj)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11628 | j++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11629 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11630 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11631 | return PyUnicode_Substring(self, i, j); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11632 | } |
| 11633 | |
| 11634 | PyObject* |
| 11635 | PyUnicode_Substring(PyObject *self, Py_ssize_t start, Py_ssize_t end) |
| 11636 | { |
| 11637 | unsigned char *data; |
| 11638 | int kind; |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11639 | Py_ssize_t length; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11640 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 11641 | if (PyUnicode_READY(self) == -1) |
| 11642 | return NULL; |
| 11643 | |
Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 11644 | length = PyUnicode_GET_LENGTH(self); |
| 11645 | end = Py_MIN(end, length); |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 11646 | |
Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 11647 | if (start == 0 && end == length) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11648 | return unicode_result_unchanged(self); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11649 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 11650 | if (start < 0 || end < 0) { |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11651 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 11652 | return NULL; |
| 11653 | } |
Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 11654 | if (start >= length || end < start) { |
Victor Stinner | 3a7f7977 | 2012-05-03 03:36:40 +0200 | [diff] [blame] | 11655 | Py_INCREF(unicode_empty); |
| 11656 | return unicode_empty; |
Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 11657 | } |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11658 | |
Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 11659 | length = end - start; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11660 | if (PyUnicode_IS_ASCII(self)) { |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11661 | data = PyUnicode_1BYTE_DATA(self); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 11662 | return _PyUnicode_FromASCII((char*)(data + start), length); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11663 | } |
| 11664 | else { |
| 11665 | kind = PyUnicode_KIND(self); |
| 11666 | data = PyUnicode_1BYTE_DATA(self); |
| 11667 | return PyUnicode_FromKindAndData(kind, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 11668 | data + kind * start, |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11669 | length); |
| 11670 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11671 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11672 | |
| 11673 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11674 | do_strip(PyObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11675 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11676 | int kind; |
| 11677 | void *data; |
| 11678 | Py_ssize_t len, i, j; |
| 11679 | |
| 11680 | if (PyUnicode_READY(self) == -1) |
| 11681 | return NULL; |
| 11682 | |
| 11683 | kind = PyUnicode_KIND(self); |
| 11684 | data = PyUnicode_DATA(self); |
| 11685 | len = PyUnicode_GET_LENGTH(self); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11686 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11687 | i = 0; |
| 11688 | if (striptype != RIGHTSTRIP) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11689 | while (i < len && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, i))) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11690 | i++; |
| 11691 | } |
| 11692 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11693 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11694 | j = len; |
| 11695 | if (striptype != LEFTSTRIP) { |
| 11696 | do { |
| 11697 | j--; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11698 | } while (j >= i && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, j))); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11699 | j++; |
| 11700 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11701 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11702 | return PyUnicode_Substring(self, i, j); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11703 | } |
| 11704 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11705 | |
| 11706 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11707 | do_argstrip(PyObject *self, int striptype, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11708 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11709 | PyObject *sep = NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11710 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11711 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
| 11712 | return NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11713 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11714 | if (sep != NULL && sep != Py_None) { |
| 11715 | if (PyUnicode_Check(sep)) |
| 11716 | return _PyUnicode_XStrip(self, striptype, sep); |
| 11717 | else { |
| 11718 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11719 | "%s arg must be None or str", |
| 11720 | STRIPNAME(striptype)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11721 | return NULL; |
| 11722 | } |
| 11723 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11724 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11725 | return do_strip(self, striptype); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11726 | } |
| 11727 | |
| 11728 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11729 | PyDoc_STRVAR(strip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11730 | "S.strip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11731 | \n\ |
| 11732 | Return a copy of the string S with leading and trailing\n\ |
| 11733 | whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11734 | 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] | 11735 | |
| 11736 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11737 | unicode_strip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11738 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11739 | if (PyTuple_GET_SIZE(args) == 0) |
| 11740 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 11741 | else |
| 11742 | return do_argstrip(self, BOTHSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11743 | } |
| 11744 | |
| 11745 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11746 | PyDoc_STRVAR(lstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11747 | "S.lstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11748 | \n\ |
| 11749 | Return a copy of the string S with leading whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11750 | 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] | 11751 | |
| 11752 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11753 | unicode_lstrip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11754 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11755 | if (PyTuple_GET_SIZE(args) == 0) |
| 11756 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 11757 | else |
| 11758 | return do_argstrip(self, LEFTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11759 | } |
| 11760 | |
| 11761 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11762 | PyDoc_STRVAR(rstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11763 | "S.rstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11764 | \n\ |
| 11765 | Return a copy of the string S with trailing whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11766 | 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] | 11767 | |
| 11768 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11769 | unicode_rstrip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11770 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11771 | if (PyTuple_GET_SIZE(args) == 0) |
| 11772 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 11773 | else |
| 11774 | return do_argstrip(self, RIGHTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11775 | } |
| 11776 | |
| 11777 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11778 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11779 | unicode_repeat(PyObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11780 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11781 | PyObject *u; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11782 | Py_ssize_t nchars, n; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11783 | |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 11784 | if (len < 1) { |
| 11785 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 11786 | return unicode_empty; |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 11787 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11788 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11789 | /* no repeat, return original string */ |
| 11790 | if (len == 1) |
| 11791 | return unicode_result_unchanged(str); |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 11792 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 11793 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11794 | return NULL; |
| 11795 | |
Victor Stinner | c759f3e | 2011-10-01 03:09:58 +0200 | [diff] [blame] | 11796 | if (PyUnicode_GET_LENGTH(str) > PY_SSIZE_T_MAX / len) { |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11797 | PyErr_SetString(PyExc_OverflowError, |
| 11798 | "repeated string is too long"); |
| 11799 | return NULL; |
| 11800 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11801 | nchars = len * PyUnicode_GET_LENGTH(str); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11802 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11803 | u = PyUnicode_New(nchars, PyUnicode_MAX_CHAR_VALUE(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11804 | if (!u) |
| 11805 | return NULL; |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11806 | assert(PyUnicode_KIND(u) == PyUnicode_KIND(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11807 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11808 | if (PyUnicode_GET_LENGTH(str) == 1) { |
| 11809 | const int kind = PyUnicode_KIND(str); |
| 11810 | const Py_UCS4 fill_char = PyUnicode_READ(kind, PyUnicode_DATA(str), 0); |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 11811 | if (kind == PyUnicode_1BYTE_KIND) { |
| 11812 | void *to = PyUnicode_DATA(u); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11813 | memset(to, (unsigned char)fill_char, len); |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 11814 | } |
| 11815 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 11816 | Py_UCS2 *ucs2 = PyUnicode_2BYTE_DATA(u); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11817 | for (n = 0; n < len; ++n) |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 11818 | ucs2[n] = fill_char; |
| 11819 | } else { |
| 11820 | Py_UCS4 *ucs4 = PyUnicode_4BYTE_DATA(u); |
| 11821 | assert(kind == PyUnicode_4BYTE_KIND); |
| 11822 | for (n = 0; n < len; ++n) |
| 11823 | ucs4[n] = fill_char; |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11824 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11825 | } |
| 11826 | else { |
| 11827 | /* number of characters copied this far */ |
| 11828 | Py_ssize_t done = PyUnicode_GET_LENGTH(str); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 11829 | const Py_ssize_t char_size = PyUnicode_KIND(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11830 | char *to = (char *) PyUnicode_DATA(u); |
| 11831 | Py_MEMCPY(to, PyUnicode_DATA(str), |
| 11832 | PyUnicode_GET_LENGTH(str) * char_size); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11833 | while (done < nchars) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11834 | n = (done <= nchars-done) ? done : nchars-done; |
| 11835 | Py_MEMCPY(to + (done * char_size), to, n * char_size); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11836 | done += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11837 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11838 | } |
| 11839 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 11840 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11841 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11842 | } |
| 11843 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11844 | PyObject * |
| 11845 | PyUnicode_Replace(PyObject *obj, |
| 11846 | PyObject *subobj, |
| 11847 | PyObject *replobj, |
| 11848 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11849 | { |
| 11850 | PyObject *self; |
| 11851 | PyObject *str1; |
| 11852 | PyObject *str2; |
| 11853 | PyObject *result; |
| 11854 | |
| 11855 | self = PyUnicode_FromObject(obj); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 11856 | if (self == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11857 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11858 | str1 = PyUnicode_FromObject(subobj); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 11859 | if (str1 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11860 | Py_DECREF(self); |
| 11861 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11862 | } |
| 11863 | str2 = PyUnicode_FromObject(replobj); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 11864 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11865 | Py_DECREF(self); |
| 11866 | Py_DECREF(str1); |
| 11867 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11868 | } |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 11869 | if (PyUnicode_READY(self) == -1 || |
| 11870 | PyUnicode_READY(str1) == -1 || |
| 11871 | PyUnicode_READY(str2) == -1) |
| 11872 | result = NULL; |
| 11873 | else |
| 11874 | result = replace(self, str1, str2, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11875 | Py_DECREF(self); |
| 11876 | Py_DECREF(str1); |
| 11877 | Py_DECREF(str2); |
| 11878 | return result; |
| 11879 | } |
| 11880 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11881 | PyDoc_STRVAR(replace__doc__, |
Ezio Melotti | c1897e7 | 2010-06-26 18:50:39 +0000 | [diff] [blame] | 11882 | "S.replace(old, new[, count]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11883 | \n\ |
| 11884 | Return a copy of S with all occurrences of substring\n\ |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 11885 | old replaced by new. If the optional argument count is\n\ |
| 11886 | given, only the first count occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11887 | |
| 11888 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11889 | unicode_replace(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11890 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11891 | PyObject *str1; |
| 11892 | PyObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11893 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11894 | PyObject *result; |
| 11895 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11896 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11897 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 11898 | if (PyUnicode_READY(self) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11899 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11900 | str1 = PyUnicode_FromObject(str1); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 11901 | if (str1 == NULL) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11902 | return NULL; |
| 11903 | str2 = PyUnicode_FromObject(str2); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 11904 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11905 | Py_DECREF(str1); |
| 11906 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 11907 | } |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 11908 | if (PyUnicode_READY(str1) == -1 || PyUnicode_READY(str2) == -1) |
| 11909 | result = NULL; |
| 11910 | else |
| 11911 | result = replace(self, str1, str2, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11912 | |
| 11913 | Py_DECREF(str1); |
| 11914 | Py_DECREF(str2); |
| 11915 | return result; |
| 11916 | } |
| 11917 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11918 | static PyObject * |
| 11919 | unicode_repr(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11920 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11921 | PyObject *repr; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11922 | Py_ssize_t isize; |
| 11923 | Py_ssize_t osize, squote, dquote, i, o; |
| 11924 | Py_UCS4 max, quote; |
| 11925 | int ikind, okind; |
| 11926 | void *idata, *odata; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11927 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11928 | if (PyUnicode_READY(unicode) == -1) |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11929 | return NULL; |
| 11930 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11931 | isize = PyUnicode_GET_LENGTH(unicode); |
| 11932 | idata = PyUnicode_DATA(unicode); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11933 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11934 | /* Compute length of output, quote characters, and |
| 11935 | maximum character */ |
| 11936 | osize = 2; /* quotes */ |
| 11937 | max = 127; |
| 11938 | squote = dquote = 0; |
| 11939 | ikind = PyUnicode_KIND(unicode); |
| 11940 | for (i = 0; i < isize; i++) { |
| 11941 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
| 11942 | switch (ch) { |
| 11943 | case '\'': squote++; osize++; break; |
| 11944 | case '"': dquote++; osize++; break; |
| 11945 | case '\\': case '\t': case '\r': case '\n': |
| 11946 | osize += 2; break; |
| 11947 | default: |
| 11948 | /* Fast-path ASCII */ |
| 11949 | if (ch < ' ' || ch == 0x7f) |
| 11950 | osize += 4; /* \xHH */ |
| 11951 | else if (ch < 0x7f) |
| 11952 | osize++; |
| 11953 | else if (Py_UNICODE_ISPRINTABLE(ch)) { |
| 11954 | osize++; |
| 11955 | max = ch > max ? ch : max; |
| 11956 | } |
| 11957 | else if (ch < 0x100) |
| 11958 | osize += 4; /* \xHH */ |
| 11959 | else if (ch < 0x10000) |
| 11960 | osize += 6; /* \uHHHH */ |
| 11961 | else |
| 11962 | osize += 10; /* \uHHHHHHHH */ |
| 11963 | } |
| 11964 | } |
| 11965 | |
| 11966 | quote = '\''; |
| 11967 | if (squote) { |
| 11968 | if (dquote) |
| 11969 | /* Both squote and dquote present. Use squote, |
| 11970 | and escape them */ |
| 11971 | osize += squote; |
| 11972 | else |
| 11973 | quote = '"'; |
| 11974 | } |
| 11975 | |
| 11976 | repr = PyUnicode_New(osize, max); |
| 11977 | if (repr == NULL) |
| 11978 | return NULL; |
| 11979 | okind = PyUnicode_KIND(repr); |
| 11980 | odata = PyUnicode_DATA(repr); |
| 11981 | |
| 11982 | PyUnicode_WRITE(okind, odata, 0, quote); |
| 11983 | PyUnicode_WRITE(okind, odata, osize-1, quote); |
| 11984 | |
| 11985 | for (i = 0, o = 1; i < isize; i++) { |
| 11986 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11987 | |
| 11988 | /* Escape quotes and backslashes */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11989 | if ((ch == quote) || (ch == '\\')) { |
| 11990 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11991 | PyUnicode_WRITE(okind, odata, o++, ch); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11992 | continue; |
| 11993 | } |
| 11994 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11995 | /* Map special whitespace to '\t', \n', '\r' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11996 | if (ch == '\t') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11997 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11998 | PyUnicode_WRITE(okind, odata, o++, 't'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11999 | } |
| 12000 | else if (ch == '\n') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12001 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12002 | PyUnicode_WRITE(okind, odata, o++, 'n'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12003 | } |
| 12004 | else if (ch == '\r') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12005 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12006 | PyUnicode_WRITE(okind, odata, o++, 'r'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12007 | } |
| 12008 | |
| 12009 | /* Map non-printable US ASCII to '\xhh' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12010 | else if (ch < ' ' || ch == 0x7F) { |
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++, 'x'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12013 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0x000F]); |
| 12014 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0x000F]); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12015 | } |
| 12016 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12017 | /* Copy ASCII characters as-is */ |
| 12018 | else if (ch < 0x7F) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12019 | PyUnicode_WRITE(okind, odata, o++, ch); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12020 | } |
| 12021 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12022 | /* Non-ASCII characters */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12023 | else { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12024 | /* Map Unicode whitespace and control characters |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12025 | (categories Z* and C* except ASCII space) |
| 12026 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12027 | if (!Py_UNICODE_ISPRINTABLE(ch)) { |
Kristján Valur Jónsson | 55e5dc8 | 2012-06-06 21:58:08 +0000 | [diff] [blame] | 12028 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12029 | /* Map 8-bit characters to '\xhh' */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12030 | if (ch <= 0xff) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12031 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12032 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0x000F]); |
| 12033 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0x000F]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12034 | } |
Kristján Valur Jónsson | 55e5dc8 | 2012-06-06 21:58:08 +0000 | [diff] [blame] | 12035 | /* Map 16-bit characters to '\uxxxx' */ |
| 12036 | else if (ch <= 0xffff) { |
| 12037 | PyUnicode_WRITE(okind, odata, o++, 'u'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12038 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 12) & 0xF]); |
| 12039 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 8) & 0xF]); |
| 12040 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0xF]); |
| 12041 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0xF]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12042 | } |
Kristján Valur Jónsson | 55e5dc8 | 2012-06-06 21:58:08 +0000 | [diff] [blame] | 12043 | /* Map 21-bit characters to '\U00xxxxxx' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12044 | else { |
Kristján Valur Jónsson | 55e5dc8 | 2012-06-06 21:58:08 +0000 | [diff] [blame] | 12045 | PyUnicode_WRITE(okind, odata, o++, 'U'); |
| 12046 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 28) & 0xF]); |
| 12047 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 24) & 0xF]); |
| 12048 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 20) & 0xF]); |
| 12049 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 16) & 0xF]); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12050 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 12) & 0xF]); |
| 12051 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 8) & 0xF]); |
| 12052 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0xF]); |
| 12053 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0xF]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12054 | } |
| 12055 | } |
| 12056 | /* Copy characters as-is */ |
| 12057 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12058 | PyUnicode_WRITE(okind, odata, o++, ch); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12059 | } |
| 12060 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12061 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12062 | /* Closing quote already added at the beginning */ |
Victor Stinner | 05d1189 | 2011-10-06 01:13:58 +0200 | [diff] [blame] | 12063 | assert(_PyUnicode_CheckConsistency(repr, 1)); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12064 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12065 | } |
| 12066 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12067 | PyDoc_STRVAR(rfind__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12068 | "S.rfind(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12069 | \n\ |
| 12070 | Return the highest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 12071 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12072 | arguments start and end are interpreted as in slice notation.\n\ |
| 12073 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12074 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12075 | |
| 12076 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12077 | unicode_rfind(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12078 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12079 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 12080 | Py_ssize_t start; |
| 12081 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12082 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12083 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12084 | if (!stringlib_parse_args_finds_unicode("rfind", args, &substring, |
| 12085 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12086 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12087 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12088 | if (PyUnicode_READY(self) == -1) |
| 12089 | return NULL; |
| 12090 | if (PyUnicode_READY(substring) == -1) |
| 12091 | return NULL; |
| 12092 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12093 | result = any_find_slice(-1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12094 | |
| 12095 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12096 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12097 | if (result == -2) |
| 12098 | return NULL; |
| 12099 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12100 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12101 | } |
| 12102 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12103 | PyDoc_STRVAR(rindex__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12104 | "S.rindex(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12105 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12106 | 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] | 12107 | |
| 12108 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12109 | unicode_rindex(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12110 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12111 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 12112 | Py_ssize_t start; |
| 12113 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12114 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12115 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12116 | if (!stringlib_parse_args_finds_unicode("rindex", args, &substring, |
| 12117 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12118 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12119 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12120 | if (PyUnicode_READY(self) == -1) |
| 12121 | return NULL; |
| 12122 | if (PyUnicode_READY(substring) == -1) |
| 12123 | return NULL; |
| 12124 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12125 | result = any_find_slice(-1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12126 | |
| 12127 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12128 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12129 | if (result == -2) |
| 12130 | return NULL; |
| 12131 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12132 | if (result < 0) { |
| 12133 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 12134 | return NULL; |
| 12135 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12136 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12137 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12138 | } |
| 12139 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12140 | PyDoc_STRVAR(rjust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12141 | "S.rjust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12142 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 12143 | 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] | 12144 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12145 | |
| 12146 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12147 | unicode_rjust(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12148 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12149 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12150 | Py_UCS4 fillchar = ' '; |
| 12151 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 12152 | 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] | 12153 | return NULL; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 12154 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 12155 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12156 | return NULL; |
| 12157 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12158 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 12159 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12160 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12161 | return pad(self, width - PyUnicode_GET_LENGTH(self), 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12162 | } |
| 12163 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12164 | PyObject * |
| 12165 | PyUnicode_Split(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12166 | { |
| 12167 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 12168 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12169 | s = PyUnicode_FromObject(s); |
| 12170 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12171 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12172 | if (sep != NULL) { |
| 12173 | sep = PyUnicode_FromObject(sep); |
| 12174 | if (sep == NULL) { |
| 12175 | Py_DECREF(s); |
| 12176 | return NULL; |
| 12177 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12178 | } |
| 12179 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12180 | result = split(s, sep, maxsplit); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12181 | |
| 12182 | Py_DECREF(s); |
| 12183 | Py_XDECREF(sep); |
| 12184 | return result; |
| 12185 | } |
| 12186 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12187 | PyDoc_STRVAR(split__doc__, |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12188 | "S.split(sep=None, maxsplit=-1) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12189 | \n\ |
| 12190 | Return a list of the words in S, using sep as the\n\ |
| 12191 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 12192 | 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] | 12193 | whitespace string is a separator and empty strings are\n\ |
| 12194 | removed from the result."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12195 | |
| 12196 | static PyObject* |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12197 | unicode_split(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12198 | { |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12199 | static char *kwlist[] = {"sep", "maxsplit", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12200 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12201 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12202 | |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12203 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|On:split", |
| 12204 | kwlist, &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12205 | return NULL; |
| 12206 | |
| 12207 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12208 | return split(self, NULL, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12209 | else if (PyUnicode_Check(substring)) |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12210 | return split(self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12211 | else |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12212 | return PyUnicode_Split(self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12213 | } |
| 12214 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12215 | PyObject * |
| 12216 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 12217 | { |
| 12218 | PyObject* str_obj; |
| 12219 | PyObject* sep_obj; |
| 12220 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12221 | int kind1, kind2, kind; |
| 12222 | void *buf1 = NULL, *buf2 = NULL; |
| 12223 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12224 | |
| 12225 | str_obj = PyUnicode_FromObject(str_in); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12226 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12227 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12228 | sep_obj = PyUnicode_FromObject(sep_in); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12229 | if (!sep_obj) { |
| 12230 | Py_DECREF(str_obj); |
| 12231 | return NULL; |
| 12232 | } |
| 12233 | if (PyUnicode_READY(sep_obj) == -1 || PyUnicode_READY(str_obj) == -1) { |
| 12234 | Py_DECREF(sep_obj); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12235 | Py_DECREF(str_obj); |
| 12236 | return NULL; |
| 12237 | } |
| 12238 | |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12239 | kind1 = PyUnicode_KIND(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12240 | kind2 = PyUnicode_KIND(sep_obj); |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12241 | kind = Py_MAX(kind1, kind2); |
| 12242 | buf1 = PyUnicode_DATA(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12243 | if (kind1 != kind) |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12244 | buf1 = _PyUnicode_AsKind(str_obj, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12245 | if (!buf1) |
| 12246 | goto onError; |
| 12247 | buf2 = PyUnicode_DATA(sep_obj); |
| 12248 | if (kind2 != kind) |
| 12249 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 12250 | if (!buf2) |
| 12251 | goto onError; |
| 12252 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 12253 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 12254 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 12255 | switch (PyUnicode_KIND(str_obj)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12256 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 12257 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) |
| 12258 | out = asciilib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12259 | else |
| 12260 | 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] | 12261 | break; |
| 12262 | case PyUnicode_2BYTE_KIND: |
| 12263 | out = ucs2lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12264 | break; |
| 12265 | case PyUnicode_4BYTE_KIND: |
| 12266 | out = ucs4lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12267 | break; |
| 12268 | default: |
| 12269 | assert(0); |
| 12270 | out = 0; |
| 12271 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12272 | |
| 12273 | Py_DECREF(sep_obj); |
| 12274 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12275 | if (kind1 != kind) |
| 12276 | PyMem_Free(buf1); |
| 12277 | if (kind2 != kind) |
| 12278 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12279 | |
| 12280 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12281 | onError: |
| 12282 | Py_DECREF(sep_obj); |
| 12283 | Py_DECREF(str_obj); |
| 12284 | if (kind1 != kind && buf1) |
| 12285 | PyMem_Free(buf1); |
| 12286 | if (kind2 != kind && buf2) |
| 12287 | PyMem_Free(buf2); |
| 12288 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12289 | } |
| 12290 | |
| 12291 | |
| 12292 | PyObject * |
| 12293 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 12294 | { |
| 12295 | PyObject* str_obj; |
| 12296 | PyObject* sep_obj; |
| 12297 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12298 | int kind1, kind2, kind; |
| 12299 | void *buf1 = NULL, *buf2 = NULL; |
| 12300 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12301 | |
| 12302 | str_obj = PyUnicode_FromObject(str_in); |
| 12303 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12304 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12305 | sep_obj = PyUnicode_FromObject(sep_in); |
| 12306 | if (!sep_obj) { |
| 12307 | Py_DECREF(str_obj); |
| 12308 | return NULL; |
| 12309 | } |
| 12310 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12311 | kind1 = PyUnicode_KIND(str_in); |
| 12312 | kind2 = PyUnicode_KIND(sep_obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 12313 | kind = Py_MAX(kind1, kind2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12314 | buf1 = PyUnicode_DATA(str_in); |
| 12315 | if (kind1 != kind) |
| 12316 | buf1 = _PyUnicode_AsKind(str_in, kind); |
| 12317 | if (!buf1) |
| 12318 | goto onError; |
| 12319 | buf2 = PyUnicode_DATA(sep_obj); |
| 12320 | if (kind2 != kind) |
| 12321 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 12322 | if (!buf2) |
| 12323 | goto onError; |
| 12324 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 12325 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 12326 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 12327 | switch (PyUnicode_KIND(str_in)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12328 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 12329 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) |
| 12330 | out = asciilib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12331 | else |
| 12332 | 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] | 12333 | break; |
| 12334 | case PyUnicode_2BYTE_KIND: |
| 12335 | out = ucs2lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12336 | break; |
| 12337 | case PyUnicode_4BYTE_KIND: |
| 12338 | out = ucs4lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12339 | break; |
| 12340 | default: |
| 12341 | assert(0); |
| 12342 | out = 0; |
| 12343 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12344 | |
| 12345 | Py_DECREF(sep_obj); |
| 12346 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12347 | if (kind1 != kind) |
| 12348 | PyMem_Free(buf1); |
| 12349 | if (kind2 != kind) |
| 12350 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12351 | |
| 12352 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12353 | onError: |
| 12354 | Py_DECREF(sep_obj); |
| 12355 | Py_DECREF(str_obj); |
| 12356 | if (kind1 != kind && buf1) |
| 12357 | PyMem_Free(buf1); |
| 12358 | if (kind2 != kind && buf2) |
| 12359 | PyMem_Free(buf2); |
| 12360 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12361 | } |
| 12362 | |
| 12363 | PyDoc_STRVAR(partition__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12364 | "S.partition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12365 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 12366 | 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] | 12367 | 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] | 12368 | found, return S and two empty strings."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12369 | |
| 12370 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12371 | unicode_partition(PyObject *self, PyObject *separator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12372 | { |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12373 | return PyUnicode_Partition(self, separator); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12374 | } |
| 12375 | |
| 12376 | PyDoc_STRVAR(rpartition__doc__, |
Ezio Melotti | 5b2b242 | 2010-01-25 11:58:28 +0000 | [diff] [blame] | 12377 | "S.rpartition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12378 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 12379 | 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] | 12380 | 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] | 12381 | separator is not found, return two empty strings and S."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12382 | |
| 12383 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12384 | unicode_rpartition(PyObject *self, PyObject *separator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12385 | { |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12386 | return PyUnicode_RPartition(self, separator); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12387 | } |
| 12388 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12389 | PyObject * |
| 12390 | PyUnicode_RSplit(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12391 | { |
| 12392 | PyObject *result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12393 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12394 | s = PyUnicode_FromObject(s); |
| 12395 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12396 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12397 | if (sep != NULL) { |
| 12398 | sep = PyUnicode_FromObject(sep); |
| 12399 | if (sep == NULL) { |
| 12400 | Py_DECREF(s); |
| 12401 | return NULL; |
| 12402 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12403 | } |
| 12404 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12405 | result = rsplit(s, sep, maxsplit); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12406 | |
| 12407 | Py_DECREF(s); |
| 12408 | Py_XDECREF(sep); |
| 12409 | return result; |
| 12410 | } |
| 12411 | |
| 12412 | PyDoc_STRVAR(rsplit__doc__, |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12413 | "S.rsplit(sep=None, maxsplit=-1) -> list of strings\n\ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12414 | \n\ |
| 12415 | Return a list of the words in S, using sep as the\n\ |
| 12416 | delimiter string, starting at the end of the string and\n\ |
| 12417 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 12418 | splits are done. If sep is not specified, any whitespace string\n\ |
| 12419 | is a separator."); |
| 12420 | |
| 12421 | static PyObject* |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12422 | unicode_rsplit(PyObject *self, PyObject *args, PyObject *kwds) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12423 | { |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12424 | static char *kwlist[] = {"sep", "maxsplit", 0}; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12425 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12426 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12427 | |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12428 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|On:rsplit", |
| 12429 | kwlist, &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12430 | return NULL; |
| 12431 | |
| 12432 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12433 | return rsplit(self, NULL, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12434 | else if (PyUnicode_Check(substring)) |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12435 | return rsplit(self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12436 | else |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12437 | return PyUnicode_RSplit(self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12438 | } |
| 12439 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12440 | PyDoc_STRVAR(splitlines__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12441 | "S.splitlines([keepends]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12442 | \n\ |
| 12443 | 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] | 12444 | 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] | 12445 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12446 | |
| 12447 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12448 | unicode_splitlines(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12449 | { |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12450 | static char *kwlist[] = {"keepends", 0}; |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 12451 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12452 | |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12453 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:splitlines", |
| 12454 | kwlist, &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12455 | return NULL; |
| 12456 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12457 | return PyUnicode_Splitlines(self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12458 | } |
| 12459 | |
| 12460 | static |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 12461 | PyObject *unicode_str(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12462 | { |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12463 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12464 | } |
| 12465 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12466 | PyDoc_STRVAR(swapcase__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12467 | "S.swapcase() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12468 | \n\ |
| 12469 | 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] | 12470 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12471 | |
| 12472 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12473 | unicode_swapcase(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12474 | { |
Benjamin Peterson | eea4846 | 2012-01-16 14:28:50 -0500 | [diff] [blame] | 12475 | if (PyUnicode_READY(self) == -1) |
| 12476 | return NULL; |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 12477 | return case_operation(self, do_swapcase); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12478 | } |
| 12479 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12480 | PyDoc_STRVAR(maketrans__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12481 | "str.maketrans(x[, y[, z]]) -> dict (static method)\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12482 | \n\ |
| 12483 | Return a translation table usable for str.translate().\n\ |
| 12484 | If there is only one argument, it must be a dictionary mapping Unicode\n\ |
| 12485 | ordinals (integers) or characters to Unicode ordinals, strings or None.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12486 | Character keys will be then converted to ordinals.\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12487 | If there are two arguments, they must be strings of equal length, and\n\ |
| 12488 | in the resulting dictionary, each character in x will be mapped to the\n\ |
| 12489 | character at the same position in y. If there is a third argument, it\n\ |
| 12490 | must be a string, whose characters will be mapped to None in the result."); |
| 12491 | |
| 12492 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12493 | unicode_maketrans(PyObject *null, PyObject *args) |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12494 | { |
| 12495 | PyObject *x, *y = NULL, *z = NULL; |
| 12496 | PyObject *new = NULL, *key, *value; |
| 12497 | Py_ssize_t i = 0; |
| 12498 | int res; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12499 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12500 | if (!PyArg_ParseTuple(args, "O|UU:maketrans", &x, &y, &z)) |
| 12501 | return NULL; |
| 12502 | new = PyDict_New(); |
| 12503 | if (!new) |
| 12504 | return NULL; |
| 12505 | if (y != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12506 | int x_kind, y_kind, z_kind; |
| 12507 | void *x_data, *y_data, *z_data; |
| 12508 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12509 | /* x must be a string too, of equal length */ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12510 | if (!PyUnicode_Check(x)) { |
| 12511 | PyErr_SetString(PyExc_TypeError, "first maketrans argument must " |
| 12512 | "be a string if there is a second argument"); |
| 12513 | goto err; |
| 12514 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12515 | if (PyUnicode_GET_LENGTH(x) != PyUnicode_GET_LENGTH(y)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12516 | PyErr_SetString(PyExc_ValueError, "the first two maketrans " |
| 12517 | "arguments must have equal length"); |
| 12518 | goto err; |
| 12519 | } |
| 12520 | /* 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] | 12521 | x_kind = PyUnicode_KIND(x); |
| 12522 | y_kind = PyUnicode_KIND(y); |
| 12523 | x_data = PyUnicode_DATA(x); |
| 12524 | y_data = PyUnicode_DATA(y); |
| 12525 | for (i = 0; i < PyUnicode_GET_LENGTH(x); i++) { |
| 12526 | key = PyLong_FromLong(PyUnicode_READ(x_kind, x_data, i)); |
Benjamin Peterson | 53aa1d7 | 2011-12-20 13:29:45 -0600 | [diff] [blame] | 12527 | if (!key) |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12528 | goto err; |
Benjamin Peterson | 822c790 | 2011-12-20 13:32:50 -0600 | [diff] [blame] | 12529 | value = PyLong_FromLong(PyUnicode_READ(y_kind, y_data, i)); |
Benjamin Peterson | 53aa1d7 | 2011-12-20 13:29:45 -0600 | [diff] [blame] | 12530 | if (!value) { |
| 12531 | Py_DECREF(key); |
| 12532 | goto err; |
| 12533 | } |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12534 | res = PyDict_SetItem(new, key, value); |
| 12535 | Py_DECREF(key); |
| 12536 | Py_DECREF(value); |
| 12537 | if (res < 0) |
| 12538 | goto err; |
| 12539 | } |
| 12540 | /* create entries for deleting chars in z */ |
| 12541 | if (z != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12542 | z_kind = PyUnicode_KIND(z); |
| 12543 | z_data = PyUnicode_DATA(z); |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 12544 | for (i = 0; i < PyUnicode_GET_LENGTH(z); i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12545 | key = PyLong_FromLong(PyUnicode_READ(z_kind, z_data, i)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12546 | if (!key) |
| 12547 | goto err; |
| 12548 | res = PyDict_SetItem(new, key, Py_None); |
| 12549 | Py_DECREF(key); |
| 12550 | if (res < 0) |
| 12551 | goto err; |
| 12552 | } |
| 12553 | } |
| 12554 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12555 | int kind; |
| 12556 | void *data; |
| 12557 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12558 | /* x must be a dict */ |
Raymond Hettinger | 3ad0576 | 2009-05-29 22:11:22 +0000 | [diff] [blame] | 12559 | if (!PyDict_CheckExact(x)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12560 | PyErr_SetString(PyExc_TypeError, "if you give only one argument " |
| 12561 | "to maketrans it must be a dict"); |
| 12562 | goto err; |
| 12563 | } |
| 12564 | /* copy entries into the new dict, converting string keys to int keys */ |
| 12565 | while (PyDict_Next(x, &i, &key, &value)) { |
| 12566 | if (PyUnicode_Check(key)) { |
| 12567 | /* convert string keys to integer keys */ |
| 12568 | PyObject *newkey; |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 12569 | if (PyUnicode_GET_LENGTH(key) != 1) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12570 | PyErr_SetString(PyExc_ValueError, "string keys in translate " |
| 12571 | "table must be of length 1"); |
| 12572 | goto err; |
| 12573 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12574 | kind = PyUnicode_KIND(key); |
| 12575 | data = PyUnicode_DATA(key); |
| 12576 | newkey = PyLong_FromLong(PyUnicode_READ(kind, data, 0)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12577 | if (!newkey) |
| 12578 | goto err; |
| 12579 | res = PyDict_SetItem(new, newkey, value); |
| 12580 | Py_DECREF(newkey); |
| 12581 | if (res < 0) |
| 12582 | goto err; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12583 | } else if (PyLong_Check(key)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12584 | /* just keep integer keys */ |
| 12585 | if (PyDict_SetItem(new, key, value) < 0) |
| 12586 | goto err; |
| 12587 | } else { |
| 12588 | PyErr_SetString(PyExc_TypeError, "keys in translate table must " |
| 12589 | "be strings or integers"); |
| 12590 | goto err; |
| 12591 | } |
| 12592 | } |
| 12593 | } |
| 12594 | return new; |
| 12595 | err: |
| 12596 | Py_DECREF(new); |
| 12597 | return NULL; |
| 12598 | } |
| 12599 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12600 | PyDoc_STRVAR(translate__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12601 | "S.translate(table) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12602 | \n\ |
| 12603 | Return a copy of the string S, where all characters have been mapped\n\ |
| 12604 | through the given translation table, which must be a mapping of\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12605 | Unicode ordinals to Unicode ordinals, strings, or None.\n\ |
Walter Dörwald | 5c1ee17 | 2002-09-04 20:31:32 +0000 | [diff] [blame] | 12606 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 12607 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12608 | |
| 12609 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12610 | unicode_translate(PyObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12611 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12612 | return _PyUnicode_TranslateCharmap(self, table, "ignore"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12613 | } |
| 12614 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12615 | PyDoc_STRVAR(upper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12616 | "S.upper() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12617 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12618 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12619 | |
| 12620 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12621 | unicode_upper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12622 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 12623 | if (PyUnicode_READY(self) == -1) |
| 12624 | return NULL; |
| 12625 | if (PyUnicode_IS_ASCII(self)) |
| 12626 | return ascii_upper_or_lower(self, 0); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 12627 | return case_operation(self, do_upper); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12628 | } |
| 12629 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12630 | PyDoc_STRVAR(zfill__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12631 | "S.zfill(width) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12632 | \n\ |
Benjamin Peterson | 9aa4299 | 2008-09-10 21:57:34 +0000 | [diff] [blame] | 12633 | Pad a numeric string S with zeros on the left, to fill a field\n\ |
| 12634 | of the specified width. The string S is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12635 | |
| 12636 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12637 | unicode_zfill(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12638 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12639 | Py_ssize_t fill; |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12640 | PyObject *u; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12641 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12642 | int kind; |
| 12643 | void *data; |
| 12644 | Py_UCS4 chr; |
| 12645 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12646 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12647 | return NULL; |
| 12648 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 12649 | if (PyUnicode_READY(self) == -1) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12650 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12651 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12652 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 12653 | return unicode_result_unchanged(self); |
| 12654 | |
| 12655 | fill = width - PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12656 | |
| 12657 | u = pad(self, fill, 0, '0'); |
| 12658 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 12659 | if (u == NULL) |
| 12660 | return NULL; |
| 12661 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12662 | kind = PyUnicode_KIND(u); |
| 12663 | data = PyUnicode_DATA(u); |
| 12664 | chr = PyUnicode_READ(kind, data, fill); |
| 12665 | |
| 12666 | if (chr == '+' || chr == '-') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12667 | /* move sign to beginning of string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12668 | PyUnicode_WRITE(kind, data, 0, chr); |
| 12669 | PyUnicode_WRITE(kind, data, fill, '0'); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12670 | } |
| 12671 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12672 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12673 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12674 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12675 | |
| 12676 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12677 | static PyObject * |
| 12678 | unicode__decimal2ascii(PyObject *self) |
| 12679 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12680 | return PyUnicode_TransformDecimalAndSpaceToASCII(self); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12681 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12682 | #endif |
| 12683 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12684 | PyDoc_STRVAR(startswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12685 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12686 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 12687 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 12688 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12689 | With optional end, stop comparing S at that position.\n\ |
| 12690 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12691 | |
| 12692 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12693 | unicode_startswith(PyObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12694 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12695 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12696 | PyObject *subobj; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12697 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12698 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12699 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12700 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12701 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12702 | if (!stringlib_parse_args_finds("startswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12703 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12704 | if (PyTuple_Check(subobj)) { |
| 12705 | Py_ssize_t i; |
| 12706 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12707 | substring = PyUnicode_FromObject(PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12708 | if (substring == NULL) |
| 12709 | return NULL; |
| 12710 | result = tailmatch(self, substring, start, end, -1); |
| 12711 | Py_DECREF(substring); |
| 12712 | if (result) { |
| 12713 | Py_RETURN_TRUE; |
| 12714 | } |
| 12715 | } |
| 12716 | /* nothing matched */ |
| 12717 | Py_RETURN_FALSE; |
| 12718 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12719 | substring = PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12720 | if (substring == NULL) { |
| 12721 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 12722 | PyErr_Format(PyExc_TypeError, "startswith first arg must be str or " |
| 12723 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12724 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12725 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12726 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12727 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12728 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12729 | } |
| 12730 | |
| 12731 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12732 | PyDoc_STRVAR(endswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12733 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12734 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 12735 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 12736 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12737 | With optional end, stop comparing S at that position.\n\ |
| 12738 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12739 | |
| 12740 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12741 | unicode_endswith(PyObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12742 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12743 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12744 | PyObject *subobj; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12745 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12746 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12747 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12748 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12749 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12750 | if (!stringlib_parse_args_finds("endswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12751 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12752 | if (PyTuple_Check(subobj)) { |
| 12753 | Py_ssize_t i; |
| 12754 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12755 | substring = PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12756 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12757 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12758 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12759 | result = tailmatch(self, substring, start, end, +1); |
| 12760 | Py_DECREF(substring); |
| 12761 | if (result) { |
| 12762 | Py_RETURN_TRUE; |
| 12763 | } |
| 12764 | } |
| 12765 | Py_RETURN_FALSE; |
| 12766 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12767 | substring = PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12768 | if (substring == NULL) { |
| 12769 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 12770 | PyErr_Format(PyExc_TypeError, "endswith first arg must be str or " |
| 12771 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12772 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12773 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12774 | result = tailmatch(self, substring, start, end, +1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12775 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12776 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12777 | } |
| 12778 | |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12779 | Py_LOCAL_INLINE(void) |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 12780 | _PyUnicodeWriter_Update(_PyUnicodeWriter *writer) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12781 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12782 | writer->size = PyUnicode_GET_LENGTH(writer->buffer); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12783 | writer->maxchar = PyUnicode_MAX_CHAR_VALUE(writer->buffer); |
| 12784 | writer->data = PyUnicode_DATA(writer->buffer); |
| 12785 | writer->kind = PyUnicode_KIND(writer->buffer); |
| 12786 | } |
| 12787 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12788 | void |
| 12789 | _PyUnicodeWriter_Init(_PyUnicodeWriter *writer, Py_ssize_t min_length) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12790 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12791 | memset(writer, 0, sizeof(*writer)); |
| 12792 | #ifdef Py_DEBUG |
| 12793 | writer->kind = 5; /* invalid kind */ |
| 12794 | #endif |
| 12795 | writer->min_length = Py_MAX(min_length, 100); |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 12796 | writer->overallocate = (min_length > 0); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12797 | } |
| 12798 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12799 | int |
| 12800 | _PyUnicodeWriter_PrepareInternal(_PyUnicodeWriter *writer, |
| 12801 | Py_ssize_t length, Py_UCS4 maxchar) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12802 | { |
| 12803 | Py_ssize_t newlen; |
| 12804 | PyObject *newbuffer; |
| 12805 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12806 | assert(length > 0); |
| 12807 | |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12808 | if (length > PY_SSIZE_T_MAX - writer->pos) { |
| 12809 | PyErr_NoMemory(); |
| 12810 | return -1; |
| 12811 | } |
| 12812 | newlen = writer->pos + length; |
| 12813 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12814 | if (writer->buffer == NULL) { |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 12815 | if (writer->overallocate) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12816 | /* overallocate 25% to limit the number of resize */ |
| 12817 | if (newlen <= (PY_SSIZE_T_MAX - newlen / 4)) |
| 12818 | newlen += newlen / 4; |
| 12819 | if (newlen < writer->min_length) |
| 12820 | newlen = writer->min_length; |
| 12821 | } |
| 12822 | writer->buffer = PyUnicode_New(newlen, maxchar); |
| 12823 | if (writer->buffer == NULL) |
| 12824 | return -1; |
| 12825 | _PyUnicodeWriter_Update(writer); |
| 12826 | return 0; |
| 12827 | } |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12828 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12829 | if (newlen > writer->size) { |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 12830 | if (writer->overallocate) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12831 | /* overallocate 25% to limit the number of resize */ |
| 12832 | if (newlen <= (PY_SSIZE_T_MAX - newlen / 4)) |
| 12833 | newlen += newlen / 4; |
| 12834 | if (newlen < writer->min_length) |
| 12835 | newlen = writer->min_length; |
| 12836 | } |
| 12837 | |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 12838 | if (maxchar > writer->maxchar || writer->readonly) { |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12839 | /* resize + widen */ |
| 12840 | newbuffer = PyUnicode_New(newlen, maxchar); |
| 12841 | if (newbuffer == NULL) |
| 12842 | return -1; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12843 | _PyUnicode_FastCopyCharacters(newbuffer, 0, |
| 12844 | writer->buffer, 0, writer->pos); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12845 | Py_DECREF(writer->buffer); |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 12846 | writer->readonly = 0; |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12847 | } |
| 12848 | else { |
| 12849 | newbuffer = resize_compact(writer->buffer, newlen); |
| 12850 | if (newbuffer == NULL) |
| 12851 | return -1; |
| 12852 | } |
| 12853 | writer->buffer = newbuffer; |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 12854 | _PyUnicodeWriter_Update(writer); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12855 | } |
| 12856 | else if (maxchar > writer->maxchar) { |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 12857 | assert(!writer->readonly); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12858 | newbuffer = PyUnicode_New(writer->size, maxchar); |
| 12859 | if (newbuffer == NULL) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12860 | return -1; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12861 | _PyUnicode_FastCopyCharacters(newbuffer, 0, |
| 12862 | writer->buffer, 0, writer->pos); |
| 12863 | Py_DECREF(writer->buffer); |
| 12864 | writer->buffer = newbuffer; |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 12865 | _PyUnicodeWriter_Update(writer); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12866 | } |
| 12867 | return 0; |
| 12868 | } |
| 12869 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12870 | int |
| 12871 | _PyUnicodeWriter_WriteStr(_PyUnicodeWriter *writer, PyObject *str) |
| 12872 | { |
| 12873 | Py_UCS4 maxchar; |
| 12874 | Py_ssize_t len; |
| 12875 | |
| 12876 | if (PyUnicode_READY(str) == -1) |
| 12877 | return -1; |
| 12878 | len = PyUnicode_GET_LENGTH(str); |
| 12879 | if (len == 0) |
| 12880 | return 0; |
| 12881 | maxchar = PyUnicode_MAX_CHAR_VALUE(str); |
| 12882 | if (maxchar > writer->maxchar || len > writer->size - writer->pos) { |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 12883 | if (writer->buffer == NULL && !writer->overallocate) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12884 | Py_INCREF(str); |
| 12885 | writer->buffer = str; |
| 12886 | _PyUnicodeWriter_Update(writer); |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 12887 | writer->readonly = 1; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12888 | writer->size = 0; |
| 12889 | writer->pos += len; |
| 12890 | return 0; |
| 12891 | } |
| 12892 | if (_PyUnicodeWriter_PrepareInternal(writer, len, maxchar) == -1) |
| 12893 | return -1; |
| 12894 | } |
| 12895 | _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos, |
| 12896 | str, 0, len); |
| 12897 | writer->pos += len; |
| 12898 | return 0; |
| 12899 | } |
| 12900 | |
| 12901 | PyObject * |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 12902 | _PyUnicodeWriter_Finish(_PyUnicodeWriter *writer) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12903 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12904 | if (writer->pos == 0) { |
| 12905 | Py_XDECREF(writer->buffer); |
| 12906 | Py_INCREF(unicode_empty); |
| 12907 | return unicode_empty; |
| 12908 | } |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 12909 | if (writer->readonly) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12910 | assert(PyUnicode_GET_LENGTH(writer->buffer) == writer->pos); |
| 12911 | return writer->buffer; |
| 12912 | } |
| 12913 | if (PyUnicode_GET_LENGTH(writer->buffer) != writer->pos) { |
| 12914 | PyObject *newbuffer; |
| 12915 | newbuffer = resize_compact(writer->buffer, writer->pos); |
| 12916 | if (newbuffer == NULL) { |
| 12917 | Py_DECREF(writer->buffer); |
| 12918 | return NULL; |
| 12919 | } |
| 12920 | writer->buffer = newbuffer; |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12921 | } |
Victor Stinner | f59c28c | 2012-05-09 03:24:14 +0200 | [diff] [blame] | 12922 | assert(_PyUnicode_CheckConsistency(writer->buffer, 1)); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12923 | return writer->buffer; |
| 12924 | } |
| 12925 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12926 | void |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 12927 | _PyUnicodeWriter_Dealloc(_PyUnicodeWriter *writer) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12928 | { |
| 12929 | Py_CLEAR(writer->buffer); |
| 12930 | } |
| 12931 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12932 | #include "stringlib/unicode_format.h" |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12933 | |
| 12934 | PyDoc_STRVAR(format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12935 | "S.format(*args, **kwargs) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12936 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 12937 | Return a formatted version of S, using substitutions from args and kwargs.\n\ |
| 12938 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12939 | |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 12940 | PyDoc_STRVAR(format_map__doc__, |
| 12941 | "S.format_map(mapping) -> str\n\ |
| 12942 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 12943 | Return a formatted version of S, using substitutions from mapping.\n\ |
| 12944 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 12945 | |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12946 | static PyObject * |
| 12947 | unicode__format__(PyObject* self, PyObject* args) |
| 12948 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12949 | PyObject *format_spec; |
| 12950 | _PyUnicodeWriter writer; |
| 12951 | int ret; |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12952 | |
| 12953 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
| 12954 | return NULL; |
| 12955 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12956 | if (PyUnicode_READY(self) == -1) |
| 12957 | return NULL; |
| 12958 | _PyUnicodeWriter_Init(&writer, 0); |
| 12959 | ret = _PyUnicode_FormatAdvancedWriter(&writer, |
| 12960 | self, format_spec, 0, |
| 12961 | PyUnicode_GET_LENGTH(format_spec)); |
| 12962 | if (ret == -1) { |
| 12963 | _PyUnicodeWriter_Dealloc(&writer); |
| 12964 | return NULL; |
| 12965 | } |
| 12966 | return _PyUnicodeWriter_Finish(&writer); |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12967 | } |
| 12968 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12969 | PyDoc_STRVAR(p_format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12970 | "S.__format__(format_spec) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12971 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 12972 | Return a formatted version of S as described by format_spec."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12973 | |
| 12974 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12975 | unicode__sizeof__(PyObject *v) |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12976 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12977 | Py_ssize_t size; |
| 12978 | |
| 12979 | /* If it's a compact object, account for base structure + |
| 12980 | character data. */ |
| 12981 | if (PyUnicode_IS_COMPACT_ASCII(v)) |
| 12982 | size = sizeof(PyASCIIObject) + PyUnicode_GET_LENGTH(v) + 1; |
| 12983 | else if (PyUnicode_IS_COMPACT(v)) |
| 12984 | size = sizeof(PyCompactUnicodeObject) + |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 12985 | (PyUnicode_GET_LENGTH(v) + 1) * PyUnicode_KIND(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12986 | else { |
| 12987 | /* If it is a two-block object, account for base object, and |
| 12988 | for character block if present. */ |
| 12989 | size = sizeof(PyUnicodeObject); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 12990 | if (_PyUnicode_DATA_ANY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12991 | size += (PyUnicode_GET_LENGTH(v) + 1) * |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 12992 | PyUnicode_KIND(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12993 | } |
| 12994 | /* 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] | 12995 | with the data pointer. Check if the data is not shared. */ |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 12996 | if (_PyUnicode_HAS_WSTR_MEMORY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12997 | size += (PyUnicode_WSTR_LENGTH(v) + 1) * sizeof(wchar_t); |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 12998 | if (_PyUnicode_HAS_UTF8_MEMORY(v)) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 12999 | size += PyUnicode_UTF8_LENGTH(v) + 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13000 | |
| 13001 | return PyLong_FromSsize_t(size); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13002 | } |
| 13003 | |
| 13004 | PyDoc_STRVAR(sizeof__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13005 | "S.__sizeof__() -> size of S in memory, in bytes"); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13006 | |
| 13007 | static PyObject * |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 13008 | unicode_getnewargs(PyObject *v) |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 13009 | { |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 13010 | PyObject *copy = _PyUnicode_Copy(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13011 | if (!copy) |
| 13012 | return NULL; |
| 13013 | return Py_BuildValue("(N)", copy); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 13014 | } |
| 13015 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13016 | static PyMethodDef unicode_methods[] = { |
Benjamin Peterson | 28a4dce | 2010-12-12 01:33:04 +0000 | [diff] [blame] | 13017 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS | METH_KEYWORDS, encode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13018 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 13019 | {"split", (PyCFunction) unicode_split, METH_VARARGS | METH_KEYWORDS, split__doc__}, |
| 13020 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS | METH_KEYWORDS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13021 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 13022 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 13023 | {"casefold", (PyCFunction) unicode_casefold, METH_NOARGS, casefold__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13024 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 13025 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 13026 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 13027 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 13028 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13029 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13030 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 13031 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 13032 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 13033 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13034 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 13035 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 13036 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 13037 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13038 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 13039 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS | METH_KEYWORDS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 13040 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13041 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 13042 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 13043 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 13044 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 13045 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 13046 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 13047 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 13048 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 13049 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 13050 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 13051 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 13052 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 13053 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 13054 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 13055 | {"isidentifier", (PyCFunction) unicode_isidentifier, METH_NOARGS, isidentifier__doc__}, |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 13056 | {"isprintable", (PyCFunction) unicode_isprintable, METH_NOARGS, isprintable__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13057 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Eric Smith | 9cd1e09 | 2007-08-31 18:39:38 +0000 | [diff] [blame] | 13058 | {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 13059 | {"format_map", (PyCFunction) do_string_format_map, METH_O, format_map__doc__}, |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13060 | {"__format__", (PyCFunction) unicode__format__, METH_VARARGS, p_format__doc__}, |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 13061 | {"maketrans", (PyCFunction) unicode_maketrans, |
| 13062 | METH_VARARGS | METH_STATIC, maketrans__doc__}, |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13063 | {"__sizeof__", (PyCFunction) unicode__sizeof__, METH_NOARGS, sizeof__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 13064 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 13065 | /* These methods are just used for debugging the implementation. */ |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 13066 | {"_decimal2ascii", (PyCFunction) unicode__decimal2ascii, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13067 | #endif |
| 13068 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13069 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13070 | {NULL, NULL} |
| 13071 | }; |
| 13072 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13073 | static PyObject * |
| 13074 | unicode_mod(PyObject *v, PyObject *w) |
| 13075 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 13076 | if (!PyUnicode_Check(v)) |
| 13077 | Py_RETURN_NOTIMPLEMENTED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13078 | return PyUnicode_Format(v, w); |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13079 | } |
| 13080 | |
| 13081 | static PyNumberMethods unicode_as_number = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13082 | 0, /*nb_add*/ |
| 13083 | 0, /*nb_subtract*/ |
| 13084 | 0, /*nb_multiply*/ |
| 13085 | unicode_mod, /*nb_remainder*/ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13086 | }; |
| 13087 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13088 | static PySequenceMethods unicode_as_sequence = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13089 | (lenfunc) unicode_length, /* sq_length */ |
| 13090 | PyUnicode_Concat, /* sq_concat */ |
| 13091 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 13092 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
| 13093 | 0, /* sq_slice */ |
| 13094 | 0, /* sq_ass_item */ |
| 13095 | 0, /* sq_ass_slice */ |
| 13096 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13097 | }; |
| 13098 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13099 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13100 | unicode_subscript(PyObject* self, PyObject* item) |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13101 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13102 | if (PyUnicode_READY(self) == -1) |
| 13103 | return NULL; |
| 13104 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 13105 | if (PyIndex_Check(item)) { |
| 13106 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13107 | if (i == -1 && PyErr_Occurred()) |
| 13108 | return NULL; |
| 13109 | if (i < 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13110 | i += PyUnicode_GET_LENGTH(self); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13111 | return unicode_getitem(self, i); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13112 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13113 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13114 | PyObject *result; |
| 13115 | void *src_data, *dest_data; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13116 | int src_kind, dest_kind; |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 13117 | Py_UCS4 ch, max_char, kind_limit; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13118 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13119 | if (PySlice_GetIndicesEx(item, PyUnicode_GET_LENGTH(self), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13120 | &start, &stop, &step, &slicelength) < 0) { |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13121 | return NULL; |
| 13122 | } |
| 13123 | |
| 13124 | if (slicelength <= 0) { |
Victor Stinner | 382955f | 2011-12-11 21:44:00 +0100 | [diff] [blame] | 13125 | Py_INCREF(unicode_empty); |
| 13126 | return unicode_empty; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13127 | } else if (start == 0 && step == 1 && |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 13128 | slicelength == PyUnicode_GET_LENGTH(self)) { |
| 13129 | return unicode_result_unchanged(self); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 13130 | } else if (step == 1) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13131 | return PyUnicode_Substring(self, |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 13132 | start, start + slicelength); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13133 | } |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13134 | /* General case */ |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13135 | src_kind = PyUnicode_KIND(self); |
| 13136 | src_data = PyUnicode_DATA(self); |
Victor Stinner | 55c9911 | 2011-10-13 01:17:06 +0200 | [diff] [blame] | 13137 | if (!PyUnicode_IS_ASCII(self)) { |
| 13138 | kind_limit = kind_maxchar_limit(src_kind); |
| 13139 | max_char = 0; |
| 13140 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 13141 | ch = PyUnicode_READ(src_kind, src_data, cur); |
| 13142 | if (ch > max_char) { |
| 13143 | max_char = ch; |
| 13144 | if (max_char >= kind_limit) |
| 13145 | break; |
| 13146 | } |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 13147 | } |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13148 | } |
Victor Stinner | 55c9911 | 2011-10-13 01:17:06 +0200 | [diff] [blame] | 13149 | else |
| 13150 | max_char = 127; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13151 | result = PyUnicode_New(slicelength, max_char); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13152 | if (result == NULL) |
| 13153 | return NULL; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13154 | dest_kind = PyUnicode_KIND(result); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13155 | dest_data = PyUnicode_DATA(result); |
| 13156 | |
| 13157 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13158 | Py_UCS4 ch = PyUnicode_READ(src_kind, src_data, cur); |
| 13159 | PyUnicode_WRITE(dest_kind, dest_data, i, ch); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13160 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13161 | assert(_PyUnicode_CheckConsistency(result, 1)); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13162 | return result; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13163 | } else { |
| 13164 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 13165 | return NULL; |
| 13166 | } |
| 13167 | } |
| 13168 | |
| 13169 | static PyMappingMethods unicode_as_mapping = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13170 | (lenfunc)unicode_length, /* mp_length */ |
| 13171 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 13172 | (objobjargproc)0, /* mp_ass_subscript */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13173 | }; |
| 13174 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13175 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13176 | /* Helpers for PyUnicode_Format() */ |
| 13177 | |
| 13178 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13179 | 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] | 13180 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13181 | Py_ssize_t argidx = *p_argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13182 | if (argidx < arglen) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13183 | (*p_argidx)++; |
| 13184 | if (arglen < 0) |
| 13185 | return args; |
| 13186 | else |
| 13187 | return PyTuple_GetItem(args, argidx); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13188 | } |
| 13189 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13190 | "not enough arguments for format string"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13191 | return NULL; |
| 13192 | } |
| 13193 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13194 | /* 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] | 13195 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13196 | static int |
| 13197 | formatfloat(PyObject *v, int flags, int prec, int type, |
| 13198 | PyObject **p_output, _PyUnicodeWriter *writer) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13199 | { |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13200 | char *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13201 | double x; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13202 | Py_ssize_t len; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 13203 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13204 | x = PyFloat_AsDouble(v); |
| 13205 | if (x == -1.0 && PyErr_Occurred()) |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13206 | return -1; |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13207 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13208 | if (prec < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13209 | prec = 6; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13210 | |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13211 | p = PyOS_double_to_string(x, type, prec, |
| 13212 | (flags & F_ALT) ? Py_DTSF_ALT : 0, NULL); |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13213 | if (p == NULL) |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13214 | return -1; |
| 13215 | len = strlen(p); |
| 13216 | if (writer) { |
| 13217 | if (_PyUnicodeWriter_Prepare(writer, len, 127) == -1) |
| 13218 | return -1; |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 13219 | unicode_write_cstr(writer->buffer, writer->pos, p, len); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13220 | writer->pos += len; |
| 13221 | } |
| 13222 | else |
| 13223 | *p_output = _PyUnicode_FromASCII(p, len); |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13224 | PyMem_Free(p); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13225 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13226 | } |
| 13227 | |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13228 | /* formatlong() emulates the format codes d, u, o, x and X, and |
| 13229 | * the F_ALT flag, for Python's long (unbounded) ints. It's not used for |
| 13230 | * Python's regular ints. |
| 13231 | * Return value: a new PyUnicodeObject*, or NULL if error. |
| 13232 | * The output string is of the form |
| 13233 | * "-"? ("0x" | "0X")? digit+ |
| 13234 | * "0x"/"0X" are present only for x and X conversions, with F_ALT |
| 13235 | * set in flags. The case of hex digits will be correct, |
| 13236 | * There will be at least prec digits, zero-filled on the left if |
| 13237 | * necessary to get that many. |
| 13238 | * val object to be converted |
| 13239 | * flags bitmask of format flags; only F_ALT is looked at |
| 13240 | * prec minimum number of digits; 0-fill on left if needed |
| 13241 | * type a character in [duoxX]; u acts the same as d |
| 13242 | * |
| 13243 | * CAUTION: o, x and X conversions on regular ints can never |
| 13244 | * produce a '-' sign, but can for Python's unbounded ints. |
| 13245 | */ |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13246 | static PyObject* |
| 13247 | formatlong(PyObject *val, int flags, int prec, int type) |
| 13248 | { |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13249 | PyObject *result = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13250 | char *buf; |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13251 | Py_ssize_t i; |
| 13252 | int sign; /* 1 if '-', else 0 */ |
| 13253 | int len; /* number of characters */ |
| 13254 | Py_ssize_t llen; |
| 13255 | int numdigits; /* len == numnondigits + numdigits */ |
| 13256 | int numnondigits = 0; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13257 | |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13258 | /* Avoid exceeding SSIZE_T_MAX */ |
| 13259 | if (prec > INT_MAX-3) { |
| 13260 | PyErr_SetString(PyExc_OverflowError, |
| 13261 | "precision too large"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13262 | return NULL; |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13263 | } |
| 13264 | |
| 13265 | assert(PyLong_Check(val)); |
| 13266 | |
| 13267 | switch (type) { |
| 13268 | case 'd': |
| 13269 | case 'u': |
| 13270 | /* Special-case boolean: we want 0/1 */ |
Victor Stinner | b11d91d | 2012-04-28 00:25:34 +0200 | [diff] [blame] | 13271 | if (PyBool_Check(val)) |
| 13272 | result = PyNumber_ToBase(val, 10); |
| 13273 | else |
| 13274 | result = Py_TYPE(val)->tp_str(val); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13275 | break; |
| 13276 | case 'o': |
| 13277 | numnondigits = 2; |
| 13278 | result = PyNumber_ToBase(val, 8); |
| 13279 | break; |
| 13280 | case 'x': |
| 13281 | case 'X': |
| 13282 | numnondigits = 2; |
| 13283 | result = PyNumber_ToBase(val, 16); |
| 13284 | break; |
| 13285 | default: |
| 13286 | assert(!"'type' not in [duoxX]"); |
| 13287 | } |
| 13288 | if (!result) |
| 13289 | return NULL; |
| 13290 | |
| 13291 | assert(unicode_modifiable(result)); |
| 13292 | assert(PyUnicode_IS_READY(result)); |
| 13293 | assert(PyUnicode_IS_ASCII(result)); |
| 13294 | |
| 13295 | /* To modify the string in-place, there can only be one reference. */ |
| 13296 | if (Py_REFCNT(result) != 1) { |
| 13297 | PyErr_BadInternalCall(); |
| 13298 | return NULL; |
| 13299 | } |
| 13300 | buf = PyUnicode_DATA(result); |
| 13301 | llen = PyUnicode_GET_LENGTH(result); |
| 13302 | if (llen > INT_MAX) { |
| 13303 | PyErr_SetString(PyExc_ValueError, |
| 13304 | "string too large in _PyBytes_FormatLong"); |
| 13305 | return NULL; |
| 13306 | } |
| 13307 | len = (int)llen; |
| 13308 | sign = buf[0] == '-'; |
| 13309 | numnondigits += sign; |
| 13310 | numdigits = len - numnondigits; |
| 13311 | assert(numdigits > 0); |
| 13312 | |
| 13313 | /* Get rid of base marker unless F_ALT */ |
| 13314 | if (((flags & F_ALT) == 0 && |
| 13315 | (type == 'o' || type == 'x' || type == 'X'))) { |
| 13316 | assert(buf[sign] == '0'); |
| 13317 | assert(buf[sign+1] == 'x' || buf[sign+1] == 'X' || |
| 13318 | buf[sign+1] == 'o'); |
| 13319 | numnondigits -= 2; |
| 13320 | buf += 2; |
| 13321 | len -= 2; |
| 13322 | if (sign) |
| 13323 | buf[0] = '-'; |
| 13324 | assert(len == numnondigits + numdigits); |
| 13325 | assert(numdigits > 0); |
| 13326 | } |
| 13327 | |
| 13328 | /* Fill with leading zeroes to meet minimum width. */ |
| 13329 | if (prec > numdigits) { |
| 13330 | PyObject *r1 = PyBytes_FromStringAndSize(NULL, |
| 13331 | numnondigits + prec); |
| 13332 | char *b1; |
| 13333 | if (!r1) { |
| 13334 | Py_DECREF(result); |
| 13335 | return NULL; |
| 13336 | } |
| 13337 | b1 = PyBytes_AS_STRING(r1); |
| 13338 | for (i = 0; i < numnondigits; ++i) |
| 13339 | *b1++ = *buf++; |
| 13340 | for (i = 0; i < prec - numdigits; i++) |
| 13341 | *b1++ = '0'; |
| 13342 | for (i = 0; i < numdigits; i++) |
| 13343 | *b1++ = *buf++; |
| 13344 | *b1 = '\0'; |
| 13345 | Py_DECREF(result); |
| 13346 | result = r1; |
| 13347 | buf = PyBytes_AS_STRING(result); |
| 13348 | len = numnondigits + prec; |
| 13349 | } |
| 13350 | |
| 13351 | /* Fix up case for hex conversions. */ |
| 13352 | if (type == 'X') { |
| 13353 | /* Need to convert all lower case letters to upper case. |
| 13354 | and need to convert 0x to 0X (and -0x to -0X). */ |
| 13355 | for (i = 0; i < len; i++) |
| 13356 | if (buf[i] >= 'a' && buf[i] <= 'x') |
| 13357 | buf[i] -= 'a'-'A'; |
| 13358 | } |
| 13359 | if (!PyUnicode_Check(result) || len != PyUnicode_GET_LENGTH(result)) { |
| 13360 | PyObject *unicode; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13361 | unicode = _PyUnicode_FromASCII(buf, len); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13362 | Py_DECREF(result); |
| 13363 | result = unicode; |
| 13364 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13365 | return result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13366 | } |
| 13367 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13368 | static Py_UCS4 |
| 13369 | formatchar(PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13370 | { |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 13371 | /* presume that the buffer is at least 3 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 13372 | if (PyUnicode_Check(v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13373 | if (PyUnicode_GET_LENGTH(v) == 1) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13374 | return PyUnicode_READ_CHAR(v, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13375 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13376 | goto onError; |
| 13377 | } |
| 13378 | else { |
| 13379 | /* Integer input truncated to a character */ |
| 13380 | long x; |
| 13381 | x = PyLong_AsLong(v); |
| 13382 | if (x == -1 && PyErr_Occurred()) |
| 13383 | goto onError; |
| 13384 | |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 13385 | if (x < 0 || x > MAX_UNICODE) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13386 | PyErr_SetString(PyExc_OverflowError, |
| 13387 | "%c arg not in range(0x110000)"); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13388 | return (Py_UCS4) -1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13389 | } |
| 13390 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13391 | return (Py_UCS4) x; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13392 | } |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 13393 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13394 | onError: |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 13395 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13396 | "%c requires int or char"); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13397 | return (Py_UCS4) -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13398 | } |
| 13399 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 13400 | PyObject * |
| 13401 | PyUnicode_Format(PyObject *format, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13402 | { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13403 | Py_ssize_t fmtcnt, fmtpos, arglen, argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13404 | int args_owned = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13405 | PyObject *dict = NULL; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13406 | PyObject *temp = NULL; |
| 13407 | PyObject *second = NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13408 | PyObject *uformat; |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13409 | void *fmt; |
| 13410 | enum PyUnicode_Kind kind, fmtkind; |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 13411 | _PyUnicodeWriter writer; |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 13412 | Py_ssize_t sublen; |
| 13413 | Py_UCS4 maxchar; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 13414 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13415 | if (format == NULL || args == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13416 | PyErr_BadInternalCall(); |
| 13417 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13418 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13419 | uformat = PyUnicode_FromObject(format); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 13420 | if (uformat == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13421 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 13422 | if (PyUnicode_READY(uformat) == -1) |
| 13423 | Py_DECREF(uformat); |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13424 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13425 | fmt = PyUnicode_DATA(uformat); |
| 13426 | fmtkind = PyUnicode_KIND(uformat); |
| 13427 | fmtcnt = PyUnicode_GET_LENGTH(uformat); |
| 13428 | fmtpos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13429 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13430 | _PyUnicodeWriter_Init(&writer, fmtcnt + 100); |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13431 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13432 | if (PyTuple_Check(args)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13433 | arglen = PyTuple_Size(args); |
| 13434 | argidx = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13435 | } |
| 13436 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13437 | arglen = -1; |
| 13438 | argidx = -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13439 | } |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 13440 | if (Py_TYPE(args)->tp_as_mapping && !PyTuple_Check(args) && |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 13441 | !PyUnicode_Check(args)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13442 | dict = args; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13443 | |
| 13444 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13445 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) != '%') { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13446 | Py_ssize_t nonfmtpos; |
| 13447 | nonfmtpos = fmtpos++; |
| 13448 | while (fmtcnt >= 0 && |
| 13449 | PyUnicode_READ(fmtkind, fmt, fmtpos) != '%') { |
| 13450 | fmtpos++; |
| 13451 | fmtcnt--; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13452 | } |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13453 | if (fmtcnt < 0) |
| 13454 | fmtpos--; |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 13455 | sublen = fmtpos - nonfmtpos; |
| 13456 | maxchar = _PyUnicode_FindMaxChar(uformat, |
| 13457 | nonfmtpos, nonfmtpos + sublen); |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 13458 | if (_PyUnicodeWriter_Prepare(&writer, sublen, maxchar) == -1) |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13459 | goto onError; |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 13460 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13461 | _PyUnicode_FastCopyCharacters(writer.buffer, writer.pos, |
| 13462 | uformat, nonfmtpos, sublen); |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 13463 | writer.pos += sublen; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13464 | } |
| 13465 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13466 | /* Got a format specifier */ |
| 13467 | int flags = 0; |
| 13468 | Py_ssize_t width = -1; |
| 13469 | int prec = -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13470 | Py_UCS4 c = '\0'; |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13471 | Py_UCS4 fill; |
| 13472 | int sign; |
| 13473 | Py_UCS4 signchar; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13474 | int isnumok; |
| 13475 | PyObject *v = NULL; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13476 | void *pbuf = NULL; |
| 13477 | Py_ssize_t pindex, len; |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 13478 | Py_UCS4 bufmaxchar; |
| 13479 | Py_ssize_t buflen; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13480 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13481 | fmtpos++; |
Victor Stinner | 438106b | 2012-05-02 00:41:57 +0200 | [diff] [blame] | 13482 | c = PyUnicode_READ(fmtkind, fmt, fmtpos); |
| 13483 | if (c == '(') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13484 | Py_ssize_t keystart; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13485 | Py_ssize_t keylen; |
| 13486 | PyObject *key; |
| 13487 | int pcount = 1; |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 13488 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13489 | if (dict == NULL) { |
| 13490 | PyErr_SetString(PyExc_TypeError, |
| 13491 | "format requires a mapping"); |
| 13492 | goto onError; |
| 13493 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13494 | ++fmtpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13495 | --fmtcnt; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13496 | keystart = fmtpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13497 | /* Skip over balanced parentheses */ |
| 13498 | while (pcount > 0 && --fmtcnt >= 0) { |
Victor Stinner | bff7c96 | 2012-05-03 01:44:59 +0200 | [diff] [blame] | 13499 | c = PyUnicode_READ(fmtkind, fmt, fmtpos); |
| 13500 | if (c == ')') |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13501 | --pcount; |
Victor Stinner | bff7c96 | 2012-05-03 01:44:59 +0200 | [diff] [blame] | 13502 | else if (c == '(') |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13503 | ++pcount; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13504 | fmtpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13505 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13506 | keylen = fmtpos - keystart - 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13507 | if (fmtcnt < 0 || pcount > 0) { |
| 13508 | PyErr_SetString(PyExc_ValueError, |
| 13509 | "incomplete format key"); |
| 13510 | goto onError; |
| 13511 | } |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 13512 | key = PyUnicode_Substring(uformat, |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 13513 | keystart, keystart + keylen); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13514 | if (key == NULL) |
| 13515 | goto onError; |
| 13516 | if (args_owned) { |
| 13517 | Py_DECREF(args); |
| 13518 | args_owned = 0; |
| 13519 | } |
| 13520 | args = PyObject_GetItem(dict, key); |
| 13521 | Py_DECREF(key); |
| 13522 | if (args == NULL) { |
| 13523 | goto onError; |
| 13524 | } |
| 13525 | args_owned = 1; |
| 13526 | arglen = -1; |
| 13527 | argidx = -2; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13528 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13529 | while (--fmtcnt >= 0) { |
Victor Stinner | 438106b | 2012-05-02 00:41:57 +0200 | [diff] [blame] | 13530 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
| 13531 | switch (c) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13532 | case '-': flags |= F_LJUST; continue; |
| 13533 | case '+': flags |= F_SIGN; continue; |
| 13534 | case ' ': flags |= F_BLANK; continue; |
| 13535 | case '#': flags |= F_ALT; continue; |
| 13536 | case '0': flags |= F_ZERO; continue; |
| 13537 | } |
| 13538 | break; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13539 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13540 | if (c == '*') { |
| 13541 | v = getnextarg(args, arglen, &argidx); |
| 13542 | if (v == NULL) |
| 13543 | goto onError; |
| 13544 | if (!PyLong_Check(v)) { |
| 13545 | PyErr_SetString(PyExc_TypeError, |
| 13546 | "* wants int"); |
| 13547 | goto onError; |
| 13548 | } |
| 13549 | width = PyLong_AsLong(v); |
| 13550 | if (width == -1 && PyErr_Occurred()) |
| 13551 | goto onError; |
| 13552 | if (width < 0) { |
| 13553 | flags |= F_LJUST; |
| 13554 | width = -width; |
| 13555 | } |
| 13556 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13557 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13558 | } |
| 13559 | else if (c >= '0' && c <= '9') { |
| 13560 | width = c - '0'; |
| 13561 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13562 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13563 | if (c < '0' || c > '9') |
| 13564 | break; |
Martin v. Löwis | b05c073 | 2012-05-15 13:45:49 +0200 | [diff] [blame] | 13565 | /* Since c is unsigned, the RHS would end up as unsigned, |
| 13566 | mixing signed and unsigned comparison. Since c is between |
| 13567 | '0' and '9', casting to int is safe. */ |
| 13568 | if (width > (PY_SSIZE_T_MAX - ((int)c - '0')) / 10) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13569 | PyErr_SetString(PyExc_ValueError, |
| 13570 | "width too big"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13571 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13572 | } |
| 13573 | width = width*10 + (c - '0'); |
| 13574 | } |
| 13575 | } |
| 13576 | if (c == '.') { |
| 13577 | prec = 0; |
| 13578 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13579 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13580 | if (c == '*') { |
| 13581 | v = getnextarg(args, arglen, &argidx); |
| 13582 | if (v == NULL) |
| 13583 | goto onError; |
| 13584 | if (!PyLong_Check(v)) { |
| 13585 | PyErr_SetString(PyExc_TypeError, |
| 13586 | "* wants int"); |
| 13587 | goto onError; |
| 13588 | } |
| 13589 | prec = PyLong_AsLong(v); |
| 13590 | if (prec == -1 && PyErr_Occurred()) |
| 13591 | goto onError; |
| 13592 | if (prec < 0) |
| 13593 | prec = 0; |
| 13594 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13595 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13596 | } |
| 13597 | else if (c >= '0' && c <= '9') { |
| 13598 | prec = c - '0'; |
| 13599 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13600 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13601 | if (c < '0' || c > '9') |
| 13602 | break; |
Martin v. Löwis | b05c073 | 2012-05-15 13:45:49 +0200 | [diff] [blame] | 13603 | if (prec > (INT_MAX - ((int)c - '0')) / 10) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13604 | PyErr_SetString(PyExc_ValueError, |
| 13605 | "prec too big"); |
| 13606 | goto onError; |
| 13607 | } |
| 13608 | prec = prec*10 + (c - '0'); |
| 13609 | } |
| 13610 | } |
| 13611 | } /* prec */ |
| 13612 | if (fmtcnt >= 0) { |
| 13613 | if (c == 'h' || c == 'l' || c == 'L') { |
| 13614 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13615 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13616 | } |
| 13617 | } |
| 13618 | if (fmtcnt < 0) { |
| 13619 | PyErr_SetString(PyExc_ValueError, |
| 13620 | "incomplete format"); |
| 13621 | goto onError; |
| 13622 | } |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13623 | if (fmtcnt == 0) |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 13624 | writer.overallocate = 0; |
Victor Stinner | aff3cc6 | 2012-04-30 05:19:21 +0200 | [diff] [blame] | 13625 | |
| 13626 | if (c == '%') { |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 13627 | if (_PyUnicodeWriter_Prepare(&writer, 1, '%') == -1) |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13628 | goto onError; |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 13629 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, '%'); |
| 13630 | writer.pos += 1; |
Victor Stinner | aff3cc6 | 2012-04-30 05:19:21 +0200 | [diff] [blame] | 13631 | continue; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13632 | } |
Victor Stinner | aff3cc6 | 2012-04-30 05:19:21 +0200 | [diff] [blame] | 13633 | |
Victor Stinner | aff3cc6 | 2012-04-30 05:19:21 +0200 | [diff] [blame] | 13634 | v = getnextarg(args, arglen, &argidx); |
| 13635 | if (v == NULL) |
| 13636 | goto onError; |
| 13637 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13638 | sign = 0; |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13639 | signchar = '\0'; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13640 | fill = ' '; |
| 13641 | switch (c) { |
| 13642 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13643 | case 's': |
| 13644 | case 'r': |
| 13645 | case 'a': |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13646 | if (PyLong_CheckExact(v) && width == -1 && prec == -1) { |
| 13647 | /* Fast path */ |
| 13648 | if (_PyLong_FormatWriter(&writer, v, 10, flags & F_ALT) == -1) |
| 13649 | goto onError; |
| 13650 | goto nextarg; |
| 13651 | } |
| 13652 | |
Victor Stinner | 808fc0a | 2010-03-22 12:50:40 +0000 | [diff] [blame] | 13653 | if (PyUnicode_CheckExact(v) && c == 's') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13654 | temp = v; |
| 13655 | Py_INCREF(temp); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13656 | } |
| 13657 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13658 | if (c == 's') |
| 13659 | temp = PyObject_Str(v); |
| 13660 | else if (c == 'r') |
| 13661 | temp = PyObject_Repr(v); |
| 13662 | else |
| 13663 | temp = PyObject_ASCII(v); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13664 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13665 | break; |
| 13666 | |
| 13667 | case 'i': |
| 13668 | case 'd': |
| 13669 | case 'u': |
| 13670 | case 'o': |
| 13671 | case 'x': |
| 13672 | case 'X': |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13673 | if (PyLong_CheckExact(v) |
| 13674 | && width == -1 && prec == -1 |
| 13675 | && !(flags & (F_SIGN | F_BLANK))) |
| 13676 | { |
| 13677 | /* Fast path */ |
| 13678 | switch(c) |
| 13679 | { |
| 13680 | case 'd': |
| 13681 | case 'i': |
| 13682 | case 'u': |
| 13683 | if (_PyLong_FormatWriter(&writer, v, 10, flags & F_ALT) == -1) |
| 13684 | goto onError; |
| 13685 | goto nextarg; |
| 13686 | case 'x': |
| 13687 | if (_PyLong_FormatWriter(&writer, v, 16, flags & F_ALT) == -1) |
| 13688 | goto onError; |
| 13689 | goto nextarg; |
| 13690 | case 'o': |
| 13691 | if (_PyLong_FormatWriter(&writer, v, 8, flags & F_ALT) == -1) |
| 13692 | goto onError; |
| 13693 | goto nextarg; |
| 13694 | default: |
| 13695 | break; |
| 13696 | } |
| 13697 | } |
| 13698 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13699 | isnumok = 0; |
| 13700 | if (PyNumber_Check(v)) { |
| 13701 | PyObject *iobj=NULL; |
| 13702 | |
| 13703 | if (PyLong_Check(v)) { |
| 13704 | iobj = v; |
| 13705 | Py_INCREF(iobj); |
| 13706 | } |
| 13707 | else { |
| 13708 | iobj = PyNumber_Long(v); |
| 13709 | } |
| 13710 | if (iobj!=NULL) { |
| 13711 | if (PyLong_Check(iobj)) { |
| 13712 | isnumok = 1; |
Victor Stinner | aff3cc6 | 2012-04-30 05:19:21 +0200 | [diff] [blame] | 13713 | sign = 1; |
Senthil Kumaran | 9ebe08d | 2011-07-03 21:03:16 -0700 | [diff] [blame] | 13714 | temp = formatlong(iobj, flags, prec, (c == 'i'? 'd': c)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13715 | Py_DECREF(iobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13716 | } |
| 13717 | else { |
| 13718 | Py_DECREF(iobj); |
| 13719 | } |
| 13720 | } |
| 13721 | } |
| 13722 | if (!isnumok) { |
| 13723 | PyErr_Format(PyExc_TypeError, |
| 13724 | "%%%c format: a number is required, " |
| 13725 | "not %.200s", (char)c, Py_TYPE(v)->tp_name); |
| 13726 | goto onError; |
| 13727 | } |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13728 | if (flags & F_ZERO) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13729 | fill = '0'; |
| 13730 | break; |
| 13731 | |
| 13732 | case 'e': |
| 13733 | case 'E': |
| 13734 | case 'f': |
| 13735 | case 'F': |
| 13736 | case 'g': |
| 13737 | case 'G': |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13738 | if (width == -1 && prec == -1 |
| 13739 | && !(flags & (F_SIGN | F_BLANK))) |
| 13740 | { |
| 13741 | /* Fast path */ |
| 13742 | if (formatfloat(v, flags, prec, c, NULL, &writer) == -1) |
| 13743 | goto onError; |
| 13744 | goto nextarg; |
| 13745 | } |
| 13746 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13747 | sign = 1; |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13748 | if (flags & F_ZERO) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13749 | fill = '0'; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13750 | if (formatfloat(v, flags, prec, c, &temp, NULL) == -1) |
| 13751 | temp = NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13752 | break; |
| 13753 | |
| 13754 | case 'c': |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13755 | { |
| 13756 | Py_UCS4 ch = formatchar(v); |
| 13757 | if (ch == (Py_UCS4) -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13758 | goto onError; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13759 | if (width == -1 && prec == -1) { |
| 13760 | /* Fast path */ |
| 13761 | if (_PyUnicodeWriter_Prepare(&writer, 1, ch) == -1) |
| 13762 | goto onError; |
| 13763 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, ch); |
| 13764 | writer.pos += 1; |
| 13765 | goto nextarg; |
| 13766 | } |
Victor Stinner | b5c3ea3 | 2012-05-02 00:29:36 +0200 | [diff] [blame] | 13767 | temp = PyUnicode_FromOrdinal(ch); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13768 | break; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13769 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13770 | |
| 13771 | default: |
| 13772 | PyErr_Format(PyExc_ValueError, |
| 13773 | "unsupported format character '%c' (0x%x) " |
| 13774 | "at index %zd", |
| 13775 | (31<=c && c<=126) ? (char)c : '?', |
| 13776 | (int)c, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13777 | fmtpos - 1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13778 | goto onError; |
| 13779 | } |
Victor Stinner | aff3cc6 | 2012-04-30 05:19:21 +0200 | [diff] [blame] | 13780 | if (temp == NULL) |
| 13781 | goto onError; |
| 13782 | assert (PyUnicode_Check(temp)); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13783 | |
| 13784 | if (width == -1 && prec == -1 |
| 13785 | && !(flags & (F_SIGN | F_BLANK))) |
| 13786 | { |
| 13787 | /* Fast path */ |
| 13788 | if (_PyUnicodeWriter_WriteStr(&writer, temp) == -1) |
| 13789 | goto onError; |
| 13790 | goto nextarg; |
| 13791 | } |
| 13792 | |
Victor Stinner | aff3cc6 | 2012-04-30 05:19:21 +0200 | [diff] [blame] | 13793 | if (PyUnicode_READY(temp) == -1) { |
| 13794 | Py_CLEAR(temp); |
| 13795 | goto onError; |
| 13796 | } |
| 13797 | kind = PyUnicode_KIND(temp); |
| 13798 | pbuf = PyUnicode_DATA(temp); |
| 13799 | len = PyUnicode_GET_LENGTH(temp); |
| 13800 | |
| 13801 | if (c == 's' || c == 'r' || c == 'a') { |
| 13802 | if (prec >= 0 && len > prec) |
| 13803 | len = prec; |
| 13804 | } |
| 13805 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13806 | /* pbuf is initialized here. */ |
| 13807 | pindex = 0; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13808 | if (sign) { |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13809 | Py_UCS4 ch = PyUnicode_READ(kind, pbuf, pindex); |
| 13810 | if (ch == '-' || ch == '+') { |
| 13811 | signchar = ch; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13812 | len--; |
| 13813 | pindex++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13814 | } |
| 13815 | else if (flags & F_SIGN) |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13816 | signchar = '+'; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13817 | else if (flags & F_BLANK) |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13818 | signchar = ' '; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13819 | else |
| 13820 | sign = 0; |
| 13821 | } |
| 13822 | if (width < len) |
| 13823 | width = len; |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 13824 | |
| 13825 | /* Compute the length and maximum character of the |
| 13826 | written characters */ |
| 13827 | bufmaxchar = 127; |
| 13828 | if (!(flags & F_LJUST)) { |
| 13829 | if (sign) { |
| 13830 | if ((width-1) > len) |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13831 | bufmaxchar = MAX_MAXCHAR(bufmaxchar, fill); |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 13832 | } |
| 13833 | else { |
| 13834 | if (width > len) |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13835 | bufmaxchar = MAX_MAXCHAR(bufmaxchar, fill); |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 13836 | } |
| 13837 | } |
| 13838 | maxchar = _PyUnicode_FindMaxChar(temp, 0, pindex+len); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13839 | bufmaxchar = MAX_MAXCHAR(bufmaxchar, maxchar); |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 13840 | |
| 13841 | buflen = width; |
| 13842 | if (sign && len == width) |
| 13843 | buflen++; |
| 13844 | |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 13845 | if (_PyUnicodeWriter_Prepare(&writer, buflen, bufmaxchar) == -1) |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 13846 | goto onError; |
| 13847 | |
| 13848 | /* Write characters */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13849 | if (sign) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13850 | if (fill != ' ') { |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 13851 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, signchar); |
| 13852 | writer.pos += 1; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13853 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13854 | if (width > len) |
| 13855 | width--; |
| 13856 | } |
| 13857 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13858 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13859 | assert(PyUnicode_READ(kind, pbuf, pindex + 1) == c); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13860 | if (fill != ' ') { |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13861 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, '0'); |
| 13862 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos+1, c); |
| 13863 | writer.pos += 2; |
| 13864 | pindex += 2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13865 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13866 | width -= 2; |
| 13867 | if (width < 0) |
| 13868 | width = 0; |
| 13869 | len -= 2; |
| 13870 | } |
| 13871 | if (width > len && !(flags & F_LJUST)) { |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13872 | sublen = width - len; |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13873 | FILL(writer.kind, writer.data, fill, writer.pos, sublen); |
| 13874 | writer.pos += sublen; |
Antoine Pitrou | 978b9d2 | 2011-10-07 12:35:48 +0200 | [diff] [blame] | 13875 | width = len; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13876 | } |
| 13877 | if (fill == ' ') { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13878 | if (sign) { |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 13879 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, signchar); |
| 13880 | writer.pos += 1; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13881 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13882 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13883 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
| 13884 | assert(PyUnicode_READ(kind, pbuf, pindex+1) == c); |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13885 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, '0'); |
| 13886 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos+1, c); |
| 13887 | writer.pos += 2; |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13888 | pindex += 2; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13889 | } |
| 13890 | } |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13891 | |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 13892 | if (len) { |
| 13893 | _PyUnicode_FastCopyCharacters(writer.buffer, writer.pos, |
| 13894 | temp, pindex, len); |
| 13895 | writer.pos += len; |
| 13896 | } |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13897 | if (width > len) { |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 13898 | sublen = width - len; |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13899 | FILL(writer.kind, writer.data, ' ', writer.pos, sublen); |
| 13900 | writer.pos += sublen; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13901 | } |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 13902 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13903 | nextarg: |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13904 | if (dict && (argidx < arglen) && c != '%') { |
| 13905 | PyErr_SetString(PyExc_TypeError, |
| 13906 | "not all arguments converted during string formatting"); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13907 | goto onError; |
| 13908 | } |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13909 | Py_CLEAR(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13910 | } /* '%' */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13911 | } /* until end */ |
| 13912 | if (argidx < arglen && !dict) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13913 | PyErr_SetString(PyExc_TypeError, |
| 13914 | "not all arguments converted during string formatting"); |
| 13915 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13916 | } |
| 13917 | |
| 13918 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13919 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13920 | } |
| 13921 | Py_DECREF(uformat); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13922 | Py_XDECREF(temp); |
| 13923 | Py_XDECREF(second); |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 13924 | return _PyUnicodeWriter_Finish(&writer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13925 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13926 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13927 | Py_DECREF(uformat); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13928 | Py_XDECREF(temp); |
| 13929 | Py_XDECREF(second); |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 13930 | _PyUnicodeWriter_Dealloc(&writer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 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 | return NULL; |
| 13935 | } |
| 13936 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 13937 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 13938 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 13939 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13940 | static PyObject * |
| 13941 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 13942 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13943 | PyObject *x = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13944 | static char *kwlist[] = {"object", "encoding", "errors", 0}; |
| 13945 | char *encoding = NULL; |
| 13946 | char *errors = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13947 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13948 | if (type != &PyUnicode_Type) |
| 13949 | return unicode_subtype_new(type, args, kwds); |
| 13950 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:str", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13951 | kwlist, &x, &encoding, &errors)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13952 | return NULL; |
Victor Stinner | 382955f | 2011-12-11 21:44:00 +0100 | [diff] [blame] | 13953 | if (x == NULL) { |
| 13954 | Py_INCREF(unicode_empty); |
| 13955 | return unicode_empty; |
| 13956 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13957 | if (encoding == NULL && errors == NULL) |
| 13958 | return PyObject_Str(x); |
| 13959 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13960 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13961 | } |
| 13962 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 13963 | static PyObject * |
| 13964 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 13965 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13966 | PyObject *unicode, *self; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13967 | Py_ssize_t length, char_size; |
| 13968 | int share_wstr, share_utf8; |
| 13969 | unsigned int kind; |
| 13970 | void *data; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 13971 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13972 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13973 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13974 | unicode = unicode_new(&PyUnicode_Type, args, kwds); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13975 | if (unicode == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13976 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 13977 | assert(_PyUnicode_CHECK(unicode)); |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 13978 | if (PyUnicode_READY(unicode) == -1) { |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 13979 | Py_DECREF(unicode); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13980 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 13981 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13982 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13983 | self = type->tp_alloc(type, 0); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13984 | if (self == NULL) { |
| 13985 | Py_DECREF(unicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13986 | return NULL; |
| 13987 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13988 | kind = PyUnicode_KIND(unicode); |
| 13989 | length = PyUnicode_GET_LENGTH(unicode); |
| 13990 | |
| 13991 | _PyUnicode_LENGTH(self) = length; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 13992 | #ifdef Py_DEBUG |
| 13993 | _PyUnicode_HASH(self) = -1; |
| 13994 | #else |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13995 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 13996 | #endif |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13997 | _PyUnicode_STATE(self).interned = 0; |
| 13998 | _PyUnicode_STATE(self).kind = kind; |
| 13999 | _PyUnicode_STATE(self).compact = 0; |
Victor Stinner | 3cf4637 | 2011-10-03 14:42:15 +0200 | [diff] [blame] | 14000 | _PyUnicode_STATE(self).ascii = _PyUnicode_STATE(unicode).ascii; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14001 | _PyUnicode_STATE(self).ready = 1; |
| 14002 | _PyUnicode_WSTR(self) = NULL; |
| 14003 | _PyUnicode_UTF8_LENGTH(self) = 0; |
| 14004 | _PyUnicode_UTF8(self) = NULL; |
| 14005 | _PyUnicode_WSTR_LENGTH(self) = 0; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 14006 | _PyUnicode_DATA_ANY(self) = NULL; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14007 | |
| 14008 | share_utf8 = 0; |
| 14009 | share_wstr = 0; |
| 14010 | if (kind == PyUnicode_1BYTE_KIND) { |
| 14011 | char_size = 1; |
| 14012 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) |
| 14013 | share_utf8 = 1; |
| 14014 | } |
| 14015 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 14016 | char_size = 2; |
| 14017 | if (sizeof(wchar_t) == 2) |
| 14018 | share_wstr = 1; |
| 14019 | } |
| 14020 | else { |
| 14021 | assert(kind == PyUnicode_4BYTE_KIND); |
| 14022 | char_size = 4; |
| 14023 | if (sizeof(wchar_t) == 4) |
| 14024 | share_wstr = 1; |
| 14025 | } |
| 14026 | |
| 14027 | /* Ensure we won't overflow the length. */ |
| 14028 | if (length > (PY_SSIZE_T_MAX / char_size - 1)) { |
| 14029 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14030 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14031 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14032 | data = PyObject_MALLOC((length + 1) * char_size); |
| 14033 | if (data == NULL) { |
| 14034 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14035 | goto onError; |
| 14036 | } |
| 14037 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 14038 | _PyUnicode_DATA_ANY(self) = data; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14039 | if (share_utf8) { |
| 14040 | _PyUnicode_UTF8_LENGTH(self) = length; |
| 14041 | _PyUnicode_UTF8(self) = data; |
| 14042 | } |
| 14043 | if (share_wstr) { |
| 14044 | _PyUnicode_WSTR_LENGTH(self) = length; |
| 14045 | _PyUnicode_WSTR(self) = (wchar_t *)data; |
| 14046 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14047 | |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14048 | Py_MEMCPY(data, PyUnicode_DATA(unicode), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 14049 | kind * (length + 1)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 14050 | assert(_PyUnicode_CheckConsistency(self, 1)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 14051 | #ifdef Py_DEBUG |
| 14052 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
| 14053 | #endif |
Victor Stinner | dd18d3a | 2011-10-22 11:08:10 +0200 | [diff] [blame] | 14054 | Py_DECREF(unicode); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 14055 | return self; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14056 | |
| 14057 | onError: |
| 14058 | Py_DECREF(unicode); |
| 14059 | Py_DECREF(self); |
| 14060 | return NULL; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14061 | } |
| 14062 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 14063 | PyDoc_STRVAR(unicode_doc, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14064 | "str(string[, encoding[, errors]]) -> str\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14065 | \n\ |
Collin Winter | d474ce8 | 2007-08-07 19:42:11 +0000 | [diff] [blame] | 14066 | Create a new string object from the given encoded string.\n\ |
Skip Montanaro | 35b37a5 | 2002-07-26 16:22:46 +0000 | [diff] [blame] | 14067 | encoding defaults to the current default string encoding.\n\ |
| 14068 | errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14069 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14070 | static PyObject *unicode_iter(PyObject *seq); |
| 14071 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14072 | PyTypeObject PyUnicode_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 14073 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14074 | "str", /* tp_name */ |
| 14075 | sizeof(PyUnicodeObject), /* tp_size */ |
| 14076 | 0, /* tp_itemsize */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14077 | /* Slots */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14078 | (destructor)unicode_dealloc, /* tp_dealloc */ |
| 14079 | 0, /* tp_print */ |
| 14080 | 0, /* tp_getattr */ |
| 14081 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 14082 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14083 | unicode_repr, /* tp_repr */ |
| 14084 | &unicode_as_number, /* tp_as_number */ |
| 14085 | &unicode_as_sequence, /* tp_as_sequence */ |
| 14086 | &unicode_as_mapping, /* tp_as_mapping */ |
| 14087 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 14088 | 0, /* tp_call*/ |
| 14089 | (reprfunc) unicode_str, /* tp_str */ |
| 14090 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 14091 | 0, /* tp_setattro */ |
| 14092 | 0, /* tp_as_buffer */ |
| 14093 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14094 | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14095 | unicode_doc, /* tp_doc */ |
| 14096 | 0, /* tp_traverse */ |
| 14097 | 0, /* tp_clear */ |
| 14098 | PyUnicode_RichCompare, /* tp_richcompare */ |
| 14099 | 0, /* tp_weaklistoffset */ |
| 14100 | unicode_iter, /* tp_iter */ |
| 14101 | 0, /* tp_iternext */ |
| 14102 | unicode_methods, /* tp_methods */ |
| 14103 | 0, /* tp_members */ |
| 14104 | 0, /* tp_getset */ |
| 14105 | &PyBaseObject_Type, /* tp_base */ |
| 14106 | 0, /* tp_dict */ |
| 14107 | 0, /* tp_descr_get */ |
| 14108 | 0, /* tp_descr_set */ |
| 14109 | 0, /* tp_dictoffset */ |
| 14110 | 0, /* tp_init */ |
| 14111 | 0, /* tp_alloc */ |
| 14112 | unicode_new, /* tp_new */ |
| 14113 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14114 | }; |
| 14115 | |
| 14116 | /* Initialize the Unicode implementation */ |
| 14117 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 14118 | int _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14119 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 14120 | int i; |
| 14121 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14122 | /* XXX - move this array to unicodectype.c ? */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14123 | Py_UCS2 linebreak[] = { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14124 | 0x000A, /* LINE FEED */ |
| 14125 | 0x000D, /* CARRIAGE RETURN */ |
| 14126 | 0x001C, /* FILE SEPARATOR */ |
| 14127 | 0x001D, /* GROUP SEPARATOR */ |
| 14128 | 0x001E, /* RECORD SEPARATOR */ |
| 14129 | 0x0085, /* NEXT LINE */ |
| 14130 | 0x2028, /* LINE SEPARATOR */ |
| 14131 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 14132 | }; |
| 14133 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 14134 | /* Init the implementation */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 14135 | unicode_empty = PyUnicode_New(0, 0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 14136 | if (!unicode_empty) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14137 | Py_FatalError("Can't create empty string"); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 14138 | assert(_PyUnicode_CheckConsistency(unicode_empty, 1)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 14139 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 14140 | for (i = 0; i < 256; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14141 | unicode_latin1[i] = NULL; |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 14142 | if (PyType_Ready(&PyUnicode_Type) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14143 | Py_FatalError("Can't initialize 'unicode'"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14144 | |
| 14145 | /* initialize the linebreak bloom filter */ |
| 14146 | bloom_linebreak = make_bloom_mask( |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14147 | PyUnicode_2BYTE_KIND, linebreak, |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 14148 | Py_ARRAY_LENGTH(linebreak)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 14149 | |
| 14150 | PyType_Ready(&EncodingMapType); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 14151 | |
| 14152 | #ifdef HAVE_MBCS |
| 14153 | winver.dwOSVersionInfoSize = sizeof(winver); |
| 14154 | if (!GetVersionEx((OSVERSIONINFO*)&winver)) { |
| 14155 | PyErr_SetFromWindowsErr(0); |
| 14156 | return -1; |
| 14157 | } |
| 14158 | #endif |
| 14159 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14160 | } |
| 14161 | |
| 14162 | /* Finalize the Unicode implementation */ |
| 14163 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 14164 | int |
| 14165 | PyUnicode_ClearFreeList(void) |
| 14166 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14167 | return 0; |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 14168 | } |
| 14169 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14170 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 14171 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14172 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 14173 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14174 | |
Guido van Rossum | 4ae8ef8 | 2000-10-03 18:09:04 +0000 | [diff] [blame] | 14175 | Py_XDECREF(unicode_empty); |
| 14176 | unicode_empty = NULL; |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 14177 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 14178 | for (i = 0; i < 256; i++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14179 | if (unicode_latin1[i]) { |
| 14180 | Py_DECREF(unicode_latin1[i]); |
| 14181 | unicode_latin1[i] = NULL; |
| 14182 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 14183 | } |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 14184 | _PyUnicode_ClearStaticStrings(); |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 14185 | (void)PyUnicode_ClearFreeList(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14186 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 14187 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14188 | void |
| 14189 | PyUnicode_InternInPlace(PyObject **p) |
| 14190 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14191 | register PyObject *s = *p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14192 | PyObject *t; |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 14193 | #ifdef Py_DEBUG |
| 14194 | assert(s != NULL); |
| 14195 | assert(_PyUnicode_CHECK(s)); |
| 14196 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14197 | if (s == NULL || !PyUnicode_Check(s)) |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 14198 | return; |
| 14199 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14200 | /* If it's a subclass, we don't really know what putting |
| 14201 | it in the interned dict might do. */ |
| 14202 | if (!PyUnicode_CheckExact(s)) |
| 14203 | return; |
| 14204 | if (PyUnicode_CHECK_INTERNED(s)) |
| 14205 | return; |
| 14206 | if (interned == NULL) { |
| 14207 | interned = PyDict_New(); |
| 14208 | if (interned == NULL) { |
| 14209 | PyErr_Clear(); /* Don't leave an exception */ |
| 14210 | return; |
| 14211 | } |
| 14212 | } |
| 14213 | /* It might be that the GetItem call fails even |
| 14214 | though the key is present in the dictionary, |
| 14215 | namely when this happens during a stack overflow. */ |
| 14216 | Py_ALLOW_RECURSION |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 14217 | t = PyDict_GetItem(interned, s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14218 | Py_END_ALLOW_RECURSION |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 14219 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14220 | if (t) { |
| 14221 | Py_INCREF(t); |
| 14222 | Py_DECREF(*p); |
| 14223 | *p = t; |
| 14224 | return; |
| 14225 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14226 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14227 | PyThreadState_GET()->recursion_critical = 1; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 14228 | if (PyDict_SetItem(interned, s, s) < 0) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14229 | PyErr_Clear(); |
| 14230 | PyThreadState_GET()->recursion_critical = 0; |
| 14231 | return; |
| 14232 | } |
| 14233 | PyThreadState_GET()->recursion_critical = 0; |
| 14234 | /* The two references in interned are not counted by refcnt. |
| 14235 | The deallocator will take care of this */ |
| 14236 | Py_REFCNT(s) -= 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14237 | _PyUnicode_STATE(s).interned = SSTATE_INTERNED_MORTAL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14238 | } |
| 14239 | |
| 14240 | void |
| 14241 | PyUnicode_InternImmortal(PyObject **p) |
| 14242 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14243 | PyUnicode_InternInPlace(p); |
| 14244 | if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { |
Victor Stinner | af9e4b8 | 2011-10-23 20:07:00 +0200 | [diff] [blame] | 14245 | _PyUnicode_STATE(*p).interned = SSTATE_INTERNED_IMMORTAL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14246 | Py_INCREF(*p); |
| 14247 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14248 | } |
| 14249 | |
| 14250 | PyObject * |
| 14251 | PyUnicode_InternFromString(const char *cp) |
| 14252 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14253 | PyObject *s = PyUnicode_FromString(cp); |
| 14254 | if (s == NULL) |
| 14255 | return NULL; |
| 14256 | PyUnicode_InternInPlace(&s); |
| 14257 | return s; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14258 | } |
| 14259 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 14260 | void |
| 14261 | _Py_ReleaseInternedUnicodeStrings(void) |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14262 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14263 | PyObject *keys; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14264 | PyObject *s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14265 | Py_ssize_t i, n; |
| 14266 | Py_ssize_t immortal_size = 0, mortal_size = 0; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14267 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14268 | if (interned == NULL || !PyDict_Check(interned)) |
| 14269 | return; |
| 14270 | keys = PyDict_Keys(interned); |
| 14271 | if (keys == NULL || !PyList_Check(keys)) { |
| 14272 | PyErr_Clear(); |
| 14273 | return; |
| 14274 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14275 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14276 | /* Since _Py_ReleaseInternedUnicodeStrings() is intended to help a leak |
| 14277 | detector, interned unicode strings are not forcibly deallocated; |
| 14278 | rather, we give them their stolen references back, and then clear |
| 14279 | and DECREF the interned dict. */ |
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 | n = PyList_GET_SIZE(keys); |
| 14282 | fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14283 | n); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14284 | for (i = 0; i < n; i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14285 | s = PyList_GET_ITEM(keys, i); |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 14286 | if (PyUnicode_READY(s) == -1) { |
| 14287 | assert(0 && "could not ready string"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14288 | fprintf(stderr, "could not ready string\n"); |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 14289 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14290 | switch (PyUnicode_CHECK_INTERNED(s)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14291 | case SSTATE_NOT_INTERNED: |
| 14292 | /* XXX Shouldn't happen */ |
| 14293 | break; |
| 14294 | case SSTATE_INTERNED_IMMORTAL: |
| 14295 | Py_REFCNT(s) += 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14296 | immortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14297 | break; |
| 14298 | case SSTATE_INTERNED_MORTAL: |
| 14299 | Py_REFCNT(s) += 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14300 | mortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14301 | break; |
| 14302 | default: |
| 14303 | Py_FatalError("Inconsistent interned string state."); |
| 14304 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14305 | _PyUnicode_STATE(s).interned = SSTATE_NOT_INTERNED; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14306 | } |
| 14307 | fprintf(stderr, "total size of all interned strings: " |
| 14308 | "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " |
| 14309 | "mortal/immortal\n", mortal_size, immortal_size); |
| 14310 | Py_DECREF(keys); |
| 14311 | PyDict_Clear(interned); |
| 14312 | Py_DECREF(interned); |
| 14313 | interned = NULL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14314 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14315 | |
| 14316 | |
| 14317 | /********************* Unicode Iterator **************************/ |
| 14318 | |
| 14319 | typedef struct { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14320 | PyObject_HEAD |
| 14321 | Py_ssize_t it_index; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14322 | PyObject *it_seq; /* Set to NULL when iterator is exhausted */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14323 | } unicodeiterobject; |
| 14324 | |
| 14325 | static void |
| 14326 | unicodeiter_dealloc(unicodeiterobject *it) |
| 14327 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14328 | _PyObject_GC_UNTRACK(it); |
| 14329 | Py_XDECREF(it->it_seq); |
| 14330 | PyObject_GC_Del(it); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14331 | } |
| 14332 | |
| 14333 | static int |
| 14334 | unicodeiter_traverse(unicodeiterobject *it, visitproc visit, void *arg) |
| 14335 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14336 | Py_VISIT(it->it_seq); |
| 14337 | return 0; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14338 | } |
| 14339 | |
| 14340 | static PyObject * |
| 14341 | unicodeiter_next(unicodeiterobject *it) |
| 14342 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14343 | PyObject *seq, *item; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14344 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14345 | assert(it != NULL); |
| 14346 | seq = it->it_seq; |
| 14347 | if (seq == NULL) |
| 14348 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 14349 | assert(_PyUnicode_CHECK(seq)); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14350 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14351 | if (it->it_index < PyUnicode_GET_LENGTH(seq)) { |
| 14352 | int kind = PyUnicode_KIND(seq); |
| 14353 | void *data = PyUnicode_DATA(seq); |
| 14354 | Py_UCS4 chr = PyUnicode_READ(kind, data, it->it_index); |
| 14355 | item = PyUnicode_FromOrdinal(chr); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14356 | if (item != NULL) |
| 14357 | ++it->it_index; |
| 14358 | return item; |
| 14359 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14360 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14361 | Py_DECREF(seq); |
| 14362 | it->it_seq = NULL; |
| 14363 | return NULL; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14364 | } |
| 14365 | |
| 14366 | static PyObject * |
| 14367 | unicodeiter_len(unicodeiterobject *it) |
| 14368 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14369 | Py_ssize_t len = 0; |
| 14370 | if (it->it_seq) |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 14371 | len = PyUnicode_GET_LENGTH(it->it_seq) - it->it_index; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14372 | return PyLong_FromSsize_t(len); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14373 | } |
| 14374 | |
| 14375 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
| 14376 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 14377 | static PyObject * |
| 14378 | unicodeiter_reduce(unicodeiterobject *it) |
| 14379 | { |
| 14380 | if (it->it_seq != NULL) { |
Antoine Pitrou | a701388 | 2012-04-05 00:04:20 +0200 | [diff] [blame] | 14381 | return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("iter"), |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 14382 | it->it_seq, it->it_index); |
| 14383 | } else { |
| 14384 | PyObject *u = PyUnicode_FromUnicode(NULL, 0); |
| 14385 | if (u == NULL) |
| 14386 | return NULL; |
Antoine Pitrou | a701388 | 2012-04-05 00:04:20 +0200 | [diff] [blame] | 14387 | return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), u); |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 14388 | } |
| 14389 | } |
| 14390 | |
| 14391 | PyDoc_STRVAR(reduce_doc, "Return state information for pickling."); |
| 14392 | |
| 14393 | static PyObject * |
| 14394 | unicodeiter_setstate(unicodeiterobject *it, PyObject *state) |
| 14395 | { |
| 14396 | Py_ssize_t index = PyLong_AsSsize_t(state); |
| 14397 | if (index == -1 && PyErr_Occurred()) |
| 14398 | return NULL; |
| 14399 | if (index < 0) |
| 14400 | index = 0; |
| 14401 | it->it_index = index; |
| 14402 | Py_RETURN_NONE; |
| 14403 | } |
| 14404 | |
| 14405 | PyDoc_STRVAR(setstate_doc, "Set state information for unpickling."); |
| 14406 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14407 | static PyMethodDef unicodeiter_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14408 | {"__length_hint__", (PyCFunction)unicodeiter_len, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14409 | length_hint_doc}, |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 14410 | {"__reduce__", (PyCFunction)unicodeiter_reduce, METH_NOARGS, |
| 14411 | reduce_doc}, |
| 14412 | {"__setstate__", (PyCFunction)unicodeiter_setstate, METH_O, |
| 14413 | setstate_doc}, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14414 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14415 | }; |
| 14416 | |
| 14417 | PyTypeObject PyUnicodeIter_Type = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14418 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 14419 | "str_iterator", /* tp_name */ |
| 14420 | sizeof(unicodeiterobject), /* tp_basicsize */ |
| 14421 | 0, /* tp_itemsize */ |
| 14422 | /* methods */ |
| 14423 | (destructor)unicodeiter_dealloc, /* tp_dealloc */ |
| 14424 | 0, /* tp_print */ |
| 14425 | 0, /* tp_getattr */ |
| 14426 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 14427 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14428 | 0, /* tp_repr */ |
| 14429 | 0, /* tp_as_number */ |
| 14430 | 0, /* tp_as_sequence */ |
| 14431 | 0, /* tp_as_mapping */ |
| 14432 | 0, /* tp_hash */ |
| 14433 | 0, /* tp_call */ |
| 14434 | 0, /* tp_str */ |
| 14435 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 14436 | 0, /* tp_setattro */ |
| 14437 | 0, /* tp_as_buffer */ |
| 14438 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 14439 | 0, /* tp_doc */ |
| 14440 | (traverseproc)unicodeiter_traverse, /* tp_traverse */ |
| 14441 | 0, /* tp_clear */ |
| 14442 | 0, /* tp_richcompare */ |
| 14443 | 0, /* tp_weaklistoffset */ |
| 14444 | PyObject_SelfIter, /* tp_iter */ |
| 14445 | (iternextfunc)unicodeiter_next, /* tp_iternext */ |
| 14446 | unicodeiter_methods, /* tp_methods */ |
| 14447 | 0, |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14448 | }; |
| 14449 | |
| 14450 | static PyObject * |
| 14451 | unicode_iter(PyObject *seq) |
| 14452 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14453 | unicodeiterobject *it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14454 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14455 | if (!PyUnicode_Check(seq)) { |
| 14456 | PyErr_BadInternalCall(); |
| 14457 | return NULL; |
| 14458 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14459 | if (PyUnicode_READY(seq) == -1) |
| 14460 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14461 | it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type); |
| 14462 | if (it == NULL) |
| 14463 | return NULL; |
| 14464 | it->it_index = 0; |
| 14465 | Py_INCREF(seq); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14466 | it->it_seq = seq; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14467 | _PyObject_GC_TRACK(it); |
| 14468 | return (PyObject *)it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14469 | } |
| 14470 | |
Martin v. Löwis | 0d3072e | 2011-10-31 08:40:56 +0100 | [diff] [blame] | 14471 | |
| 14472 | size_t |
| 14473 | Py_UNICODE_strlen(const Py_UNICODE *u) |
| 14474 | { |
| 14475 | int res = 0; |
| 14476 | while(*u++) |
| 14477 | res++; |
| 14478 | return res; |
| 14479 | } |
| 14480 | |
| 14481 | Py_UNICODE* |
| 14482 | Py_UNICODE_strcpy(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 14483 | { |
| 14484 | Py_UNICODE *u = s1; |
| 14485 | while ((*u++ = *s2++)); |
| 14486 | return s1; |
| 14487 | } |
| 14488 | |
| 14489 | Py_UNICODE* |
| 14490 | Py_UNICODE_strncpy(Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 14491 | { |
| 14492 | Py_UNICODE *u = s1; |
| 14493 | while ((*u++ = *s2++)) |
| 14494 | if (n-- == 0) |
| 14495 | break; |
| 14496 | return s1; |
| 14497 | } |
| 14498 | |
| 14499 | Py_UNICODE* |
| 14500 | Py_UNICODE_strcat(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 14501 | { |
| 14502 | Py_UNICODE *u1 = s1; |
| 14503 | u1 += Py_UNICODE_strlen(u1); |
| 14504 | Py_UNICODE_strcpy(u1, s2); |
| 14505 | return s1; |
| 14506 | } |
| 14507 | |
| 14508 | int |
| 14509 | Py_UNICODE_strcmp(const Py_UNICODE *s1, const Py_UNICODE *s2) |
| 14510 | { |
| 14511 | while (*s1 && *s2 && *s1 == *s2) |
| 14512 | s1++, s2++; |
| 14513 | if (*s1 && *s2) |
| 14514 | return (*s1 < *s2) ? -1 : +1; |
| 14515 | if (*s1) |
| 14516 | return 1; |
| 14517 | if (*s2) |
| 14518 | return -1; |
| 14519 | return 0; |
| 14520 | } |
| 14521 | |
| 14522 | int |
| 14523 | Py_UNICODE_strncmp(const Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 14524 | { |
| 14525 | register Py_UNICODE u1, u2; |
| 14526 | for (; n != 0; n--) { |
| 14527 | u1 = *s1; |
| 14528 | u2 = *s2; |
| 14529 | if (u1 != u2) |
| 14530 | return (u1 < u2) ? -1 : +1; |
| 14531 | if (u1 == '\0') |
| 14532 | return 0; |
| 14533 | s1++; |
| 14534 | s2++; |
| 14535 | } |
| 14536 | return 0; |
| 14537 | } |
| 14538 | |
| 14539 | Py_UNICODE* |
| 14540 | Py_UNICODE_strchr(const Py_UNICODE *s, Py_UNICODE c) |
| 14541 | { |
| 14542 | const Py_UNICODE *p; |
| 14543 | for (p = s; *p; p++) |
| 14544 | if (*p == c) |
| 14545 | return (Py_UNICODE*)p; |
| 14546 | return NULL; |
| 14547 | } |
| 14548 | |
| 14549 | Py_UNICODE* |
| 14550 | Py_UNICODE_strrchr(const Py_UNICODE *s, Py_UNICODE c) |
| 14551 | { |
| 14552 | const Py_UNICODE *p; |
| 14553 | p = s + Py_UNICODE_strlen(s); |
| 14554 | while (p != s) { |
| 14555 | p--; |
| 14556 | if (*p == c) |
| 14557 | return (Py_UNICODE*)p; |
| 14558 | } |
| 14559 | return NULL; |
| 14560 | } |
Victor Stinner | 331ea92 | 2010-08-10 16:37:20 +0000 | [diff] [blame] | 14561 | |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14562 | Py_UNICODE* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14563 | PyUnicode_AsUnicodeCopy(PyObject *unicode) |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14564 | { |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 14565 | Py_UNICODE *u, *copy; |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14566 | Py_ssize_t len, size; |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14567 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14568 | if (!PyUnicode_Check(unicode)) { |
| 14569 | PyErr_BadArgument(); |
| 14570 | return NULL; |
| 14571 | } |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14572 | u = PyUnicode_AsUnicodeAndSize(unicode, &len); |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 14573 | if (u == NULL) |
| 14574 | return NULL; |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14575 | /* Ensure we won't overflow the size. */ |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14576 | if (len > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14577 | PyErr_NoMemory(); |
| 14578 | return NULL; |
| 14579 | } |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14580 | size = len + 1; /* copy the null character */ |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14581 | size *= sizeof(Py_UNICODE); |
| 14582 | copy = PyMem_Malloc(size); |
| 14583 | if (copy == NULL) { |
| 14584 | PyErr_NoMemory(); |
| 14585 | return NULL; |
| 14586 | } |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 14587 | memcpy(copy, u, size); |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14588 | return copy; |
| 14589 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 14590 | |
Georg Brandl | 66c221e | 2010-10-14 07:04:07 +0000 | [diff] [blame] | 14591 | /* A _string module, to export formatter_parser and formatter_field_name_split |
| 14592 | to the string.Formatter class implemented in Python. */ |
| 14593 | |
| 14594 | static PyMethodDef _string_methods[] = { |
| 14595 | {"formatter_field_name_split", (PyCFunction) formatter_field_name_split, |
| 14596 | METH_O, PyDoc_STR("split the argument as a field name")}, |
| 14597 | {"formatter_parser", (PyCFunction) formatter_parser, |
| 14598 | METH_O, PyDoc_STR("parse the argument as a format string")}, |
| 14599 | {NULL, NULL} |
| 14600 | }; |
| 14601 | |
| 14602 | static struct PyModuleDef _string_module = { |
| 14603 | PyModuleDef_HEAD_INIT, |
| 14604 | "_string", |
| 14605 | PyDoc_STR("string helper module"), |
| 14606 | 0, |
| 14607 | _string_methods, |
| 14608 | NULL, |
| 14609 | NULL, |
| 14610 | NULL, |
| 14611 | NULL |
| 14612 | }; |
| 14613 | |
| 14614 | PyMODINIT_FUNC |
| 14615 | PyInit__string(void) |
| 14616 | { |
| 14617 | return PyModule_Create(&_string_module); |
| 14618 | } |
| 14619 | |
| 14620 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 14621 | #ifdef __cplusplus |
| 14622 | } |
| 14623 | #endif |