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 | f185226 | 2012-06-16 16:38:26 +0200 | [diff] [blame] | 1151 | #ifdef Py_DEBUG |
| 1152 | if (!check_maxchar |
| 1153 | && PyUnicode_MAX_CHAR_VALUE(from) > PyUnicode_MAX_CHAR_VALUE(to)) |
| 1154 | { |
| 1155 | const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to); |
| 1156 | Py_UCS4 ch; |
| 1157 | Py_ssize_t i; |
| 1158 | for (i=0; i < how_many; i++) { |
| 1159 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
| 1160 | assert(ch <= to_maxchar); |
| 1161 | } |
| 1162 | } |
| 1163 | #endif |
| 1164 | |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1165 | if (from_kind == to_kind) { |
Victor Stinner | f185226 | 2012-06-16 16:38:26 +0200 | [diff] [blame] | 1166 | if (check_maxchar |
| 1167 | && !PyUnicode_IS_ASCII(from) && PyUnicode_IS_ASCII(to)) |
| 1168 | { |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1169 | /* Writing Latin-1 characters into an ASCII string requires to |
| 1170 | check that all written characters are pure ASCII */ |
Victor Stinner | f185226 | 2012-06-16 16:38:26 +0200 | [diff] [blame] | 1171 | Py_UCS4 max_char; |
| 1172 | max_char = ucs1lib_find_max_char(from_data, |
| 1173 | (Py_UCS1*)from_data + how_many); |
| 1174 | if (max_char >= 128) |
| 1175 | return -1; |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1176 | } |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 1177 | Py_MEMCPY((char*)to_data + to_kind * to_start, |
| 1178 | (char*)from_data + from_kind * from_start, |
| 1179 | to_kind * how_many); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1180 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1181 | else if (from_kind == PyUnicode_1BYTE_KIND |
| 1182 | && to_kind == PyUnicode_2BYTE_KIND) |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1183 | { |
| 1184 | _PyUnicode_CONVERT_BYTES( |
| 1185 | Py_UCS1, Py_UCS2, |
| 1186 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 1187 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 1188 | PyUnicode_2BYTE_DATA(to) + to_start |
| 1189 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1190 | } |
Victor Stinner | 157f83f | 2011-09-28 21:41:31 +0200 | [diff] [blame] | 1191 | else if (from_kind == PyUnicode_1BYTE_KIND |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1192 | && to_kind == PyUnicode_4BYTE_KIND) |
| 1193 | { |
| 1194 | _PyUnicode_CONVERT_BYTES( |
| 1195 | Py_UCS1, Py_UCS4, |
| 1196 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 1197 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 1198 | PyUnicode_4BYTE_DATA(to) + to_start |
| 1199 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1200 | } |
| 1201 | else if (from_kind == PyUnicode_2BYTE_KIND |
| 1202 | && to_kind == PyUnicode_4BYTE_KIND) |
| 1203 | { |
| 1204 | _PyUnicode_CONVERT_BYTES( |
| 1205 | Py_UCS2, Py_UCS4, |
| 1206 | PyUnicode_2BYTE_DATA(from) + from_start, |
| 1207 | PyUnicode_2BYTE_DATA(from) + from_start + how_many, |
| 1208 | PyUnicode_4BYTE_DATA(to) + to_start |
| 1209 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1210 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1211 | else { |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1212 | assert (PyUnicode_MAX_CHAR_VALUE(from) > PyUnicode_MAX_CHAR_VALUE(to)); |
| 1213 | |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1214 | if (!check_maxchar) { |
| 1215 | if (from_kind == PyUnicode_2BYTE_KIND |
| 1216 | && to_kind == PyUnicode_1BYTE_KIND) |
| 1217 | { |
| 1218 | _PyUnicode_CONVERT_BYTES( |
| 1219 | Py_UCS2, Py_UCS1, |
| 1220 | PyUnicode_2BYTE_DATA(from) + from_start, |
| 1221 | PyUnicode_2BYTE_DATA(from) + from_start + how_many, |
| 1222 | PyUnicode_1BYTE_DATA(to) + to_start |
| 1223 | ); |
| 1224 | } |
| 1225 | else if (from_kind == PyUnicode_4BYTE_KIND |
| 1226 | && to_kind == PyUnicode_1BYTE_KIND) |
| 1227 | { |
| 1228 | _PyUnicode_CONVERT_BYTES( |
| 1229 | Py_UCS4, Py_UCS1, |
| 1230 | PyUnicode_4BYTE_DATA(from) + from_start, |
| 1231 | PyUnicode_4BYTE_DATA(from) + from_start + how_many, |
| 1232 | PyUnicode_1BYTE_DATA(to) + to_start |
| 1233 | ); |
| 1234 | } |
| 1235 | else if (from_kind == PyUnicode_4BYTE_KIND |
| 1236 | && to_kind == PyUnicode_2BYTE_KIND) |
| 1237 | { |
| 1238 | _PyUnicode_CONVERT_BYTES( |
| 1239 | Py_UCS4, Py_UCS2, |
| 1240 | PyUnicode_4BYTE_DATA(from) + from_start, |
| 1241 | PyUnicode_4BYTE_DATA(from) + from_start + how_many, |
| 1242 | PyUnicode_2BYTE_DATA(to) + to_start |
| 1243 | ); |
| 1244 | } |
| 1245 | else { |
| 1246 | assert(0); |
| 1247 | return -1; |
| 1248 | } |
| 1249 | } |
Victor Stinner | f185226 | 2012-06-16 16:38:26 +0200 | [diff] [blame] | 1250 | else { |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1251 | const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1252 | Py_UCS4 ch; |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1253 | Py_ssize_t i; |
| 1254 | |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1255 | for (i=0; i < how_many; i++) { |
| 1256 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1257 | if (ch > to_maxchar) |
| 1258 | return -1; |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1259 | PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); |
| 1260 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1261 | } |
| 1262 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1263 | return 0; |
| 1264 | } |
| 1265 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1266 | void |
| 1267 | _PyUnicode_FastCopyCharacters( |
| 1268 | PyObject *to, Py_ssize_t to_start, |
| 1269 | PyObject *from, Py_ssize_t from_start, Py_ssize_t how_many) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1270 | { |
| 1271 | (void)_copy_characters(to, to_start, from, from_start, how_many, 0); |
| 1272 | } |
| 1273 | |
| 1274 | Py_ssize_t |
| 1275 | PyUnicode_CopyCharacters(PyObject *to, Py_ssize_t to_start, |
| 1276 | PyObject *from, Py_ssize_t from_start, |
| 1277 | Py_ssize_t how_many) |
| 1278 | { |
| 1279 | int err; |
| 1280 | |
| 1281 | if (!PyUnicode_Check(from) || !PyUnicode_Check(to)) { |
| 1282 | PyErr_BadInternalCall(); |
| 1283 | return -1; |
| 1284 | } |
| 1285 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 1286 | if (PyUnicode_READY(from) == -1) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1287 | return -1; |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 1288 | if (PyUnicode_READY(to) == -1) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1289 | return -1; |
| 1290 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1291 | if (from_start < 0) { |
| 1292 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 1293 | return -1; |
| 1294 | } |
| 1295 | if (to_start < 0) { |
| 1296 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 1297 | return -1; |
| 1298 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1299 | how_many = Py_MIN(PyUnicode_GET_LENGTH(from), how_many); |
| 1300 | if (to_start + how_many > PyUnicode_GET_LENGTH(to)) { |
| 1301 | PyErr_Format(PyExc_SystemError, |
| 1302 | "Cannot write %zi characters at %zi " |
| 1303 | "in a string of %zi characters", |
| 1304 | how_many, to_start, PyUnicode_GET_LENGTH(to)); |
| 1305 | return -1; |
| 1306 | } |
| 1307 | |
| 1308 | if (how_many == 0) |
| 1309 | return 0; |
| 1310 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1311 | if (unicode_check_modifiable(to)) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1312 | return -1; |
| 1313 | |
| 1314 | err = _copy_characters(to, to_start, from, from_start, how_many, 1); |
| 1315 | if (err) { |
| 1316 | PyErr_Format(PyExc_SystemError, |
| 1317 | "Cannot copy %s characters " |
| 1318 | "into a string of %s characters", |
| 1319 | unicode_kind_name(from), |
| 1320 | unicode_kind_name(to)); |
| 1321 | return -1; |
| 1322 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1323 | return how_many; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1324 | } |
| 1325 | |
Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 1326 | /* Find the maximum code point and count the number of surrogate pairs so a |
| 1327 | correct string length can be computed before converting a string to UCS4. |
| 1328 | This function counts single surrogates as a character and not as a pair. |
| 1329 | |
| 1330 | Return 0 on success, or -1 on error. */ |
| 1331 | static int |
| 1332 | find_maxchar_surrogates(const wchar_t *begin, const wchar_t *end, |
| 1333 | Py_UCS4 *maxchar, Py_ssize_t *num_surrogates) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1334 | { |
| 1335 | const wchar_t *iter; |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1336 | Py_UCS4 ch; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1337 | |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1338 | assert(num_surrogates != NULL && maxchar != NULL); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1339 | *num_surrogates = 0; |
| 1340 | *maxchar = 0; |
| 1341 | |
| 1342 | for (iter = begin; iter < end; ) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1343 | #if SIZEOF_WCHAR_T == 2 |
Victor Stinner | ca4f207 | 2011-11-22 03:38:40 +0100 | [diff] [blame] | 1344 | if (Py_UNICODE_IS_HIGH_SURROGATE(iter[0]) |
| 1345 | && (iter+1) < end |
| 1346 | && Py_UNICODE_IS_LOW_SURROGATE(iter[1])) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1347 | { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1348 | ch = Py_UNICODE_JOIN_SURROGATES(iter[0], iter[1]); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1349 | ++(*num_surrogates); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1350 | iter += 2; |
| 1351 | } |
| 1352 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1353 | #endif |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1354 | { |
| 1355 | ch = *iter; |
| 1356 | iter++; |
| 1357 | } |
| 1358 | if (ch > *maxchar) { |
| 1359 | *maxchar = ch; |
| 1360 | if (*maxchar > MAX_UNICODE) { |
| 1361 | PyErr_Format(PyExc_ValueError, |
| 1362 | "character U+%x is not in range [U+0000; U+10ffff]", |
| 1363 | ch); |
| 1364 | return -1; |
| 1365 | } |
| 1366 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1367 | } |
| 1368 | return 0; |
| 1369 | } |
| 1370 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1371 | int |
| 1372 | _PyUnicode_Ready(PyObject *unicode) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1373 | { |
| 1374 | wchar_t *end; |
| 1375 | Py_UCS4 maxchar = 0; |
| 1376 | Py_ssize_t num_surrogates; |
| 1377 | #if SIZEOF_WCHAR_T == 2 |
| 1378 | Py_ssize_t length_wo_surrogates; |
| 1379 | #endif |
| 1380 | |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 1381 | /* _PyUnicode_Ready() is only intended for old-style API usage where |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1382 | strings were created using _PyObject_New() and where no canonical |
| 1383 | representation (the str field) has been set yet aka strings |
| 1384 | which are not yet ready. */ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1385 | assert(_PyUnicode_CHECK(unicode)); |
| 1386 | assert(_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1387 | assert(_PyUnicode_WSTR(unicode) != NULL); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1388 | assert(_PyUnicode_DATA_ANY(unicode) == NULL); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1389 | assert(_PyUnicode_UTF8(unicode) == NULL); |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1390 | /* Actually, it should neither be interned nor be anything else: */ |
| 1391 | assert(_PyUnicode_STATE(unicode).interned == SSTATE_NOT_INTERNED); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1392 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1393 | end = _PyUnicode_WSTR(unicode) + _PyUnicode_WSTR_LENGTH(unicode); |
Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 1394 | if (find_maxchar_surrogates(_PyUnicode_WSTR(unicode), end, |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1395 | &maxchar, &num_surrogates) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1396 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1397 | |
| 1398 | if (maxchar < 256) { |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1399 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(_PyUnicode_WSTR_LENGTH(unicode) + 1); |
| 1400 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1401 | PyErr_NoMemory(); |
| 1402 | return -1; |
| 1403 | } |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1404 | _PyUnicode_CONVERT_BYTES(wchar_t, unsigned char, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1405 | _PyUnicode_WSTR(unicode), end, |
| 1406 | PyUnicode_1BYTE_DATA(unicode)); |
| 1407 | PyUnicode_1BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1408 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1409 | _PyUnicode_STATE(unicode).kind = PyUnicode_1BYTE_KIND; |
| 1410 | if (maxchar < 128) { |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1411 | _PyUnicode_STATE(unicode).ascii = 1; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1412 | _PyUnicode_UTF8(unicode) = _PyUnicode_DATA_ANY(unicode); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1413 | _PyUnicode_UTF8_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1414 | } |
| 1415 | else { |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1416 | _PyUnicode_STATE(unicode).ascii = 0; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1417 | _PyUnicode_UTF8(unicode) = NULL; |
| 1418 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1419 | } |
| 1420 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1421 | _PyUnicode_WSTR(unicode) = NULL; |
| 1422 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1423 | } |
| 1424 | /* In this case we might have to convert down from 4-byte native |
| 1425 | wchar_t to 2-byte unicode. */ |
| 1426 | else if (maxchar < 65536) { |
| 1427 | assert(num_surrogates == 0 && |
| 1428 | "FindMaxCharAndNumSurrogatePairs() messed up"); |
| 1429 | |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1430 | #if SIZEOF_WCHAR_T == 2 |
| 1431 | /* We can share representations and are done. */ |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1432 | _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1433 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1434 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1435 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1436 | _PyUnicode_UTF8(unicode) = NULL; |
| 1437 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1438 | #else |
| 1439 | /* sizeof(wchar_t) == 4 */ |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1440 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC( |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1441 | 2 * (_PyUnicode_WSTR_LENGTH(unicode) + 1)); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1442 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1443 | PyErr_NoMemory(); |
| 1444 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1445 | } |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1446 | _PyUnicode_CONVERT_BYTES(wchar_t, Py_UCS2, |
| 1447 | _PyUnicode_WSTR(unicode), end, |
| 1448 | PyUnicode_2BYTE_DATA(unicode)); |
| 1449 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1450 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1451 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1452 | _PyUnicode_UTF8(unicode) = NULL; |
| 1453 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1454 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1455 | _PyUnicode_WSTR(unicode) = NULL; |
| 1456 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1457 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1458 | } |
| 1459 | /* maxchar exeeds 16 bit, wee need 4 bytes for unicode characters */ |
| 1460 | else { |
| 1461 | #if SIZEOF_WCHAR_T == 2 |
| 1462 | /* in case the native representation is 2-bytes, we need to allocate a |
| 1463 | new normalized 4-byte version. */ |
| 1464 | length_wo_surrogates = _PyUnicode_WSTR_LENGTH(unicode) - num_surrogates; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1465 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(4 * (length_wo_surrogates + 1)); |
| 1466 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1467 | PyErr_NoMemory(); |
| 1468 | return -1; |
| 1469 | } |
| 1470 | _PyUnicode_LENGTH(unicode) = length_wo_surrogates; |
| 1471 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1472 | _PyUnicode_UTF8(unicode) = NULL; |
| 1473 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 126c559 | 2011-10-03 04:17:10 +0200 | [diff] [blame] | 1474 | /* unicode_convert_wchar_to_ucs4() requires a ready string */ |
| 1475 | _PyUnicode_STATE(unicode).ready = 1; |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1476 | unicode_convert_wchar_to_ucs4(_PyUnicode_WSTR(unicode), end, unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1477 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1478 | _PyUnicode_WSTR(unicode) = NULL; |
| 1479 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1480 | #else |
| 1481 | assert(num_surrogates == 0); |
| 1482 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1483 | _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1484 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1485 | _PyUnicode_UTF8(unicode) = NULL; |
| 1486 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1487 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; |
| 1488 | #endif |
| 1489 | PyUnicode_4BYTE_DATA(unicode)[_PyUnicode_LENGTH(unicode)] = '\0'; |
| 1490 | } |
| 1491 | _PyUnicode_STATE(unicode).ready = 1; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1492 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1493 | return 0; |
| 1494 | } |
| 1495 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1496 | static void |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1497 | unicode_dealloc(register PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1498 | { |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1499 | switch (PyUnicode_CHECK_INTERNED(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1500 | case SSTATE_NOT_INTERNED: |
| 1501 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1502 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1503 | case SSTATE_INTERNED_MORTAL: |
| 1504 | /* revive dead object temporarily for DelItem */ |
| 1505 | Py_REFCNT(unicode) = 3; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 1506 | if (PyDict_DelItem(interned, unicode) != 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1507 | Py_FatalError( |
| 1508 | "deletion of interned string failed"); |
| 1509 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1510 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1511 | case SSTATE_INTERNED_IMMORTAL: |
| 1512 | Py_FatalError("Immortal interned string died."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1513 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1514 | default: |
| 1515 | Py_FatalError("Inconsistent interned string state."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1516 | } |
| 1517 | |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 1518 | if (_PyUnicode_HAS_WSTR_MEMORY(unicode)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1519 | PyObject_DEL(_PyUnicode_WSTR(unicode)); |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 1520 | if (_PyUnicode_HAS_UTF8_MEMORY(unicode)) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1521 | PyObject_DEL(_PyUnicode_UTF8(unicode)); |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1522 | if (!PyUnicode_IS_COMPACT(unicode) && _PyUnicode_DATA_ANY(unicode)) |
| 1523 | PyObject_DEL(_PyUnicode_DATA_ANY(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1524 | |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1525 | Py_TYPE(unicode)->tp_free(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1526 | } |
| 1527 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1528 | #ifdef Py_DEBUG |
| 1529 | static int |
| 1530 | unicode_is_singleton(PyObject *unicode) |
| 1531 | { |
| 1532 | PyASCIIObject *ascii = (PyASCIIObject *)unicode; |
| 1533 | if (unicode == unicode_empty) |
| 1534 | return 1; |
| 1535 | if (ascii->state.kind != PyUnicode_WCHAR_KIND && ascii->length == 1) |
| 1536 | { |
| 1537 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, 0); |
| 1538 | if (ch < 256 && unicode_latin1[ch] == unicode) |
| 1539 | return 1; |
| 1540 | } |
| 1541 | return 0; |
| 1542 | } |
| 1543 | #endif |
| 1544 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1545 | static int |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1546 | unicode_modifiable(PyObject *unicode) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1547 | { |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1548 | assert(_PyUnicode_CHECK(unicode)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1549 | if (Py_REFCNT(unicode) != 1) |
| 1550 | return 0; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1551 | if (_PyUnicode_HASH(unicode) != -1) |
| 1552 | return 0; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1553 | if (PyUnicode_CHECK_INTERNED(unicode)) |
| 1554 | return 0; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1555 | if (!PyUnicode_CheckExact(unicode)) |
| 1556 | return 0; |
Victor Stinner | 77bb47b | 2011-10-03 20:06:05 +0200 | [diff] [blame] | 1557 | #ifdef Py_DEBUG |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1558 | /* singleton refcount is greater than 1 */ |
| 1559 | assert(!unicode_is_singleton(unicode)); |
Victor Stinner | 77bb47b | 2011-10-03 20:06:05 +0200 | [diff] [blame] | 1560 | #endif |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1561 | return 1; |
| 1562 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1563 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1564 | static int |
| 1565 | unicode_resize(PyObject **p_unicode, Py_ssize_t length) |
| 1566 | { |
| 1567 | PyObject *unicode; |
| 1568 | Py_ssize_t old_length; |
| 1569 | |
| 1570 | assert(p_unicode != NULL); |
| 1571 | unicode = *p_unicode; |
| 1572 | |
| 1573 | assert(unicode != NULL); |
| 1574 | assert(PyUnicode_Check(unicode)); |
| 1575 | assert(0 <= length); |
| 1576 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1577 | if (_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1578 | old_length = PyUnicode_WSTR_LENGTH(unicode); |
| 1579 | else |
| 1580 | old_length = PyUnicode_GET_LENGTH(unicode); |
| 1581 | if (old_length == length) |
| 1582 | return 0; |
| 1583 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1584 | if (length == 0) { |
| 1585 | Py_DECREF(*p_unicode); |
| 1586 | *p_unicode = unicode_empty; |
| 1587 | Py_INCREF(*p_unicode); |
| 1588 | return 0; |
| 1589 | } |
| 1590 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1591 | if (!unicode_modifiable(unicode)) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1592 | PyObject *copy = resize_copy(unicode, length); |
| 1593 | if (copy == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1594 | return -1; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1595 | Py_DECREF(*p_unicode); |
| 1596 | *p_unicode = copy; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1597 | return 0; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1598 | } |
| 1599 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1600 | if (PyUnicode_IS_COMPACT(unicode)) { |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1601 | PyObject *new_unicode = resize_compact(unicode, length); |
| 1602 | if (new_unicode == NULL) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1603 | return -1; |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1604 | *p_unicode = new_unicode; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1605 | return 0; |
Benjamin Peterson | 4bfce8f | 2011-10-03 19:35:07 -0400 | [diff] [blame] | 1606 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1607 | return resize_inplace(unicode, length); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1608 | } |
| 1609 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1610 | int |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1611 | PyUnicode_Resize(PyObject **p_unicode, Py_ssize_t length) |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1612 | { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1613 | PyObject *unicode; |
| 1614 | if (p_unicode == NULL) { |
| 1615 | PyErr_BadInternalCall(); |
| 1616 | return -1; |
| 1617 | } |
| 1618 | unicode = *p_unicode; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1619 | if (unicode == NULL || !PyUnicode_Check(unicode) || length < 0) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1620 | { |
| 1621 | PyErr_BadInternalCall(); |
| 1622 | return -1; |
| 1623 | } |
| 1624 | return unicode_resize(p_unicode, length); |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1625 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1626 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1627 | static int |
Victor Stinner | 1b487b4 | 2012-05-03 12:29:04 +0200 | [diff] [blame] | 1628 | unicode_widen(PyObject **p_unicode, Py_ssize_t length, |
| 1629 | unsigned int maxchar) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1630 | { |
| 1631 | PyObject *result; |
| 1632 | assert(PyUnicode_IS_READY(*p_unicode)); |
Victor Stinner | 1b487b4 | 2012-05-03 12:29:04 +0200 | [diff] [blame] | 1633 | assert(length <= PyUnicode_GET_LENGTH(*p_unicode)); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1634 | if (maxchar <= PyUnicode_MAX_CHAR_VALUE(*p_unicode)) |
| 1635 | return 0; |
| 1636 | result = PyUnicode_New(PyUnicode_GET_LENGTH(*p_unicode), |
| 1637 | maxchar); |
| 1638 | if (result == NULL) |
| 1639 | return -1; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1640 | _PyUnicode_FastCopyCharacters(result, 0, *p_unicode, 0, length); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1641 | Py_DECREF(*p_unicode); |
| 1642 | *p_unicode = result; |
| 1643 | return 0; |
| 1644 | } |
| 1645 | |
| 1646 | static int |
| 1647 | unicode_putchar(PyObject **p_unicode, Py_ssize_t *pos, |
| 1648 | Py_UCS4 ch) |
| 1649 | { |
Victor Stinner | 15e9ed2 | 2012-02-22 13:36:20 +0100 | [diff] [blame] | 1650 | assert(ch <= MAX_UNICODE); |
Victor Stinner | 1b487b4 | 2012-05-03 12:29:04 +0200 | [diff] [blame] | 1651 | if (unicode_widen(p_unicode, *pos, ch) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1652 | return -1; |
| 1653 | PyUnicode_WRITE(PyUnicode_KIND(*p_unicode), |
| 1654 | PyUnicode_DATA(*p_unicode), |
| 1655 | (*pos)++, ch); |
| 1656 | return 0; |
| 1657 | } |
| 1658 | |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1659 | /* Copy a ASCII or latin1 char* string into a Python Unicode string. |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1660 | |
Victor Stinner | b429d3b | 2012-02-22 21:22:20 +0100 | [diff] [blame] | 1661 | WARNING: The function doesn't copy the terminating null character and |
| 1662 | doesn't check the maximum character (may write a latin1 character in an |
| 1663 | ASCII string). */ |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1664 | static void |
| 1665 | unicode_write_cstr(PyObject *unicode, Py_ssize_t index, |
| 1666 | const char *str, Py_ssize_t len) |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1667 | { |
| 1668 | enum PyUnicode_Kind kind = PyUnicode_KIND(unicode); |
| 1669 | void *data = PyUnicode_DATA(unicode); |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1670 | const char *end = str + len; |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1671 | |
| 1672 | switch (kind) { |
| 1673 | case PyUnicode_1BYTE_KIND: { |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1674 | assert(index + len <= PyUnicode_GET_LENGTH(unicode)); |
Antoine Pitrou | ba6bafc | 2012-02-22 16:41:50 +0100 | [diff] [blame] | 1675 | memcpy((char *) data + index, str, len); |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1676 | break; |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1677 | } |
| 1678 | case PyUnicode_2BYTE_KIND: { |
| 1679 | Py_UCS2 *start = (Py_UCS2 *)data + index; |
| 1680 | Py_UCS2 *ucs2 = start; |
| 1681 | assert(index <= PyUnicode_GET_LENGTH(unicode)); |
| 1682 | |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1683 | for (; str < end; ++ucs2, ++str) |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1684 | *ucs2 = (Py_UCS2)*str; |
| 1685 | |
| 1686 | assert((ucs2 - start) <= PyUnicode_GET_LENGTH(unicode)); |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1687 | break; |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1688 | } |
| 1689 | default: { |
| 1690 | Py_UCS4 *start = (Py_UCS4 *)data + index; |
| 1691 | Py_UCS4 *ucs4 = start; |
| 1692 | assert(kind == PyUnicode_4BYTE_KIND); |
| 1693 | assert(index <= PyUnicode_GET_LENGTH(unicode)); |
| 1694 | |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1695 | for (; str < end; ++ucs4, ++str) |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1696 | *ucs4 = (Py_UCS4)*str; |
| 1697 | |
| 1698 | assert((ucs4 - start) <= PyUnicode_GET_LENGTH(unicode)); |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1699 | } |
| 1700 | } |
| 1701 | } |
| 1702 | |
| 1703 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1704 | static PyObject* |
| 1705 | get_latin1_char(unsigned char ch) |
| 1706 | { |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1707 | PyObject *unicode = unicode_latin1[ch]; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1708 | if (!unicode) { |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1709 | unicode = PyUnicode_New(1, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1710 | if (!unicode) |
| 1711 | return NULL; |
| 1712 | PyUnicode_1BYTE_DATA(unicode)[0] = ch; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1713 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1714 | unicode_latin1[ch] = unicode; |
| 1715 | } |
| 1716 | Py_INCREF(unicode); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1717 | return unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1718 | } |
| 1719 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1720 | PyObject * |
| 1721 | PyUnicode_FromUnicode(const Py_UNICODE *u, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1722 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1723 | PyObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1724 | Py_UCS4 maxchar = 0; |
| 1725 | Py_ssize_t num_surrogates; |
| 1726 | |
| 1727 | if (u == NULL) |
| 1728 | return (PyObject*)_PyUnicode_New(size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1729 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1730 | /* If the Unicode data is known at construction time, we can apply |
| 1731 | some optimizations which share commonly used objects. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1732 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1733 | /* Optimization for empty strings */ |
| 1734 | if (size == 0 && unicode_empty != NULL) { |
| 1735 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1736 | return unicode_empty; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1737 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1738 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1739 | /* Single character Unicode objects in the Latin-1 range are |
| 1740 | shared when using this constructor */ |
| 1741 | if (size == 1 && *u < 256) |
| 1742 | return get_latin1_char((unsigned char)*u); |
| 1743 | |
| 1744 | /* If not empty and not single character, copy the Unicode data |
| 1745 | into the new object */ |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1746 | if (find_maxchar_surrogates(u, u + size, |
| 1747 | &maxchar, &num_surrogates) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1748 | return NULL; |
| 1749 | |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1750 | unicode = PyUnicode_New(size - num_surrogates, maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1751 | if (!unicode) |
| 1752 | return NULL; |
| 1753 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1754 | switch (PyUnicode_KIND(unicode)) { |
| 1755 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1756 | _PyUnicode_CONVERT_BYTES(Py_UNICODE, unsigned char, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1757 | u, u + size, PyUnicode_1BYTE_DATA(unicode)); |
| 1758 | break; |
| 1759 | case PyUnicode_2BYTE_KIND: |
| 1760 | #if Py_UNICODE_SIZE == 2 |
| 1761 | Py_MEMCPY(PyUnicode_2BYTE_DATA(unicode), u, size * 2); |
| 1762 | #else |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1763 | _PyUnicode_CONVERT_BYTES(Py_UNICODE, Py_UCS2, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1764 | u, u + size, PyUnicode_2BYTE_DATA(unicode)); |
| 1765 | #endif |
| 1766 | break; |
| 1767 | case PyUnicode_4BYTE_KIND: |
| 1768 | #if SIZEOF_WCHAR_T == 2 |
| 1769 | /* This is the only case which has to process surrogates, thus |
| 1770 | a simple copy loop is not enough and we need a function. */ |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1771 | unicode_convert_wchar_to_ucs4(u, u + size, unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1772 | #else |
| 1773 | assert(num_surrogates == 0); |
| 1774 | Py_MEMCPY(PyUnicode_4BYTE_DATA(unicode), u, size * 4); |
| 1775 | #endif |
| 1776 | break; |
| 1777 | default: |
| 1778 | assert(0 && "Impossible state"); |
| 1779 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1780 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1781 | return unicode_result(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1782 | } |
| 1783 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1784 | PyObject * |
| 1785 | PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size) |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1786 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1787 | if (size < 0) { |
| 1788 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1789 | "Negative size passed to PyUnicode_FromStringAndSize"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1790 | return NULL; |
| 1791 | } |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 1792 | if (u != NULL) |
| 1793 | return PyUnicode_DecodeUTF8Stateful(u, size, NULL, NULL); |
| 1794 | else |
| 1795 | return (PyObject *)_PyUnicode_New(size); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1796 | } |
| 1797 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1798 | PyObject * |
| 1799 | PyUnicode_FromString(const char *u) |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1800 | { |
| 1801 | size_t size = strlen(u); |
| 1802 | if (size > PY_SSIZE_T_MAX) { |
| 1803 | PyErr_SetString(PyExc_OverflowError, "input too long"); |
| 1804 | return NULL; |
| 1805 | } |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 1806 | return PyUnicode_DecodeUTF8Stateful(u, (Py_ssize_t)size, NULL, NULL); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1807 | } |
| 1808 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1809 | PyObject * |
| 1810 | _PyUnicode_FromId(_Py_Identifier *id) |
| 1811 | { |
| 1812 | if (!id->object) { |
Victor Stinner | d1cd99b | 2012-02-07 23:05:55 +0100 | [diff] [blame] | 1813 | id->object = PyUnicode_DecodeUTF8Stateful(id->string, |
| 1814 | strlen(id->string), |
| 1815 | NULL, NULL); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1816 | if (!id->object) |
| 1817 | return NULL; |
| 1818 | PyUnicode_InternInPlace(&id->object); |
| 1819 | assert(!id->next); |
| 1820 | id->next = static_strings; |
| 1821 | static_strings = id; |
| 1822 | } |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1823 | return id->object; |
| 1824 | } |
| 1825 | |
| 1826 | void |
| 1827 | _PyUnicode_ClearStaticStrings() |
| 1828 | { |
| 1829 | _Py_Identifier *i; |
| 1830 | for (i = static_strings; i; i = i->next) { |
| 1831 | Py_DECREF(i->object); |
| 1832 | i->object = NULL; |
| 1833 | i->next = NULL; |
| 1834 | } |
| 1835 | } |
| 1836 | |
Benjamin Peterson | 0df5429 | 2012-03-26 14:50:32 -0400 | [diff] [blame] | 1837 | /* Internal function, doesn't check maximum character */ |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1838 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1839 | PyObject* |
| 1840 | _PyUnicode_FromASCII(const char *buffer, Py_ssize_t size) |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1841 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1842 | const unsigned char *s = (const unsigned char *)buffer; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 1843 | PyObject *unicode; |
Victor Stinner | e6b2d44 | 2011-12-11 21:54:30 +0100 | [diff] [blame] | 1844 | if (size == 1) { |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1845 | #ifdef Py_DEBUG |
Victor Stinner | e6b2d44 | 2011-12-11 21:54:30 +0100 | [diff] [blame] | 1846 | assert(s[0] < 128); |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1847 | #endif |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1848 | return get_latin1_char(s[0]); |
Victor Stinner | e6b2d44 | 2011-12-11 21:54:30 +0100 | [diff] [blame] | 1849 | } |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 1850 | unicode = PyUnicode_New(size, 127); |
| 1851 | if (!unicode) |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1852 | return NULL; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 1853 | memcpy(PyUnicode_1BYTE_DATA(unicode), s, size); |
| 1854 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 1855 | return unicode; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1856 | } |
| 1857 | |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 1858 | static Py_UCS4 |
| 1859 | kind_maxchar_limit(unsigned int kind) |
| 1860 | { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 1861 | switch (kind) { |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 1862 | case PyUnicode_1BYTE_KIND: |
| 1863 | return 0x80; |
| 1864 | case PyUnicode_2BYTE_KIND: |
| 1865 | return 0x100; |
| 1866 | case PyUnicode_4BYTE_KIND: |
| 1867 | return 0x10000; |
| 1868 | default: |
| 1869 | assert(0 && "invalid kind"); |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1870 | return MAX_UNICODE; |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 1871 | } |
| 1872 | } |
| 1873 | |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 1874 | Py_LOCAL_INLINE(Py_UCS4) |
| 1875 | align_maxchar(Py_UCS4 maxchar) |
| 1876 | { |
| 1877 | if (maxchar <= 127) |
| 1878 | return 127; |
| 1879 | else if (maxchar <= 255) |
| 1880 | return 255; |
| 1881 | else if (maxchar <= 65535) |
| 1882 | return 65535; |
| 1883 | else |
| 1884 | return MAX_UNICODE; |
| 1885 | } |
| 1886 | |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1887 | static PyObject* |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1888 | _PyUnicode_FromUCS1(const unsigned char* u, Py_ssize_t size) |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1889 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1890 | PyObject *res; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1891 | unsigned char max_char; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1892 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1893 | if (size == 0) { |
| 1894 | Py_INCREF(unicode_empty); |
| 1895 | return unicode_empty; |
| 1896 | } |
| 1897 | assert(size > 0); |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1898 | if (size == 1) |
| 1899 | return get_latin1_char(u[0]); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1900 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1901 | max_char = ucs1lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1902 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1903 | if (!res) |
| 1904 | return NULL; |
| 1905 | memcpy(PyUnicode_1BYTE_DATA(res), u, size); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1906 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1907 | return res; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1908 | } |
| 1909 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1910 | static PyObject* |
| 1911 | _PyUnicode_FromUCS2(const Py_UCS2 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1912 | { |
| 1913 | PyObject *res; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1914 | Py_UCS2 max_char; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1915 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1916 | if (size == 0) { |
| 1917 | Py_INCREF(unicode_empty); |
| 1918 | return unicode_empty; |
| 1919 | } |
| 1920 | assert(size > 0); |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 1921 | if (size == 1) { |
| 1922 | Py_UCS4 ch = u[0]; |
| 1923 | if (ch < 256) |
| 1924 | return get_latin1_char((unsigned char)ch); |
| 1925 | |
| 1926 | res = PyUnicode_New(1, ch); |
| 1927 | if (res == NULL) |
| 1928 | return NULL; |
| 1929 | PyUnicode_WRITE(PyUnicode_KIND(res), PyUnicode_DATA(res), 0, ch); |
| 1930 | assert(_PyUnicode_CheckConsistency(res, 1)); |
| 1931 | return res; |
| 1932 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1933 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1934 | max_char = ucs2lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1935 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1936 | if (!res) |
| 1937 | return NULL; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1938 | if (max_char >= 256) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1939 | memcpy(PyUnicode_2BYTE_DATA(res), u, sizeof(Py_UCS2)*size); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1940 | else { |
| 1941 | _PyUnicode_CONVERT_BYTES( |
| 1942 | Py_UCS2, Py_UCS1, u, u + size, PyUnicode_1BYTE_DATA(res)); |
| 1943 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1944 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1945 | return res; |
| 1946 | } |
| 1947 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1948 | static PyObject* |
| 1949 | _PyUnicode_FromUCS4(const Py_UCS4 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1950 | { |
| 1951 | PyObject *res; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1952 | Py_UCS4 max_char; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1953 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1954 | if (size == 0) { |
| 1955 | Py_INCREF(unicode_empty); |
| 1956 | return unicode_empty; |
| 1957 | } |
| 1958 | assert(size > 0); |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 1959 | if (size == 1) { |
| 1960 | Py_UCS4 ch = u[0]; |
| 1961 | if (ch < 256) |
| 1962 | return get_latin1_char((unsigned char)ch); |
| 1963 | |
| 1964 | res = PyUnicode_New(1, ch); |
| 1965 | if (res == NULL) |
| 1966 | return NULL; |
| 1967 | PyUnicode_WRITE(PyUnicode_KIND(res), PyUnicode_DATA(res), 0, ch); |
| 1968 | assert(_PyUnicode_CheckConsistency(res, 1)); |
| 1969 | return res; |
| 1970 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1971 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1972 | max_char = ucs4lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1973 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1974 | if (!res) |
| 1975 | return NULL; |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 1976 | if (max_char < 256) |
| 1977 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS1, u, u + size, |
| 1978 | PyUnicode_1BYTE_DATA(res)); |
| 1979 | else if (max_char < 0x10000) |
| 1980 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS2, u, u + size, |
| 1981 | PyUnicode_2BYTE_DATA(res)); |
| 1982 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1983 | memcpy(PyUnicode_4BYTE_DATA(res), u, sizeof(Py_UCS4)*size); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1984 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1985 | return res; |
| 1986 | } |
| 1987 | |
| 1988 | PyObject* |
| 1989 | PyUnicode_FromKindAndData(int kind, const void *buffer, Py_ssize_t size) |
| 1990 | { |
Victor Stinner | cfed46e | 2011-11-22 01:29:14 +0100 | [diff] [blame] | 1991 | if (size < 0) { |
| 1992 | PyErr_SetString(PyExc_ValueError, "size must be positive"); |
| 1993 | return NULL; |
| 1994 | } |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 1995 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1996 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1997 | return _PyUnicode_FromUCS1(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1998 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1999 | return _PyUnicode_FromUCS2(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2000 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 2001 | return _PyUnicode_FromUCS4(buffer, size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 2002 | default: |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 2003 | PyErr_SetString(PyExc_SystemError, "invalid kind"); |
| 2004 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2005 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2006 | } |
| 2007 | |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2008 | Py_UCS4 |
| 2009 | _PyUnicode_FindMaxChar(PyObject *unicode, Py_ssize_t start, Py_ssize_t end) |
| 2010 | { |
| 2011 | enum PyUnicode_Kind kind; |
| 2012 | void *startptr, *endptr; |
| 2013 | |
| 2014 | assert(PyUnicode_IS_READY(unicode)); |
| 2015 | assert(0 <= start); |
| 2016 | assert(end <= PyUnicode_GET_LENGTH(unicode)); |
| 2017 | assert(start <= end); |
| 2018 | |
| 2019 | if (start == 0 && end == PyUnicode_GET_LENGTH(unicode)) |
| 2020 | return PyUnicode_MAX_CHAR_VALUE(unicode); |
| 2021 | |
| 2022 | if (start == end) |
| 2023 | return 127; |
| 2024 | |
Victor Stinner | 94d558b | 2012-04-27 22:26:58 +0200 | [diff] [blame] | 2025 | if (PyUnicode_IS_ASCII(unicode)) |
| 2026 | return 127; |
| 2027 | |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2028 | kind = PyUnicode_KIND(unicode); |
Benjamin Peterson | f3b7d86 | 2012-04-23 18:07:01 -0400 | [diff] [blame] | 2029 | startptr = PyUnicode_DATA(unicode); |
Benjamin Peterson | b9f4c9d | 2012-04-23 21:45:40 -0400 | [diff] [blame] | 2030 | endptr = (char *)startptr + end * kind; |
| 2031 | startptr = (char *)startptr + start * kind; |
Benjamin Peterson | 2844a7a | 2012-04-23 18:00:25 -0400 | [diff] [blame] | 2032 | switch(kind) { |
| 2033 | case PyUnicode_1BYTE_KIND: |
| 2034 | return ucs1lib_find_max_char(startptr, endptr); |
| 2035 | case PyUnicode_2BYTE_KIND: |
| 2036 | return ucs2lib_find_max_char(startptr, endptr); |
| 2037 | case PyUnicode_4BYTE_KIND: |
| 2038 | return ucs4lib_find_max_char(startptr, endptr); |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2039 | default: |
Benjamin Peterson | 2844a7a | 2012-04-23 18:00:25 -0400 | [diff] [blame] | 2040 | assert(0); |
| 2041 | return 0; |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2042 | } |
| 2043 | } |
| 2044 | |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2045 | /* Ensure that a string uses the most efficient storage, if it is not the |
| 2046 | case: create a new string with of the right kind. Write NULL into *p_unicode |
| 2047 | on error. */ |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 2048 | static void |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2049 | unicode_adjust_maxchar(PyObject **p_unicode) |
| 2050 | { |
| 2051 | PyObject *unicode, *copy; |
| 2052 | Py_UCS4 max_char; |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2053 | Py_ssize_t len; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2054 | unsigned int kind; |
| 2055 | |
| 2056 | assert(p_unicode != NULL); |
| 2057 | unicode = *p_unicode; |
| 2058 | assert(PyUnicode_IS_READY(unicode)); |
| 2059 | if (PyUnicode_IS_ASCII(unicode)) |
| 2060 | return; |
| 2061 | |
| 2062 | len = PyUnicode_GET_LENGTH(unicode); |
| 2063 | kind = PyUnicode_KIND(unicode); |
| 2064 | if (kind == PyUnicode_1BYTE_KIND) { |
| 2065 | const Py_UCS1 *u = PyUnicode_1BYTE_DATA(unicode); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2066 | max_char = ucs1lib_find_max_char(u, u + len); |
| 2067 | if (max_char >= 128) |
| 2068 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2069 | } |
| 2070 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 2071 | const Py_UCS2 *u = PyUnicode_2BYTE_DATA(unicode); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2072 | max_char = ucs2lib_find_max_char(u, u + len); |
| 2073 | if (max_char >= 256) |
| 2074 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2075 | } |
| 2076 | else { |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2077 | const Py_UCS4 *u = PyUnicode_4BYTE_DATA(unicode); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2078 | assert(kind == PyUnicode_4BYTE_KIND); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2079 | max_char = ucs4lib_find_max_char(u, u + len); |
| 2080 | if (max_char >= 0x10000) |
| 2081 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2082 | } |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2083 | copy = PyUnicode_New(len, max_char); |
Victor Stinner | ca439ee | 2012-06-16 03:17:34 +0200 | [diff] [blame] | 2084 | if (copy != NULL) |
| 2085 | _PyUnicode_FastCopyCharacters(copy, 0, unicode, 0, len); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2086 | Py_DECREF(unicode); |
| 2087 | *p_unicode = copy; |
| 2088 | } |
| 2089 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2090 | PyObject* |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 2091 | _PyUnicode_Copy(PyObject *unicode) |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2092 | { |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 2093 | Py_ssize_t length; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2094 | PyObject *copy; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2095 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2096 | if (!PyUnicode_Check(unicode)) { |
| 2097 | PyErr_BadInternalCall(); |
| 2098 | return NULL; |
| 2099 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 2100 | if (PyUnicode_READY(unicode) == -1) |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2101 | return NULL; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2102 | |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 2103 | length = PyUnicode_GET_LENGTH(unicode); |
| 2104 | copy = PyUnicode_New(length, PyUnicode_MAX_CHAR_VALUE(unicode)); |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2105 | if (!copy) |
| 2106 | return NULL; |
| 2107 | assert(PyUnicode_KIND(copy) == PyUnicode_KIND(unicode)); |
| 2108 | |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 2109 | Py_MEMCPY(PyUnicode_DATA(copy), PyUnicode_DATA(unicode), |
| 2110 | length * PyUnicode_KIND(unicode)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2111 | assert(_PyUnicode_CheckConsistency(copy, 1)); |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2112 | return copy; |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2113 | } |
| 2114 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2115 | |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2116 | /* Widen Unicode objects to larger buffers. Don't write terminating null |
| 2117 | character. Return NULL on error. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2118 | |
| 2119 | void* |
| 2120 | _PyUnicode_AsKind(PyObject *s, unsigned int kind) |
| 2121 | { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2122 | Py_ssize_t len; |
| 2123 | void *result; |
| 2124 | unsigned int skind; |
| 2125 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 2126 | if (PyUnicode_READY(s) == -1) |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2127 | return NULL; |
| 2128 | |
| 2129 | len = PyUnicode_GET_LENGTH(s); |
| 2130 | skind = PyUnicode_KIND(s); |
| 2131 | if (skind >= kind) { |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 2132 | PyErr_SetString(PyExc_SystemError, "invalid widening attempt"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2133 | return NULL; |
| 2134 | } |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 2135 | switch (kind) { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2136 | case PyUnicode_2BYTE_KIND: |
| 2137 | result = PyMem_Malloc(len * sizeof(Py_UCS2)); |
| 2138 | if (!result) |
| 2139 | return PyErr_NoMemory(); |
| 2140 | assert(skind == PyUnicode_1BYTE_KIND); |
| 2141 | _PyUnicode_CONVERT_BYTES( |
| 2142 | Py_UCS1, Py_UCS2, |
| 2143 | PyUnicode_1BYTE_DATA(s), |
| 2144 | PyUnicode_1BYTE_DATA(s) + len, |
| 2145 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2146 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2147 | case PyUnicode_4BYTE_KIND: |
| 2148 | result = PyMem_Malloc(len * sizeof(Py_UCS4)); |
| 2149 | if (!result) |
| 2150 | return PyErr_NoMemory(); |
| 2151 | if (skind == PyUnicode_2BYTE_KIND) { |
| 2152 | _PyUnicode_CONVERT_BYTES( |
| 2153 | Py_UCS2, Py_UCS4, |
| 2154 | PyUnicode_2BYTE_DATA(s), |
| 2155 | PyUnicode_2BYTE_DATA(s) + len, |
| 2156 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2157 | } |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2158 | else { |
| 2159 | assert(skind == PyUnicode_1BYTE_KIND); |
| 2160 | _PyUnicode_CONVERT_BYTES( |
| 2161 | Py_UCS1, Py_UCS4, |
| 2162 | PyUnicode_1BYTE_DATA(s), |
| 2163 | PyUnicode_1BYTE_DATA(s) + len, |
| 2164 | result); |
| 2165 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2166 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2167 | default: |
| 2168 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2169 | } |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 2170 | PyErr_SetString(PyExc_SystemError, "invalid kind"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2171 | return NULL; |
| 2172 | } |
| 2173 | |
| 2174 | static Py_UCS4* |
| 2175 | as_ucs4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 2176 | int copy_null) |
| 2177 | { |
| 2178 | int kind; |
| 2179 | void *data; |
| 2180 | Py_ssize_t len, targetlen; |
| 2181 | if (PyUnicode_READY(string) == -1) |
| 2182 | return NULL; |
| 2183 | kind = PyUnicode_KIND(string); |
| 2184 | data = PyUnicode_DATA(string); |
| 2185 | len = PyUnicode_GET_LENGTH(string); |
| 2186 | targetlen = len; |
| 2187 | if (copy_null) |
| 2188 | targetlen++; |
| 2189 | if (!target) { |
| 2190 | if (PY_SSIZE_T_MAX / sizeof(Py_UCS4) < targetlen) { |
| 2191 | PyErr_NoMemory(); |
| 2192 | return NULL; |
| 2193 | } |
| 2194 | target = PyMem_Malloc(targetlen * sizeof(Py_UCS4)); |
| 2195 | if (!target) { |
| 2196 | PyErr_NoMemory(); |
| 2197 | return NULL; |
| 2198 | } |
| 2199 | } |
| 2200 | else { |
| 2201 | if (targetsize < targetlen) { |
| 2202 | PyErr_Format(PyExc_SystemError, |
| 2203 | "string is longer than the buffer"); |
| 2204 | if (copy_null && 0 < targetsize) |
| 2205 | target[0] = 0; |
| 2206 | return NULL; |
| 2207 | } |
| 2208 | } |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2209 | if (kind == PyUnicode_1BYTE_KIND) { |
| 2210 | Py_UCS1 *start = (Py_UCS1 *) data; |
| 2211 | _PyUnicode_CONVERT_BYTES(Py_UCS1, Py_UCS4, start, start + len, target); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2212 | } |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2213 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 2214 | Py_UCS2 *start = (Py_UCS2 *) data; |
| 2215 | _PyUnicode_CONVERT_BYTES(Py_UCS2, Py_UCS4, start, start + len, target); |
| 2216 | } |
| 2217 | else { |
| 2218 | assert(kind == PyUnicode_4BYTE_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2219 | Py_MEMCPY(target, data, len * sizeof(Py_UCS4)); |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2220 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2221 | if (copy_null) |
| 2222 | target[len] = 0; |
| 2223 | return target; |
| 2224 | } |
| 2225 | |
| 2226 | Py_UCS4* |
| 2227 | PyUnicode_AsUCS4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 2228 | int copy_null) |
| 2229 | { |
Antoine Pitrou | de20b0b | 2011-11-10 21:47:38 +0100 | [diff] [blame] | 2230 | if (target == NULL || targetsize < 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2231 | PyErr_BadInternalCall(); |
| 2232 | return NULL; |
| 2233 | } |
| 2234 | return as_ucs4(string, target, targetsize, copy_null); |
| 2235 | } |
| 2236 | |
| 2237 | Py_UCS4* |
| 2238 | PyUnicode_AsUCS4Copy(PyObject *string) |
| 2239 | { |
| 2240 | return as_ucs4(string, NULL, 0, 1); |
| 2241 | } |
| 2242 | |
| 2243 | #ifdef HAVE_WCHAR_H |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 2244 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2245 | PyObject * |
| 2246 | PyUnicode_FromWideChar(register const wchar_t *w, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2247 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2248 | if (w == NULL) { |
Victor Stinner | 382955f | 2011-12-11 21:44:00 +0100 | [diff] [blame] | 2249 | if (size == 0) { |
| 2250 | Py_INCREF(unicode_empty); |
| 2251 | return unicode_empty; |
| 2252 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2253 | PyErr_BadInternalCall(); |
| 2254 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2255 | } |
| 2256 | |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 2257 | if (size == -1) { |
| 2258 | size = wcslen(w); |
| 2259 | } |
| 2260 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2261 | return PyUnicode_FromUnicode(w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2262 | } |
| 2263 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2264 | #endif /* HAVE_WCHAR_H */ |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 2265 | |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2266 | static void |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2267 | makefmt(char *fmt, int longflag, int longlongflag, int size_tflag, |
| 2268 | int zeropad, int width, int precision, char c) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2269 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2270 | *fmt++ = '%'; |
| 2271 | if (width) { |
| 2272 | if (zeropad) |
| 2273 | *fmt++ = '0'; |
| 2274 | fmt += sprintf(fmt, "%d", width); |
| 2275 | } |
| 2276 | if (precision) |
| 2277 | fmt += sprintf(fmt, ".%d", precision); |
| 2278 | if (longflag) |
| 2279 | *fmt++ = 'l'; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2280 | else if (longlongflag) { |
| 2281 | /* longlongflag should only ever be nonzero on machines with |
| 2282 | HAVE_LONG_LONG defined */ |
| 2283 | #ifdef HAVE_LONG_LONG |
| 2284 | char *f = PY_FORMAT_LONG_LONG; |
| 2285 | while (*f) |
| 2286 | *fmt++ = *f++; |
| 2287 | #else |
| 2288 | /* we shouldn't ever get here */ |
| 2289 | assert(0); |
| 2290 | *fmt++ = 'l'; |
| 2291 | #endif |
| 2292 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2293 | else if (size_tflag) { |
| 2294 | char *f = PY_FORMAT_SIZE_T; |
| 2295 | while (*f) |
| 2296 | *fmt++ = *f++; |
| 2297 | } |
| 2298 | *fmt++ = c; |
| 2299 | *fmt = '\0'; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2300 | } |
| 2301 | |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2302 | /* helper for PyUnicode_FromFormatV() */ |
| 2303 | |
| 2304 | static const char* |
| 2305 | parse_format_flags(const char *f, |
| 2306 | int *p_width, int *p_precision, |
| 2307 | int *p_longflag, int *p_longlongflag, int *p_size_tflag) |
| 2308 | { |
| 2309 | int width, precision, longflag, longlongflag, size_tflag; |
| 2310 | |
| 2311 | /* parse the width.precision part, e.g. "%2.5s" => width=2, precision=5 */ |
| 2312 | f++; |
| 2313 | width = 0; |
| 2314 | while (Py_ISDIGIT((unsigned)*f)) |
| 2315 | width = (width*10) + *f++ - '0'; |
| 2316 | precision = 0; |
| 2317 | if (*f == '.') { |
| 2318 | f++; |
| 2319 | while (Py_ISDIGIT((unsigned)*f)) |
| 2320 | precision = (precision*10) + *f++ - '0'; |
| 2321 | if (*f == '%') { |
| 2322 | /* "%.3%s" => f points to "3" */ |
| 2323 | f--; |
| 2324 | } |
| 2325 | } |
| 2326 | if (*f == '\0') { |
| 2327 | /* bogus format "%.1" => go backward, f points to "1" */ |
| 2328 | f--; |
| 2329 | } |
| 2330 | if (p_width != NULL) |
| 2331 | *p_width = width; |
| 2332 | if (p_precision != NULL) |
| 2333 | *p_precision = precision; |
| 2334 | |
| 2335 | /* Handle %ld, %lu, %lld and %llu. */ |
| 2336 | longflag = 0; |
| 2337 | longlongflag = 0; |
Victor Stinner | e7faec1 | 2011-03-02 00:01:53 +0000 | [diff] [blame] | 2338 | size_tflag = 0; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2339 | |
| 2340 | if (*f == 'l') { |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2341 | if (f[1] == 'd' || f[1] == 'u' || f[1] == 'i') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2342 | longflag = 1; |
| 2343 | ++f; |
| 2344 | } |
| 2345 | #ifdef HAVE_LONG_LONG |
| 2346 | else if (f[1] == 'l' && |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2347 | (f[2] == 'd' || f[2] == 'u' || f[2] == 'i')) { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2348 | longlongflag = 1; |
| 2349 | f += 2; |
| 2350 | } |
| 2351 | #endif |
| 2352 | } |
| 2353 | /* handle the size_t flag. */ |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2354 | else if (*f == 'z' && (f[1] == 'd' || f[1] == 'u' || f[1] == 'i')) { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2355 | size_tflag = 1; |
| 2356 | ++f; |
| 2357 | } |
| 2358 | if (p_longflag != NULL) |
| 2359 | *p_longflag = longflag; |
| 2360 | if (p_longlongflag != NULL) |
| 2361 | *p_longlongflag = longlongflag; |
| 2362 | if (p_size_tflag != NULL) |
| 2363 | *p_size_tflag = size_tflag; |
| 2364 | return f; |
| 2365 | } |
| 2366 | |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2367 | /* maximum number of characters required for output of %ld. 21 characters |
| 2368 | allows for 64-bit integers (in decimal) and an optional sign. */ |
| 2369 | #define MAX_LONG_CHARS 21 |
| 2370 | /* maximum number of characters required for output of %lld. |
| 2371 | We need at most ceil(log10(256)*SIZEOF_LONG_LONG) digits, |
| 2372 | plus 1 for the sign. 53/22 is an upper bound for log10(256). */ |
| 2373 | #define MAX_LONG_LONG_CHARS (2 + (SIZEOF_LONG_LONG*53-1) / 22) |
| 2374 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2375 | PyObject * |
| 2376 | PyUnicode_FromFormatV(const char *format, va_list vargs) |
| 2377 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2378 | va_list count; |
| 2379 | Py_ssize_t callcount = 0; |
| 2380 | PyObject **callresults = NULL; |
| 2381 | PyObject **callresult = NULL; |
| 2382 | Py_ssize_t n = 0; |
| 2383 | int width = 0; |
| 2384 | int precision = 0; |
| 2385 | int zeropad; |
| 2386 | const char* f; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2387 | PyObject *string; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2388 | /* used by sprintf */ |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2389 | char fmt[61]; /* should be enough for %0width.precisionlld */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2390 | Py_UCS4 maxchar = 127; /* result is ASCII by default */ |
| 2391 | Py_UCS4 argmaxchar; |
| 2392 | Py_ssize_t numbersize = 0; |
| 2393 | char *numberresults = NULL; |
| 2394 | char *numberresult = NULL; |
| 2395 | Py_ssize_t i; |
| 2396 | int kind; |
| 2397 | void *data; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2398 | |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 2399 | Py_VA_COPY(count, vargs); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2400 | /* step 1: count the number of %S/%R/%A/%s format specifications |
| 2401 | * (we call PyObject_Str()/PyObject_Repr()/PyObject_ASCII()/ |
| 2402 | * PyUnicode_DecodeUTF8() for these objects once during step 3 and put the |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2403 | * result in an array) |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 2404 | * also estimate a upper bound for all the number formats in the string, |
| 2405 | * numbers will be formatted in step 3 and be kept in a '\0'-separated |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2406 | * buffer before putting everything together. */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2407 | for (f = format; *f; f++) { |
| 2408 | if (*f == '%') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2409 | int longlongflag; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2410 | /* skip width or width.precision (eg. "1.2" of "%1.2f") */ |
| 2411 | f = parse_format_flags(f, &width, NULL, NULL, &longlongflag, NULL); |
| 2412 | if (*f == 's' || *f=='S' || *f=='R' || *f=='A' || *f=='V') |
| 2413 | ++callcount; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2414 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2415 | else if (*f == 'd' || *f=='u' || *f=='i' || *f=='x' || *f=='p') { |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2416 | #ifdef HAVE_LONG_LONG |
| 2417 | if (longlongflag) { |
| 2418 | if (width < MAX_LONG_LONG_CHARS) |
| 2419 | width = MAX_LONG_LONG_CHARS; |
| 2420 | } |
| 2421 | else |
| 2422 | #endif |
| 2423 | /* MAX_LONG_CHARS is enough to hold a 64-bit integer, |
| 2424 | including sign. Decimal takes the most space. This |
| 2425 | isn't enough for octal. If a width is specified we |
| 2426 | need more (which we allocate later). */ |
| 2427 | if (width < MAX_LONG_CHARS) |
| 2428 | width = MAX_LONG_CHARS; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2429 | |
| 2430 | /* account for the size + '\0' to separate numbers |
| 2431 | inside of the numberresults buffer */ |
| 2432 | numbersize += (width + 1); |
| 2433 | } |
| 2434 | } |
| 2435 | else if ((unsigned char)*f > 127) { |
| 2436 | PyErr_Format(PyExc_ValueError, |
| 2437 | "PyUnicode_FromFormatV() expects an ASCII-encoded format " |
| 2438 | "string, got a non-ASCII byte: 0x%02x", |
| 2439 | (unsigned char)*f); |
| 2440 | return NULL; |
| 2441 | } |
| 2442 | } |
| 2443 | /* step 2: allocate memory for the results of |
| 2444 | * PyObject_Str()/PyObject_Repr()/PyUnicode_DecodeUTF8() calls */ |
| 2445 | if (callcount) { |
| 2446 | callresults = PyObject_Malloc(sizeof(PyObject *) * callcount); |
| 2447 | if (!callresults) { |
| 2448 | PyErr_NoMemory(); |
| 2449 | return NULL; |
| 2450 | } |
| 2451 | callresult = callresults; |
| 2452 | } |
| 2453 | /* step 2.5: allocate memory for the results of formating numbers */ |
| 2454 | if (numbersize) { |
| 2455 | numberresults = PyObject_Malloc(numbersize); |
| 2456 | if (!numberresults) { |
| 2457 | PyErr_NoMemory(); |
| 2458 | goto fail; |
| 2459 | } |
| 2460 | numberresult = numberresults; |
| 2461 | } |
| 2462 | |
| 2463 | /* step 3: format numbers and figure out how large a buffer we need */ |
| 2464 | for (f = format; *f; f++) { |
| 2465 | if (*f == '%') { |
| 2466 | const char* p; |
| 2467 | int longflag; |
| 2468 | int longlongflag; |
| 2469 | int size_tflag; |
| 2470 | int numprinted; |
| 2471 | |
| 2472 | p = f; |
| 2473 | zeropad = (f[1] == '0'); |
| 2474 | f = parse_format_flags(f, &width, &precision, |
| 2475 | &longflag, &longlongflag, &size_tflag); |
| 2476 | switch (*f) { |
| 2477 | case 'c': |
| 2478 | { |
| 2479 | Py_UCS4 ordinal = va_arg(count, int); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 2480 | maxchar = MAX_MAXCHAR(maxchar, ordinal); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2481 | n++; |
| 2482 | break; |
| 2483 | } |
| 2484 | case '%': |
| 2485 | n++; |
| 2486 | break; |
| 2487 | case 'i': |
| 2488 | case 'd': |
| 2489 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, |
| 2490 | width, precision, *f); |
| 2491 | if (longflag) |
| 2492 | numprinted = sprintf(numberresult, fmt, |
| 2493 | va_arg(count, long)); |
| 2494 | #ifdef HAVE_LONG_LONG |
| 2495 | else if (longlongflag) |
| 2496 | numprinted = sprintf(numberresult, fmt, |
| 2497 | va_arg(count, PY_LONG_LONG)); |
| 2498 | #endif |
| 2499 | else if (size_tflag) |
| 2500 | numprinted = sprintf(numberresult, fmt, |
| 2501 | va_arg(count, Py_ssize_t)); |
| 2502 | else |
| 2503 | numprinted = sprintf(numberresult, fmt, |
| 2504 | va_arg(count, int)); |
| 2505 | n += numprinted; |
| 2506 | /* advance by +1 to skip over the '\0' */ |
| 2507 | numberresult += (numprinted + 1); |
| 2508 | assert(*(numberresult - 1) == '\0'); |
| 2509 | assert(*(numberresult - 2) != '\0'); |
| 2510 | assert(numprinted >= 0); |
| 2511 | assert(numberresult <= numberresults + numbersize); |
| 2512 | break; |
| 2513 | case 'u': |
| 2514 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, |
| 2515 | width, precision, 'u'); |
| 2516 | if (longflag) |
| 2517 | numprinted = sprintf(numberresult, fmt, |
| 2518 | va_arg(count, unsigned long)); |
| 2519 | #ifdef HAVE_LONG_LONG |
| 2520 | else if (longlongflag) |
| 2521 | numprinted = sprintf(numberresult, fmt, |
| 2522 | va_arg(count, unsigned PY_LONG_LONG)); |
| 2523 | #endif |
| 2524 | else if (size_tflag) |
| 2525 | numprinted = sprintf(numberresult, fmt, |
| 2526 | va_arg(count, size_t)); |
| 2527 | else |
| 2528 | numprinted = sprintf(numberresult, fmt, |
| 2529 | va_arg(count, unsigned int)); |
| 2530 | n += numprinted; |
| 2531 | numberresult += (numprinted + 1); |
| 2532 | assert(*(numberresult - 1) == '\0'); |
| 2533 | assert(*(numberresult - 2) != '\0'); |
| 2534 | assert(numprinted >= 0); |
| 2535 | assert(numberresult <= numberresults + numbersize); |
| 2536 | break; |
| 2537 | case 'x': |
| 2538 | makefmt(fmt, 0, 0, 0, zeropad, width, precision, 'x'); |
| 2539 | numprinted = sprintf(numberresult, fmt, va_arg(count, int)); |
| 2540 | n += numprinted; |
| 2541 | numberresult += (numprinted + 1); |
| 2542 | assert(*(numberresult - 1) == '\0'); |
| 2543 | assert(*(numberresult - 2) != '\0'); |
| 2544 | assert(numprinted >= 0); |
| 2545 | assert(numberresult <= numberresults + numbersize); |
| 2546 | break; |
| 2547 | case 'p': |
| 2548 | numprinted = sprintf(numberresult, "%p", va_arg(count, void*)); |
| 2549 | /* %p is ill-defined: ensure leading 0x. */ |
| 2550 | if (numberresult[1] == 'X') |
| 2551 | numberresult[1] = 'x'; |
| 2552 | else if (numberresult[1] != 'x') { |
| 2553 | memmove(numberresult + 2, numberresult, |
| 2554 | strlen(numberresult) + 1); |
| 2555 | numberresult[0] = '0'; |
| 2556 | numberresult[1] = 'x'; |
| 2557 | numprinted += 2; |
| 2558 | } |
| 2559 | n += numprinted; |
| 2560 | numberresult += (numprinted + 1); |
| 2561 | assert(*(numberresult - 1) == '\0'); |
| 2562 | assert(*(numberresult - 2) != '\0'); |
| 2563 | assert(numprinted >= 0); |
| 2564 | assert(numberresult <= numberresults + numbersize); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2565 | break; |
| 2566 | case 's': |
| 2567 | { |
| 2568 | /* UTF-8 */ |
Georg Brandl | 780b2a6 | 2009-05-05 09:19:59 +0000 | [diff] [blame] | 2569 | const char *s = va_arg(count, const char*); |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 2570 | PyObject *str = PyUnicode_DecodeUTF8Stateful(s, strlen(s), "replace", NULL); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2571 | if (!str) |
| 2572 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2573 | /* since PyUnicode_DecodeUTF8 returns already flexible |
| 2574 | unicode objects, there is no need to call ready on them */ |
| 2575 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 2576 | maxchar = MAX_MAXCHAR(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2577 | n += PyUnicode_GET_LENGTH(str); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2578 | /* Remember the str and switch to the next slot */ |
| 2579 | *callresult++ = str; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2580 | break; |
| 2581 | } |
| 2582 | case 'U': |
| 2583 | { |
| 2584 | PyObject *obj = va_arg(count, PyObject *); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 2585 | assert(obj && _PyUnicode_CHECK(obj)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2586 | if (PyUnicode_READY(obj) == -1) |
| 2587 | goto fail; |
| 2588 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(obj); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 2589 | maxchar = MAX_MAXCHAR(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2590 | n += PyUnicode_GET_LENGTH(obj); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2591 | break; |
| 2592 | } |
| 2593 | case 'V': |
| 2594 | { |
| 2595 | PyObject *obj = va_arg(count, PyObject *); |
| 2596 | const char *str = va_arg(count, const char *); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2597 | PyObject *str_obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2598 | assert(obj || str); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 2599 | assert(!obj || _PyUnicode_CHECK(obj)); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2600 | if (obj) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2601 | if (PyUnicode_READY(obj) == -1) |
| 2602 | goto fail; |
| 2603 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(obj); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 2604 | maxchar = MAX_MAXCHAR(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2605 | n += PyUnicode_GET_LENGTH(obj); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2606 | *callresult++ = NULL; |
| 2607 | } |
| 2608 | else { |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 2609 | str_obj = PyUnicode_DecodeUTF8Stateful(str, strlen(str), "replace", NULL); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2610 | if (!str_obj) |
| 2611 | goto fail; |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 2612 | if (PyUnicode_READY(str_obj) == -1) { |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 2613 | Py_DECREF(str_obj); |
| 2614 | goto fail; |
| 2615 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2616 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str_obj); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 2617 | maxchar = MAX_MAXCHAR(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2618 | n += PyUnicode_GET_LENGTH(str_obj); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2619 | *callresult++ = str_obj; |
| 2620 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2621 | break; |
| 2622 | } |
| 2623 | case 'S': |
| 2624 | { |
| 2625 | PyObject *obj = va_arg(count, PyObject *); |
| 2626 | PyObject *str; |
| 2627 | assert(obj); |
| 2628 | str = PyObject_Str(obj); |
Benjamin Peterson | c8d8b88 | 2012-01-14 13:37:31 -0500 | [diff] [blame] | 2629 | if (!str) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2630 | goto fail; |
Benjamin Peterson | c8d8b88 | 2012-01-14 13:37:31 -0500 | [diff] [blame] | 2631 | if (PyUnicode_READY(str) == -1) { |
| 2632 | Py_DECREF(str); |
| 2633 | goto fail; |
| 2634 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2635 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 2636 | maxchar = MAX_MAXCHAR(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2637 | n += PyUnicode_GET_LENGTH(str); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2638 | /* Remember the str and switch to the next slot */ |
| 2639 | *callresult++ = str; |
| 2640 | break; |
| 2641 | } |
| 2642 | case 'R': |
| 2643 | { |
| 2644 | PyObject *obj = va_arg(count, PyObject *); |
| 2645 | PyObject *repr; |
| 2646 | assert(obj); |
| 2647 | repr = PyObject_Repr(obj); |
Benjamin Peterson | c8d8b88 | 2012-01-14 13:37:31 -0500 | [diff] [blame] | 2648 | if (!repr) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2649 | goto fail; |
Benjamin Peterson | c8d8b88 | 2012-01-14 13:37:31 -0500 | [diff] [blame] | 2650 | if (PyUnicode_READY(repr) == -1) { |
| 2651 | Py_DECREF(repr); |
| 2652 | goto fail; |
| 2653 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2654 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(repr); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 2655 | maxchar = MAX_MAXCHAR(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2656 | n += PyUnicode_GET_LENGTH(repr); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2657 | /* Remember the repr and switch to the next slot */ |
| 2658 | *callresult++ = repr; |
| 2659 | break; |
| 2660 | } |
| 2661 | case 'A': |
| 2662 | { |
| 2663 | PyObject *obj = va_arg(count, PyObject *); |
| 2664 | PyObject *ascii; |
| 2665 | assert(obj); |
| 2666 | ascii = PyObject_ASCII(obj); |
Benjamin Peterson | c8d8b88 | 2012-01-14 13:37:31 -0500 | [diff] [blame] | 2667 | if (!ascii) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2668 | goto fail; |
Benjamin Peterson | c8d8b88 | 2012-01-14 13:37:31 -0500 | [diff] [blame] | 2669 | if (PyUnicode_READY(ascii) == -1) { |
| 2670 | Py_DECREF(ascii); |
| 2671 | goto fail; |
| 2672 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2673 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(ascii); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 2674 | maxchar = MAX_MAXCHAR(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2675 | n += PyUnicode_GET_LENGTH(ascii); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2676 | /* Remember the repr and switch to the next slot */ |
| 2677 | *callresult++ = ascii; |
| 2678 | break; |
| 2679 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2680 | default: |
| 2681 | /* if we stumble upon an unknown |
| 2682 | formatting code, copy the rest of |
| 2683 | the format string to the output |
| 2684 | string. (we cannot just skip the |
| 2685 | code, since there's no way to know |
| 2686 | what's in the argument list) */ |
| 2687 | n += strlen(p); |
| 2688 | goto expand; |
| 2689 | } |
| 2690 | } else |
| 2691 | n++; |
| 2692 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2693 | expand: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2694 | /* step 4: fill the buffer */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2695 | /* Since we've analyzed how much space we need, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2696 | we don't have to resize the string. |
| 2697 | There can be no errors beyond this point. */ |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2698 | string = PyUnicode_New(n, maxchar); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2699 | if (!string) |
| 2700 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2701 | kind = PyUnicode_KIND(string); |
| 2702 | data = PyUnicode_DATA(string); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2703 | callresult = callresults; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2704 | numberresult = numberresults; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2705 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2706 | for (i = 0, f = format; *f; f++) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2707 | if (*f == '%') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2708 | const char* p; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2709 | |
| 2710 | p = f; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2711 | f = parse_format_flags(f, NULL, NULL, NULL, NULL, NULL); |
| 2712 | /* checking for == because the last argument could be a empty |
| 2713 | string, which causes i to point to end, the assert at the end of |
| 2714 | the loop */ |
| 2715 | assert(i <= PyUnicode_GET_LENGTH(string)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2716 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2717 | switch (*f) { |
| 2718 | case 'c': |
Victor Stinner | 5ed8b2c | 2011-02-21 21:13:44 +0000 | [diff] [blame] | 2719 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2720 | const int ordinal = va_arg(vargs, int); |
| 2721 | PyUnicode_WRITE(kind, data, i++, ordinal); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2722 | break; |
Victor Stinner | 5ed8b2c | 2011-02-21 21:13:44 +0000 | [diff] [blame] | 2723 | } |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2724 | case 'i': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2725 | case 'd': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2726 | case 'u': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2727 | case 'x': |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2728 | case 'p': |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 2729 | { |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 2730 | Py_ssize_t len; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2731 | /* unused, since we already have the result */ |
| 2732 | if (*f == 'p') |
| 2733 | (void) va_arg(vargs, void *); |
| 2734 | else |
| 2735 | (void) va_arg(vargs, int); |
| 2736 | /* extract the result from numberresults and append. */ |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 2737 | len = strlen(numberresult); |
| 2738 | unicode_write_cstr(string, i, numberresult, len); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2739 | /* skip over the separating '\0' */ |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 2740 | i += len; |
| 2741 | numberresult += len; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2742 | assert(*numberresult == '\0'); |
| 2743 | numberresult++; |
| 2744 | assert(numberresult <= numberresults + numbersize); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2745 | break; |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 2746 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2747 | case 's': |
| 2748 | { |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2749 | /* unused, since we already have the result */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2750 | Py_ssize_t size; |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2751 | (void) va_arg(vargs, char *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2752 | size = PyUnicode_GET_LENGTH(*callresult); |
| 2753 | assert(PyUnicode_KIND(*callresult) <= PyUnicode_KIND(string)); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 2754 | _PyUnicode_FastCopyCharacters(string, i, *callresult, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2755 | i += size; |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2756 | /* We're done with the unicode()/repr() => forget it */ |
| 2757 | Py_DECREF(*callresult); |
| 2758 | /* switch to next unicode()/repr() result */ |
| 2759 | ++callresult; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2760 | break; |
| 2761 | } |
| 2762 | case 'U': |
| 2763 | { |
| 2764 | PyObject *obj = va_arg(vargs, PyObject *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2765 | Py_ssize_t size; |
| 2766 | assert(PyUnicode_KIND(obj) <= PyUnicode_KIND(string)); |
| 2767 | size = PyUnicode_GET_LENGTH(obj); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 2768 | _PyUnicode_FastCopyCharacters(string, i, obj, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2769 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2770 | break; |
| 2771 | } |
| 2772 | case 'V': |
| 2773 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2774 | Py_ssize_t size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2775 | PyObject *obj = va_arg(vargs, PyObject *); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2776 | va_arg(vargs, const char *); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2777 | if (obj) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2778 | size = PyUnicode_GET_LENGTH(obj); |
| 2779 | assert(PyUnicode_KIND(obj) <= PyUnicode_KIND(string)); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 2780 | _PyUnicode_FastCopyCharacters(string, i, obj, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2781 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2782 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2783 | size = PyUnicode_GET_LENGTH(*callresult); |
| 2784 | assert(PyUnicode_KIND(*callresult) <= |
| 2785 | PyUnicode_KIND(string)); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 2786 | _PyUnicode_FastCopyCharacters(string, i, *callresult, 0, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2787 | i += size; |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2788 | Py_DECREF(*callresult); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2789 | } |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2790 | ++callresult; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2791 | break; |
| 2792 | } |
| 2793 | case 'S': |
| 2794 | case 'R': |
Victor Stinner | 9a90900 | 2010-10-18 20:59:24 +0000 | [diff] [blame] | 2795 | case 'A': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2796 | { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2797 | Py_ssize_t size = PyUnicode_GET_LENGTH(*callresult); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2798 | /* unused, since we already have the result */ |
| 2799 | (void) va_arg(vargs, PyObject *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2800 | assert(PyUnicode_KIND(*callresult) <= PyUnicode_KIND(string)); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 2801 | _PyUnicode_FastCopyCharacters(string, i, *callresult, 0, size); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 2802 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2803 | /* We're done with the unicode()/repr() => forget it */ |
| 2804 | Py_DECREF(*callresult); |
| 2805 | /* switch to next unicode()/repr() result */ |
| 2806 | ++callresult; |
| 2807 | break; |
| 2808 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2809 | case '%': |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2810 | PyUnicode_WRITE(kind, data, i++, '%'); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2811 | break; |
| 2812 | default: |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 2813 | { |
| 2814 | Py_ssize_t len = strlen(p); |
| 2815 | unicode_write_cstr(string, i, p, len); |
| 2816 | i += len; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2817 | assert(i == PyUnicode_GET_LENGTH(string)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2818 | goto end; |
| 2819 | } |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 2820 | } |
Victor Stinner | 1205f27 | 2010-09-11 00:54:47 +0000 | [diff] [blame] | 2821 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2822 | else { |
| 2823 | assert(i < PyUnicode_GET_LENGTH(string)); |
| 2824 | PyUnicode_WRITE(kind, data, i++, *f); |
| 2825 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2826 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2827 | assert(i == PyUnicode_GET_LENGTH(string)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2828 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2829 | end: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2830 | if (callresults) |
| 2831 | PyObject_Free(callresults); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2832 | if (numberresults) |
| 2833 | PyObject_Free(numberresults); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2834 | return unicode_result(string); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2835 | fail: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2836 | if (callresults) { |
| 2837 | PyObject **callresult2 = callresults; |
| 2838 | while (callresult2 < callresult) { |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2839 | Py_XDECREF(*callresult2); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2840 | ++callresult2; |
| 2841 | } |
| 2842 | PyObject_Free(callresults); |
| 2843 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2844 | if (numberresults) |
| 2845 | PyObject_Free(numberresults); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2846 | return NULL; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2847 | } |
| 2848 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2849 | PyObject * |
| 2850 | PyUnicode_FromFormat(const char *format, ...) |
| 2851 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2852 | PyObject* ret; |
| 2853 | va_list vargs; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2854 | |
| 2855 | #ifdef HAVE_STDARG_PROTOTYPES |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2856 | va_start(vargs, format); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2857 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2858 | va_start(vargs); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2859 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2860 | ret = PyUnicode_FromFormatV(format, vargs); |
| 2861 | va_end(vargs); |
| 2862 | return ret; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2863 | } |
| 2864 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2865 | #ifdef HAVE_WCHAR_H |
| 2866 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2867 | /* Helper function for PyUnicode_AsWideChar() and PyUnicode_AsWideCharString(): |
| 2868 | convert a Unicode object to a wide character string. |
| 2869 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2870 | - If w is NULL: return the number of wide characters (including the null |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2871 | character) required to convert the unicode object. Ignore size argument. |
| 2872 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2873 | - Otherwise: return the number of wide characters (excluding the null |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2874 | character) written into w. Write at most size wide characters (including |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2875 | the null character). */ |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2876 | static Py_ssize_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2877 | unicode_aswidechar(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2878 | wchar_t *w, |
| 2879 | Py_ssize_t size) |
| 2880 | { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2881 | Py_ssize_t res; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2882 | const wchar_t *wstr; |
| 2883 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2884 | wstr = PyUnicode_AsUnicodeAndSize(unicode, &res); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2885 | if (wstr == NULL) |
| 2886 | return -1; |
| 2887 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2888 | if (w != NULL) { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2889 | if (size > res) |
| 2890 | size = res + 1; |
| 2891 | else |
| 2892 | res = size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2893 | Py_MEMCPY(w, wstr, size * sizeof(wchar_t)); |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2894 | return res; |
| 2895 | } |
| 2896 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2897 | return res + 1; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2898 | } |
| 2899 | |
| 2900 | Py_ssize_t |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2901 | PyUnicode_AsWideChar(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2902 | wchar_t *w, |
| 2903 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2904 | { |
| 2905 | if (unicode == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2906 | PyErr_BadInternalCall(); |
| 2907 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2908 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2909 | return unicode_aswidechar(unicode, w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2910 | } |
| 2911 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2912 | wchar_t* |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 2913 | PyUnicode_AsWideCharString(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2914 | Py_ssize_t *size) |
| 2915 | { |
| 2916 | wchar_t* buffer; |
| 2917 | Py_ssize_t buflen; |
| 2918 | |
| 2919 | if (unicode == NULL) { |
| 2920 | PyErr_BadInternalCall(); |
| 2921 | return NULL; |
| 2922 | } |
| 2923 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2924 | buflen = unicode_aswidechar(unicode, NULL, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2925 | if (buflen == -1) |
| 2926 | return NULL; |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2927 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < buflen) { |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2928 | PyErr_NoMemory(); |
| 2929 | return NULL; |
| 2930 | } |
| 2931 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2932 | buffer = PyMem_MALLOC(buflen * sizeof(wchar_t)); |
| 2933 | if (buffer == NULL) { |
| 2934 | PyErr_NoMemory(); |
| 2935 | return NULL; |
| 2936 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2937 | buflen = unicode_aswidechar(unicode, buffer, buflen); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2938 | if (buflen == -1) |
| 2939 | return NULL; |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2940 | if (size != NULL) |
| 2941 | *size = buflen; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2942 | return buffer; |
| 2943 | } |
| 2944 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2945 | #endif /* HAVE_WCHAR_H */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2946 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2947 | PyObject * |
| 2948 | PyUnicode_FromOrdinal(int ordinal) |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2949 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2950 | PyObject *v; |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 2951 | if (ordinal < 0 || ordinal > MAX_UNICODE) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2952 | PyErr_SetString(PyExc_ValueError, |
| 2953 | "chr() arg not in range(0x110000)"); |
| 2954 | return NULL; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2955 | } |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 2956 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2957 | if (ordinal < 256) |
| 2958 | return get_latin1_char(ordinal); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2959 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2960 | v = PyUnicode_New(1, ordinal); |
| 2961 | if (v == NULL) |
| 2962 | return NULL; |
| 2963 | PyUnicode_WRITE(PyUnicode_KIND(v), PyUnicode_DATA(v), 0, ordinal); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2964 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2965 | return v; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2966 | } |
| 2967 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2968 | PyObject * |
| 2969 | PyUnicode_FromObject(register PyObject *obj) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2970 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2971 | /* XXX Perhaps we should make this API an alias of |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2972 | PyObject_Str() instead ?! */ |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2973 | if (PyUnicode_CheckExact(obj)) { |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 2974 | if (PyUnicode_READY(obj) == -1) |
Victor Stinner | d3a83d5 | 2011-10-01 03:09:33 +0200 | [diff] [blame] | 2975 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2976 | Py_INCREF(obj); |
| 2977 | return obj; |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2978 | } |
| 2979 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2980 | /* For a Unicode subtype that's not a Unicode object, |
| 2981 | return a true Unicode object with the same data. */ |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 2982 | return _PyUnicode_Copy(obj); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2983 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2984 | PyErr_Format(PyExc_TypeError, |
| 2985 | "Can't convert '%.100s' object to str implicitly", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2986 | Py_TYPE(obj)->tp_name); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2987 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2988 | } |
| 2989 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2990 | PyObject * |
| 2991 | PyUnicode_FromEncodedObject(register PyObject *obj, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2992 | const char *encoding, |
| 2993 | const char *errors) |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2994 | { |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2995 | Py_buffer buffer; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2996 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2997 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2998 | if (obj == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2999 | PyErr_BadInternalCall(); |
| 3000 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3001 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 3002 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 3003 | /* Decoding bytes objects is the most common case and should be fast */ |
| 3004 | if (PyBytes_Check(obj)) { |
| 3005 | if (PyBytes_GET_SIZE(obj) == 0) { |
| 3006 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 3007 | v = unicode_empty; |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 3008 | } |
| 3009 | else { |
| 3010 | v = PyUnicode_Decode( |
| 3011 | PyBytes_AS_STRING(obj), PyBytes_GET_SIZE(obj), |
| 3012 | encoding, errors); |
| 3013 | } |
| 3014 | return v; |
| 3015 | } |
| 3016 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 3017 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3018 | PyErr_SetString(PyExc_TypeError, |
| 3019 | "decoding str is not supported"); |
| 3020 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3021 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 3022 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 3023 | /* Retrieve a bytes buffer view through the PEP 3118 buffer interface */ |
| 3024 | if (PyObject_GetBuffer(obj, &buffer, PyBUF_SIMPLE) < 0) { |
| 3025 | PyErr_Format(PyExc_TypeError, |
| 3026 | "coercing to str: need bytes, bytearray " |
| 3027 | "or buffer-like object, %.80s found", |
| 3028 | Py_TYPE(obj)->tp_name); |
| 3029 | return NULL; |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 3030 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3031 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 3032 | if (buffer.len == 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3033 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 3034 | v = unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3035 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3036 | else |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 3037 | v = PyUnicode_Decode((char*) buffer.buf, buffer.len, encoding, errors); |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 3038 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 3039 | PyBuffer_Release(&buffer); |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 3040 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3041 | } |
| 3042 | |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3043 | /* Convert encoding to lower case and replace '_' with '-' in order to |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3044 | catch e.g. UTF_8. Return 0 on error (encoding is longer than lower_len-1), |
| 3045 | 1 on success. */ |
| 3046 | static int |
| 3047 | normalize_encoding(const char *encoding, |
| 3048 | char *lower, |
| 3049 | size_t lower_len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3050 | { |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 3051 | const char *e; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3052 | char *l; |
| 3053 | char *l_end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3054 | |
Benjamin Peterson | 7a6debe | 2011-10-15 09:25:28 -0400 | [diff] [blame] | 3055 | if (encoding == NULL) { |
| 3056 | strcpy(lower, "utf-8"); |
| 3057 | return 1; |
| 3058 | } |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 3059 | e = encoding; |
| 3060 | l = lower; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3061 | l_end = &lower[lower_len - 1]; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3062 | while (*e) { |
| 3063 | if (l == l_end) |
| 3064 | return 0; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 3065 | if (Py_ISUPPER(*e)) { |
| 3066 | *l++ = Py_TOLOWER(*e++); |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 3067 | } |
| 3068 | else if (*e == '_') { |
| 3069 | *l++ = '-'; |
| 3070 | e++; |
| 3071 | } |
| 3072 | else { |
| 3073 | *l++ = *e++; |
| 3074 | } |
| 3075 | } |
| 3076 | *l = '\0'; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3077 | return 1; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3078 | } |
| 3079 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3080 | PyObject * |
| 3081 | PyUnicode_Decode(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3082 | Py_ssize_t size, |
| 3083 | const char *encoding, |
| 3084 | const char *errors) |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3085 | { |
| 3086 | PyObject *buffer = NULL, *unicode; |
| 3087 | Py_buffer info; |
| 3088 | char lower[11]; /* Enough for any encoding shortcut */ |
| 3089 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3090 | /* Shortcuts for common default encodings */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3091 | if (normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3092 | if ((strcmp(lower, "utf-8") == 0) || |
| 3093 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 3094 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3095 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3096 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3097 | (strcmp(lower, "iso-8859-1") == 0)) |
| 3098 | return PyUnicode_DecodeLatin1(s, size, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3099 | #ifdef HAVE_MBCS |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3100 | else if (strcmp(lower, "mbcs") == 0) |
| 3101 | return PyUnicode_DecodeMBCS(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 3102 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3103 | else if (strcmp(lower, "ascii") == 0) |
| 3104 | return PyUnicode_DecodeASCII(s, size, errors); |
| 3105 | else if (strcmp(lower, "utf-16") == 0) |
| 3106 | return PyUnicode_DecodeUTF16(s, size, errors, 0); |
| 3107 | else if (strcmp(lower, "utf-32") == 0) |
| 3108 | return PyUnicode_DecodeUTF32(s, size, errors, 0); |
| 3109 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3110 | |
| 3111 | /* Decode via the codec registry */ |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 3112 | buffer = NULL; |
Antoine Pitrou | c3b3924 | 2009-01-03 16:59:18 +0000 | [diff] [blame] | 3113 | 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] | 3114 | goto onError; |
Antoine Pitrou | ee58fa4 | 2008-08-19 18:22:14 +0000 | [diff] [blame] | 3115 | buffer = PyMemoryView_FromBuffer(&info); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3116 | if (buffer == NULL) |
| 3117 | goto onError; |
| 3118 | unicode = PyCodec_Decode(buffer, encoding, errors); |
| 3119 | if (unicode == NULL) |
| 3120 | goto onError; |
| 3121 | if (!PyUnicode_Check(unicode)) { |
| 3122 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3123 | "decoder did not return a str object (type=%.400s)", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 3124 | Py_TYPE(unicode)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3125 | Py_DECREF(unicode); |
| 3126 | goto onError; |
| 3127 | } |
| 3128 | Py_DECREF(buffer); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 3129 | return unicode_result(unicode); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3130 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3131 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3132 | Py_XDECREF(buffer); |
| 3133 | return NULL; |
| 3134 | } |
| 3135 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3136 | PyObject * |
| 3137 | PyUnicode_AsDecodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3138 | const char *encoding, |
| 3139 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3140 | { |
| 3141 | PyObject *v; |
| 3142 | |
| 3143 | if (!PyUnicode_Check(unicode)) { |
| 3144 | PyErr_BadArgument(); |
| 3145 | goto onError; |
| 3146 | } |
| 3147 | |
| 3148 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3149 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3150 | |
| 3151 | /* Decode via the codec registry */ |
| 3152 | v = PyCodec_Decode(unicode, encoding, errors); |
| 3153 | if (v == NULL) |
| 3154 | goto onError; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 3155 | return unicode_result(v); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3156 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3157 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3158 | return NULL; |
| 3159 | } |
| 3160 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3161 | PyObject * |
| 3162 | PyUnicode_AsDecodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3163 | const char *encoding, |
| 3164 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3165 | { |
| 3166 | PyObject *v; |
| 3167 | |
| 3168 | if (!PyUnicode_Check(unicode)) { |
| 3169 | PyErr_BadArgument(); |
| 3170 | goto onError; |
| 3171 | } |
| 3172 | |
| 3173 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3174 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3175 | |
| 3176 | /* Decode via the codec registry */ |
| 3177 | v = PyCodec_Decode(unicode, encoding, errors); |
| 3178 | if (v == NULL) |
| 3179 | goto onError; |
| 3180 | if (!PyUnicode_Check(v)) { |
| 3181 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3182 | "decoder did not return a str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3183 | Py_TYPE(v)->tp_name); |
| 3184 | Py_DECREF(v); |
| 3185 | goto onError; |
| 3186 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 3187 | return unicode_result(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3188 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3189 | onError: |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3190 | return NULL; |
| 3191 | } |
| 3192 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3193 | PyObject * |
| 3194 | PyUnicode_Encode(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3195 | Py_ssize_t size, |
| 3196 | const char *encoding, |
| 3197 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3198 | { |
| 3199 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3200 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3201 | unicode = PyUnicode_FromUnicode(s, size); |
| 3202 | if (unicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3203 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3204 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 3205 | Py_DECREF(unicode); |
| 3206 | return v; |
| 3207 | } |
| 3208 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3209 | PyObject * |
| 3210 | PyUnicode_AsEncodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3211 | const char *encoding, |
| 3212 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3213 | { |
| 3214 | PyObject *v; |
| 3215 | |
| 3216 | if (!PyUnicode_Check(unicode)) { |
| 3217 | PyErr_BadArgument(); |
| 3218 | goto onError; |
| 3219 | } |
| 3220 | |
| 3221 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3222 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3223 | |
| 3224 | /* Encode via the codec registry */ |
| 3225 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3226 | if (v == NULL) |
| 3227 | goto onError; |
| 3228 | return v; |
| 3229 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3230 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3231 | return NULL; |
| 3232 | } |
| 3233 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3234 | static size_t |
| 3235 | wcstombs_errorpos(const wchar_t *wstr) |
| 3236 | { |
| 3237 | size_t len; |
| 3238 | #if SIZEOF_WCHAR_T == 2 |
| 3239 | wchar_t buf[3]; |
| 3240 | #else |
| 3241 | wchar_t buf[2]; |
| 3242 | #endif |
| 3243 | char outbuf[MB_LEN_MAX]; |
| 3244 | const wchar_t *start, *previous; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3245 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3246 | #if SIZEOF_WCHAR_T == 2 |
| 3247 | buf[2] = 0; |
| 3248 | #else |
| 3249 | buf[1] = 0; |
| 3250 | #endif |
| 3251 | start = wstr; |
| 3252 | while (*wstr != L'\0') |
| 3253 | { |
| 3254 | previous = wstr; |
| 3255 | #if SIZEOF_WCHAR_T == 2 |
| 3256 | if (Py_UNICODE_IS_HIGH_SURROGATE(wstr[0]) |
| 3257 | && Py_UNICODE_IS_LOW_SURROGATE(wstr[1])) |
| 3258 | { |
| 3259 | buf[0] = wstr[0]; |
| 3260 | buf[1] = wstr[1]; |
| 3261 | wstr += 2; |
| 3262 | } |
| 3263 | else { |
| 3264 | buf[0] = *wstr; |
| 3265 | buf[1] = 0; |
| 3266 | wstr++; |
| 3267 | } |
| 3268 | #else |
| 3269 | buf[0] = *wstr; |
| 3270 | wstr++; |
| 3271 | #endif |
| 3272 | len = wcstombs(outbuf, buf, sizeof(outbuf)); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3273 | if (len == (size_t)-1) |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3274 | return previous - start; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3275 | } |
| 3276 | |
| 3277 | /* failed to find the unencodable character */ |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3278 | return 0; |
| 3279 | } |
| 3280 | |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3281 | static int |
| 3282 | locale_error_handler(const char *errors, int *surrogateescape) |
| 3283 | { |
| 3284 | if (errors == NULL) { |
| 3285 | *surrogateescape = 0; |
| 3286 | return 0; |
| 3287 | } |
| 3288 | |
| 3289 | if (strcmp(errors, "strict") == 0) { |
| 3290 | *surrogateescape = 0; |
| 3291 | return 0; |
| 3292 | } |
| 3293 | if (strcmp(errors, "surrogateescape") == 0) { |
| 3294 | *surrogateescape = 1; |
| 3295 | return 0; |
| 3296 | } |
| 3297 | PyErr_Format(PyExc_ValueError, |
| 3298 | "only 'strict' and 'surrogateescape' error handlers " |
| 3299 | "are supported, not '%s'", |
| 3300 | errors); |
| 3301 | return -1; |
| 3302 | } |
| 3303 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3304 | PyObject * |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3305 | PyUnicode_EncodeLocale(PyObject *unicode, const char *errors) |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3306 | { |
| 3307 | Py_ssize_t wlen, wlen2; |
| 3308 | wchar_t *wstr; |
| 3309 | PyObject *bytes = NULL; |
| 3310 | char *errmsg; |
Victor Stinner | 1f33f2b | 2011-12-17 04:45:09 +0100 | [diff] [blame] | 3311 | PyObject *reason; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3312 | PyObject *exc; |
| 3313 | size_t error_pos; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3314 | int surrogateescape; |
| 3315 | |
| 3316 | if (locale_error_handler(errors, &surrogateescape) < 0) |
| 3317 | return NULL; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3318 | |
| 3319 | wstr = PyUnicode_AsWideCharString(unicode, &wlen); |
| 3320 | if (wstr == NULL) |
| 3321 | return NULL; |
| 3322 | |
| 3323 | wlen2 = wcslen(wstr); |
| 3324 | if (wlen2 != wlen) { |
| 3325 | PyMem_Free(wstr); |
| 3326 | PyErr_SetString(PyExc_TypeError, "embedded null character"); |
| 3327 | return NULL; |
| 3328 | } |
| 3329 | |
| 3330 | if (surrogateescape) { |
| 3331 | /* locale encoding with surrogateescape */ |
| 3332 | char *str; |
| 3333 | |
| 3334 | str = _Py_wchar2char(wstr, &error_pos); |
| 3335 | if (str == NULL) { |
| 3336 | if (error_pos == (size_t)-1) { |
| 3337 | PyErr_NoMemory(); |
| 3338 | PyMem_Free(wstr); |
| 3339 | return NULL; |
| 3340 | } |
| 3341 | else { |
| 3342 | goto encode_error; |
| 3343 | } |
| 3344 | } |
| 3345 | PyMem_Free(wstr); |
| 3346 | |
| 3347 | bytes = PyBytes_FromString(str); |
| 3348 | PyMem_Free(str); |
| 3349 | } |
| 3350 | else { |
| 3351 | size_t len, len2; |
| 3352 | |
| 3353 | len = wcstombs(NULL, wstr, 0); |
| 3354 | if (len == (size_t)-1) { |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3355 | error_pos = (size_t)-1; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3356 | goto encode_error; |
| 3357 | } |
| 3358 | |
| 3359 | bytes = PyBytes_FromStringAndSize(NULL, len); |
| 3360 | if (bytes == NULL) { |
| 3361 | PyMem_Free(wstr); |
| 3362 | return NULL; |
| 3363 | } |
| 3364 | |
| 3365 | len2 = wcstombs(PyBytes_AS_STRING(bytes), wstr, len+1); |
| 3366 | if (len2 == (size_t)-1 || len2 > len) { |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3367 | error_pos = (size_t)-1; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3368 | goto encode_error; |
| 3369 | } |
| 3370 | PyMem_Free(wstr); |
| 3371 | } |
| 3372 | return bytes; |
| 3373 | |
| 3374 | encode_error: |
| 3375 | errmsg = strerror(errno); |
| 3376 | assert(errmsg != NULL); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3377 | |
| 3378 | if (error_pos == (size_t)-1) |
| 3379 | error_pos = wcstombs_errorpos(wstr); |
| 3380 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3381 | PyMem_Free(wstr); |
| 3382 | Py_XDECREF(bytes); |
| 3383 | |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3384 | if (errmsg != NULL) { |
| 3385 | size_t errlen; |
| 3386 | wstr = _Py_char2wchar(errmsg, &errlen); |
| 3387 | if (wstr != NULL) { |
| 3388 | reason = PyUnicode_FromWideChar(wstr, errlen); |
| 3389 | PyMem_Free(wstr); |
| 3390 | } else |
| 3391 | errmsg = NULL; |
| 3392 | } |
| 3393 | if (errmsg == NULL) |
Victor Stinner | 1f33f2b | 2011-12-17 04:45:09 +0100 | [diff] [blame] | 3394 | reason = PyUnicode_FromString( |
| 3395 | "wcstombs() encountered an unencodable " |
| 3396 | "wide character"); |
| 3397 | if (reason == NULL) |
| 3398 | return NULL; |
| 3399 | |
| 3400 | exc = PyObject_CallFunction(PyExc_UnicodeEncodeError, "sOnnO", |
| 3401 | "locale", unicode, |
| 3402 | (Py_ssize_t)error_pos, |
| 3403 | (Py_ssize_t)(error_pos+1), |
| 3404 | reason); |
| 3405 | Py_DECREF(reason); |
| 3406 | if (exc != NULL) { |
| 3407 | PyCodec_StrictErrors(exc); |
| 3408 | Py_XDECREF(exc); |
| 3409 | } |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3410 | return NULL; |
| 3411 | } |
| 3412 | |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3413 | PyObject * |
| 3414 | PyUnicode_EncodeFSDefault(PyObject *unicode) |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3415 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3416 | #ifdef HAVE_MBCS |
Victor Stinner | ac931b1 | 2011-11-20 18:27:03 +0100 | [diff] [blame] | 3417 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, NULL); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3418 | #elif defined(__APPLE__) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3419 | return _PyUnicode_AsUTF8String(unicode, "surrogateescape"); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3420 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 3421 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 3422 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 3423 | cannot use it to encode and decode filenames before it is loaded. Load |
| 3424 | the Python codec requires to encode at least its own filename. Use the C |
| 3425 | version of the locale codec until the codec registry is initialized and |
| 3426 | the Python codec is loaded. |
| 3427 | |
| 3428 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 3429 | cannot only rely on it: check also interp->fscodec_initialized for |
| 3430 | subinterpreters. */ |
| 3431 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3432 | return PyUnicode_AsEncodedString(unicode, |
| 3433 | Py_FileSystemDefaultEncoding, |
| 3434 | "surrogateescape"); |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 3435 | } |
| 3436 | else { |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3437 | return PyUnicode_EncodeLocale(unicode, "surrogateescape"); |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 3438 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3439 | #endif |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3440 | } |
| 3441 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3442 | PyObject * |
| 3443 | PyUnicode_AsEncodedString(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3444 | const char *encoding, |
| 3445 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3446 | { |
| 3447 | PyObject *v; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3448 | char lower[11]; /* Enough for any encoding shortcut */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3449 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3450 | if (!PyUnicode_Check(unicode)) { |
| 3451 | PyErr_BadArgument(); |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3452 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3453 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3454 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3455 | /* Shortcuts for common default encodings */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3456 | if (normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3457 | if ((strcmp(lower, "utf-8") == 0) || |
| 3458 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3459 | { |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3460 | if (errors == NULL || strcmp(errors, "strict") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3461 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3462 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3463 | return _PyUnicode_AsUTF8String(unicode, errors); |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3464 | } |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3465 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3466 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3467 | (strcmp(lower, "iso-8859-1") == 0)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3468 | return _PyUnicode_AsLatin1String(unicode, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3469 | #ifdef HAVE_MBCS |
Victor Stinner | ac931b1 | 2011-11-20 18:27:03 +0100 | [diff] [blame] | 3470 | else if (strcmp(lower, "mbcs") == 0) |
| 3471 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 3472 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3473 | else if (strcmp(lower, "ascii") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3474 | return _PyUnicode_AsASCIIString(unicode, errors); |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3475 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3476 | |
| 3477 | /* Encode via the codec registry */ |
| 3478 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3479 | if (v == NULL) |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3480 | return NULL; |
| 3481 | |
| 3482 | /* The normal path */ |
| 3483 | if (PyBytes_Check(v)) |
| 3484 | return v; |
| 3485 | |
| 3486 | /* 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] | 3487 | if (PyByteArray_Check(v)) { |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3488 | int error; |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3489 | PyObject *b; |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3490 | |
| 3491 | error = PyErr_WarnFormat(PyExc_RuntimeWarning, 1, |
| 3492 | "encoder %s returned bytearray instead of bytes", |
| 3493 | encoding); |
| 3494 | if (error) { |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3495 | Py_DECREF(v); |
| 3496 | return NULL; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3497 | } |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3498 | |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3499 | b = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(v), Py_SIZE(v)); |
| 3500 | Py_DECREF(v); |
| 3501 | return b; |
| 3502 | } |
| 3503 | |
| 3504 | PyErr_Format(PyExc_TypeError, |
| 3505 | "encoder did not return a bytes object (type=%.400s)", |
| 3506 | Py_TYPE(v)->tp_name); |
| 3507 | Py_DECREF(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3508 | return NULL; |
| 3509 | } |
| 3510 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3511 | PyObject * |
| 3512 | PyUnicode_AsEncodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3513 | const char *encoding, |
| 3514 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3515 | { |
| 3516 | PyObject *v; |
| 3517 | |
| 3518 | if (!PyUnicode_Check(unicode)) { |
| 3519 | PyErr_BadArgument(); |
| 3520 | goto onError; |
| 3521 | } |
| 3522 | |
| 3523 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3524 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3525 | |
| 3526 | /* Encode via the codec registry */ |
| 3527 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3528 | if (v == NULL) |
| 3529 | goto onError; |
| 3530 | if (!PyUnicode_Check(v)) { |
| 3531 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3532 | "encoder did not return an str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3533 | Py_TYPE(v)->tp_name); |
| 3534 | Py_DECREF(v); |
| 3535 | goto onError; |
| 3536 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3537 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3538 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3539 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3540 | return NULL; |
| 3541 | } |
| 3542 | |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3543 | static size_t |
| 3544 | mbstowcs_errorpos(const char *str, size_t len) |
| 3545 | { |
| 3546 | #ifdef HAVE_MBRTOWC |
| 3547 | const char *start = str; |
| 3548 | mbstate_t mbs; |
| 3549 | size_t converted; |
| 3550 | wchar_t ch; |
| 3551 | |
| 3552 | memset(&mbs, 0, sizeof mbs); |
| 3553 | while (len) |
| 3554 | { |
| 3555 | converted = mbrtowc(&ch, (char*)str, len, &mbs); |
| 3556 | if (converted == 0) |
| 3557 | /* Reached end of string */ |
| 3558 | break; |
| 3559 | if (converted == (size_t)-1 || converted == (size_t)-2) { |
| 3560 | /* Conversion error or incomplete character */ |
| 3561 | return str - start; |
| 3562 | } |
| 3563 | else { |
| 3564 | str += converted; |
| 3565 | len -= converted; |
| 3566 | } |
| 3567 | } |
| 3568 | /* failed to find the undecodable byte sequence */ |
| 3569 | return 0; |
| 3570 | #endif |
| 3571 | return 0; |
| 3572 | } |
| 3573 | |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3574 | PyObject* |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3575 | PyUnicode_DecodeLocaleAndSize(const char *str, Py_ssize_t len, |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3576 | const char *errors) |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3577 | { |
| 3578 | wchar_t smallbuf[256]; |
| 3579 | size_t smallbuf_len = Py_ARRAY_LENGTH(smallbuf); |
| 3580 | wchar_t *wstr; |
| 3581 | size_t wlen, wlen2; |
| 3582 | PyObject *unicode; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3583 | int surrogateescape; |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3584 | size_t error_pos; |
| 3585 | char *errmsg; |
| 3586 | PyObject *reason, *exc; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3587 | |
| 3588 | if (locale_error_handler(errors, &surrogateescape) < 0) |
| 3589 | return NULL; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3590 | |
| 3591 | if (str[len] != '\0' || len != strlen(str)) { |
| 3592 | PyErr_SetString(PyExc_TypeError, "embedded null character"); |
| 3593 | return NULL; |
| 3594 | } |
| 3595 | |
| 3596 | if (surrogateescape) |
| 3597 | { |
| 3598 | wstr = _Py_char2wchar(str, &wlen); |
| 3599 | if (wstr == NULL) { |
| 3600 | if (wlen == (size_t)-1) |
| 3601 | PyErr_NoMemory(); |
| 3602 | else |
| 3603 | PyErr_SetFromErrno(PyExc_OSError); |
| 3604 | return NULL; |
| 3605 | } |
| 3606 | |
| 3607 | unicode = PyUnicode_FromWideChar(wstr, wlen); |
| 3608 | PyMem_Free(wstr); |
| 3609 | } |
| 3610 | else { |
| 3611 | #ifndef HAVE_BROKEN_MBSTOWCS |
| 3612 | wlen = mbstowcs(NULL, str, 0); |
| 3613 | #else |
| 3614 | wlen = len; |
| 3615 | #endif |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3616 | if (wlen == (size_t)-1) |
| 3617 | goto decode_error; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3618 | if (wlen+1 <= smallbuf_len) { |
| 3619 | wstr = smallbuf; |
| 3620 | } |
| 3621 | else { |
| 3622 | if (wlen > PY_SSIZE_T_MAX / sizeof(wchar_t) - 1) |
| 3623 | return PyErr_NoMemory(); |
| 3624 | |
| 3625 | wstr = PyMem_Malloc((wlen+1) * sizeof(wchar_t)); |
| 3626 | if (!wstr) |
| 3627 | return PyErr_NoMemory(); |
| 3628 | } |
| 3629 | |
| 3630 | /* This shouldn't fail now */ |
| 3631 | wlen2 = mbstowcs(wstr, str, wlen+1); |
| 3632 | if (wlen2 == (size_t)-1) { |
| 3633 | if (wstr != smallbuf) |
| 3634 | PyMem_Free(wstr); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3635 | goto decode_error; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3636 | } |
| 3637 | #ifdef HAVE_BROKEN_MBSTOWCS |
| 3638 | assert(wlen2 == wlen); |
| 3639 | #endif |
| 3640 | unicode = PyUnicode_FromWideChar(wstr, wlen2); |
| 3641 | if (wstr != smallbuf) |
| 3642 | PyMem_Free(wstr); |
| 3643 | } |
| 3644 | return unicode; |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3645 | |
| 3646 | decode_error: |
| 3647 | errmsg = strerror(errno); |
| 3648 | assert(errmsg != NULL); |
| 3649 | |
| 3650 | error_pos = mbstowcs_errorpos(str, len); |
| 3651 | if (errmsg != NULL) { |
| 3652 | size_t errlen; |
| 3653 | wstr = _Py_char2wchar(errmsg, &errlen); |
| 3654 | if (wstr != NULL) { |
| 3655 | reason = PyUnicode_FromWideChar(wstr, errlen); |
| 3656 | PyMem_Free(wstr); |
| 3657 | } else |
| 3658 | errmsg = NULL; |
| 3659 | } |
| 3660 | if (errmsg == NULL) |
| 3661 | reason = PyUnicode_FromString( |
| 3662 | "mbstowcs() encountered an invalid multibyte sequence"); |
| 3663 | if (reason == NULL) |
| 3664 | return NULL; |
| 3665 | |
| 3666 | exc = PyObject_CallFunction(PyExc_UnicodeDecodeError, "sy#nnO", |
| 3667 | "locale", str, len, |
| 3668 | (Py_ssize_t)error_pos, |
| 3669 | (Py_ssize_t)(error_pos+1), |
| 3670 | reason); |
| 3671 | Py_DECREF(reason); |
| 3672 | if (exc != NULL) { |
| 3673 | PyCodec_StrictErrors(exc); |
| 3674 | Py_XDECREF(exc); |
| 3675 | } |
| 3676 | return NULL; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3677 | } |
| 3678 | |
| 3679 | PyObject* |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3680 | PyUnicode_DecodeLocale(const char *str, const char *errors) |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3681 | { |
| 3682 | Py_ssize_t size = (Py_ssize_t)strlen(str); |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3683 | return PyUnicode_DecodeLocaleAndSize(str, size, errors); |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3684 | } |
| 3685 | |
| 3686 | |
| 3687 | PyObject* |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3688 | PyUnicode_DecodeFSDefault(const char *s) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3689 | Py_ssize_t size = (Py_ssize_t)strlen(s); |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3690 | return PyUnicode_DecodeFSDefaultAndSize(s, size); |
| 3691 | } |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3692 | |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3693 | PyObject* |
| 3694 | PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size) |
| 3695 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3696 | #ifdef HAVE_MBCS |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3697 | return PyUnicode_DecodeMBCS(s, size, NULL); |
| 3698 | #elif defined(__APPLE__) |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 3699 | return PyUnicode_DecodeUTF8Stateful(s, size, "surrogateescape", NULL); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3700 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 3701 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 3702 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 3703 | cannot use it to encode and decode filenames before it is loaded. Load |
| 3704 | the Python codec requires to encode at least its own filename. Use the C |
| 3705 | version of the locale codec until the codec registry is initialized and |
| 3706 | the Python codec is loaded. |
| 3707 | |
| 3708 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 3709 | cannot only rely on it: check also interp->fscodec_initialized for |
| 3710 | subinterpreters. */ |
| 3711 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3712 | return PyUnicode_Decode(s, size, |
| 3713 | Py_FileSystemDefaultEncoding, |
Victor Stinner | b9a20ad | 2010-04-30 16:37:52 +0000 | [diff] [blame] | 3714 | "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3715 | } |
| 3716 | else { |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3717 | return PyUnicode_DecodeLocaleAndSize(s, size, "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3718 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3719 | #endif |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3720 | } |
| 3721 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3722 | |
| 3723 | int |
Antoine Pitrou | 1334884 | 2012-01-29 18:36:34 +0100 | [diff] [blame] | 3724 | _PyUnicode_HasNULChars(PyObject* s) |
| 3725 | { |
| 3726 | static PyObject *nul = NULL; |
| 3727 | |
| 3728 | if (nul == NULL) |
| 3729 | nul = PyUnicode_FromStringAndSize("\0", 1); |
| 3730 | if (nul == NULL) |
| 3731 | return -1; |
| 3732 | return PyUnicode_Contains(s, nul); |
| 3733 | } |
| 3734 | |
| 3735 | |
| 3736 | int |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3737 | PyUnicode_FSConverter(PyObject* arg, void* addr) |
| 3738 | { |
| 3739 | PyObject *output = NULL; |
| 3740 | Py_ssize_t size; |
| 3741 | void *data; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3742 | if (arg == NULL) { |
| 3743 | Py_DECREF(*(PyObject**)addr); |
| 3744 | return 1; |
| 3745 | } |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 3746 | if (PyBytes_Check(arg)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3747 | output = arg; |
| 3748 | Py_INCREF(output); |
| 3749 | } |
| 3750 | else { |
| 3751 | arg = PyUnicode_FromObject(arg); |
| 3752 | if (!arg) |
| 3753 | return 0; |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3754 | output = PyUnicode_EncodeFSDefault(arg); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3755 | Py_DECREF(arg); |
| 3756 | if (!output) |
| 3757 | return 0; |
| 3758 | if (!PyBytes_Check(output)) { |
| 3759 | Py_DECREF(output); |
| 3760 | PyErr_SetString(PyExc_TypeError, "encoder failed to return bytes"); |
| 3761 | return 0; |
| 3762 | } |
| 3763 | } |
Victor Stinner | 0ea2a46 | 2010-04-30 00:22:08 +0000 | [diff] [blame] | 3764 | size = PyBytes_GET_SIZE(output); |
| 3765 | data = PyBytes_AS_STRING(output); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3766 | if (size != strlen(data)) { |
Benjamin Peterson | 7a6b44a | 2011-08-18 13:51:47 -0500 | [diff] [blame] | 3767 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3768 | Py_DECREF(output); |
| 3769 | return 0; |
| 3770 | } |
| 3771 | *(PyObject**)addr = output; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3772 | return Py_CLEANUP_SUPPORTED; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3773 | } |
| 3774 | |
| 3775 | |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3776 | int |
| 3777 | PyUnicode_FSDecoder(PyObject* arg, void* addr) |
| 3778 | { |
| 3779 | PyObject *output = NULL; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3780 | if (arg == NULL) { |
| 3781 | Py_DECREF(*(PyObject**)addr); |
| 3782 | return 1; |
| 3783 | } |
| 3784 | if (PyUnicode_Check(arg)) { |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 3785 | if (PyUnicode_READY(arg) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3786 | return 0; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3787 | output = arg; |
| 3788 | Py_INCREF(output); |
| 3789 | } |
| 3790 | else { |
| 3791 | arg = PyBytes_FromObject(arg); |
| 3792 | if (!arg) |
| 3793 | return 0; |
| 3794 | output = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AS_STRING(arg), |
| 3795 | PyBytes_GET_SIZE(arg)); |
| 3796 | Py_DECREF(arg); |
| 3797 | if (!output) |
| 3798 | return 0; |
| 3799 | if (!PyUnicode_Check(output)) { |
| 3800 | Py_DECREF(output); |
| 3801 | PyErr_SetString(PyExc_TypeError, "decoder failed to return unicode"); |
| 3802 | return 0; |
| 3803 | } |
| 3804 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 3805 | if (PyUnicode_READY(output) == -1) { |
Victor Stinner | 065836e | 2011-10-27 01:56:33 +0200 | [diff] [blame] | 3806 | Py_DECREF(output); |
| 3807 | return 0; |
| 3808 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3809 | if (findchar(PyUnicode_DATA(output), PyUnicode_KIND(output), |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 3810 | PyUnicode_GET_LENGTH(output), 0, 1) >= 0) { |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3811 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 3812 | Py_DECREF(output); |
| 3813 | return 0; |
| 3814 | } |
| 3815 | *(PyObject**)addr = output; |
| 3816 | return Py_CLEANUP_SUPPORTED; |
| 3817 | } |
| 3818 | |
| 3819 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3820 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3821 | PyUnicode_AsUTF8AndSize(PyObject *unicode, Py_ssize_t *psize) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3822 | { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 3823 | PyObject *bytes; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3824 | |
Neal Norwitz | e0a0a6e | 2007-08-25 01:04:21 +0000 | [diff] [blame] | 3825 | if (!PyUnicode_Check(unicode)) { |
| 3826 | PyErr_BadArgument(); |
| 3827 | return NULL; |
| 3828 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3829 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3830 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3831 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3832 | if (PyUnicode_UTF8(unicode) == NULL) { |
| 3833 | assert(!PyUnicode_IS_COMPACT_ASCII(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3834 | bytes = _PyUnicode_AsUTF8String(unicode, "strict"); |
| 3835 | if (bytes == NULL) |
| 3836 | return NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3837 | _PyUnicode_UTF8(unicode) = PyObject_MALLOC(PyBytes_GET_SIZE(bytes) + 1); |
| 3838 | if (_PyUnicode_UTF8(unicode) == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3839 | Py_DECREF(bytes); |
| 3840 | return NULL; |
| 3841 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3842 | _PyUnicode_UTF8_LENGTH(unicode) = PyBytes_GET_SIZE(bytes); |
| 3843 | Py_MEMCPY(_PyUnicode_UTF8(unicode), |
| 3844 | PyBytes_AS_STRING(bytes), |
| 3845 | _PyUnicode_UTF8_LENGTH(unicode) + 1); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3846 | Py_DECREF(bytes); |
| 3847 | } |
| 3848 | |
| 3849 | if (psize) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3850 | *psize = PyUnicode_UTF8_LENGTH(unicode); |
| 3851 | return PyUnicode_UTF8(unicode); |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3852 | } |
| 3853 | |
| 3854 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3855 | PyUnicode_AsUTF8(PyObject *unicode) |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3856 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3857 | return PyUnicode_AsUTF8AndSize(unicode, NULL); |
| 3858 | } |
| 3859 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3860 | Py_UNICODE * |
| 3861 | PyUnicode_AsUnicodeAndSize(PyObject *unicode, Py_ssize_t *size) |
| 3862 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3863 | const unsigned char *one_byte; |
| 3864 | #if SIZEOF_WCHAR_T == 4 |
| 3865 | const Py_UCS2 *two_bytes; |
| 3866 | #else |
| 3867 | const Py_UCS4 *four_bytes; |
| 3868 | const Py_UCS4 *ucs4_end; |
| 3869 | Py_ssize_t num_surrogates; |
| 3870 | #endif |
| 3871 | wchar_t *w; |
| 3872 | wchar_t *wchar_end; |
| 3873 | |
| 3874 | if (!PyUnicode_Check(unicode)) { |
| 3875 | PyErr_BadArgument(); |
| 3876 | return NULL; |
| 3877 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3878 | if (_PyUnicode_WSTR(unicode) == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3879 | /* Non-ASCII compact unicode object */ |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3880 | assert(_PyUnicode_KIND(unicode) != 0); |
| 3881 | assert(PyUnicode_IS_READY(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3882 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3883 | if (PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3884 | #if SIZEOF_WCHAR_T == 2 |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3885 | four_bytes = PyUnicode_4BYTE_DATA(unicode); |
| 3886 | ucs4_end = four_bytes + _PyUnicode_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3887 | num_surrogates = 0; |
| 3888 | |
| 3889 | for (; four_bytes < ucs4_end; ++four_bytes) { |
| 3890 | if (*four_bytes > 0xFFFF) |
| 3891 | ++num_surrogates; |
| 3892 | } |
| 3893 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3894 | _PyUnicode_WSTR(unicode) = (wchar_t *) PyObject_MALLOC( |
| 3895 | sizeof(wchar_t) * (_PyUnicode_LENGTH(unicode) + 1 + num_surrogates)); |
| 3896 | if (!_PyUnicode_WSTR(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3897 | PyErr_NoMemory(); |
| 3898 | return NULL; |
| 3899 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3900 | _PyUnicode_WSTR_LENGTH(unicode) = _PyUnicode_LENGTH(unicode) + num_surrogates; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3901 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3902 | w = _PyUnicode_WSTR(unicode); |
| 3903 | wchar_end = w + _PyUnicode_WSTR_LENGTH(unicode); |
| 3904 | four_bytes = PyUnicode_4BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3905 | for (; four_bytes < ucs4_end; ++four_bytes, ++w) { |
| 3906 | if (*four_bytes > 0xFFFF) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 3907 | assert(*four_bytes <= MAX_UNICODE); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3908 | /* encode surrogate pair in this case */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 3909 | *w++ = Py_UNICODE_HIGH_SURROGATE(*four_bytes); |
| 3910 | *w = Py_UNICODE_LOW_SURROGATE(*four_bytes); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3911 | } |
| 3912 | else |
| 3913 | *w = *four_bytes; |
| 3914 | |
| 3915 | if (w > wchar_end) { |
| 3916 | assert(0 && "Miscalculated string end"); |
| 3917 | } |
| 3918 | } |
| 3919 | *w = 0; |
| 3920 | #else |
| 3921 | /* sizeof(wchar_t) == 4 */ |
| 3922 | Py_FatalError("Impossible unicode object state, wstr and str " |
| 3923 | "should share memory already."); |
| 3924 | return NULL; |
| 3925 | #endif |
| 3926 | } |
| 3927 | else { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3928 | _PyUnicode_WSTR(unicode) = (wchar_t *) PyObject_MALLOC(sizeof(wchar_t) * |
| 3929 | (_PyUnicode_LENGTH(unicode) + 1)); |
| 3930 | if (!_PyUnicode_WSTR(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3931 | PyErr_NoMemory(); |
| 3932 | return NULL; |
| 3933 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3934 | if (!PyUnicode_IS_COMPACT_ASCII(unicode)) |
| 3935 | _PyUnicode_WSTR_LENGTH(unicode) = _PyUnicode_LENGTH(unicode); |
| 3936 | w = _PyUnicode_WSTR(unicode); |
| 3937 | wchar_end = w + _PyUnicode_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3938 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3939 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) { |
| 3940 | one_byte = PyUnicode_1BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3941 | for (; w < wchar_end; ++one_byte, ++w) |
| 3942 | *w = *one_byte; |
| 3943 | /* null-terminate the wstr */ |
| 3944 | *w = 0; |
| 3945 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3946 | else if (PyUnicode_KIND(unicode) == PyUnicode_2BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3947 | #if SIZEOF_WCHAR_T == 4 |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3948 | two_bytes = PyUnicode_2BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3949 | for (; w < wchar_end; ++two_bytes, ++w) |
| 3950 | *w = *two_bytes; |
| 3951 | /* null-terminate the wstr */ |
| 3952 | *w = 0; |
| 3953 | #else |
| 3954 | /* sizeof(wchar_t) == 2 */ |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3955 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 3956 | _PyUnicode_WSTR(unicode) = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3957 | Py_FatalError("Impossible unicode object state, wstr " |
| 3958 | "and str should share memory already."); |
| 3959 | return NULL; |
| 3960 | #endif |
| 3961 | } |
| 3962 | else { |
| 3963 | assert(0 && "This should never happen."); |
| 3964 | } |
| 3965 | } |
| 3966 | } |
| 3967 | if (size != NULL) |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3968 | *size = PyUnicode_WSTR_LENGTH(unicode); |
| 3969 | return _PyUnicode_WSTR(unicode); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3970 | } |
| 3971 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3972 | Py_UNICODE * |
| 3973 | PyUnicode_AsUnicode(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3974 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3975 | return PyUnicode_AsUnicodeAndSize(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3976 | } |
| 3977 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3978 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3979 | Py_ssize_t |
| 3980 | PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3981 | { |
| 3982 | if (!PyUnicode_Check(unicode)) { |
| 3983 | PyErr_BadArgument(); |
| 3984 | goto onError; |
| 3985 | } |
| 3986 | return PyUnicode_GET_SIZE(unicode); |
| 3987 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3988 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3989 | return -1; |
| 3990 | } |
| 3991 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3992 | Py_ssize_t |
| 3993 | PyUnicode_GetLength(PyObject *unicode) |
| 3994 | { |
Victor Stinner | 0762133 | 2012-06-16 04:53:46 +0200 | [diff] [blame] | 3995 | if (!PyUnicode_Check(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3996 | PyErr_BadArgument(); |
| 3997 | return -1; |
| 3998 | } |
Victor Stinner | 0762133 | 2012-06-16 04:53:46 +0200 | [diff] [blame] | 3999 | if (PyUnicode_READY(unicode) == -1) |
| 4000 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4001 | return PyUnicode_GET_LENGTH(unicode); |
| 4002 | } |
| 4003 | |
| 4004 | Py_UCS4 |
| 4005 | PyUnicode_ReadChar(PyObject *unicode, Py_ssize_t index) |
| 4006 | { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 4007 | if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) { |
| 4008 | PyErr_BadArgument(); |
| 4009 | return (Py_UCS4)-1; |
| 4010 | } |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 4011 | if (index < 0 || index >= PyUnicode_GET_LENGTH(unicode)) { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 4012 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4013 | return (Py_UCS4)-1; |
| 4014 | } |
| 4015 | return PyUnicode_READ_CHAR(unicode, index); |
| 4016 | } |
| 4017 | |
| 4018 | int |
| 4019 | PyUnicode_WriteChar(PyObject *unicode, Py_ssize_t index, Py_UCS4 ch) |
| 4020 | { |
| 4021 | if (!PyUnicode_Check(unicode) || !PyUnicode_IS_COMPACT(unicode)) { |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 4022 | PyErr_BadArgument(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4023 | return -1; |
| 4024 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 4025 | assert(PyUnicode_IS_READY(unicode)); |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 4026 | if (index < 0 || index >= PyUnicode_GET_LENGTH(unicode)) { |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 4027 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 4028 | return -1; |
| 4029 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 4030 | if (unicode_check_modifiable(unicode)) |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 4031 | return -1; |
Victor Stinner | c9590ad | 2012-03-04 01:34:37 +0100 | [diff] [blame] | 4032 | if (ch > PyUnicode_MAX_CHAR_VALUE(unicode)) { |
| 4033 | PyErr_SetString(PyExc_ValueError, "character out of range"); |
| 4034 | return -1; |
| 4035 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4036 | PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), |
| 4037 | index, ch); |
| 4038 | return 0; |
| 4039 | } |
| 4040 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4041 | const char * |
| 4042 | PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 4043 | { |
Victor Stinner | 42cb462 | 2010-09-01 19:39:01 +0000 | [diff] [blame] | 4044 | return "utf-8"; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 4045 | } |
| 4046 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4047 | /* create or adjust a UnicodeDecodeError */ |
| 4048 | static void |
| 4049 | make_decode_exception(PyObject **exceptionObject, |
| 4050 | const char *encoding, |
| 4051 | const char *input, Py_ssize_t length, |
| 4052 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 4053 | const char *reason) |
| 4054 | { |
| 4055 | if (*exceptionObject == NULL) { |
| 4056 | *exceptionObject = PyUnicodeDecodeError_Create( |
| 4057 | encoding, input, length, startpos, endpos, reason); |
| 4058 | } |
| 4059 | else { |
| 4060 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, startpos)) |
| 4061 | goto onError; |
| 4062 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, endpos)) |
| 4063 | goto onError; |
| 4064 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 4065 | goto onError; |
| 4066 | } |
| 4067 | return; |
| 4068 | |
| 4069 | onError: |
| 4070 | Py_DECREF(*exceptionObject); |
| 4071 | *exceptionObject = NULL; |
| 4072 | } |
| 4073 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4074 | /* error handling callback helper: |
| 4075 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 4076 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4077 | and adjust various state variables. |
| 4078 | return 0 on success, -1 on error |
| 4079 | */ |
| 4080 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4081 | static int |
| 4082 | unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4083 | const char *encoding, const char *reason, |
| 4084 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 4085 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4086 | PyObject **output, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4087 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 4088 | 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] | 4089 | |
| 4090 | PyObject *restuple = NULL; |
| 4091 | PyObject *repunicode = NULL; |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 4092 | Py_ssize_t outsize; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4093 | Py_ssize_t insize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4094 | Py_ssize_t requiredsize; |
| 4095 | Py_ssize_t newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4096 | PyObject *inputobj = NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4097 | int res = -1; |
| 4098 | |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 4099 | if (_PyUnicode_KIND(*output) != PyUnicode_WCHAR_KIND) |
| 4100 | outsize = PyUnicode_GET_LENGTH(*output); |
| 4101 | else |
| 4102 | outsize = _PyUnicode_WSTR_LENGTH(*output); |
| 4103 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4104 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4105 | *errorHandler = PyCodec_LookupError(errors); |
| 4106 | if (*errorHandler == NULL) |
| 4107 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4108 | } |
| 4109 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4110 | make_decode_exception(exceptionObject, |
| 4111 | encoding, |
| 4112 | *input, *inend - *input, |
| 4113 | *startinpos, *endinpos, |
| 4114 | reason); |
| 4115 | if (*exceptionObject == NULL) |
| 4116 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4117 | |
| 4118 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 4119 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4120 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4121 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 4122 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4123 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4124 | } |
| 4125 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4126 | goto onError; |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 4127 | if (PyUnicode_READY(repunicode) == -1) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4128 | goto onError; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4129 | |
| 4130 | /* Copy back the bytes variables, which might have been modified by the |
| 4131 | callback */ |
| 4132 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 4133 | if (!inputobj) |
| 4134 | goto onError; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4135 | if (!PyBytes_Check(inputobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4136 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4137 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4138 | *input = PyBytes_AS_STRING(inputobj); |
| 4139 | insize = PyBytes_GET_SIZE(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4140 | *inend = *input + insize; |
Walter Dörwald | 36f938f | 2007-08-10 10:11:43 +0000 | [diff] [blame] | 4141 | /* we can DECREF safely, as the exception has another reference, |
| 4142 | so the object won't go away. */ |
| 4143 | Py_DECREF(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4144 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4145 | if (newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4146 | newpos = insize+newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4147 | if (newpos<0 || newpos>insize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4148 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos); |
| 4149 | goto onError; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4150 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4151 | |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 4152 | if (_PyUnicode_KIND(*output) != PyUnicode_WCHAR_KIND) { |
| 4153 | /* need more space? (at least enough for what we |
| 4154 | have+the replacement+the rest of the string (starting |
| 4155 | at the new input position), so we won't have to check space |
| 4156 | when there are no errors in the rest of the string) */ |
| 4157 | Py_ssize_t replen = PyUnicode_GET_LENGTH(repunicode); |
| 4158 | requiredsize = *outpos + replen + insize-newpos; |
| 4159 | if (requiredsize > outsize) { |
| 4160 | if (requiredsize<2*outsize) |
| 4161 | requiredsize = 2*outsize; |
| 4162 | if (unicode_resize(output, requiredsize) < 0) |
| 4163 | goto onError; |
| 4164 | } |
Victor Stinner | 1b487b4 | 2012-05-03 12:29:04 +0200 | [diff] [blame] | 4165 | if (unicode_widen(output, *outpos, |
| 4166 | PyUnicode_MAX_CHAR_VALUE(repunicode)) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4167 | goto onError; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 4168 | _PyUnicode_FastCopyCharacters(*output, *outpos, repunicode, 0, replen); |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 4169 | *outpos += replen; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4170 | } |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 4171 | else { |
| 4172 | wchar_t *repwstr; |
| 4173 | Py_ssize_t repwlen; |
| 4174 | repwstr = PyUnicode_AsUnicodeAndSize(repunicode, &repwlen); |
| 4175 | if (repwstr == NULL) |
| 4176 | goto onError; |
| 4177 | /* need more space? (at least enough for what we |
| 4178 | have+the replacement+the rest of the string (starting |
| 4179 | at the new input position), so we won't have to check space |
| 4180 | when there are no errors in the rest of the string) */ |
| 4181 | requiredsize = *outpos + repwlen + insize-newpos; |
| 4182 | if (requiredsize > outsize) { |
| 4183 | if (requiredsize < 2*outsize) |
| 4184 | requiredsize = 2*outsize; |
| 4185 | if (unicode_resize(output, requiredsize) < 0) |
| 4186 | goto onError; |
| 4187 | } |
| 4188 | wcsncpy(_PyUnicode_WSTR(*output) + *outpos, repwstr, repwlen); |
| 4189 | *outpos += repwlen; |
| 4190 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4191 | *endinpos = newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4192 | *inptr = *input + newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4193 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4194 | /* we made it! */ |
| 4195 | res = 0; |
| 4196 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4197 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4198 | Py_XDECREF(restuple); |
| 4199 | return res; |
| 4200 | } |
| 4201 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4202 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 4203 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4204 | /* See RFC2152 for details. We encode conservatively and decode liberally. */ |
| 4205 | |
| 4206 | /* Three simple macros defining base-64. */ |
| 4207 | |
| 4208 | /* Is c a base-64 character? */ |
| 4209 | |
| 4210 | #define IS_BASE64(c) \ |
| 4211 | (((c) >= 'A' && (c) <= 'Z') || \ |
| 4212 | ((c) >= 'a' && (c) <= 'z') || \ |
| 4213 | ((c) >= '0' && (c) <= '9') || \ |
| 4214 | (c) == '+' || (c) == '/') |
| 4215 | |
| 4216 | /* given that c is a base-64 character, what is its base-64 value? */ |
| 4217 | |
| 4218 | #define FROM_BASE64(c) \ |
| 4219 | (((c) >= 'A' && (c) <= 'Z') ? (c) - 'A' : \ |
| 4220 | ((c) >= 'a' && (c) <= 'z') ? (c) - 'a' + 26 : \ |
| 4221 | ((c) >= '0' && (c) <= '9') ? (c) - '0' + 52 : \ |
| 4222 | (c) == '+' ? 62 : 63) |
| 4223 | |
| 4224 | /* What is the base-64 character of the bottom 6 bits of n? */ |
| 4225 | |
| 4226 | #define TO_BASE64(n) \ |
| 4227 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 4228 | |
| 4229 | /* DECODE_DIRECT: this byte encountered in a UTF-7 string should be |
| 4230 | * decoded as itself. We are permissive on decoding; the only ASCII |
| 4231 | * byte not decoding to itself is the + which begins a base64 |
| 4232 | * string. */ |
| 4233 | |
| 4234 | #define DECODE_DIRECT(c) \ |
| 4235 | ((c) <= 127 && (c) != '+') |
| 4236 | |
| 4237 | /* The UTF-7 encoder treats ASCII characters differently according to |
| 4238 | * whether they are Set D, Set O, Whitespace, or special (i.e. none of |
| 4239 | * the above). See RFC2152. This array identifies these different |
| 4240 | * sets: |
| 4241 | * 0 : "Set D" |
| 4242 | * alphanumeric and '(),-./:? |
| 4243 | * 1 : "Set O" |
| 4244 | * !"#$%&*;<=>@[]^_`{|} |
| 4245 | * 2 : "whitespace" |
| 4246 | * ht nl cr sp |
| 4247 | * 3 : special (must be base64 encoded) |
| 4248 | * everything else (i.e. +\~ and non-printing codes 0-8 11-12 14-31 127) |
| 4249 | */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4250 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4251 | static |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4252 | char utf7_category[128] = { |
| 4253 | /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */ |
| 4254 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 3, 3, 2, 3, 3, |
| 4255 | /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */ |
| 4256 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 4257 | /* sp ! " # $ % & ' ( ) * + , - . / */ |
| 4258 | 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 3, 0, 0, 0, 0, |
| 4259 | /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */ |
| 4260 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, |
| 4261 | /* @ A B C D E F G H I J K L M N O */ |
| 4262 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4263 | /* P Q R S T U V W X Y Z [ \ ] ^ _ */ |
| 4264 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 1, 1, 1, |
| 4265 | /* ` a b c d e f g h i j k l m n o */ |
| 4266 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4267 | /* p q r s t u v w x y z { | } ~ del */ |
| 4268 | 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] | 4269 | }; |
| 4270 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4271 | /* ENCODE_DIRECT: this character should be encoded as itself. The |
| 4272 | * answer depends on whether we are encoding set O as itself, and also |
| 4273 | * on whether we are encoding whitespace as itself. RFC2152 makes it |
| 4274 | * clear that the answers to these questions vary between |
| 4275 | * applications, so this code needs to be flexible. */ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 4276 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4277 | #define ENCODE_DIRECT(c, directO, directWS) \ |
| 4278 | ((c) < 128 && (c) > 0 && \ |
| 4279 | ((utf7_category[(c)] == 0) || \ |
| 4280 | (directWS && (utf7_category[(c)] == 2)) || \ |
| 4281 | (directO && (utf7_category[(c)] == 1)))) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4282 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4283 | PyObject * |
| 4284 | PyUnicode_DecodeUTF7(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4285 | Py_ssize_t size, |
| 4286 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4287 | { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4288 | return PyUnicode_DecodeUTF7Stateful(s, size, errors, NULL); |
| 4289 | } |
| 4290 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4291 | /* The decoder. The only state we preserve is our read position, |
| 4292 | * i.e. how many characters we have consumed. So if we end in the |
| 4293 | * middle of a shift sequence we have to back off the read position |
| 4294 | * and the output to the beginning of the sequence, otherwise we lose |
| 4295 | * all the shift state (seen bits, number of bits seen, high |
| 4296 | * surrogate). */ |
| 4297 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4298 | PyObject * |
| 4299 | PyUnicode_DecodeUTF7Stateful(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4300 | Py_ssize_t size, |
| 4301 | const char *errors, |
| 4302 | Py_ssize_t *consumed) |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4303 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4304 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4305 | Py_ssize_t startinpos; |
| 4306 | Py_ssize_t endinpos; |
| 4307 | Py_ssize_t outpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4308 | const char *e; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4309 | PyObject *unicode; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4310 | const char *errmsg = ""; |
| 4311 | int inShift = 0; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4312 | Py_ssize_t shiftOutStart; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4313 | unsigned int base64bits = 0; |
| 4314 | unsigned long base64buffer = 0; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 4315 | Py_UCS4 surrogate = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4316 | PyObject *errorHandler = NULL; |
| 4317 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4318 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4319 | /* Start off assuming it's all ASCII. Widen later as necessary. */ |
| 4320 | unicode = PyUnicode_New(size, 127); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4321 | if (!unicode) |
| 4322 | return NULL; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4323 | if (size == 0) { |
| 4324 | if (consumed) |
| 4325 | *consumed = 0; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4326 | return unicode; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4327 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4328 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4329 | shiftOutStart = outpos = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4330 | e = s + size; |
| 4331 | |
| 4332 | while (s < e) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4333 | Py_UCS4 ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4334 | restart: |
Antoine Pitrou | 5ffd9e9 | 2008-07-25 18:05:24 +0000 | [diff] [blame] | 4335 | ch = (unsigned char) *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4336 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4337 | if (inShift) { /* in a base-64 section */ |
| 4338 | if (IS_BASE64(ch)) { /* consume a base-64 character */ |
| 4339 | base64buffer = (base64buffer << 6) | FROM_BASE64(ch); |
| 4340 | base64bits += 6; |
| 4341 | s++; |
| 4342 | if (base64bits >= 16) { |
| 4343 | /* we have enough bits for a UTF-16 value */ |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 4344 | Py_UCS4 outCh = (Py_UCS4)(base64buffer >> (base64bits-16)); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4345 | base64bits -= 16; |
| 4346 | base64buffer &= (1 << base64bits) - 1; /* clear high bits */ |
| 4347 | if (surrogate) { |
| 4348 | /* expecting a second surrogate */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4349 | if (Py_UNICODE_IS_LOW_SURROGATE(outCh)) { |
| 4350 | Py_UCS4 ch2 = Py_UNICODE_JOIN_SURROGATES(surrogate, outCh); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4351 | if (unicode_putchar(&unicode, &outpos, ch2) < 0) |
| 4352 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4353 | surrogate = 0; |
Antoine Pitrou | 5418ee0 | 2011-11-15 01:42:21 +0100 | [diff] [blame] | 4354 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4355 | } |
| 4356 | else { |
Antoine Pitrou | 78edf75 | 2011-11-15 01:44:16 +0100 | [diff] [blame] | 4357 | if (unicode_putchar(&unicode, &outpos, surrogate) < 0) |
| 4358 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4359 | surrogate = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4360 | } |
| 4361 | } |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4362 | if (Py_UNICODE_IS_HIGH_SURROGATE(outCh)) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4363 | /* first surrogate */ |
| 4364 | surrogate = outCh; |
| 4365 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4366 | else { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4367 | if (unicode_putchar(&unicode, &outpos, outCh) < 0) |
| 4368 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4369 | } |
| 4370 | } |
| 4371 | } |
| 4372 | else { /* now leaving a base-64 section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4373 | inShift = 0; |
| 4374 | s++; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4375 | if (surrogate) { |
Antoine Pitrou | 78edf75 | 2011-11-15 01:44:16 +0100 | [diff] [blame] | 4376 | if (unicode_putchar(&unicode, &outpos, surrogate) < 0) |
| 4377 | goto onError; |
Antoine Pitrou | 5418ee0 | 2011-11-15 01:42:21 +0100 | [diff] [blame] | 4378 | surrogate = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4379 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4380 | if (base64bits > 0) { /* left-over bits */ |
| 4381 | if (base64bits >= 6) { |
| 4382 | /* We've seen at least one base-64 character */ |
| 4383 | errmsg = "partial character in shift sequence"; |
| 4384 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4385 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4386 | else { |
| 4387 | /* Some bits remain; they should be zero */ |
| 4388 | if (base64buffer != 0) { |
| 4389 | errmsg = "non-zero padding bits in shift sequence"; |
| 4390 | goto utf7Error; |
| 4391 | } |
| 4392 | } |
| 4393 | } |
| 4394 | if (ch != '-') { |
| 4395 | /* '-' is absorbed; other terminating |
| 4396 | characters are preserved */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4397 | if (unicode_putchar(&unicode, &outpos, ch) < 0) |
| 4398 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4399 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4400 | } |
| 4401 | } |
| 4402 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4403 | startinpos = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4404 | s++; /* consume '+' */ |
| 4405 | if (s < e && *s == '-') { /* '+-' encodes '+' */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4406 | s++; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4407 | if (unicode_putchar(&unicode, &outpos, '+') < 0) |
| 4408 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4409 | } |
| 4410 | else { /* begin base64-encoded section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4411 | inShift = 1; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4412 | shiftOutStart = outpos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4413 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4414 | } |
| 4415 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4416 | else if (DECODE_DIRECT(ch)) { /* character decodes as itself */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4417 | if (unicode_putchar(&unicode, &outpos, ch) < 0) |
| 4418 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4419 | s++; |
| 4420 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4421 | else { |
| 4422 | startinpos = s-starts; |
| 4423 | s++; |
| 4424 | errmsg = "unexpected special character"; |
| 4425 | goto utf7Error; |
| 4426 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4427 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4428 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4429 | endinpos = s-starts; |
| 4430 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4431 | errors, &errorHandler, |
| 4432 | "utf7", errmsg, |
| 4433 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4434 | &unicode, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4435 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4436 | } |
| 4437 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4438 | /* end of string */ |
| 4439 | |
| 4440 | if (inShift && !consumed) { /* in shift sequence, no more to follow */ |
| 4441 | /* if we're in an inconsistent state, that's an error */ |
| 4442 | if (surrogate || |
| 4443 | (base64bits >= 6) || |
| 4444 | (base64bits > 0 && base64buffer != 0)) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4445 | endinpos = size; |
| 4446 | if (unicode_decode_call_errorhandler( |
| 4447 | errors, &errorHandler, |
| 4448 | "utf7", "unterminated shift sequence", |
| 4449 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4450 | &unicode, &outpos)) |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4451 | goto onError; |
| 4452 | if (s < e) |
| 4453 | goto restart; |
| 4454 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4455 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4456 | |
| 4457 | /* return state */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4458 | if (consumed) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4459 | if (inShift) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4460 | outpos = shiftOutStart; /* back off output */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4461 | *consumed = startinpos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4462 | } |
| 4463 | else { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4464 | *consumed = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4465 | } |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4466 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4467 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4468 | if (unicode_resize(&unicode, outpos) < 0) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4469 | goto onError; |
| 4470 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4471 | Py_XDECREF(errorHandler); |
| 4472 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 4473 | return unicode_result(unicode); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4474 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4475 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4476 | Py_XDECREF(errorHandler); |
| 4477 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4478 | Py_DECREF(unicode); |
| 4479 | return NULL; |
| 4480 | } |
| 4481 | |
| 4482 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4483 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4484 | _PyUnicode_EncodeUTF7(PyObject *str, |
| 4485 | int base64SetO, |
| 4486 | int base64WhiteSpace, |
| 4487 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4488 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4489 | int kind; |
| 4490 | void *data; |
| 4491 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4492 | PyObject *v; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4493 | Py_ssize_t allocated; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4494 | int inShift = 0; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4495 | Py_ssize_t i; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4496 | unsigned int base64bits = 0; |
| 4497 | unsigned long base64buffer = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4498 | char * out; |
| 4499 | char * start; |
| 4500 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 4501 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4502 | return NULL; |
| 4503 | kind = PyUnicode_KIND(str); |
| 4504 | data = PyUnicode_DATA(str); |
| 4505 | len = PyUnicode_GET_LENGTH(str); |
| 4506 | |
| 4507 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4508 | return PyBytes_FromStringAndSize(NULL, 0); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4509 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4510 | /* It might be possible to tighten this worst case */ |
| 4511 | allocated = 8 * len; |
| 4512 | if (allocated / 8 != len) |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4513 | return PyErr_NoMemory(); |
| 4514 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4515 | v = PyBytes_FromStringAndSize(NULL, allocated); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4516 | if (v == NULL) |
| 4517 | return NULL; |
| 4518 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4519 | start = out = PyBytes_AS_STRING(v); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4520 | for (i = 0; i < len; ++i) { |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 4521 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4522 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4523 | if (inShift) { |
| 4524 | if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 4525 | /* shifting out */ |
| 4526 | if (base64bits) { /* output remaining bits */ |
| 4527 | *out++ = TO_BASE64(base64buffer << (6-base64bits)); |
| 4528 | base64buffer = 0; |
| 4529 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4530 | } |
| 4531 | inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4532 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 4533 | so no '-' is required, except if the character is itself a '-' */ |
| 4534 | if (IS_BASE64(ch) || ch == '-') { |
| 4535 | *out++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4536 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4537 | *out++ = (char) ch; |
| 4538 | } |
| 4539 | else { |
| 4540 | goto encode_char; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4541 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4542 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4543 | else { /* not in a shift sequence */ |
| 4544 | if (ch == '+') { |
| 4545 | *out++ = '+'; |
| 4546 | *out++ = '-'; |
| 4547 | } |
| 4548 | else if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 4549 | *out++ = (char) ch; |
| 4550 | } |
| 4551 | else { |
| 4552 | *out++ = '+'; |
| 4553 | inShift = 1; |
| 4554 | goto encode_char; |
| 4555 | } |
| 4556 | } |
| 4557 | continue; |
| 4558 | encode_char: |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4559 | if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 4560 | assert(ch <= MAX_UNICODE); |
Victor Stinner | 0d3721d | 2011-11-22 03:27:53 +0100 | [diff] [blame] | 4561 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4562 | /* code first surrogate */ |
| 4563 | base64bits += 16; |
| 4564 | base64buffer = (base64buffer << 16) | 0xd800 | ((ch-0x10000) >> 10); |
| 4565 | while (base64bits >= 6) { |
| 4566 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 4567 | base64bits -= 6; |
| 4568 | } |
| 4569 | /* prepare second surrogate */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4570 | ch = Py_UNICODE_LOW_SURROGATE(ch); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4571 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4572 | base64bits += 16; |
| 4573 | base64buffer = (base64buffer << 16) | ch; |
| 4574 | while (base64bits >= 6) { |
| 4575 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 4576 | base64bits -= 6; |
| 4577 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 4578 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4579 | if (base64bits) |
| 4580 | *out++= TO_BASE64(base64buffer << (6-base64bits) ); |
| 4581 | if (inShift) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4582 | *out++ = '-'; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4583 | if (_PyBytes_Resize(&v, out - start) < 0) |
| 4584 | return NULL; |
| 4585 | return v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4586 | } |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4587 | PyObject * |
| 4588 | PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
| 4589 | Py_ssize_t size, |
| 4590 | int base64SetO, |
| 4591 | int base64WhiteSpace, |
| 4592 | const char *errors) |
| 4593 | { |
| 4594 | PyObject *result; |
| 4595 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 4596 | if (tmp == NULL) |
| 4597 | return NULL; |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 4598 | result = _PyUnicode_EncodeUTF7(tmp, base64SetO, |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4599 | base64WhiteSpace, errors); |
| 4600 | Py_DECREF(tmp); |
| 4601 | return result; |
| 4602 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4603 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4604 | #undef IS_BASE64 |
| 4605 | #undef FROM_BASE64 |
| 4606 | #undef TO_BASE64 |
| 4607 | #undef DECODE_DIRECT |
| 4608 | #undef ENCODE_DIRECT |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4609 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4610 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 4611 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4612 | PyObject * |
| 4613 | PyUnicode_DecodeUTF8(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4614 | Py_ssize_t size, |
| 4615 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4616 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4617 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 4618 | } |
| 4619 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4620 | #include "stringlib/asciilib.h" |
| 4621 | #include "stringlib/codecs.h" |
| 4622 | #include "stringlib/undef.h" |
| 4623 | |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4624 | #include "stringlib/ucs1lib.h" |
| 4625 | #include "stringlib/codecs.h" |
| 4626 | #include "stringlib/undef.h" |
| 4627 | |
| 4628 | #include "stringlib/ucs2lib.h" |
| 4629 | #include "stringlib/codecs.h" |
| 4630 | #include "stringlib/undef.h" |
| 4631 | |
| 4632 | #include "stringlib/ucs4lib.h" |
| 4633 | #include "stringlib/codecs.h" |
| 4634 | #include "stringlib/undef.h" |
| 4635 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4636 | /* Mask to check or force alignment of a pointer to C 'long' boundaries */ |
| 4637 | #define LONG_PTR_MASK (size_t) (SIZEOF_LONG - 1) |
| 4638 | |
| 4639 | /* Mask to quickly check whether a C 'long' contains a |
| 4640 | non-ASCII, UTF8-encoded char. */ |
| 4641 | #if (SIZEOF_LONG == 8) |
| 4642 | # define ASCII_CHAR_MASK 0x8080808080808080L |
| 4643 | #elif (SIZEOF_LONG == 4) |
| 4644 | # define ASCII_CHAR_MASK 0x80808080L |
| 4645 | #else |
| 4646 | # error C 'long' size should be either 4 or 8! |
| 4647 | #endif |
| 4648 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4649 | static Py_ssize_t |
| 4650 | 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] | 4651 | { |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4652 | const char *p = start; |
| 4653 | 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] | 4654 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4655 | #if SIZEOF_LONG <= SIZEOF_VOID_P |
| 4656 | assert(!((size_t) dest & LONG_PTR_MASK)); |
| 4657 | if (!((size_t) p & LONG_PTR_MASK)) { |
| 4658 | /* Fast path, see in STRINGLIB(utf8_decode) for |
| 4659 | an explanation. */ |
| 4660 | /* Help register allocation */ |
| 4661 | register const char *_p = p; |
| 4662 | register Py_UCS1 * q = dest; |
| 4663 | while (_p < aligned_end) { |
| 4664 | unsigned long value = *(const unsigned long *) _p; |
| 4665 | if (value & ASCII_CHAR_MASK) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4666 | break; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4667 | *((unsigned long *)q) = value; |
| 4668 | _p += SIZEOF_LONG; |
| 4669 | q += SIZEOF_LONG; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4670 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4671 | p = _p; |
| 4672 | while (p < end) { |
| 4673 | if ((unsigned char)*p & 0x80) |
| 4674 | break; |
| 4675 | *q++ = *p++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4676 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4677 | return p - start; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4678 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4679 | #endif |
| 4680 | while (p < end) { |
| 4681 | /* Fast path, see in STRINGLIB(utf8_decode) in stringlib/codecs.h |
| 4682 | for an explanation. */ |
| 4683 | if (!((size_t) p & LONG_PTR_MASK)) { |
| 4684 | /* Help register allocation */ |
| 4685 | register const char *_p = p; |
| 4686 | while (_p < aligned_end) { |
| 4687 | unsigned long value = *(unsigned long *) _p; |
| 4688 | if (value & ASCII_CHAR_MASK) |
| 4689 | break; |
| 4690 | _p += SIZEOF_LONG; |
| 4691 | } |
| 4692 | p = _p; |
| 4693 | if (_p == end) |
| 4694 | break; |
| 4695 | } |
| 4696 | if ((unsigned char)*p & 0x80) |
| 4697 | break; |
| 4698 | ++p; |
| 4699 | } |
| 4700 | memcpy(dest, start, p - start); |
| 4701 | return p - start; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4702 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4703 | |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4704 | PyObject * |
| 4705 | PyUnicode_DecodeUTF8Stateful(const char *s, |
| 4706 | Py_ssize_t size, |
| 4707 | const char *errors, |
| 4708 | Py_ssize_t *consumed) |
| 4709 | { |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4710 | PyObject *unicode; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4711 | const char *starts = s; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4712 | const char *end = s + size; |
| 4713 | Py_ssize_t outpos; |
| 4714 | |
| 4715 | Py_ssize_t startinpos; |
| 4716 | Py_ssize_t endinpos; |
| 4717 | const char *errmsg = ""; |
| 4718 | PyObject *errorHandler = NULL; |
| 4719 | PyObject *exc = NULL; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4720 | |
| 4721 | if (size == 0) { |
| 4722 | if (consumed) |
| 4723 | *consumed = 0; |
Victor Stinner | 382955f | 2011-12-11 21:44:00 +0100 | [diff] [blame] | 4724 | Py_INCREF(unicode_empty); |
| 4725 | return unicode_empty; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4726 | } |
| 4727 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4728 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
| 4729 | if (size == 1 && (unsigned char)s[0] < 128) { |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4730 | if (consumed) |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4731 | *consumed = 1; |
| 4732 | return get_latin1_char((unsigned char)s[0]); |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4733 | } |
| 4734 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4735 | unicode = PyUnicode_New(size, 127); |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4736 | if (!unicode) |
| 4737 | return NULL; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4738 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4739 | outpos = ascii_decode(s, end, PyUnicode_1BYTE_DATA(unicode)); |
| 4740 | s += outpos; |
| 4741 | while (s < end) { |
| 4742 | Py_UCS4 ch; |
| 4743 | int kind = PyUnicode_KIND(unicode); |
| 4744 | if (kind == PyUnicode_1BYTE_KIND) { |
| 4745 | if (PyUnicode_IS_ASCII(unicode)) |
| 4746 | ch = asciilib_utf8_decode(&s, end, |
| 4747 | PyUnicode_1BYTE_DATA(unicode), &outpos); |
| 4748 | else |
| 4749 | ch = ucs1lib_utf8_decode(&s, end, |
| 4750 | PyUnicode_1BYTE_DATA(unicode), &outpos); |
| 4751 | } else if (kind == PyUnicode_2BYTE_KIND) { |
| 4752 | ch = ucs2lib_utf8_decode(&s, end, |
| 4753 | PyUnicode_2BYTE_DATA(unicode), &outpos); |
| 4754 | } else { |
| 4755 | assert(kind == PyUnicode_4BYTE_KIND); |
| 4756 | ch = ucs4lib_utf8_decode(&s, end, |
| 4757 | PyUnicode_4BYTE_DATA(unicode), &outpos); |
| 4758 | } |
| 4759 | |
| 4760 | switch (ch) { |
| 4761 | case 0: |
| 4762 | if (s == end || consumed) |
| 4763 | goto End; |
| 4764 | errmsg = "unexpected end of data"; |
| 4765 | startinpos = s - starts; |
| 4766 | endinpos = startinpos + 1; |
| 4767 | while (endinpos < size && (starts[endinpos] & 0xC0) == 0x80) |
| 4768 | endinpos++; |
| 4769 | break; |
| 4770 | case 1: |
| 4771 | errmsg = "invalid start byte"; |
| 4772 | startinpos = s - starts; |
| 4773 | endinpos = startinpos + 1; |
| 4774 | break; |
| 4775 | case 2: |
| 4776 | errmsg = "invalid continuation byte"; |
| 4777 | startinpos = s - starts; |
| 4778 | endinpos = startinpos + 1; |
| 4779 | while (endinpos < size && (starts[endinpos] & 0xC0) == 0x80) |
| 4780 | endinpos++; |
| 4781 | break; |
| 4782 | default: |
| 4783 | if (unicode_putchar(&unicode, &outpos, ch) < 0) |
| 4784 | goto onError; |
| 4785 | continue; |
| 4786 | } |
| 4787 | |
| 4788 | if (unicode_decode_call_errorhandler( |
| 4789 | errors, &errorHandler, |
| 4790 | "utf-8", errmsg, |
| 4791 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 4792 | &unicode, &outpos)) |
| 4793 | goto onError; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4794 | } |
| 4795 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4796 | End: |
| 4797 | if (unicode_resize(&unicode, outpos) < 0) |
| 4798 | goto onError; |
| 4799 | |
| 4800 | if (consumed) |
| 4801 | *consumed = s - starts; |
| 4802 | |
| 4803 | Py_XDECREF(errorHandler); |
| 4804 | Py_XDECREF(exc); |
| 4805 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 4806 | return unicode; |
| 4807 | |
| 4808 | onError: |
| 4809 | Py_XDECREF(errorHandler); |
| 4810 | Py_XDECREF(exc); |
| 4811 | Py_XDECREF(unicode); |
| 4812 | return NULL; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4813 | } |
| 4814 | |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4815 | #ifdef __APPLE__ |
| 4816 | |
| 4817 | /* Simplified UTF-8 decoder using surrogateescape error handler, |
| 4818 | used to decode the command line arguments on Mac OS X. */ |
| 4819 | |
| 4820 | wchar_t* |
| 4821 | _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size) |
| 4822 | { |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4823 | const char *e; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4824 | wchar_t *unicode; |
| 4825 | Py_ssize_t outpos; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4826 | |
| 4827 | /* Note: size will always be longer than the resulting Unicode |
| 4828 | character count */ |
| 4829 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < (size + 1)) { |
| 4830 | PyErr_NoMemory(); |
| 4831 | return NULL; |
| 4832 | } |
| 4833 | unicode = PyMem_Malloc((size + 1) * sizeof(wchar_t)); |
| 4834 | if (!unicode) |
| 4835 | return NULL; |
| 4836 | |
| 4837 | /* Unpack UTF-8 encoded data */ |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4838 | e = s + size; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4839 | outpos = 0; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4840 | while (s < e) { |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4841 | Py_UCS4 ch; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4842 | #if SIZEOF_WCHAR_T == 4 |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4843 | ch = ucs4lib_utf8_decode(&s, e, (Py_UCS4 *)unicode, &outpos); |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4844 | #else |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4845 | ch = ucs2lib_utf8_decode(&s, e, (Py_UCS2 *)unicode, &outpos); |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4846 | #endif |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4847 | if (ch > 0xFF) { |
| 4848 | #if SIZEOF_WCHAR_T == 4 |
| 4849 | assert(0); |
| 4850 | #else |
| 4851 | assert(Py_UNICODE_IS_SURROGATE(ch)); |
| 4852 | /* compute and append the two surrogates: */ |
| 4853 | unicode[outpos++] = (wchar_t)Py_UNICODE_HIGH_SURROGATE(ch); |
| 4854 | unicode[outpos++] = (wchar_t)Py_UNICODE_LOW_SURROGATE(ch); |
| 4855 | #endif |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4856 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4857 | else { |
| 4858 | if (!ch && s == e) |
| 4859 | break; |
| 4860 | /* surrogateescape */ |
| 4861 | unicode[outpos++] = 0xDC00 + (unsigned char)*s++; |
| 4862 | } |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4863 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4864 | unicode[outpos] = L'\0'; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4865 | return unicode; |
| 4866 | } |
| 4867 | |
| 4868 | #endif /* __APPLE__ */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4869 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4870 | /* Primary internal function which creates utf8 encoded bytes objects. |
| 4871 | |
| 4872 | Allocation strategy: if the string is short, convert into a stack buffer |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4873 | and allocate exactly as much space needed at the end. Else allocate the |
| 4874 | maximum possible needed (4 result bytes per Unicode character), and return |
| 4875 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4876 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 4877 | PyObject * |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4878 | _PyUnicode_AsUTF8String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4879 | { |
Victor Stinner | 6099a03 | 2011-12-18 14:22:26 +0100 | [diff] [blame] | 4880 | enum PyUnicode_Kind kind; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4881 | void *data; |
| 4882 | Py_ssize_t size; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 4883 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4884 | if (!PyUnicode_Check(unicode)) { |
| 4885 | PyErr_BadArgument(); |
| 4886 | return NULL; |
| 4887 | } |
| 4888 | |
| 4889 | if (PyUnicode_READY(unicode) == -1) |
| 4890 | return NULL; |
| 4891 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 4892 | if (PyUnicode_UTF8(unicode)) |
| 4893 | return PyBytes_FromStringAndSize(PyUnicode_UTF8(unicode), |
| 4894 | PyUnicode_UTF8_LENGTH(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4895 | |
| 4896 | kind = PyUnicode_KIND(unicode); |
| 4897 | data = PyUnicode_DATA(unicode); |
| 4898 | size = PyUnicode_GET_LENGTH(unicode); |
| 4899 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 4900 | switch (kind) { |
Victor Stinner | 6099a03 | 2011-12-18 14:22:26 +0100 | [diff] [blame] | 4901 | default: |
| 4902 | assert(0); |
| 4903 | case PyUnicode_1BYTE_KIND: |
| 4904 | /* the string cannot be ASCII, or PyUnicode_UTF8() would be set */ |
| 4905 | assert(!PyUnicode_IS_ASCII(unicode)); |
| 4906 | return ucs1lib_utf8_encoder(unicode, data, size, errors); |
| 4907 | case PyUnicode_2BYTE_KIND: |
| 4908 | return ucs2lib_utf8_encoder(unicode, data, size, errors); |
| 4909 | case PyUnicode_4BYTE_KIND: |
| 4910 | return ucs4lib_utf8_encoder(unicode, data, size, errors); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4911 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4912 | } |
| 4913 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4914 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4915 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
| 4916 | Py_ssize_t size, |
| 4917 | const char *errors) |
| 4918 | { |
| 4919 | PyObject *v, *unicode; |
| 4920 | |
| 4921 | unicode = PyUnicode_FromUnicode(s, size); |
| 4922 | if (unicode == NULL) |
| 4923 | return NULL; |
| 4924 | v = _PyUnicode_AsUTF8String(unicode, errors); |
| 4925 | Py_DECREF(unicode); |
| 4926 | return v; |
| 4927 | } |
| 4928 | |
| 4929 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4930 | PyUnicode_AsUTF8String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4931 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4932 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4933 | } |
| 4934 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4935 | /* --- UTF-32 Codec ------------------------------------------------------- */ |
| 4936 | |
| 4937 | PyObject * |
| 4938 | PyUnicode_DecodeUTF32(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4939 | Py_ssize_t size, |
| 4940 | const char *errors, |
| 4941 | int *byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4942 | { |
| 4943 | return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); |
| 4944 | } |
| 4945 | |
| 4946 | PyObject * |
| 4947 | PyUnicode_DecodeUTF32Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4948 | Py_ssize_t size, |
| 4949 | const char *errors, |
| 4950 | int *byteorder, |
| 4951 | Py_ssize_t *consumed) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4952 | { |
| 4953 | const char *starts = s; |
| 4954 | Py_ssize_t startinpos; |
| 4955 | Py_ssize_t endinpos; |
| 4956 | Py_ssize_t outpos; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4957 | PyObject *unicode; |
Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 4958 | const unsigned char *q, *e; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4959 | int bo = 0; /* assume native ordering by default */ |
| 4960 | const char *errmsg = ""; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4961 | /* Offsets from q for retrieving bytes in the right order. */ |
| 4962 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 4963 | int iorder[] = {0, 1, 2, 3}; |
| 4964 | #else |
| 4965 | int iorder[] = {3, 2, 1, 0}; |
| 4966 | #endif |
| 4967 | PyObject *errorHandler = NULL; |
| 4968 | PyObject *exc = NULL; |
Victor Stinner | 313a120 | 2010-06-11 23:56:51 +0000 | [diff] [blame] | 4969 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4970 | q = (unsigned char *)s; |
| 4971 | e = q + size; |
| 4972 | |
| 4973 | if (byteorder) |
| 4974 | bo = *byteorder; |
| 4975 | |
| 4976 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 4977 | byte order setting accordingly. In native mode, the leading BOM |
| 4978 | mark is skipped, in all other modes, it is copied to the output |
| 4979 | stream as-is (giving a ZWNBSP character). */ |
| 4980 | if (bo == 0) { |
| 4981 | if (size >= 4) { |
| 4982 | const Py_UCS4 bom = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4983 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4984 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4985 | if (bom == 0x0000FEFF) { |
| 4986 | q += 4; |
| 4987 | bo = -1; |
| 4988 | } |
| 4989 | else if (bom == 0xFFFE0000) { |
| 4990 | q += 4; |
| 4991 | bo = 1; |
| 4992 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4993 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4994 | if (bom == 0x0000FEFF) { |
| 4995 | q += 4; |
| 4996 | bo = 1; |
| 4997 | } |
| 4998 | else if (bom == 0xFFFE0000) { |
| 4999 | q += 4; |
| 5000 | bo = -1; |
| 5001 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5002 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5003 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5004 | } |
| 5005 | |
| 5006 | if (bo == -1) { |
| 5007 | /* force LE */ |
| 5008 | iorder[0] = 0; |
| 5009 | iorder[1] = 1; |
| 5010 | iorder[2] = 2; |
| 5011 | iorder[3] = 3; |
| 5012 | } |
| 5013 | else if (bo == 1) { |
| 5014 | /* force BE */ |
| 5015 | iorder[0] = 3; |
| 5016 | iorder[1] = 2; |
| 5017 | iorder[2] = 1; |
| 5018 | iorder[3] = 0; |
| 5019 | } |
| 5020 | |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 5021 | /* This might be one to much, because of a BOM */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5022 | unicode = PyUnicode_New((size+3)/4, 127); |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 5023 | if (!unicode) |
| 5024 | return NULL; |
| 5025 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5026 | return unicode; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5027 | outpos = 0; |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 5028 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5029 | while (q < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5030 | Py_UCS4 ch; |
| 5031 | /* remaining bytes at the end? (size should be divisible by 4) */ |
| 5032 | if (e-q<4) { |
| 5033 | if (consumed) |
| 5034 | break; |
| 5035 | errmsg = "truncated data"; |
| 5036 | startinpos = ((const char *)q)-starts; |
| 5037 | endinpos = ((const char *)e)-starts; |
| 5038 | goto utf32Error; |
| 5039 | /* The remaining input chars are ignored if the callback |
| 5040 | chooses to skip the input */ |
| 5041 | } |
| 5042 | ch = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
| 5043 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5044 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5045 | if (ch >= 0x110000) |
| 5046 | { |
| 5047 | errmsg = "codepoint not in range(0x110000)"; |
| 5048 | startinpos = ((const char *)q)-starts; |
| 5049 | endinpos = startinpos+4; |
| 5050 | goto utf32Error; |
| 5051 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5052 | if (unicode_putchar(&unicode, &outpos, ch) < 0) |
| 5053 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5054 | q += 4; |
| 5055 | continue; |
| 5056 | utf32Error: |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5057 | if (unicode_decode_call_errorhandler( |
| 5058 | errors, &errorHandler, |
| 5059 | "utf32", errmsg, |
| 5060 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5061 | &unicode, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5062 | goto onError; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5063 | } |
| 5064 | |
| 5065 | if (byteorder) |
| 5066 | *byteorder = bo; |
| 5067 | |
| 5068 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5069 | *consumed = (const char *)q-starts; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5070 | |
| 5071 | /* Adjust length */ |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 5072 | if (unicode_resize(&unicode, outpos) < 0) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5073 | goto onError; |
| 5074 | |
| 5075 | Py_XDECREF(errorHandler); |
| 5076 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 5077 | return unicode_result(unicode); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5078 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5079 | onError: |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5080 | Py_DECREF(unicode); |
| 5081 | Py_XDECREF(errorHandler); |
| 5082 | Py_XDECREF(exc); |
| 5083 | return NULL; |
| 5084 | } |
| 5085 | |
| 5086 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5087 | _PyUnicode_EncodeUTF32(PyObject *str, |
| 5088 | const char *errors, |
| 5089 | int byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5090 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5091 | int kind; |
| 5092 | void *data; |
| 5093 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5094 | PyObject *v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5095 | unsigned char *p; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5096 | Py_ssize_t nsize, bytesize, i; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5097 | /* Offsets from p for storing byte pairs in the right order. */ |
| 5098 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5099 | int iorder[] = {0, 1, 2, 3}; |
| 5100 | #else |
| 5101 | int iorder[] = {3, 2, 1, 0}; |
| 5102 | #endif |
| 5103 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5104 | #define STORECHAR(CH) \ |
| 5105 | do { \ |
| 5106 | p[iorder[3]] = ((CH) >> 24) & 0xff; \ |
| 5107 | p[iorder[2]] = ((CH) >> 16) & 0xff; \ |
| 5108 | p[iorder[1]] = ((CH) >> 8) & 0xff; \ |
| 5109 | p[iorder[0]] = (CH) & 0xff; \ |
| 5110 | p += 4; \ |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5111 | } while(0) |
| 5112 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5113 | if (!PyUnicode_Check(str)) { |
| 5114 | PyErr_BadArgument(); |
| 5115 | return NULL; |
| 5116 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 5117 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5118 | return NULL; |
| 5119 | kind = PyUnicode_KIND(str); |
| 5120 | data = PyUnicode_DATA(str); |
| 5121 | len = PyUnicode_GET_LENGTH(str); |
| 5122 | |
| 5123 | nsize = len + (byteorder == 0); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5124 | bytesize = nsize * 4; |
| 5125 | if (bytesize / 4 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5126 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5127 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5128 | if (v == NULL) |
| 5129 | return NULL; |
| 5130 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5131 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5132 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5133 | STORECHAR(0xFEFF); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5134 | if (len == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5135 | goto done; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5136 | |
| 5137 | if (byteorder == -1) { |
| 5138 | /* force LE */ |
| 5139 | iorder[0] = 0; |
| 5140 | iorder[1] = 1; |
| 5141 | iorder[2] = 2; |
| 5142 | iorder[3] = 3; |
| 5143 | } |
| 5144 | else if (byteorder == 1) { |
| 5145 | /* force BE */ |
| 5146 | iorder[0] = 3; |
| 5147 | iorder[1] = 2; |
| 5148 | iorder[2] = 1; |
| 5149 | iorder[3] = 0; |
| 5150 | } |
| 5151 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5152 | for (i = 0; i < len; i++) |
| 5153 | STORECHAR(PyUnicode_READ(kind, data, i)); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5154 | |
| 5155 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5156 | return v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5157 | #undef STORECHAR |
| 5158 | } |
| 5159 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5160 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5161 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
| 5162 | Py_ssize_t size, |
| 5163 | const char *errors, |
| 5164 | int byteorder) |
| 5165 | { |
| 5166 | PyObject *result; |
| 5167 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5168 | if (tmp == NULL) |
| 5169 | return NULL; |
| 5170 | result = _PyUnicode_EncodeUTF32(tmp, errors, byteorder); |
| 5171 | Py_DECREF(tmp); |
| 5172 | return result; |
| 5173 | } |
| 5174 | |
| 5175 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5176 | PyUnicode_AsUTF32String(PyObject *unicode) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5177 | { |
Victor Stinner | b960b34 | 2011-11-20 19:12:52 +0100 | [diff] [blame] | 5178 | return _PyUnicode_EncodeUTF32(unicode, NULL, 0); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5179 | } |
| 5180 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5181 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 5182 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5183 | PyObject * |
| 5184 | PyUnicode_DecodeUTF16(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5185 | Py_ssize_t size, |
| 5186 | const char *errors, |
| 5187 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5188 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5189 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 5190 | } |
| 5191 | |
| 5192 | PyObject * |
| 5193 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5194 | Py_ssize_t size, |
| 5195 | const char *errors, |
| 5196 | int *byteorder, |
| 5197 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5198 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5199 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5200 | Py_ssize_t startinpos; |
| 5201 | Py_ssize_t endinpos; |
| 5202 | Py_ssize_t outpos; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5203 | PyObject *unicode; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5204 | const unsigned char *q, *e; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5205 | int bo = 0; /* assume native ordering by default */ |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5206 | int native_ordering; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 5207 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5208 | PyObject *errorHandler = NULL; |
| 5209 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5210 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5211 | q = (unsigned char *)s; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5212 | e = q + size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5213 | |
| 5214 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5215 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5216 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5217 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 5218 | byte order setting accordingly. In native mode, the leading BOM |
| 5219 | mark is skipped, in all other modes, it is copied to the output |
| 5220 | stream as-is (giving a ZWNBSP character). */ |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5221 | if (bo == 0 && size >= 2) { |
| 5222 | const Py_UCS4 bom = (q[1] << 8) | q[0]; |
| 5223 | if (bom == 0xFEFF) { |
| 5224 | q += 2; |
| 5225 | bo = -1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5226 | } |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5227 | else if (bom == 0xFFFE) { |
| 5228 | q += 2; |
| 5229 | bo = 1; |
| 5230 | } |
| 5231 | if (byteorder) |
| 5232 | *byteorder = bo; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5233 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5234 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5235 | if (q == e) { |
| 5236 | if (consumed) |
| 5237 | *consumed = size; |
| 5238 | Py_INCREF(unicode_empty); |
| 5239 | return unicode_empty; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5240 | } |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5241 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5242 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5243 | native_ordering = bo <= 0; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5244 | #else |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5245 | native_ordering = bo >= 0; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5246 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5247 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5248 | /* Note: size will always be longer than the resulting Unicode |
| 5249 | character count */ |
| 5250 | unicode = PyUnicode_New((e - q + 1) / 2, 127); |
| 5251 | if (!unicode) |
| 5252 | return NULL; |
| 5253 | |
| 5254 | outpos = 0; |
| 5255 | while (1) { |
| 5256 | Py_UCS4 ch = 0; |
| 5257 | if (e - q >= 2) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5258 | int kind = PyUnicode_KIND(unicode); |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5259 | if (kind == PyUnicode_1BYTE_KIND) { |
| 5260 | if (PyUnicode_IS_ASCII(unicode)) |
| 5261 | ch = asciilib_utf16_decode(&q, e, |
| 5262 | PyUnicode_1BYTE_DATA(unicode), &outpos, |
| 5263 | native_ordering); |
| 5264 | else |
| 5265 | ch = ucs1lib_utf16_decode(&q, e, |
| 5266 | PyUnicode_1BYTE_DATA(unicode), &outpos, |
| 5267 | native_ordering); |
| 5268 | } else if (kind == PyUnicode_2BYTE_KIND) { |
| 5269 | ch = ucs2lib_utf16_decode(&q, e, |
| 5270 | PyUnicode_2BYTE_DATA(unicode), &outpos, |
| 5271 | native_ordering); |
| 5272 | } else { |
| 5273 | assert(kind == PyUnicode_4BYTE_KIND); |
| 5274 | ch = ucs4lib_utf16_decode(&q, e, |
| 5275 | PyUnicode_4BYTE_DATA(unicode), &outpos, |
| 5276 | native_ordering); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5277 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5278 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5279 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5280 | switch (ch) |
| 5281 | { |
| 5282 | case 0: |
| 5283 | /* remaining byte at the end? (size should be even) */ |
| 5284 | if (q == e || consumed) |
| 5285 | goto End; |
| 5286 | errmsg = "truncated data"; |
| 5287 | startinpos = ((const char *)q) - starts; |
| 5288 | endinpos = ((const char *)e) - starts; |
| 5289 | break; |
| 5290 | /* The remaining input chars are ignored if the callback |
| 5291 | chooses to skip the input */ |
| 5292 | case 1: |
| 5293 | errmsg = "unexpected end of data"; |
| 5294 | startinpos = ((const char *)q) - 2 - starts; |
| 5295 | endinpos = ((const char *)e) - starts; |
| 5296 | break; |
| 5297 | case 2: |
| 5298 | errmsg = "illegal encoding"; |
| 5299 | startinpos = ((const char *)q) - 2 - starts; |
| 5300 | endinpos = startinpos + 2; |
| 5301 | break; |
| 5302 | case 3: |
| 5303 | errmsg = "illegal UTF-16 surrogate"; |
| 5304 | startinpos = ((const char *)q) - 4 - starts; |
| 5305 | endinpos = startinpos + 2; |
| 5306 | break; |
| 5307 | default: |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5308 | if (unicode_putchar(&unicode, &outpos, ch) < 0) |
| 5309 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5310 | continue; |
| 5311 | } |
| 5312 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5313 | if (unicode_decode_call_errorhandler( |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5314 | errors, |
| 5315 | &errorHandler, |
| 5316 | "utf16", errmsg, |
| 5317 | &starts, |
| 5318 | (const char **)&e, |
| 5319 | &startinpos, |
| 5320 | &endinpos, |
| 5321 | &exc, |
| 5322 | (const char **)&q, |
| 5323 | &unicode, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5324 | &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5325 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5326 | } |
| 5327 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5328 | End: |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5329 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5330 | *consumed = (const char *)q-starts; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5331 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5332 | /* Adjust length */ |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 5333 | if (unicode_resize(&unicode, outpos) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5334 | goto onError; |
| 5335 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5336 | Py_XDECREF(errorHandler); |
| 5337 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 5338 | return unicode_result(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5339 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5340 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5341 | Py_DECREF(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5342 | Py_XDECREF(errorHandler); |
| 5343 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5344 | return NULL; |
| 5345 | } |
| 5346 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5347 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5348 | _PyUnicode_EncodeUTF16(PyObject *str, |
| 5349 | const char *errors, |
| 5350 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5351 | { |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5352 | enum PyUnicode_Kind kind; |
| 5353 | const void *data; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5354 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5355 | PyObject *v; |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5356 | unsigned short *out; |
| 5357 | Py_ssize_t bytesize; |
| 5358 | Py_ssize_t pairs; |
| 5359 | #ifdef WORDS_BIGENDIAN |
| 5360 | int native_ordering = byteorder >= 0; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5361 | #else |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5362 | int native_ordering = byteorder <= 0; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5363 | #endif |
| 5364 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5365 | if (!PyUnicode_Check(str)) { |
| 5366 | PyErr_BadArgument(); |
| 5367 | return NULL; |
| 5368 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 5369 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5370 | return NULL; |
| 5371 | kind = PyUnicode_KIND(str); |
| 5372 | data = PyUnicode_DATA(str); |
| 5373 | len = PyUnicode_GET_LENGTH(str); |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 5374 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5375 | pairs = 0; |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5376 | if (kind == PyUnicode_4BYTE_KIND) { |
| 5377 | const Py_UCS4 *in = (const Py_UCS4 *)data; |
| 5378 | const Py_UCS4 *end = in + len; |
| 5379 | while (in < end) |
| 5380 | if (*in++ >= 0x10000) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5381 | pairs++; |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5382 | } |
| 5383 | if (len > PY_SSIZE_T_MAX / 2 - pairs - (byteorder == 0)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5384 | return PyErr_NoMemory(); |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5385 | bytesize = (len + pairs + (byteorder == 0)) * 2; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5386 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5387 | if (v == NULL) |
| 5388 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5389 | |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5390 | /* output buffer is 2-bytes aligned */ |
| 5391 | assert(((Py_uintptr_t)PyBytes_AS_STRING(v) & 1) == 0); |
| 5392 | out = (unsigned short *)PyBytes_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5393 | if (byteorder == 0) |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5394 | *out++ = 0xFEFF; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5395 | if (len == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5396 | goto done; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5397 | |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5398 | switch (kind) { |
| 5399 | case PyUnicode_1BYTE_KIND: { |
| 5400 | ucs1lib_utf16_encode(out, (const Py_UCS1 *)data, len, native_ordering); |
| 5401 | break; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5402 | } |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5403 | case PyUnicode_2BYTE_KIND: { |
| 5404 | ucs2lib_utf16_encode(out, (const Py_UCS2 *)data, len, native_ordering); |
| 5405 | break; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5406 | } |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5407 | case PyUnicode_4BYTE_KIND: { |
| 5408 | ucs4lib_utf16_encode(out, (const Py_UCS4 *)data, len, native_ordering); |
| 5409 | break; |
| 5410 | } |
| 5411 | default: |
| 5412 | assert(0); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5413 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5414 | |
| 5415 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5416 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5417 | } |
| 5418 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5419 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5420 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
| 5421 | Py_ssize_t size, |
| 5422 | const char *errors, |
| 5423 | int byteorder) |
| 5424 | { |
| 5425 | PyObject *result; |
| 5426 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5427 | if (tmp == NULL) |
| 5428 | return NULL; |
| 5429 | result = _PyUnicode_EncodeUTF16(tmp, errors, byteorder); |
| 5430 | Py_DECREF(tmp); |
| 5431 | return result; |
| 5432 | } |
| 5433 | |
| 5434 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5435 | PyUnicode_AsUTF16String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5436 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5437 | return _PyUnicode_EncodeUTF16(unicode, NULL, 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5438 | } |
| 5439 | |
| 5440 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 5441 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5442 | /* Helper function for PyUnicode_DecodeUnicodeEscape, determines |
| 5443 | if all the escapes in the string make it still a valid ASCII string. |
| 5444 | Returns -1 if any escapes were found which cause the string to |
| 5445 | pop out of ASCII range. Otherwise returns the length of the |
| 5446 | required buffer to hold the string. |
| 5447 | */ |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 5448 | static Py_ssize_t |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5449 | length_of_escaped_ascii_string(const char *s, Py_ssize_t size) |
| 5450 | { |
| 5451 | const unsigned char *p = (const unsigned char *)s; |
| 5452 | const unsigned char *end = p + size; |
| 5453 | Py_ssize_t length = 0; |
| 5454 | |
| 5455 | if (size < 0) |
| 5456 | return -1; |
| 5457 | |
| 5458 | for (; p < end; ++p) { |
| 5459 | if (*p > 127) { |
| 5460 | /* Non-ASCII */ |
| 5461 | return -1; |
| 5462 | } |
| 5463 | else if (*p != '\\') { |
| 5464 | /* Normal character */ |
| 5465 | ++length; |
| 5466 | } |
| 5467 | else { |
| 5468 | /* Backslash-escape, check next char */ |
| 5469 | ++p; |
| 5470 | /* Escape sequence reaches till end of string or |
| 5471 | non-ASCII follow-up. */ |
| 5472 | if (p >= end || *p > 127) |
| 5473 | return -1; |
| 5474 | switch (*p) { |
| 5475 | case '\n': |
| 5476 | /* backslash + \n result in zero characters */ |
| 5477 | break; |
| 5478 | case '\\': case '\'': case '\"': |
| 5479 | case 'b': case 'f': case 't': |
| 5480 | case 'n': case 'r': case 'v': case 'a': |
| 5481 | ++length; |
| 5482 | break; |
| 5483 | case '0': case '1': case '2': case '3': |
| 5484 | case '4': case '5': case '6': case '7': |
| 5485 | case 'x': case 'u': case 'U': case 'N': |
| 5486 | /* these do not guarantee ASCII characters */ |
| 5487 | return -1; |
| 5488 | default: |
| 5489 | /* count the backslash + the other character */ |
| 5490 | length += 2; |
| 5491 | } |
| 5492 | } |
| 5493 | } |
| 5494 | return length; |
| 5495 | } |
| 5496 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5497 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 5498 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5499 | PyObject * |
| 5500 | PyUnicode_DecodeUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5501 | Py_ssize_t size, |
Victor Stinner | c17f540 | 2011-09-29 00:16:58 +0200 | [diff] [blame] | 5502 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5503 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5504 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5505 | Py_ssize_t startinpos; |
| 5506 | Py_ssize_t endinpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5507 | int j; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5508 | PyObject *v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5509 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5510 | char* message; |
| 5511 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5512 | PyObject *errorHandler = NULL; |
| 5513 | PyObject *exc = NULL; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5514 | Py_ssize_t len; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5515 | Py_ssize_t i; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5516 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5517 | len = length_of_escaped_ascii_string(s, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5518 | |
| 5519 | /* After length_of_escaped_ascii_string() there are two alternatives, |
| 5520 | either the string is pure ASCII with named escapes like \n, etc. |
| 5521 | and we determined it's exact size (common case) |
| 5522 | or it contains \x, \u, ... escape sequences. then we create a |
| 5523 | 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] | 5524 | if (len >= 0) { |
| 5525 | v = PyUnicode_New(len, 127); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5526 | if (!v) |
| 5527 | goto onError; |
| 5528 | assert(PyUnicode_KIND(v) == PyUnicode_1BYTE_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5529 | } |
| 5530 | else { |
| 5531 | /* Escaped strings will always be longer than the resulting |
| 5532 | Unicode string, so we start with size here and then reduce the |
| 5533 | length after conversion to the true value. |
| 5534 | (but if the error callback returns a long replacement string |
| 5535 | we'll have to allocate more space) */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5536 | v = PyUnicode_New(size, 127); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5537 | if (!v) |
| 5538 | goto onError; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5539 | len = size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5540 | } |
| 5541 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5542 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5543 | return v; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5544 | i = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5545 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5546 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5547 | while (s < end) { |
| 5548 | unsigned char c; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 5549 | Py_UCS4 x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5550 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5551 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5552 | /* The only case in which i == ascii_length is a backslash |
| 5553 | followed by a newline. */ |
| 5554 | assert(i <= len); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5555 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5556 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 5557 | if (*s != '\\') { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5558 | if (unicode_putchar(&v, &i, (unsigned char) *s++) < 0) |
| 5559 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5560 | continue; |
| 5561 | } |
| 5562 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5563 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5564 | /* \ - Escapes */ |
| 5565 | s++; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5566 | c = *s++; |
| 5567 | if (s > end) |
| 5568 | c = '\0'; /* Invalid after \ */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5569 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5570 | /* The only case in which i == ascii_length is a backslash |
| 5571 | followed by a newline. */ |
| 5572 | assert(i < len || (i == len && c == '\n')); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5573 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5574 | switch (c) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5575 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5576 | /* \x escapes */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5577 | #define WRITECHAR(ch) \ |
| 5578 | do { \ |
| 5579 | if (unicode_putchar(&v, &i, ch) < 0) \ |
| 5580 | goto onError; \ |
| 5581 | }while(0) |
| 5582 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5583 | case '\n': break; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5584 | case '\\': WRITECHAR('\\'); break; |
| 5585 | case '\'': WRITECHAR('\''); break; |
| 5586 | case '\"': WRITECHAR('\"'); break; |
| 5587 | case 'b': WRITECHAR('\b'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5588 | /* FF */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5589 | case 'f': WRITECHAR('\014'); break; |
| 5590 | case 't': WRITECHAR('\t'); break; |
| 5591 | case 'n': WRITECHAR('\n'); break; |
| 5592 | case 'r': WRITECHAR('\r'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5593 | /* VT */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5594 | case 'v': WRITECHAR('\013'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5595 | /* BEL, not classic C */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5596 | case 'a': WRITECHAR('\007'); break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5597 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5598 | /* \OOO (octal) escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5599 | case '0': case '1': case '2': case '3': |
| 5600 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5601 | x = s[-1] - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5602 | if (s < end && '0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5603 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5604 | if (s < end && '0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5605 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5606 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5607 | WRITECHAR(x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5608 | break; |
| 5609 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5610 | /* hex escapes */ |
| 5611 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5612 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5613 | digits = 2; |
| 5614 | message = "truncated \\xXX escape"; |
| 5615 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5616 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5617 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5618 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5619 | digits = 4; |
| 5620 | message = "truncated \\uXXXX escape"; |
| 5621 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5622 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5623 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5624 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5625 | digits = 8; |
| 5626 | message = "truncated \\UXXXXXXXX escape"; |
| 5627 | hexescape: |
| 5628 | chr = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5629 | if (s+digits>end) { |
| 5630 | endinpos = size; |
| 5631 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5632 | errors, &errorHandler, |
| 5633 | "unicodeescape", "end of string in escape sequence", |
| 5634 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5635 | &v, &i)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5636 | goto onError; |
| 5637 | goto nextByte; |
| 5638 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5639 | for (j = 0; j < digits; ++j) { |
| 5640 | c = (unsigned char) s[j]; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 5641 | if (!Py_ISXDIGIT(c)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5642 | endinpos = (s+j+1)-starts; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5643 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5644 | errors, &errorHandler, |
| 5645 | "unicodeescape", message, |
| 5646 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5647 | &v, &i)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5648 | goto onError; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5649 | len = PyUnicode_GET_LENGTH(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5650 | goto nextByte; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5651 | } |
| 5652 | chr = (chr<<4) & ~0xF; |
| 5653 | if (c >= '0' && c <= '9') |
| 5654 | chr += c - '0'; |
| 5655 | else if (c >= 'a' && c <= 'f') |
| 5656 | chr += 10 + c - 'a'; |
| 5657 | else |
| 5658 | chr += 10 + c - 'A'; |
| 5659 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5660 | s += j; |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 5661 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5662 | /* _decoding_error will have already written into the |
| 5663 | target buffer. */ |
| 5664 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5665 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5666 | /* when we get here, chr is a 32-bit unicode character */ |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 5667 | if (chr <= MAX_UNICODE) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5668 | WRITECHAR(chr); |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5669 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5670 | endinpos = s-starts; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5671 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5672 | errors, &errorHandler, |
| 5673 | "unicodeescape", "illegal Unicode character", |
| 5674 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5675 | &v, &i)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5676 | goto onError; |
| 5677 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5678 | break; |
| 5679 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5680 | /* \N{name} */ |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5681 | case 'N': |
| 5682 | message = "malformed \\N character escape"; |
| 5683 | if (ucnhash_CAPI == NULL) { |
| 5684 | /* load the unicode data module */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5685 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCapsule_Import( |
| 5686 | PyUnicodeData_CAPSULE_NAME, 1); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5687 | if (ucnhash_CAPI == NULL) |
| 5688 | goto ucnhashError; |
| 5689 | } |
| 5690 | if (*s == '{') { |
| 5691 | const char *start = s+1; |
| 5692 | /* look for the closing brace */ |
| 5693 | while (*s != '}' && s < end) |
| 5694 | s++; |
| 5695 | if (s > start && s < end && *s == '}') { |
| 5696 | /* found a name. look it up in the unicode database */ |
| 5697 | message = "unknown Unicode character name"; |
| 5698 | s++; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5699 | if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), |
Ezio Melotti | 931b8aa | 2011-10-21 21:57:36 +0300 | [diff] [blame] | 5700 | &chr, 0)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5701 | goto store; |
| 5702 | } |
| 5703 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5704 | endinpos = s-starts; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5705 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5706 | errors, &errorHandler, |
| 5707 | "unicodeescape", message, |
| 5708 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5709 | &v, &i)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5710 | goto onError; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5711 | break; |
| 5712 | |
| 5713 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5714 | if (s > end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5715 | message = "\\ at end of string"; |
| 5716 | s--; |
| 5717 | endinpos = s-starts; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5718 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5719 | errors, &errorHandler, |
| 5720 | "unicodeescape", message, |
| 5721 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5722 | &v, &i)) |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5723 | goto onError; |
| 5724 | } |
| 5725 | else { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5726 | WRITECHAR('\\'); |
| 5727 | WRITECHAR(s[-1]); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5728 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5729 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5730 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5731 | nextByte: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5732 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5733 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5734 | #undef WRITECHAR |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5735 | |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 5736 | if (unicode_resize(&v, i) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5737 | goto onError; |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 5738 | Py_XDECREF(errorHandler); |
| 5739 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 5740 | return unicode_result(v); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5741 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5742 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5743 | PyErr_SetString( |
| 5744 | PyExc_UnicodeError, |
| 5745 | "\\N escapes not supported (can't load unicodedata module)" |
| 5746 | ); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 5747 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5748 | Py_XDECREF(errorHandler); |
| 5749 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 5750 | return NULL; |
| 5751 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5752 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5753 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5754 | Py_XDECREF(errorHandler); |
| 5755 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5756 | return NULL; |
| 5757 | } |
| 5758 | |
| 5759 | /* Return a Unicode-Escape string version of the Unicode object. |
| 5760 | |
| 5761 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 5762 | appropriate. |
| 5763 | |
| 5764 | */ |
| 5765 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5766 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5767 | PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5768 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5769 | Py_ssize_t i, len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5770 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5771 | char *p; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5772 | int kind; |
| 5773 | void *data; |
| 5774 | Py_ssize_t expandsize = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5775 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5776 | /* Initial allocation is based on the longest-possible unichr |
| 5777 | escape. |
| 5778 | |
| 5779 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 5780 | unichr, so in this case it's the longest unichr escape. In |
| 5781 | narrow (UTF-16) builds this is five chars per source unichr |
| 5782 | since there are two unichrs in the surrogate pair, so in narrow |
| 5783 | (UTF-16) builds it's not the longest unichr escape. |
| 5784 | |
| 5785 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 5786 | so in the narrow (UTF-16) build case it's the longest unichr |
| 5787 | escape. |
| 5788 | */ |
| 5789 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5790 | if (!PyUnicode_Check(unicode)) { |
| 5791 | PyErr_BadArgument(); |
| 5792 | return NULL; |
| 5793 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 5794 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5795 | return NULL; |
| 5796 | len = PyUnicode_GET_LENGTH(unicode); |
| 5797 | kind = PyUnicode_KIND(unicode); |
| 5798 | data = PyUnicode_DATA(unicode); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 5799 | switch (kind) { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5800 | case PyUnicode_1BYTE_KIND: expandsize = 4; break; |
| 5801 | case PyUnicode_2BYTE_KIND: expandsize = 6; break; |
| 5802 | case PyUnicode_4BYTE_KIND: expandsize = 10; break; |
| 5803 | } |
| 5804 | |
| 5805 | if (len == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5806 | return PyBytes_FromStringAndSize(NULL, 0); |
| 5807 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5808 | if (len > (PY_SSIZE_T_MAX - 2 - 1) / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5809 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5810 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5811 | repr = PyBytes_FromStringAndSize(NULL, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5812 | 2 |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5813 | + expandsize*len |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5814 | + 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5815 | if (repr == NULL) |
| 5816 | return NULL; |
| 5817 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5818 | p = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5819 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5820 | for (i = 0; i < len; i++) { |
Victor Stinner | 3326cb6 | 2011-11-10 20:15:25 +0100 | [diff] [blame] | 5821 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5822 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5823 | /* Escape backslashes */ |
| 5824 | if (ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5825 | *p++ = '\\'; |
| 5826 | *p++ = (char) ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5827 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5828 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5829 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5830 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 5831 | else if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 5832 | assert(ch <= MAX_UNICODE); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5833 | *p++ = '\\'; |
| 5834 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 5835 | *p++ = Py_hexdigits[(ch >> 28) & 0x0000000F]; |
| 5836 | *p++ = Py_hexdigits[(ch >> 24) & 0x0000000F]; |
| 5837 | *p++ = Py_hexdigits[(ch >> 20) & 0x0000000F]; |
| 5838 | *p++ = Py_hexdigits[(ch >> 16) & 0x0000000F]; |
| 5839 | *p++ = Py_hexdigits[(ch >> 12) & 0x0000000F]; |
| 5840 | *p++ = Py_hexdigits[(ch >> 8) & 0x0000000F]; |
| 5841 | *p++ = Py_hexdigits[(ch >> 4) & 0x0000000F]; |
| 5842 | *p++ = Py_hexdigits[ch & 0x0000000F]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5843 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5844 | } |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5845 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5846 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5847 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5848 | *p++ = '\\'; |
| 5849 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 5850 | *p++ = Py_hexdigits[(ch >> 12) & 0x000F]; |
| 5851 | *p++ = Py_hexdigits[(ch >> 8) & 0x000F]; |
| 5852 | *p++ = Py_hexdigits[(ch >> 4) & 0x000F]; |
| 5853 | *p++ = Py_hexdigits[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5854 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5855 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5856 | /* Map special whitespace to '\t', \n', '\r' */ |
| 5857 | else if (ch == '\t') { |
| 5858 | *p++ = '\\'; |
| 5859 | *p++ = 't'; |
| 5860 | } |
| 5861 | else if (ch == '\n') { |
| 5862 | *p++ = '\\'; |
| 5863 | *p++ = 'n'; |
| 5864 | } |
| 5865 | else if (ch == '\r') { |
| 5866 | *p++ = '\\'; |
| 5867 | *p++ = 'r'; |
| 5868 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5869 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5870 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 5871 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5872 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5873 | *p++ = 'x'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 5874 | *p++ = Py_hexdigits[(ch >> 4) & 0x000F]; |
| 5875 | *p++ = Py_hexdigits[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5876 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5877 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5878 | /* Copy everything else as-is */ |
| 5879 | else |
| 5880 | *p++ = (char) ch; |
| 5881 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5882 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5883 | assert(p - PyBytes_AS_STRING(repr) > 0); |
| 5884 | if (_PyBytes_Resize(&repr, p - PyBytes_AS_STRING(repr)) < 0) |
| 5885 | return NULL; |
| 5886 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5887 | } |
| 5888 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5889 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5890 | PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
| 5891 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5892 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5893 | PyObject *result; |
| 5894 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5895 | if (tmp == NULL) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5896 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5897 | result = PyUnicode_AsUnicodeEscapeString(tmp); |
| 5898 | Py_DECREF(tmp); |
| 5899 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5900 | } |
| 5901 | |
| 5902 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 5903 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5904 | PyObject * |
| 5905 | PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5906 | Py_ssize_t size, |
| 5907 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5908 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5909 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5910 | Py_ssize_t startinpos; |
| 5911 | Py_ssize_t endinpos; |
| 5912 | Py_ssize_t outpos; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5913 | PyObject *v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5914 | const char *end; |
| 5915 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5916 | PyObject *errorHandler = NULL; |
| 5917 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5918 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5919 | /* Escaped strings will always be longer than the resulting |
| 5920 | 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] | 5921 | length after conversion to the true value. (But decoding error |
| 5922 | handler might have to resize the string) */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5923 | v = PyUnicode_New(size, 127); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5924 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5925 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5926 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5927 | return v; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5928 | outpos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5929 | end = s + size; |
| 5930 | while (s < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5931 | unsigned char c; |
| 5932 | Py_UCS4 x; |
| 5933 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5934 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5935 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5936 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 5937 | if (*s != '\\') { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5938 | if (unicode_putchar(&v, &outpos, (unsigned char)*s++) < 0) |
| 5939 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5940 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5941 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5942 | startinpos = s-starts; |
| 5943 | |
| 5944 | /* \u-escapes are only interpreted iff the number of leading |
| 5945 | backslashes if odd */ |
| 5946 | bs = s; |
| 5947 | for (;s < end;) { |
| 5948 | if (*s != '\\') |
| 5949 | break; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5950 | if (unicode_putchar(&v, &outpos, (unsigned char)*s++) < 0) |
| 5951 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5952 | } |
| 5953 | if (((s - bs) & 1) == 0 || |
| 5954 | s >= end || |
| 5955 | (*s != 'u' && *s != 'U')) { |
| 5956 | continue; |
| 5957 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5958 | outpos--; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5959 | count = *s=='u' ? 4 : 8; |
| 5960 | s++; |
| 5961 | |
| 5962 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5963 | for (x = 0, i = 0; i < count; ++i, ++s) { |
| 5964 | c = (unsigned char)*s; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 5965 | if (!Py_ISXDIGIT(c)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5966 | endinpos = s-starts; |
| 5967 | if (unicode_decode_call_errorhandler( |
| 5968 | errors, &errorHandler, |
| 5969 | "rawunicodeescape", "truncated \\uXXXX", |
| 5970 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5971 | &v, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5972 | goto onError; |
| 5973 | goto nextByte; |
| 5974 | } |
| 5975 | x = (x<<4) & ~0xF; |
| 5976 | if (c >= '0' && c <= '9') |
| 5977 | x += c - '0'; |
| 5978 | else if (c >= 'a' && c <= 'f') |
| 5979 | x += 10 + c - 'a'; |
| 5980 | else |
| 5981 | x += 10 + c - 'A'; |
| 5982 | } |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 5983 | if (x <= MAX_UNICODE) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5984 | if (unicode_putchar(&v, &outpos, x) < 0) |
| 5985 | goto onError; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 5986 | } else { |
| 5987 | endinpos = s-starts; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5988 | if (unicode_decode_call_errorhandler( |
| 5989 | errors, &errorHandler, |
| 5990 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5991 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5992 | &v, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5993 | goto onError; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5994 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5995 | nextByte: |
| 5996 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5997 | } |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 5998 | if (unicode_resize(&v, outpos) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5999 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6000 | Py_XDECREF(errorHandler); |
| 6001 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 6002 | return unicode_result(v); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6003 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6004 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6005 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6006 | Py_XDECREF(errorHandler); |
| 6007 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6008 | return NULL; |
| 6009 | } |
| 6010 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6011 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6012 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6013 | PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6014 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6015 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6016 | char *p; |
| 6017 | char *q; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6018 | Py_ssize_t expandsize, pos; |
| 6019 | int kind; |
| 6020 | void *data; |
| 6021 | Py_ssize_t len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6022 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6023 | if (!PyUnicode_Check(unicode)) { |
| 6024 | PyErr_BadArgument(); |
| 6025 | return NULL; |
| 6026 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6027 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6028 | return NULL; |
| 6029 | kind = PyUnicode_KIND(unicode); |
| 6030 | data = PyUnicode_DATA(unicode); |
| 6031 | len = PyUnicode_GET_LENGTH(unicode); |
Benjamin Peterson | 1518e87 | 2011-11-23 10:44:52 -0600 | [diff] [blame] | 6032 | /* 4 byte characters can take up 10 bytes, 2 byte characters can take up 6 |
| 6033 | bytes, and 1 byte characters 4. */ |
| 6034 | expandsize = kind * 2 + 2; |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 6035 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6036 | if (len > PY_SSIZE_T_MAX / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6037 | return PyErr_NoMemory(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6038 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6039 | repr = PyBytes_FromStringAndSize(NULL, expandsize * len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6040 | if (repr == NULL) |
| 6041 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6042 | if (len == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6043 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6044 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6045 | p = q = PyBytes_AS_STRING(repr); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6046 | for (pos = 0; pos < len; pos++) { |
| 6047 | Py_UCS4 ch = PyUnicode_READ(kind, data, pos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6048 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 6049 | if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 6050 | assert(ch <= MAX_UNICODE); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6051 | *p++ = '\\'; |
| 6052 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6053 | *p++ = Py_hexdigits[(ch >> 28) & 0xf]; |
| 6054 | *p++ = Py_hexdigits[(ch >> 24) & 0xf]; |
| 6055 | *p++ = Py_hexdigits[(ch >> 20) & 0xf]; |
| 6056 | *p++ = Py_hexdigits[(ch >> 16) & 0xf]; |
| 6057 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 6058 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 6059 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 6060 | *p++ = Py_hexdigits[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6061 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6062 | /* Map 16-bit characters to '\uxxxx' */ |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6063 | else if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6064 | *p++ = '\\'; |
| 6065 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6066 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 6067 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 6068 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 6069 | *p++ = Py_hexdigits[ch & 15]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6070 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6071 | /* Copy everything else as-is */ |
| 6072 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6073 | *p++ = (char) ch; |
| 6074 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6075 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6076 | assert(p > q); |
| 6077 | if (_PyBytes_Resize(&repr, p - q) < 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6078 | return NULL; |
| 6079 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6080 | } |
| 6081 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6082 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6083 | PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
| 6084 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6085 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6086 | PyObject *result; |
| 6087 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 6088 | if (tmp == NULL) |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 6089 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6090 | result = PyUnicode_AsRawUnicodeEscapeString(tmp); |
| 6091 | Py_DECREF(tmp); |
| 6092 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6093 | } |
| 6094 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6095 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 6096 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6097 | PyObject * |
| 6098 | _PyUnicode_DecodeUnicodeInternal(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6099 | Py_ssize_t size, |
| 6100 | const char *errors) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6101 | { |
| 6102 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6103 | Py_ssize_t startinpos; |
| 6104 | Py_ssize_t endinpos; |
| 6105 | Py_ssize_t outpos; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6106 | PyObject *v; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6107 | const char *end; |
| 6108 | const char *reason; |
| 6109 | PyObject *errorHandler = NULL; |
| 6110 | PyObject *exc = NULL; |
| 6111 | |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6112 | if (PyErr_WarnEx(PyExc_DeprecationWarning, |
Ezio Melotti | 11060a4 | 2011-11-16 09:39:10 +0200 | [diff] [blame] | 6113 | "unicode_internal codec has been deprecated", |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6114 | 1)) |
| 6115 | return NULL; |
| 6116 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6117 | /* XXX overflow detection missing */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6118 | 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] | 6119 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6120 | goto onError; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6121 | if (PyUnicode_GET_LENGTH(v) == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 6122 | return v; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6123 | outpos = 0; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6124 | end = s + size; |
| 6125 | |
| 6126 | while (s < end) { |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6127 | Py_UNICODE uch; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6128 | Py_UCS4 ch; |
| 6129 | /* We copy the raw representation one byte at a time because the |
| 6130 | pointer may be unaligned (see test_codeccallbacks). */ |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6131 | ((char *) &uch)[0] = s[0]; |
| 6132 | ((char *) &uch)[1] = s[1]; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6133 | #ifdef Py_UNICODE_WIDE |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6134 | ((char *) &uch)[2] = s[2]; |
| 6135 | ((char *) &uch)[3] = s[3]; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6136 | #endif |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6137 | ch = uch; |
| 6138 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6139 | /* We have to sanity check the raw data, otherwise doom looms for |
| 6140 | some malformed UCS-4 data. */ |
| 6141 | if ( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6142 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6143 | ch > 0x10ffff || |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6144 | #endif |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6145 | end-s < Py_UNICODE_SIZE |
| 6146 | ) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6147 | { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6148 | startinpos = s - starts; |
| 6149 | if (end-s < Py_UNICODE_SIZE) { |
| 6150 | endinpos = end-starts; |
| 6151 | reason = "truncated input"; |
| 6152 | } |
| 6153 | else { |
| 6154 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 6155 | reason = "illegal code point (> 0x10FFFF)"; |
| 6156 | } |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6157 | if (unicode_decode_call_errorhandler( |
| 6158 | errors, &errorHandler, |
| 6159 | "unicode_internal", reason, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 6160 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6161 | &v, &outpos)) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6162 | goto onError; |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6163 | continue; |
| 6164 | } |
| 6165 | |
| 6166 | s += Py_UNICODE_SIZE; |
| 6167 | #ifndef Py_UNICODE_WIDE |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 6168 | if (Py_UNICODE_IS_HIGH_SURROGATE(ch) && s < end) |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6169 | { |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6170 | Py_UNICODE uch2; |
| 6171 | ((char *) &uch2)[0] = s[0]; |
| 6172 | ((char *) &uch2)[1] = s[1]; |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 6173 | if (Py_UNICODE_IS_LOW_SURROGATE(uch2)) |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6174 | { |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 6175 | ch = Py_UNICODE_JOIN_SURROGATES(uch, uch2); |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6176 | s += Py_UNICODE_SIZE; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6177 | } |
| 6178 | } |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6179 | #endif |
| 6180 | |
| 6181 | if (unicode_putchar(&v, &outpos, ch) < 0) |
| 6182 | goto onError; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6183 | } |
| 6184 | |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 6185 | if (unicode_resize(&v, outpos) < 0) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6186 | goto onError; |
| 6187 | Py_XDECREF(errorHandler); |
| 6188 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 6189 | return unicode_result(v); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6190 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6191 | onError: |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6192 | Py_XDECREF(v); |
| 6193 | Py_XDECREF(errorHandler); |
| 6194 | Py_XDECREF(exc); |
| 6195 | return NULL; |
| 6196 | } |
| 6197 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6198 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 6199 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6200 | PyObject * |
| 6201 | PyUnicode_DecodeLatin1(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6202 | Py_ssize_t size, |
| 6203 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6204 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6205 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 6206 | return _PyUnicode_FromUCS1((unsigned char*)s, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6207 | } |
| 6208 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6209 | /* create or adjust a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6210 | static void |
| 6211 | make_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6212 | const char *encoding, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6213 | PyObject *unicode, |
| 6214 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6215 | const char *reason) |
| 6216 | { |
| 6217 | if (*exceptionObject == NULL) { |
| 6218 | *exceptionObject = PyObject_CallFunction( |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6219 | PyExc_UnicodeEncodeError, "sOnns", |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6220 | encoding, unicode, startpos, endpos, reason); |
| 6221 | } |
| 6222 | else { |
| 6223 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 6224 | goto onError; |
| 6225 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 6226 | goto onError; |
| 6227 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 6228 | goto onError; |
| 6229 | return; |
| 6230 | onError: |
| 6231 | Py_DECREF(*exceptionObject); |
| 6232 | *exceptionObject = NULL; |
| 6233 | } |
| 6234 | } |
| 6235 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6236 | /* raises a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6237 | static void |
| 6238 | raise_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6239 | const char *encoding, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6240 | PyObject *unicode, |
| 6241 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6242 | const char *reason) |
| 6243 | { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6244 | make_encode_exception(exceptionObject, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6245 | encoding, unicode, startpos, endpos, reason); |
| 6246 | if (*exceptionObject != NULL) |
| 6247 | PyCodec_StrictErrors(*exceptionObject); |
| 6248 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6249 | |
| 6250 | /* error handling callback helper: |
| 6251 | build arguments, call the callback and check the arguments, |
| 6252 | put the result into newpos and return the replacement string, which |
| 6253 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6254 | static PyObject * |
| 6255 | unicode_encode_call_errorhandler(const char *errors, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6256 | PyObject **errorHandler, |
| 6257 | const char *encoding, const char *reason, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6258 | PyObject *unicode, PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6259 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6260 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6261 | { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6262 | 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] | 6263 | Py_ssize_t len; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6264 | PyObject *restuple; |
| 6265 | PyObject *resunicode; |
| 6266 | |
| 6267 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6268 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6269 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6270 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6271 | } |
| 6272 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6273 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6274 | return NULL; |
| 6275 | len = PyUnicode_GET_LENGTH(unicode); |
| 6276 | |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6277 | make_encode_exception(exceptionObject, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6278 | encoding, unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6279 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6280 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6281 | |
| 6282 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6283 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6284 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6285 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6286 | if (!PyTuple_Check(restuple)) { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6287 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6288 | Py_DECREF(restuple); |
| 6289 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6290 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6291 | if (!PyArg_ParseTuple(restuple, argparse, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6292 | &resunicode, newpos)) { |
| 6293 | Py_DECREF(restuple); |
| 6294 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6295 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6296 | if (!PyUnicode_Check(resunicode) && !PyBytes_Check(resunicode)) { |
| 6297 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
| 6298 | Py_DECREF(restuple); |
| 6299 | return NULL; |
| 6300 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6301 | if (*newpos<0) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6302 | *newpos = len + *newpos; |
| 6303 | if (*newpos<0 || *newpos>len) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6304 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 6305 | Py_DECREF(restuple); |
| 6306 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 6307 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6308 | Py_INCREF(resunicode); |
| 6309 | Py_DECREF(restuple); |
| 6310 | return resunicode; |
| 6311 | } |
| 6312 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6313 | static PyObject * |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6314 | unicode_encode_ucs1(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6315 | const char *errors, |
Victor Stinner | fcd9653 | 2011-11-04 00:28:50 +0100 | [diff] [blame] | 6316 | unsigned int limit) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6317 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6318 | /* input state */ |
| 6319 | Py_ssize_t pos=0, size; |
| 6320 | int kind; |
| 6321 | void *data; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6322 | /* output object */ |
| 6323 | PyObject *res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6324 | /* pointer into the output */ |
| 6325 | char *str; |
| 6326 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6327 | Py_ssize_t ressize; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6328 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 6329 | 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] | 6330 | PyObject *errorHandler = NULL; |
| 6331 | PyObject *exc = NULL; |
| 6332 | /* the following variable is used for caching string comparisons |
| 6333 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 6334 | int known_errorHandler = -1; |
| 6335 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6336 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6337 | return NULL; |
| 6338 | size = PyUnicode_GET_LENGTH(unicode); |
| 6339 | kind = PyUnicode_KIND(unicode); |
| 6340 | data = PyUnicode_DATA(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6341 | /* allocate enough for a simple encoding without |
| 6342 | replacements, if we need more, we'll resize */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6343 | if (size == 0) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6344 | return PyBytes_FromStringAndSize(NULL, 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6345 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6346 | if (res == NULL) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6347 | return NULL; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6348 | str = PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6349 | ressize = size; |
| 6350 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6351 | while (pos < size) { |
| 6352 | Py_UCS4 c = PyUnicode_READ(kind, data, pos); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6353 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6354 | /* can we encode this? */ |
| 6355 | if (c<limit) { |
| 6356 | /* no overflow check, because we know that the space is enough */ |
| 6357 | *str++ = (char)c; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6358 | ++pos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6359 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6360 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6361 | Py_ssize_t requiredsize; |
| 6362 | PyObject *repunicode; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6363 | Py_ssize_t repsize, newpos, respos, i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6364 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6365 | Py_ssize_t collstart = pos; |
| 6366 | Py_ssize_t collend = pos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6367 | /* find all unecodable characters */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6368 | while ((collend < size) && (PyUnicode_READ(kind, data, collend)>=limit)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6369 | ++collend; |
| 6370 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 6371 | if (known_errorHandler==-1) { |
| 6372 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 6373 | known_errorHandler = 1; |
| 6374 | else if (!strcmp(errors, "replace")) |
| 6375 | known_errorHandler = 2; |
| 6376 | else if (!strcmp(errors, "ignore")) |
| 6377 | known_errorHandler = 3; |
| 6378 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 6379 | known_errorHandler = 4; |
| 6380 | else |
| 6381 | known_errorHandler = 0; |
| 6382 | } |
| 6383 | switch (known_errorHandler) { |
| 6384 | case 1: /* strict */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6385 | raise_encode_exception(&exc, encoding, unicode, collstart, collend, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6386 | goto onError; |
| 6387 | case 2: /* replace */ |
| 6388 | while (collstart++<collend) |
| 6389 | *str++ = '?'; /* fall through */ |
| 6390 | case 3: /* ignore */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6391 | pos = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6392 | break; |
| 6393 | case 4: /* xmlcharrefreplace */ |
| 6394 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6395 | /* determine replacement size */ |
| 6396 | for (i = collstart, repsize = 0; i < collend; ++i) { |
| 6397 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 6398 | if (ch < 10) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6399 | repsize += 2+1+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6400 | else if (ch < 100) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6401 | repsize += 2+2+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6402 | else if (ch < 1000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6403 | repsize += 2+3+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6404 | else if (ch < 10000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6405 | repsize += 2+4+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6406 | else if (ch < 100000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6407 | repsize += 2+5+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6408 | else if (ch < 1000000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6409 | repsize += 2+6+1; |
Victor Stinner | 0d3721d | 2011-11-22 03:27:53 +0100 | [diff] [blame] | 6410 | else { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 6411 | assert(ch <= MAX_UNICODE); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6412 | repsize += 2+7+1; |
Victor Stinner | 0d3721d | 2011-11-22 03:27:53 +0100 | [diff] [blame] | 6413 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6414 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6415 | requiredsize = respos+repsize+(size-collend); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6416 | if (requiredsize > ressize) { |
| 6417 | if (requiredsize<2*ressize) |
| 6418 | requiredsize = 2*ressize; |
| 6419 | if (_PyBytes_Resize(&res, requiredsize)) |
| 6420 | goto onError; |
| 6421 | str = PyBytes_AS_STRING(res) + respos; |
| 6422 | ressize = requiredsize; |
| 6423 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6424 | /* generate replacement */ |
| 6425 | for (i = collstart; i < collend; ++i) { |
| 6426 | str += sprintf(str, "&#%d;", PyUnicode_READ(kind, data, i)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6427 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6428 | pos = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6429 | break; |
| 6430 | default: |
| 6431 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6432 | encoding, reason, unicode, &exc, |
| 6433 | collstart, collend, &newpos); |
| 6434 | if (repunicode == NULL || (PyUnicode_Check(repunicode) && |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6435 | PyUnicode_READY(repunicode) == -1)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6436 | goto onError; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6437 | if (PyBytes_Check(repunicode)) { |
| 6438 | /* Directly copy bytes result to output. */ |
| 6439 | repsize = PyBytes_Size(repunicode); |
| 6440 | if (repsize > 1) { |
| 6441 | /* Make room for all additional bytes. */ |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6442 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6443 | if (_PyBytes_Resize(&res, ressize+repsize-1)) { |
| 6444 | Py_DECREF(repunicode); |
| 6445 | goto onError; |
| 6446 | } |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6447 | str = PyBytes_AS_STRING(res) + respos; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6448 | ressize += repsize-1; |
| 6449 | } |
| 6450 | memcpy(str, PyBytes_AsString(repunicode), repsize); |
| 6451 | str += repsize; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6452 | pos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6453 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6454 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6455 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6456 | /* need more space? (at least enough for what we |
| 6457 | have+the replacement+the rest of the string, so |
| 6458 | we won't have to check space for encodable characters) */ |
| 6459 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6460 | repsize = PyUnicode_GET_LENGTH(repunicode); |
| 6461 | requiredsize = respos+repsize+(size-collend); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6462 | if (requiredsize > ressize) { |
| 6463 | if (requiredsize<2*ressize) |
| 6464 | requiredsize = 2*ressize; |
| 6465 | if (_PyBytes_Resize(&res, requiredsize)) { |
| 6466 | Py_DECREF(repunicode); |
| 6467 | goto onError; |
| 6468 | } |
| 6469 | str = PyBytes_AS_STRING(res) + respos; |
| 6470 | ressize = requiredsize; |
| 6471 | } |
| 6472 | /* check if there is anything unencodable in the replacement |
| 6473 | and copy it to the output */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6474 | for (i = 0; repsize-->0; ++i, ++str) { |
| 6475 | c = PyUnicode_READ_CHAR(repunicode, i); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6476 | if (c >= limit) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6477 | raise_encode_exception(&exc, encoding, unicode, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6478 | pos, pos+1, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6479 | Py_DECREF(repunicode); |
| 6480 | goto onError; |
| 6481 | } |
| 6482 | *str = (char)c; |
| 6483 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6484 | pos = newpos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6485 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6486 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6487 | } |
| 6488 | } |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6489 | /* Resize if we allocated to much */ |
| 6490 | size = str - PyBytes_AS_STRING(res); |
| 6491 | if (size < ressize) { /* If this falls res will be NULL */ |
Alexandre Vassalotti | bad1b92 | 2008-12-27 09:49:09 +0000 | [diff] [blame] | 6492 | assert(size >= 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6493 | if (_PyBytes_Resize(&res, size) < 0) |
| 6494 | goto onError; |
| 6495 | } |
| 6496 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6497 | Py_XDECREF(errorHandler); |
| 6498 | Py_XDECREF(exc); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6499 | return res; |
| 6500 | |
| 6501 | onError: |
| 6502 | Py_XDECREF(res); |
| 6503 | Py_XDECREF(errorHandler); |
| 6504 | Py_XDECREF(exc); |
| 6505 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6506 | } |
| 6507 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6508 | /* Deprecated */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6509 | PyObject * |
| 6510 | PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6511 | Py_ssize_t size, |
| 6512 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6513 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6514 | PyObject *result; |
| 6515 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 6516 | if (unicode == NULL) |
| 6517 | return NULL; |
| 6518 | result = unicode_encode_ucs1(unicode, errors, 256); |
| 6519 | Py_DECREF(unicode); |
| 6520 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6521 | } |
| 6522 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6523 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6524 | _PyUnicode_AsLatin1String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6525 | { |
| 6526 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6527 | PyErr_BadArgument(); |
| 6528 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6529 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6530 | if (PyUnicode_READY(unicode) == -1) |
| 6531 | return NULL; |
| 6532 | /* Fast path: if it is a one-byte string, construct |
| 6533 | bytes object directly. */ |
| 6534 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) |
| 6535 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6536 | PyUnicode_GET_LENGTH(unicode)); |
| 6537 | /* Non-Latin-1 characters present. Defer to above function to |
| 6538 | raise the exception. */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6539 | return unicode_encode_ucs1(unicode, errors, 256); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6540 | } |
| 6541 | |
| 6542 | PyObject* |
| 6543 | PyUnicode_AsLatin1String(PyObject *unicode) |
| 6544 | { |
| 6545 | return _PyUnicode_AsLatin1String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6546 | } |
| 6547 | |
| 6548 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 6549 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6550 | PyObject * |
| 6551 | PyUnicode_DecodeASCII(const char *s, |
| 6552 | Py_ssize_t size, |
| 6553 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6554 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6555 | const char *starts = s; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6556 | PyObject *unicode; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6557 | int kind; |
| 6558 | void *data; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6559 | Py_ssize_t startinpos; |
| 6560 | Py_ssize_t endinpos; |
| 6561 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6562 | const char *e; |
| 6563 | PyObject *errorHandler = NULL; |
| 6564 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6565 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 6566 | if (size == 0) { |
| 6567 | Py_INCREF(unicode_empty); |
| 6568 | return unicode_empty; |
| 6569 | } |
| 6570 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6571 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6572 | if (size == 1 && (unsigned char)s[0] < 128) |
| 6573 | return get_latin1_char((unsigned char)s[0]); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6574 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6575 | unicode = PyUnicode_New(size, 127); |
| 6576 | if (unicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6577 | goto onError; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6578 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6579 | e = s + size; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6580 | data = PyUnicode_1BYTE_DATA(unicode); |
| 6581 | outpos = ascii_decode(s, e, (Py_UCS1 *)data); |
| 6582 | if (outpos == size) |
| 6583 | return unicode; |
| 6584 | |
| 6585 | s += outpos; |
| 6586 | kind = PyUnicode_1BYTE_KIND; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6587 | while (s < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6588 | register unsigned char c = (unsigned char)*s; |
| 6589 | if (c < 128) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6590 | PyUnicode_WRITE(kind, data, outpos++, c); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6591 | ++s; |
| 6592 | } |
| 6593 | else { |
| 6594 | startinpos = s-starts; |
| 6595 | endinpos = startinpos + 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6596 | if (unicode_decode_call_errorhandler( |
| 6597 | errors, &errorHandler, |
| 6598 | "ascii", "ordinal not in range(128)", |
| 6599 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6600 | &unicode, &outpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6601 | goto onError; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6602 | kind = PyUnicode_KIND(unicode); |
| 6603 | data = PyUnicode_DATA(unicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6604 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6605 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6606 | if (unicode_resize(&unicode, outpos) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6607 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6608 | Py_XDECREF(errorHandler); |
| 6609 | Py_XDECREF(exc); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6610 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 6611 | return unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6612 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6613 | onError: |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6614 | Py_XDECREF(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6615 | Py_XDECREF(errorHandler); |
| 6616 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6617 | return NULL; |
| 6618 | } |
| 6619 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6620 | /* Deprecated */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6621 | PyObject * |
| 6622 | PyUnicode_EncodeASCII(const Py_UNICODE *p, |
| 6623 | Py_ssize_t size, |
| 6624 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6625 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6626 | PyObject *result; |
| 6627 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 6628 | if (unicode == NULL) |
| 6629 | return NULL; |
| 6630 | result = unicode_encode_ucs1(unicode, errors, 128); |
| 6631 | Py_DECREF(unicode); |
| 6632 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6633 | } |
| 6634 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6635 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6636 | _PyUnicode_AsASCIIString(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6637 | { |
| 6638 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6639 | PyErr_BadArgument(); |
| 6640 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6641 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6642 | if (PyUnicode_READY(unicode) == -1) |
| 6643 | return NULL; |
| 6644 | /* Fast path: if it is an ASCII-only string, construct bytes object |
| 6645 | directly. Else defer to above function to raise the exception. */ |
| 6646 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) |
| 6647 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6648 | PyUnicode_GET_LENGTH(unicode)); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6649 | return unicode_encode_ucs1(unicode, errors, 128); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6650 | } |
| 6651 | |
| 6652 | PyObject * |
| 6653 | PyUnicode_AsASCIIString(PyObject *unicode) |
| 6654 | { |
| 6655 | return _PyUnicode_AsASCIIString(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6656 | } |
| 6657 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 6658 | #ifdef HAVE_MBCS |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6659 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6660 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6661 | |
Hirokazu Yamamoto | 3530246 | 2009-03-21 13:23:27 +0000 | [diff] [blame] | 6662 | #if SIZEOF_INT < SIZEOF_SIZE_T |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6663 | #define NEED_RETRY |
| 6664 | #endif |
| 6665 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6666 | #ifndef WC_ERR_INVALID_CHARS |
| 6667 | # define WC_ERR_INVALID_CHARS 0x0080 |
| 6668 | #endif |
| 6669 | |
| 6670 | static char* |
| 6671 | code_page_name(UINT code_page, PyObject **obj) |
| 6672 | { |
| 6673 | *obj = NULL; |
| 6674 | if (code_page == CP_ACP) |
| 6675 | return "mbcs"; |
| 6676 | if (code_page == CP_UTF7) |
| 6677 | return "CP_UTF7"; |
| 6678 | if (code_page == CP_UTF8) |
| 6679 | return "CP_UTF8"; |
| 6680 | |
| 6681 | *obj = PyBytes_FromFormat("cp%u", code_page); |
| 6682 | if (*obj == NULL) |
| 6683 | return NULL; |
| 6684 | return PyBytes_AS_STRING(*obj); |
| 6685 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6686 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6687 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6688 | is_dbcs_lead_byte(UINT code_page, const char *s, int offset) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6689 | { |
| 6690 | const char *curr = s + offset; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6691 | const char *prev; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6692 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6693 | if (!IsDBCSLeadByteEx(code_page, *curr)) |
| 6694 | return 0; |
| 6695 | |
| 6696 | prev = CharPrevExA(code_page, s, curr, 0); |
| 6697 | if (prev == curr) |
| 6698 | return 1; |
| 6699 | /* FIXME: This code is limited to "true" double-byte encodings, |
| 6700 | as it assumes an incomplete character consists of a single |
| 6701 | byte. */ |
| 6702 | if (curr - prev == 2) |
| 6703 | return 1; |
| 6704 | if (!IsDBCSLeadByteEx(code_page, *prev)) |
| 6705 | return 1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6706 | return 0; |
| 6707 | } |
| 6708 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6709 | static DWORD |
| 6710 | decode_code_page_flags(UINT code_page) |
| 6711 | { |
| 6712 | if (code_page == CP_UTF7) { |
| 6713 | /* The CP_UTF7 decoder only supports flags=0 */ |
| 6714 | return 0; |
| 6715 | } |
| 6716 | else |
| 6717 | return MB_ERR_INVALID_CHARS; |
| 6718 | } |
| 6719 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6720 | /* |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6721 | * Decode a byte string from a Windows code page into unicode object in strict |
| 6722 | * mode. |
| 6723 | * |
| 6724 | * Returns consumed size if succeed, returns -2 on decode error, or raise a |
| 6725 | * WindowsError and returns -1 on other error. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6726 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6727 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6728 | decode_code_page_strict(UINT code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6729 | PyObject **v, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6730 | const char *in, |
| 6731 | int insize) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6732 | { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6733 | const DWORD flags = decode_code_page_flags(code_page); |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 6734 | wchar_t *out; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6735 | DWORD outsize; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6736 | |
| 6737 | /* First get the size of the result */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6738 | assert(insize > 0); |
| 6739 | outsize = MultiByteToWideChar(code_page, flags, in, insize, NULL, 0); |
| 6740 | if (outsize <= 0) |
| 6741 | goto error; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6742 | |
| 6743 | if (*v == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6744 | /* Create unicode object */ |
Victor Stinner | ab59594 | 2011-12-17 04:59:06 +0100 | [diff] [blame] | 6745 | /* FIXME: don't use _PyUnicode_New(), but allocate a wchar_t* buffer */ |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6746 | *v = (PyObject*)_PyUnicode_New(outsize); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6747 | if (*v == NULL) |
| 6748 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6749 | out = PyUnicode_AS_UNICODE(*v); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6750 | } |
| 6751 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6752 | /* Extend unicode object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6753 | Py_ssize_t n = PyUnicode_GET_SIZE(*v); |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 6754 | if (unicode_resize(v, n + outsize) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6755 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6756 | out = PyUnicode_AS_UNICODE(*v) + n; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6757 | } |
| 6758 | |
| 6759 | /* Do the conversion */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6760 | outsize = MultiByteToWideChar(code_page, flags, in, insize, out, outsize); |
| 6761 | if (outsize <= 0) |
| 6762 | goto error; |
| 6763 | return insize; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6764 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6765 | error: |
| 6766 | if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION) |
| 6767 | return -2; |
| 6768 | PyErr_SetFromWindowsErr(0); |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6769 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6770 | } |
| 6771 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6772 | /* |
| 6773 | * Decode a byte string from a code page into unicode object with an error |
| 6774 | * handler. |
| 6775 | * |
| 6776 | * Returns consumed size if succeed, or raise a WindowsError or |
| 6777 | * UnicodeDecodeError exception and returns -1 on error. |
| 6778 | */ |
| 6779 | static int |
| 6780 | decode_code_page_errors(UINT code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6781 | PyObject **v, |
| 6782 | const char *in, const int size, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6783 | const char *errors) |
| 6784 | { |
| 6785 | const char *startin = in; |
| 6786 | const char *endin = in + size; |
| 6787 | const DWORD flags = decode_code_page_flags(code_page); |
| 6788 | /* Ideally, we should get reason from FormatMessage. This is the Windows |
| 6789 | 2000 English version of the message. */ |
| 6790 | const char *reason = "No mapping for the Unicode character exists " |
| 6791 | "in the target code page."; |
| 6792 | /* each step cannot decode more than 1 character, but a character can be |
| 6793 | represented as a surrogate pair */ |
| 6794 | wchar_t buffer[2], *startout, *out; |
| 6795 | int insize, outsize; |
| 6796 | PyObject *errorHandler = NULL; |
| 6797 | PyObject *exc = NULL; |
| 6798 | PyObject *encoding_obj = NULL; |
| 6799 | char *encoding; |
| 6800 | DWORD err; |
| 6801 | int ret = -1; |
| 6802 | |
| 6803 | assert(size > 0); |
| 6804 | |
| 6805 | encoding = code_page_name(code_page, &encoding_obj); |
| 6806 | if (encoding == NULL) |
| 6807 | return -1; |
| 6808 | |
| 6809 | if (errors == NULL || strcmp(errors, "strict") == 0) { |
| 6810 | /* The last error was ERROR_NO_UNICODE_TRANSLATION, then we raise a |
| 6811 | UnicodeDecodeError. */ |
| 6812 | make_decode_exception(&exc, encoding, in, size, 0, 0, reason); |
| 6813 | if (exc != NULL) { |
| 6814 | PyCodec_StrictErrors(exc); |
| 6815 | Py_CLEAR(exc); |
| 6816 | } |
| 6817 | goto error; |
| 6818 | } |
| 6819 | |
| 6820 | if (*v == NULL) { |
| 6821 | /* Create unicode object */ |
| 6822 | if (size > PY_SSIZE_T_MAX / (Py_ssize_t)Py_ARRAY_LENGTH(buffer)) { |
| 6823 | PyErr_NoMemory(); |
| 6824 | goto error; |
| 6825 | } |
Victor Stinner | ab59594 | 2011-12-17 04:59:06 +0100 | [diff] [blame] | 6826 | /* FIXME: don't use _PyUnicode_New(), but allocate a wchar_t* buffer */ |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6827 | *v = (PyObject*)_PyUnicode_New(size * Py_ARRAY_LENGTH(buffer)); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6828 | if (*v == NULL) |
| 6829 | goto error; |
| 6830 | startout = PyUnicode_AS_UNICODE(*v); |
| 6831 | } |
| 6832 | else { |
| 6833 | /* Extend unicode object */ |
| 6834 | Py_ssize_t n = PyUnicode_GET_SIZE(*v); |
| 6835 | if (size > (PY_SSIZE_T_MAX - n) / (Py_ssize_t)Py_ARRAY_LENGTH(buffer)) { |
| 6836 | PyErr_NoMemory(); |
| 6837 | goto error; |
| 6838 | } |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 6839 | if (unicode_resize(v, n + size * Py_ARRAY_LENGTH(buffer)) < 0) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6840 | goto error; |
| 6841 | startout = PyUnicode_AS_UNICODE(*v) + n; |
| 6842 | } |
| 6843 | |
| 6844 | /* Decode the byte string character per character */ |
| 6845 | out = startout; |
| 6846 | while (in < endin) |
| 6847 | { |
| 6848 | /* Decode a character */ |
| 6849 | insize = 1; |
| 6850 | do |
| 6851 | { |
| 6852 | outsize = MultiByteToWideChar(code_page, flags, |
| 6853 | in, insize, |
| 6854 | buffer, Py_ARRAY_LENGTH(buffer)); |
| 6855 | if (outsize > 0) |
| 6856 | break; |
| 6857 | err = GetLastError(); |
| 6858 | if (err != ERROR_NO_UNICODE_TRANSLATION |
| 6859 | && err != ERROR_INSUFFICIENT_BUFFER) |
| 6860 | { |
| 6861 | PyErr_SetFromWindowsErr(0); |
| 6862 | goto error; |
| 6863 | } |
| 6864 | insize++; |
| 6865 | } |
| 6866 | /* 4=maximum length of a UTF-8 sequence */ |
| 6867 | while (insize <= 4 && (in + insize) <= endin); |
| 6868 | |
| 6869 | if (outsize <= 0) { |
| 6870 | Py_ssize_t startinpos, endinpos, outpos; |
| 6871 | |
| 6872 | startinpos = in - startin; |
| 6873 | endinpos = startinpos + 1; |
| 6874 | outpos = out - PyUnicode_AS_UNICODE(*v); |
| 6875 | if (unicode_decode_call_errorhandler( |
| 6876 | errors, &errorHandler, |
| 6877 | encoding, reason, |
| 6878 | &startin, &endin, &startinpos, &endinpos, &exc, &in, |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 6879 | v, &outpos)) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6880 | { |
| 6881 | goto error; |
| 6882 | } |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 6883 | out = PyUnicode_AS_UNICODE(*v) + outpos; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6884 | } |
| 6885 | else { |
| 6886 | in += insize; |
| 6887 | memcpy(out, buffer, outsize * sizeof(wchar_t)); |
| 6888 | out += outsize; |
| 6889 | } |
| 6890 | } |
| 6891 | |
| 6892 | /* write a NUL character at the end */ |
| 6893 | *out = 0; |
| 6894 | |
| 6895 | /* Extend unicode object */ |
| 6896 | outsize = out - startout; |
| 6897 | assert(outsize <= PyUnicode_WSTR_LENGTH(*v)); |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 6898 | if (unicode_resize(v, outsize) < 0) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6899 | goto error; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6900 | ret = size; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6901 | |
| 6902 | error: |
| 6903 | Py_XDECREF(encoding_obj); |
| 6904 | Py_XDECREF(errorHandler); |
| 6905 | Py_XDECREF(exc); |
| 6906 | return ret; |
| 6907 | } |
| 6908 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6909 | static PyObject * |
| 6910 | decode_code_page_stateful(int code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6911 | const char *s, Py_ssize_t size, |
| 6912 | const char *errors, Py_ssize_t *consumed) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6913 | { |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6914 | PyObject *v = NULL; |
| 6915 | int chunk_size, final, converted, done; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6916 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6917 | if (code_page < 0) { |
| 6918 | PyErr_SetString(PyExc_ValueError, "invalid code page number"); |
| 6919 | return NULL; |
| 6920 | } |
| 6921 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6922 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6923 | *consumed = 0; |
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 | do |
| 6926 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6927 | #ifdef NEED_RETRY |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6928 | if (size > INT_MAX) { |
| 6929 | chunk_size = INT_MAX; |
| 6930 | final = 0; |
| 6931 | done = 0; |
| 6932 | } |
| 6933 | else |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6934 | #endif |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6935 | { |
| 6936 | chunk_size = (int)size; |
| 6937 | final = (consumed == NULL); |
| 6938 | done = 1; |
| 6939 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6940 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6941 | /* Skip trailing lead-byte unless 'final' is set */ |
| 6942 | if (!final && is_dbcs_lead_byte(code_page, s, chunk_size - 1)) |
| 6943 | --chunk_size; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6944 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6945 | if (chunk_size == 0 && done) { |
| 6946 | if (v != NULL) |
| 6947 | break; |
| 6948 | Py_INCREF(unicode_empty); |
| 6949 | return unicode_empty; |
| 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 | |
| 6953 | converted = decode_code_page_strict(code_page, &v, |
| 6954 | s, chunk_size); |
| 6955 | if (converted == -2) |
| 6956 | converted = decode_code_page_errors(code_page, &v, |
| 6957 | s, chunk_size, |
| 6958 | errors); |
| 6959 | assert(converted != 0); |
| 6960 | |
| 6961 | if (converted < 0) { |
| 6962 | Py_XDECREF(v); |
| 6963 | return NULL; |
| 6964 | } |
| 6965 | |
| 6966 | if (consumed) |
| 6967 | *consumed += converted; |
| 6968 | |
| 6969 | s += converted; |
| 6970 | size -= converted; |
| 6971 | } while (!done); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6972 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 6973 | return unicode_result(v); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6974 | } |
| 6975 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6976 | PyObject * |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6977 | PyUnicode_DecodeCodePageStateful(int code_page, |
| 6978 | const char *s, |
| 6979 | Py_ssize_t size, |
| 6980 | const char *errors, |
| 6981 | Py_ssize_t *consumed) |
| 6982 | { |
| 6983 | return decode_code_page_stateful(code_page, s, size, errors, consumed); |
| 6984 | } |
| 6985 | |
| 6986 | PyObject * |
| 6987 | PyUnicode_DecodeMBCSStateful(const char *s, |
| 6988 | Py_ssize_t size, |
| 6989 | const char *errors, |
| 6990 | Py_ssize_t *consumed) |
| 6991 | { |
| 6992 | return decode_code_page_stateful(CP_ACP, s, size, errors, consumed); |
| 6993 | } |
| 6994 | |
| 6995 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6996 | PyUnicode_DecodeMBCS(const char *s, |
| 6997 | Py_ssize_t size, |
| 6998 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6999 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7000 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 7001 | } |
| 7002 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7003 | static DWORD |
| 7004 | encode_code_page_flags(UINT code_page, const char *errors) |
| 7005 | { |
| 7006 | if (code_page == CP_UTF8) { |
| 7007 | if (winver.dwMajorVersion >= 6) |
| 7008 | /* CP_UTF8 supports WC_ERR_INVALID_CHARS on Windows Vista |
| 7009 | and later */ |
| 7010 | return WC_ERR_INVALID_CHARS; |
| 7011 | else |
| 7012 | /* CP_UTF8 only supports flags=0 on Windows older than Vista */ |
| 7013 | return 0; |
| 7014 | } |
| 7015 | else if (code_page == CP_UTF7) { |
| 7016 | /* CP_UTF7 only supports flags=0 */ |
| 7017 | return 0; |
| 7018 | } |
| 7019 | else { |
| 7020 | if (errors != NULL && strcmp(errors, "replace") == 0) |
| 7021 | return 0; |
| 7022 | else |
| 7023 | return WC_NO_BEST_FIT_CHARS; |
| 7024 | } |
| 7025 | } |
| 7026 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7027 | /* |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7028 | * Encode a Unicode string to a Windows code page into a byte string in strict |
| 7029 | * mode. |
| 7030 | * |
| 7031 | * Returns consumed characters if succeed, returns -2 on encode error, or raise |
| 7032 | * a WindowsError and returns -1 on other error. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7033 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7034 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7035 | encode_code_page_strict(UINT code_page, PyObject **outbytes, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7036 | PyObject *unicode, Py_ssize_t offset, int len, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7037 | const char* errors) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7038 | { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7039 | BOOL usedDefaultChar = FALSE; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7040 | BOOL *pusedDefaultChar = &usedDefaultChar; |
| 7041 | int outsize; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7042 | PyObject *exc = NULL; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 7043 | wchar_t *p; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7044 | Py_ssize_t size; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7045 | const DWORD flags = encode_code_page_flags(code_page, NULL); |
| 7046 | char *out; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7047 | /* Create a substring so that we can get the UTF-16 representation |
| 7048 | of just the slice under consideration. */ |
| 7049 | PyObject *substring; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7050 | |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7051 | assert(len > 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7052 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7053 | if (code_page != CP_UTF8 && code_page != CP_UTF7) |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7054 | pusedDefaultChar = &usedDefaultChar; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7055 | else |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7056 | pusedDefaultChar = NULL; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7057 | |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7058 | substring = PyUnicode_Substring(unicode, offset, offset+len); |
| 7059 | if (substring == NULL) |
| 7060 | return -1; |
| 7061 | p = PyUnicode_AsUnicodeAndSize(substring, &size); |
| 7062 | if (p == NULL) { |
| 7063 | Py_DECREF(substring); |
| 7064 | return -1; |
| 7065 | } |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7066 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7067 | /* First get the size of the result */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7068 | outsize = WideCharToMultiByte(code_page, flags, |
| 7069 | p, size, |
| 7070 | NULL, 0, |
| 7071 | NULL, pusedDefaultChar); |
| 7072 | if (outsize <= 0) |
| 7073 | goto error; |
| 7074 | /* If we used a default char, then we failed! */ |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7075 | if (pusedDefaultChar && *pusedDefaultChar) { |
| 7076 | Py_DECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7077 | return -2; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7078 | } |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7079 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7080 | if (*outbytes == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7081 | /* Create string object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7082 | *outbytes = PyBytes_FromStringAndSize(NULL, outsize); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7083 | if (*outbytes == NULL) { |
| 7084 | Py_DECREF(substring); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7085 | return -1; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7086 | } |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7087 | out = PyBytes_AS_STRING(*outbytes); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7088 | } |
| 7089 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7090 | /* Extend string object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7091 | const Py_ssize_t n = PyBytes_Size(*outbytes); |
| 7092 | if (outsize > PY_SSIZE_T_MAX - n) { |
| 7093 | PyErr_NoMemory(); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7094 | Py_DECREF(substring); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7095 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7096 | } |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7097 | if (_PyBytes_Resize(outbytes, n + outsize) < 0) { |
| 7098 | Py_DECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7099 | return -1; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7100 | } |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7101 | out = PyBytes_AS_STRING(*outbytes) + n; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7102 | } |
| 7103 | |
| 7104 | /* Do the conversion */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7105 | outsize = WideCharToMultiByte(code_page, flags, |
| 7106 | p, size, |
| 7107 | out, outsize, |
| 7108 | NULL, pusedDefaultChar); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7109 | Py_CLEAR(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7110 | if (outsize <= 0) |
| 7111 | goto error; |
| 7112 | if (pusedDefaultChar && *pusedDefaultChar) |
| 7113 | return -2; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7114 | return 0; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7115 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7116 | error: |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7117 | Py_XDECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7118 | if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION) |
| 7119 | return -2; |
| 7120 | PyErr_SetFromWindowsErr(0); |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7121 | return -1; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7122 | } |
| 7123 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7124 | /* |
| 7125 | * Encode a Unicode string to a Windows code page into a byte string using a |
| 7126 | * error handler. |
| 7127 | * |
| 7128 | * Returns consumed characters if succeed, or raise a WindowsError and returns |
| 7129 | * -1 on other error. |
| 7130 | */ |
| 7131 | static int |
| 7132 | encode_code_page_errors(UINT code_page, PyObject **outbytes, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7133 | PyObject *unicode, Py_ssize_t unicode_offset, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7134 | Py_ssize_t insize, const char* errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7135 | { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7136 | const DWORD flags = encode_code_page_flags(code_page, errors); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7137 | Py_ssize_t pos = unicode_offset; |
| 7138 | Py_ssize_t endin = unicode_offset + insize; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7139 | /* Ideally, we should get reason from FormatMessage. This is the Windows |
| 7140 | 2000 English version of the message. */ |
| 7141 | const char *reason = "invalid character"; |
| 7142 | /* 4=maximum length of a UTF-8 sequence */ |
| 7143 | char buffer[4]; |
| 7144 | BOOL usedDefaultChar = FALSE, *pusedDefaultChar; |
| 7145 | Py_ssize_t outsize; |
| 7146 | char *out; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7147 | PyObject *errorHandler = NULL; |
| 7148 | PyObject *exc = NULL; |
| 7149 | PyObject *encoding_obj = NULL; |
| 7150 | char *encoding; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7151 | Py_ssize_t newpos, newoutsize; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7152 | PyObject *rep; |
| 7153 | int ret = -1; |
| 7154 | |
| 7155 | assert(insize > 0); |
| 7156 | |
| 7157 | encoding = code_page_name(code_page, &encoding_obj); |
| 7158 | if (encoding == NULL) |
| 7159 | return -1; |
| 7160 | |
| 7161 | if (errors == NULL || strcmp(errors, "strict") == 0) { |
| 7162 | /* The last error was ERROR_NO_UNICODE_TRANSLATION, |
| 7163 | then we raise a UnicodeEncodeError. */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7164 | make_encode_exception(&exc, encoding, unicode, 0, 0, reason); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7165 | if (exc != NULL) { |
| 7166 | PyCodec_StrictErrors(exc); |
| 7167 | Py_DECREF(exc); |
| 7168 | } |
| 7169 | Py_XDECREF(encoding_obj); |
| 7170 | return -1; |
| 7171 | } |
| 7172 | |
| 7173 | if (code_page != CP_UTF8 && code_page != CP_UTF7) |
| 7174 | pusedDefaultChar = &usedDefaultChar; |
| 7175 | else |
| 7176 | pusedDefaultChar = NULL; |
| 7177 | |
| 7178 | if (Py_ARRAY_LENGTH(buffer) > PY_SSIZE_T_MAX / insize) { |
| 7179 | PyErr_NoMemory(); |
| 7180 | goto error; |
| 7181 | } |
| 7182 | outsize = insize * Py_ARRAY_LENGTH(buffer); |
| 7183 | |
| 7184 | if (*outbytes == NULL) { |
| 7185 | /* Create string object */ |
| 7186 | *outbytes = PyBytes_FromStringAndSize(NULL, outsize); |
| 7187 | if (*outbytes == NULL) |
| 7188 | goto error; |
| 7189 | out = PyBytes_AS_STRING(*outbytes); |
| 7190 | } |
| 7191 | else { |
| 7192 | /* Extend string object */ |
| 7193 | Py_ssize_t n = PyBytes_Size(*outbytes); |
| 7194 | if (n > PY_SSIZE_T_MAX - outsize) { |
| 7195 | PyErr_NoMemory(); |
| 7196 | goto error; |
| 7197 | } |
| 7198 | if (_PyBytes_Resize(outbytes, n + outsize) < 0) |
| 7199 | goto error; |
| 7200 | out = PyBytes_AS_STRING(*outbytes) + n; |
| 7201 | } |
| 7202 | |
| 7203 | /* Encode the string character per character */ |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7204 | while (pos < endin) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7205 | { |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7206 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, pos); |
| 7207 | wchar_t chars[2]; |
| 7208 | int charsize; |
| 7209 | if (ch < 0x10000) { |
| 7210 | chars[0] = (wchar_t)ch; |
| 7211 | charsize = 1; |
| 7212 | } |
| 7213 | else { |
| 7214 | ch -= 0x10000; |
| 7215 | chars[0] = 0xd800 + (ch >> 10); |
| 7216 | chars[1] = 0xdc00 + (ch & 0x3ff); |
| 7217 | charsize = 2; |
| 7218 | } |
| 7219 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7220 | outsize = WideCharToMultiByte(code_page, flags, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7221 | chars, charsize, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7222 | buffer, Py_ARRAY_LENGTH(buffer), |
| 7223 | NULL, pusedDefaultChar); |
| 7224 | if (outsize > 0) { |
| 7225 | if (pusedDefaultChar == NULL || !(*pusedDefaultChar)) |
| 7226 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7227 | pos++; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7228 | memcpy(out, buffer, outsize); |
| 7229 | out += outsize; |
| 7230 | continue; |
| 7231 | } |
| 7232 | } |
| 7233 | else if (GetLastError() != ERROR_NO_UNICODE_TRANSLATION) { |
| 7234 | PyErr_SetFromWindowsErr(0); |
| 7235 | goto error; |
| 7236 | } |
| 7237 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7238 | rep = unicode_encode_call_errorhandler( |
| 7239 | errors, &errorHandler, encoding, reason, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7240 | unicode, &exc, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7241 | pos, pos + 1, &newpos); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7242 | if (rep == NULL) |
| 7243 | goto error; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7244 | pos = newpos; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7245 | |
| 7246 | if (PyBytes_Check(rep)) { |
| 7247 | outsize = PyBytes_GET_SIZE(rep); |
| 7248 | if (outsize != 1) { |
| 7249 | Py_ssize_t offset = out - PyBytes_AS_STRING(*outbytes); |
| 7250 | newoutsize = PyBytes_GET_SIZE(*outbytes) + (outsize - 1); |
| 7251 | if (_PyBytes_Resize(outbytes, newoutsize) < 0) { |
| 7252 | Py_DECREF(rep); |
| 7253 | goto error; |
| 7254 | } |
| 7255 | out = PyBytes_AS_STRING(*outbytes) + offset; |
| 7256 | } |
| 7257 | memcpy(out, PyBytes_AS_STRING(rep), outsize); |
| 7258 | out += outsize; |
| 7259 | } |
| 7260 | else { |
| 7261 | Py_ssize_t i; |
| 7262 | enum PyUnicode_Kind kind; |
| 7263 | void *data; |
| 7264 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7265 | if (PyUnicode_READY(rep) == -1) { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7266 | Py_DECREF(rep); |
| 7267 | goto error; |
| 7268 | } |
| 7269 | |
| 7270 | outsize = PyUnicode_GET_LENGTH(rep); |
| 7271 | if (outsize != 1) { |
| 7272 | Py_ssize_t offset = out - PyBytes_AS_STRING(*outbytes); |
| 7273 | newoutsize = PyBytes_GET_SIZE(*outbytes) + (outsize - 1); |
| 7274 | if (_PyBytes_Resize(outbytes, newoutsize) < 0) { |
| 7275 | Py_DECREF(rep); |
| 7276 | goto error; |
| 7277 | } |
| 7278 | out = PyBytes_AS_STRING(*outbytes) + offset; |
| 7279 | } |
| 7280 | kind = PyUnicode_KIND(rep); |
| 7281 | data = PyUnicode_DATA(rep); |
| 7282 | for (i=0; i < outsize; i++) { |
| 7283 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 7284 | if (ch > 127) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7285 | raise_encode_exception(&exc, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7286 | encoding, unicode, |
| 7287 | pos, pos + 1, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7288 | "unable to encode error handler result to ASCII"); |
| 7289 | Py_DECREF(rep); |
| 7290 | goto error; |
| 7291 | } |
| 7292 | *out = (unsigned char)ch; |
| 7293 | out++; |
| 7294 | } |
| 7295 | } |
| 7296 | Py_DECREF(rep); |
| 7297 | } |
| 7298 | /* write a NUL byte */ |
| 7299 | *out = 0; |
| 7300 | outsize = out - PyBytes_AS_STRING(*outbytes); |
| 7301 | assert(outsize <= PyBytes_GET_SIZE(*outbytes)); |
| 7302 | if (_PyBytes_Resize(outbytes, outsize) < 0) |
| 7303 | goto error; |
| 7304 | ret = 0; |
| 7305 | |
| 7306 | error: |
| 7307 | Py_XDECREF(encoding_obj); |
| 7308 | Py_XDECREF(errorHandler); |
| 7309 | Py_XDECREF(exc); |
| 7310 | return ret; |
| 7311 | } |
| 7312 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7313 | static PyObject * |
| 7314 | encode_code_page(int code_page, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7315 | PyObject *unicode, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7316 | const char *errors) |
| 7317 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7318 | Py_ssize_t len; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7319 | PyObject *outbytes = NULL; |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7320 | Py_ssize_t offset; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7321 | int chunk_len, ret, done; |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7322 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7323 | if (PyUnicode_READY(unicode) == -1) |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7324 | return NULL; |
| 7325 | len = PyUnicode_GET_LENGTH(unicode); |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 7326 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7327 | if (code_page < 0) { |
| 7328 | PyErr_SetString(PyExc_ValueError, "invalid code page number"); |
| 7329 | return NULL; |
| 7330 | } |
| 7331 | |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7332 | if (len == 0) |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7333 | return PyBytes_FromStringAndSize(NULL, 0); |
| 7334 | |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7335 | offset = 0; |
| 7336 | do |
| 7337 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7338 | #ifdef NEED_RETRY |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7339 | /* 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] | 7340 | chunks. */ |
| 7341 | if (len > INT_MAX/2) { |
| 7342 | chunk_len = INT_MAX/2; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7343 | done = 0; |
| 7344 | } |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7345 | else |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7346 | #endif |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7347 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7348 | chunk_len = (int)len; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7349 | done = 1; |
| 7350 | } |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7351 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7352 | ret = encode_code_page_strict(code_page, &outbytes, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7353 | unicode, offset, chunk_len, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7354 | errors); |
| 7355 | if (ret == -2) |
| 7356 | ret = encode_code_page_errors(code_page, &outbytes, |
| 7357 | unicode, offset, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7358 | chunk_len, errors); |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7359 | if (ret < 0) { |
| 7360 | Py_XDECREF(outbytes); |
| 7361 | return NULL; |
| 7362 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7363 | |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7364 | offset += chunk_len; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7365 | len -= chunk_len; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7366 | } while (!done); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7367 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7368 | return outbytes; |
| 7369 | } |
| 7370 | |
| 7371 | PyObject * |
| 7372 | PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
| 7373 | Py_ssize_t size, |
| 7374 | const char *errors) |
| 7375 | { |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7376 | PyObject *unicode, *res; |
| 7377 | unicode = PyUnicode_FromUnicode(p, size); |
| 7378 | if (unicode == NULL) |
| 7379 | return NULL; |
| 7380 | res = encode_code_page(CP_ACP, unicode, errors); |
| 7381 | Py_DECREF(unicode); |
| 7382 | return res; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7383 | } |
| 7384 | |
| 7385 | PyObject * |
| 7386 | PyUnicode_EncodeCodePage(int code_page, |
| 7387 | PyObject *unicode, |
| 7388 | const char *errors) |
| 7389 | { |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7390 | return encode_code_page(code_page, unicode, errors); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7391 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 7392 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7393 | PyObject * |
| 7394 | PyUnicode_AsMBCSString(PyObject *unicode) |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7395 | { |
| 7396 | if (!PyUnicode_Check(unicode)) { |
| 7397 | PyErr_BadArgument(); |
| 7398 | return NULL; |
| 7399 | } |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7400 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, NULL); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7401 | } |
| 7402 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7403 | #undef NEED_RETRY |
| 7404 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 7405 | #endif /* HAVE_MBCS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7406 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7407 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 7408 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7409 | PyObject * |
| 7410 | PyUnicode_DecodeCharmap(const char *s, |
| 7411 | Py_ssize_t size, |
| 7412 | PyObject *mapping, |
| 7413 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7414 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7415 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7416 | Py_ssize_t startinpos; |
| 7417 | Py_ssize_t endinpos; |
| 7418 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7419 | const char *e; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 7420 | PyObject *v; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7421 | Py_ssize_t extrachars = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7422 | PyObject *errorHandler = NULL; |
| 7423 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7424 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7425 | /* Default to Latin-1 */ |
| 7426 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7427 | return PyUnicode_DecodeLatin1(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7428 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7429 | v = PyUnicode_New(size, 127); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7430 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7431 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7432 | if (size == 0) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 7433 | return v; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7434 | outpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7435 | e = s + size; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7436 | if (PyUnicode_CheckExact(mapping)) { |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7437 | Py_ssize_t maplen; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame^] | 7438 | enum PyUnicode_Kind mapkind; |
| 7439 | void *mapdata; |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7440 | Py_UCS4 x; |
| 7441 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7442 | if (PyUnicode_READY(mapping) == -1) |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7443 | return NULL; |
| 7444 | |
| 7445 | maplen = PyUnicode_GET_LENGTH(mapping); |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame^] | 7446 | mapdata = PyUnicode_DATA(mapping); |
| 7447 | mapkind = PyUnicode_KIND(mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7448 | while (s < e) { |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame^] | 7449 | unsigned char ch; |
| 7450 | if (mapkind == PyUnicode_2BYTE_KIND && maplen >= 256) { |
| 7451 | enum PyUnicode_Kind outkind = PyUnicode_KIND(v); |
| 7452 | if (outkind == PyUnicode_1BYTE_KIND) { |
| 7453 | void *outdata = PyUnicode_DATA(v); |
| 7454 | Py_UCS4 maxchar = PyUnicode_MAX_CHAR_VALUE(v); |
| 7455 | while (s < e) { |
| 7456 | unsigned char ch = *s; |
| 7457 | x = PyUnicode_READ(PyUnicode_2BYTE_KIND, mapdata, ch); |
| 7458 | if (x > maxchar) |
| 7459 | goto Error; |
| 7460 | PyUnicode_WRITE(PyUnicode_1BYTE_KIND, outdata, outpos++, x); |
| 7461 | ++s; |
| 7462 | } |
| 7463 | break; |
| 7464 | } |
| 7465 | else if (outkind == PyUnicode_2BYTE_KIND) { |
| 7466 | void *outdata = PyUnicode_DATA(v); |
| 7467 | while (s < e) { |
| 7468 | unsigned char ch = *s; |
| 7469 | x = PyUnicode_READ(PyUnicode_2BYTE_KIND, mapdata, ch); |
| 7470 | if (x == 0xFFFE) |
| 7471 | goto Error; |
| 7472 | PyUnicode_WRITE(PyUnicode_2BYTE_KIND, outdata, outpos++, x); |
| 7473 | ++s; |
| 7474 | } |
| 7475 | break; |
| 7476 | } |
| 7477 | } |
| 7478 | ch = *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7479 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7480 | if (ch < maplen) |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame^] | 7481 | x = PyUnicode_READ(mapkind, mapdata, ch); |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7482 | else |
| 7483 | x = 0xfffe; /* invalid value */ |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame^] | 7484 | Error: |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7485 | if (x == 0xfffe) |
| 7486 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7487 | /* undefined mapping */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7488 | startinpos = s-starts; |
| 7489 | endinpos = startinpos+1; |
| 7490 | if (unicode_decode_call_errorhandler( |
| 7491 | errors, &errorHandler, |
| 7492 | "charmap", "character maps to <undefined>", |
| 7493 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7494 | &v, &outpos)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7495 | goto onError; |
| 7496 | } |
| 7497 | continue; |
| 7498 | } |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7499 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7500 | if (unicode_putchar(&v, &outpos, x) < 0) |
| 7501 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7502 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7503 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7504 | } |
| 7505 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7506 | while (s < e) { |
| 7507 | unsigned char ch = *s; |
| 7508 | PyObject *w, *x; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7509 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7510 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 7511 | w = PyLong_FromLong((long)ch); |
| 7512 | if (w == NULL) |
| 7513 | goto onError; |
| 7514 | x = PyObject_GetItem(mapping, w); |
| 7515 | Py_DECREF(w); |
| 7516 | if (x == NULL) { |
| 7517 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 7518 | /* No mapping found means: mapping is undefined. */ |
| 7519 | PyErr_Clear(); |
| 7520 | x = Py_None; |
| 7521 | Py_INCREF(x); |
| 7522 | } else |
| 7523 | goto onError; |
| 7524 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7525 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7526 | /* Apply mapping */ |
| 7527 | if (PyLong_Check(x)) { |
| 7528 | long value = PyLong_AS_LONG(x); |
| 7529 | if (value < 0 || value > 65535) { |
| 7530 | PyErr_SetString(PyExc_TypeError, |
| 7531 | "character mapping must be in range(65536)"); |
| 7532 | Py_DECREF(x); |
| 7533 | goto onError; |
| 7534 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7535 | if (unicode_putchar(&v, &outpos, value) < 0) |
| 7536 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7537 | } |
| 7538 | else if (x == Py_None) { |
| 7539 | /* undefined mapping */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7540 | startinpos = s-starts; |
| 7541 | endinpos = startinpos+1; |
| 7542 | if (unicode_decode_call_errorhandler( |
| 7543 | errors, &errorHandler, |
| 7544 | "charmap", "character maps to <undefined>", |
| 7545 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7546 | &v, &outpos)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7547 | Py_DECREF(x); |
| 7548 | goto onError; |
| 7549 | } |
| 7550 | Py_DECREF(x); |
| 7551 | continue; |
| 7552 | } |
| 7553 | else if (PyUnicode_Check(x)) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7554 | Py_ssize_t targetsize; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7555 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7556 | if (PyUnicode_READY(x) == -1) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7557 | goto onError; |
| 7558 | targetsize = PyUnicode_GET_LENGTH(x); |
| 7559 | |
| 7560 | if (targetsize == 1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7561 | /* 1-1 mapping */ |
Victor Stinner | 62aa4d0 | 2011-11-09 00:03:45 +0100 | [diff] [blame] | 7562 | if (unicode_putchar(&v, &outpos, |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7563 | PyUnicode_READ_CHAR(x, 0)) < 0) |
| 7564 | goto onError; |
| 7565 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7566 | else if (targetsize > 1) { |
| 7567 | /* 1-n mapping */ |
| 7568 | if (targetsize > extrachars) { |
| 7569 | /* resize first */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7570 | Py_ssize_t needed = (targetsize - extrachars) + \ |
| 7571 | (targetsize << 2); |
| 7572 | extrachars += needed; |
| 7573 | /* XXX overflow detection missing */ |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 7574 | if (unicode_resize(&v, |
| 7575 | PyUnicode_GET_LENGTH(v) + needed) < 0) |
| 7576 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7577 | Py_DECREF(x); |
| 7578 | goto onError; |
| 7579 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7580 | } |
Victor Stinner | 1b487b4 | 2012-05-03 12:29:04 +0200 | [diff] [blame] | 7581 | 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] | 7582 | goto onError; |
| 7583 | PyUnicode_CopyCharacters(v, outpos, x, 0, targetsize); |
| 7584 | outpos += targetsize; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7585 | extrachars -= targetsize; |
| 7586 | } |
| 7587 | /* 1-0 mapping: skip the character */ |
| 7588 | } |
| 7589 | else { |
| 7590 | /* wrong return value */ |
| 7591 | PyErr_SetString(PyExc_TypeError, |
| 7592 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7593 | Py_DECREF(x); |
| 7594 | goto onError; |
| 7595 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7596 | Py_DECREF(x); |
| 7597 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7598 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7599 | } |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 7600 | if (unicode_resize(&v, outpos) < 0) |
Antoine Pitrou | a8f63c0 | 2011-11-08 18:37:16 +0100 | [diff] [blame] | 7601 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7602 | Py_XDECREF(errorHandler); |
| 7603 | Py_XDECREF(exc); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 7604 | return unicode_result(v); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7605 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7606 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7607 | Py_XDECREF(errorHandler); |
| 7608 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7609 | Py_XDECREF(v); |
| 7610 | return NULL; |
| 7611 | } |
| 7612 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7613 | /* Charmap encoding: the lookup table */ |
| 7614 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7615 | struct encoding_map { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7616 | PyObject_HEAD |
| 7617 | unsigned char level1[32]; |
| 7618 | int count2, count3; |
| 7619 | unsigned char level23[1]; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7620 | }; |
| 7621 | |
| 7622 | static PyObject* |
| 7623 | encoding_map_size(PyObject *obj, PyObject* args) |
| 7624 | { |
| 7625 | struct encoding_map *map = (struct encoding_map*)obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7626 | return PyLong_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7627 | 128*map->count3); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7628 | } |
| 7629 | |
| 7630 | static PyMethodDef encoding_map_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7631 | {"size", encoding_map_size, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7632 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 7633 | { 0 } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7634 | }; |
| 7635 | |
| 7636 | static void |
| 7637 | encoding_map_dealloc(PyObject* o) |
| 7638 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7639 | PyObject_FREE(o); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7640 | } |
| 7641 | |
| 7642 | static PyTypeObject EncodingMapType = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7643 | PyVarObject_HEAD_INIT(NULL, 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7644 | "EncodingMap", /*tp_name*/ |
| 7645 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 7646 | 0, /*tp_itemsize*/ |
| 7647 | /* methods */ |
| 7648 | encoding_map_dealloc, /*tp_dealloc*/ |
| 7649 | 0, /*tp_print*/ |
| 7650 | 0, /*tp_getattr*/ |
| 7651 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 7652 | 0, /*tp_reserved*/ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7653 | 0, /*tp_repr*/ |
| 7654 | 0, /*tp_as_number*/ |
| 7655 | 0, /*tp_as_sequence*/ |
| 7656 | 0, /*tp_as_mapping*/ |
| 7657 | 0, /*tp_hash*/ |
| 7658 | 0, /*tp_call*/ |
| 7659 | 0, /*tp_str*/ |
| 7660 | 0, /*tp_getattro*/ |
| 7661 | 0, /*tp_setattro*/ |
| 7662 | 0, /*tp_as_buffer*/ |
| 7663 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 7664 | 0, /*tp_doc*/ |
| 7665 | 0, /*tp_traverse*/ |
| 7666 | 0, /*tp_clear*/ |
| 7667 | 0, /*tp_richcompare*/ |
| 7668 | 0, /*tp_weaklistoffset*/ |
| 7669 | 0, /*tp_iter*/ |
| 7670 | 0, /*tp_iternext*/ |
| 7671 | encoding_map_methods, /*tp_methods*/ |
| 7672 | 0, /*tp_members*/ |
| 7673 | 0, /*tp_getset*/ |
| 7674 | 0, /*tp_base*/ |
| 7675 | 0, /*tp_dict*/ |
| 7676 | 0, /*tp_descr_get*/ |
| 7677 | 0, /*tp_descr_set*/ |
| 7678 | 0, /*tp_dictoffset*/ |
| 7679 | 0, /*tp_init*/ |
| 7680 | 0, /*tp_alloc*/ |
| 7681 | 0, /*tp_new*/ |
| 7682 | 0, /*tp_free*/ |
| 7683 | 0, /*tp_is_gc*/ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7684 | }; |
| 7685 | |
| 7686 | PyObject* |
| 7687 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 7688 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7689 | PyObject *result; |
| 7690 | struct encoding_map *mresult; |
| 7691 | int i; |
| 7692 | int need_dict = 0; |
| 7693 | unsigned char level1[32]; |
| 7694 | unsigned char level2[512]; |
| 7695 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 7696 | int count2 = 0, count3 = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7697 | int kind; |
| 7698 | void *data; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame^] | 7699 | Py_ssize_t length; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7700 | Py_UCS4 ch; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7701 | |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame^] | 7702 | if (!PyUnicode_Check(string) || !PyUnicode_GET_LENGTH(string)) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7703 | PyErr_BadArgument(); |
| 7704 | return NULL; |
| 7705 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7706 | kind = PyUnicode_KIND(string); |
| 7707 | data = PyUnicode_DATA(string); |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame^] | 7708 | length = PyUnicode_GET_LENGTH(string); |
| 7709 | length = Py_MIN(length, 256); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7710 | memset(level1, 0xFF, sizeof level1); |
| 7711 | memset(level2, 0xFF, sizeof level2); |
| 7712 | |
| 7713 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 7714 | or if there are non-BMP characters, we need to use |
| 7715 | a mapping dictionary. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7716 | if (PyUnicode_READ(kind, data, 0) != 0) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7717 | need_dict = 1; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame^] | 7718 | for (i = 1; i < length; i++) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7719 | int l1, l2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7720 | ch = PyUnicode_READ(kind, data, i); |
| 7721 | if (ch == 0 || ch > 0xFFFF) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7722 | need_dict = 1; |
| 7723 | break; |
| 7724 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7725 | if (ch == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7726 | /* unmapped character */ |
| 7727 | continue; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7728 | l1 = ch >> 11; |
| 7729 | l2 = ch >> 7; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7730 | if (level1[l1] == 0xFF) |
| 7731 | level1[l1] = count2++; |
| 7732 | if (level2[l2] == 0xFF) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7733 | level2[l2] = count3++; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7734 | } |
| 7735 | |
| 7736 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 7737 | need_dict = 1; |
| 7738 | |
| 7739 | if (need_dict) { |
| 7740 | PyObject *result = PyDict_New(); |
| 7741 | PyObject *key, *value; |
| 7742 | if (!result) |
| 7743 | return NULL; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame^] | 7744 | for (i = 0; i < length; i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7745 | key = PyLong_FromLong(PyUnicode_READ(kind, data, i)); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7746 | value = PyLong_FromLong(i); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7747 | if (!key || !value) |
| 7748 | goto failed1; |
| 7749 | if (PyDict_SetItem(result, key, value) == -1) |
| 7750 | goto failed1; |
| 7751 | Py_DECREF(key); |
| 7752 | Py_DECREF(value); |
| 7753 | } |
| 7754 | return result; |
| 7755 | failed1: |
| 7756 | Py_XDECREF(key); |
| 7757 | Py_XDECREF(value); |
| 7758 | Py_DECREF(result); |
| 7759 | return NULL; |
| 7760 | } |
| 7761 | |
| 7762 | /* Create a three-level trie */ |
| 7763 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 7764 | 16*count2 + 128*count3 - 1); |
| 7765 | if (!result) |
| 7766 | return PyErr_NoMemory(); |
| 7767 | PyObject_Init(result, &EncodingMapType); |
| 7768 | mresult = (struct encoding_map*)result; |
| 7769 | mresult->count2 = count2; |
| 7770 | mresult->count3 = count3; |
| 7771 | mlevel1 = mresult->level1; |
| 7772 | mlevel2 = mresult->level23; |
| 7773 | mlevel3 = mresult->level23 + 16*count2; |
| 7774 | memcpy(mlevel1, level1, 32); |
| 7775 | memset(mlevel2, 0xFF, 16*count2); |
| 7776 | memset(mlevel3, 0, 128*count3); |
| 7777 | count3 = 0; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame^] | 7778 | for (i = 1; i < length; i++) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7779 | int o1, o2, o3, i2, i3; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame^] | 7780 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 7781 | if (ch == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7782 | /* unmapped character */ |
| 7783 | continue; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame^] | 7784 | o1 = ch>>11; |
| 7785 | o2 = (ch>>7) & 0xF; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7786 | i2 = 16*mlevel1[o1] + o2; |
| 7787 | if (mlevel2[i2] == 0xFF) |
| 7788 | mlevel2[i2] = count3++; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame^] | 7789 | o3 = ch & 0x7F; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7790 | i3 = 128*mlevel2[i2] + o3; |
| 7791 | mlevel3[i3] = i; |
| 7792 | } |
| 7793 | return result; |
| 7794 | } |
| 7795 | |
| 7796 | static int |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 7797 | encoding_map_lookup(Py_UCS4 c, PyObject *mapping) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7798 | { |
| 7799 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 7800 | int l1 = c>>11; |
| 7801 | int l2 = (c>>7) & 0xF; |
| 7802 | int l3 = c & 0x7F; |
| 7803 | int i; |
| 7804 | |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 7805 | if (c > 0xFFFF) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7806 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7807 | if (c == 0) |
| 7808 | return 0; |
| 7809 | /* level 1*/ |
| 7810 | i = map->level1[l1]; |
| 7811 | if (i == 0xFF) { |
| 7812 | return -1; |
| 7813 | } |
| 7814 | /* level 2*/ |
| 7815 | i = map->level23[16*i+l2]; |
| 7816 | if (i == 0xFF) { |
| 7817 | return -1; |
| 7818 | } |
| 7819 | /* level 3 */ |
| 7820 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 7821 | if (i == 0) { |
| 7822 | return -1; |
| 7823 | } |
| 7824 | return i; |
| 7825 | } |
| 7826 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7827 | /* Lookup the character ch in the mapping. If the character |
| 7828 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 7829 | error occurred). */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7830 | static PyObject * |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 7831 | charmapencode_lookup(Py_UCS4 c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7832 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7833 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7834 | PyObject *x; |
| 7835 | |
| 7836 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7837 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7838 | x = PyObject_GetItem(mapping, w); |
| 7839 | Py_DECREF(w); |
| 7840 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7841 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 7842 | /* No mapping found means: mapping is undefined. */ |
| 7843 | PyErr_Clear(); |
| 7844 | x = Py_None; |
| 7845 | Py_INCREF(x); |
| 7846 | return x; |
| 7847 | } else |
| 7848 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7849 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 7850 | else if (x == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7851 | return x; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7852 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7853 | long value = PyLong_AS_LONG(x); |
| 7854 | if (value < 0 || value > 255) { |
| 7855 | PyErr_SetString(PyExc_TypeError, |
| 7856 | "character mapping must be in range(256)"); |
| 7857 | Py_DECREF(x); |
| 7858 | return NULL; |
| 7859 | } |
| 7860 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7861 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7862 | else if (PyBytes_Check(x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7863 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7864 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7865 | /* wrong return value */ |
| 7866 | PyErr_Format(PyExc_TypeError, |
| 7867 | "character mapping must return integer, bytes or None, not %.400s", |
| 7868 | x->ob_type->tp_name); |
| 7869 | Py_DECREF(x); |
| 7870 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7871 | } |
| 7872 | } |
| 7873 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7874 | static int |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 7875 | charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7876 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7877 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
| 7878 | /* exponentially overallocate to minimize reallocations */ |
| 7879 | if (requiredsize < 2*outsize) |
| 7880 | requiredsize = 2*outsize; |
| 7881 | if (_PyBytes_Resize(outobj, requiredsize)) |
| 7882 | return -1; |
| 7883 | return 0; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7884 | } |
| 7885 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7886 | typedef enum charmapencode_result { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7887 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7888 | } charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7889 | /* 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] | 7890 | various state variables. Resize the output bytes object if not enough |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7891 | space is available. Return a new reference to the object that |
| 7892 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 7893 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 7894 | reallocation error occurred. The caller must decref the result */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7895 | static charmapencode_result |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 7896 | charmapencode_output(Py_UCS4 c, PyObject *mapping, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7897 | PyObject **outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7898 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7899 | PyObject *rep; |
| 7900 | char *outstart; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7901 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7902 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7903 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7904 | int res = encoding_map_lookup(c, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7905 | Py_ssize_t requiredsize = *outpos+1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7906 | if (res == -1) |
| 7907 | return enc_FAILED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7908 | if (outsize<requiredsize) |
| 7909 | if (charmapencode_resize(outobj, outpos, requiredsize)) |
| 7910 | return enc_EXCEPTION; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7911 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7912 | outstart[(*outpos)++] = (char)res; |
| 7913 | return enc_SUCCESS; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7914 | } |
| 7915 | |
| 7916 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7917 | if (rep==NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7918 | return enc_EXCEPTION; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7919 | else if (rep==Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7920 | Py_DECREF(rep); |
| 7921 | return enc_FAILED; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7922 | } else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7923 | if (PyLong_Check(rep)) { |
| 7924 | Py_ssize_t requiredsize = *outpos+1; |
| 7925 | if (outsize<requiredsize) |
| 7926 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 7927 | Py_DECREF(rep); |
| 7928 | return enc_EXCEPTION; |
| 7929 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7930 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7931 | outstart[(*outpos)++] = (char)PyLong_AS_LONG(rep); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7932 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7933 | else { |
| 7934 | const char *repchars = PyBytes_AS_STRING(rep); |
| 7935 | Py_ssize_t repsize = PyBytes_GET_SIZE(rep); |
| 7936 | Py_ssize_t requiredsize = *outpos+repsize; |
| 7937 | if (outsize<requiredsize) |
| 7938 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 7939 | Py_DECREF(rep); |
| 7940 | return enc_EXCEPTION; |
| 7941 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7942 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7943 | memcpy(outstart + *outpos, repchars, repsize); |
| 7944 | *outpos += repsize; |
| 7945 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7946 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7947 | Py_DECREF(rep); |
| 7948 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7949 | } |
| 7950 | |
| 7951 | /* handle an error in PyUnicode_EncodeCharmap |
| 7952 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7953 | static int |
| 7954 | charmap_encoding_error( |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7955 | PyObject *unicode, Py_ssize_t *inpos, PyObject *mapping, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7956 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 7957 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 7958 | PyObject **res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7959 | { |
| 7960 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7961 | Py_ssize_t size, repsize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7962 | Py_ssize_t newpos; |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 7963 | enum PyUnicode_Kind kind; |
| 7964 | void *data; |
| 7965 | Py_ssize_t index; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7966 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7967 | Py_ssize_t collstartpos = *inpos; |
| 7968 | Py_ssize_t collendpos = *inpos+1; |
| 7969 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7970 | char *encoding = "charmap"; |
| 7971 | char *reason = "character maps to <undefined>"; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7972 | charmapencode_result x; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7973 | Py_UCS4 ch; |
Brian Curtin | 2787ea4 | 2011-11-02 15:09:37 -0500 | [diff] [blame] | 7974 | int val; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7975 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7976 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7977 | return -1; |
| 7978 | size = PyUnicode_GET_LENGTH(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7979 | /* find all unencodable characters */ |
| 7980 | while (collendpos < size) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7981 | PyObject *rep; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7982 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7983 | ch = PyUnicode_READ_CHAR(unicode, collendpos); |
Brian Curtin | 2787ea4 | 2011-11-02 15:09:37 -0500 | [diff] [blame] | 7984 | val = encoding_map_lookup(ch, mapping); |
| 7985 | if (val != -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7986 | break; |
| 7987 | ++collendpos; |
| 7988 | continue; |
| 7989 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7990 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7991 | ch = PyUnicode_READ_CHAR(unicode, collendpos); |
| 7992 | rep = charmapencode_lookup(ch, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7993 | if (rep==NULL) |
| 7994 | return -1; |
| 7995 | else if (rep!=Py_None) { |
| 7996 | Py_DECREF(rep); |
| 7997 | break; |
| 7998 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7999 | Py_DECREF(rep); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8000 | ++collendpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8001 | } |
| 8002 | /* cache callback name lookup |
| 8003 | * (if not done yet, i.e. it's the first error) */ |
| 8004 | if (*known_errorHandler==-1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8005 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 8006 | *known_errorHandler = 1; |
| 8007 | else if (!strcmp(errors, "replace")) |
| 8008 | *known_errorHandler = 2; |
| 8009 | else if (!strcmp(errors, "ignore")) |
| 8010 | *known_errorHandler = 3; |
| 8011 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 8012 | *known_errorHandler = 4; |
| 8013 | else |
| 8014 | *known_errorHandler = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8015 | } |
| 8016 | switch (*known_errorHandler) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8017 | case 1: /* strict */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8018 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8019 | return -1; |
| 8020 | case 2: /* replace */ |
| 8021 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8022 | x = charmapencode_output('?', mapping, res, respos); |
| 8023 | if (x==enc_EXCEPTION) { |
| 8024 | return -1; |
| 8025 | } |
| 8026 | else if (x==enc_FAILED) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8027 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8028 | return -1; |
| 8029 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8030 | } |
| 8031 | /* fall through */ |
| 8032 | case 3: /* ignore */ |
| 8033 | *inpos = collendpos; |
| 8034 | break; |
| 8035 | case 4: /* xmlcharrefreplace */ |
| 8036 | /* generate replacement (temporarily (mis)uses p) */ |
| 8037 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8038 | char buffer[2+29+1+1]; |
| 8039 | char *cp; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8040 | sprintf(buffer, "&#%d;", (int)PyUnicode_READ_CHAR(unicode, collpos)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8041 | for (cp = buffer; *cp; ++cp) { |
| 8042 | x = charmapencode_output(*cp, mapping, res, respos); |
| 8043 | if (x==enc_EXCEPTION) |
| 8044 | return -1; |
| 8045 | else if (x==enc_FAILED) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8046 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8047 | return -1; |
| 8048 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8049 | } |
| 8050 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8051 | *inpos = collendpos; |
| 8052 | break; |
| 8053 | default: |
| 8054 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8055 | encoding, reason, unicode, exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8056 | collstartpos, collendpos, &newpos); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8057 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8058 | return -1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8059 | if (PyBytes_Check(repunicode)) { |
| 8060 | /* Directly copy bytes result to output. */ |
| 8061 | Py_ssize_t outsize = PyBytes_Size(*res); |
| 8062 | Py_ssize_t requiredsize; |
| 8063 | repsize = PyBytes_Size(repunicode); |
| 8064 | requiredsize = *respos + repsize; |
| 8065 | if (requiredsize > outsize) |
| 8066 | /* Make room for all additional bytes. */ |
| 8067 | if (charmapencode_resize(res, respos, requiredsize)) { |
| 8068 | Py_DECREF(repunicode); |
| 8069 | return -1; |
| 8070 | } |
| 8071 | memcpy(PyBytes_AsString(*res) + *respos, |
| 8072 | PyBytes_AsString(repunicode), repsize); |
| 8073 | *respos += repsize; |
| 8074 | *inpos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8075 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8076 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8077 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8078 | /* generate replacement */ |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 8079 | if (PyUnicode_READY(repunicode) == -1) { |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8080 | Py_DECREF(repunicode); |
| 8081 | return -1; |
| 8082 | } |
Victor Stinner | 9e30aa5 | 2011-11-21 02:49:52 +0100 | [diff] [blame] | 8083 | repsize = PyUnicode_GET_LENGTH(repunicode); |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8084 | data = PyUnicode_DATA(repunicode); |
| 8085 | kind = PyUnicode_KIND(repunicode); |
| 8086 | for (index = 0; index < repsize; index++) { |
| 8087 | Py_UCS4 repch = PyUnicode_READ(kind, data, index); |
| 8088 | x = charmapencode_output(repch, mapping, res, respos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8089 | if (x==enc_EXCEPTION) { |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8090 | Py_DECREF(repunicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8091 | return -1; |
| 8092 | } |
| 8093 | else if (x==enc_FAILED) { |
| 8094 | Py_DECREF(repunicode); |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8095 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8096 | return -1; |
| 8097 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8098 | } |
| 8099 | *inpos = newpos; |
| 8100 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8101 | } |
| 8102 | return 0; |
| 8103 | } |
| 8104 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8105 | PyObject * |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8106 | _PyUnicode_EncodeCharmap(PyObject *unicode, |
| 8107 | PyObject *mapping, |
| 8108 | const char *errors) |
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 | /* output object */ |
| 8111 | PyObject *res = NULL; |
| 8112 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8113 | Py_ssize_t inpos = 0; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8114 | Py_ssize_t size; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8115 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8116 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8117 | PyObject *errorHandler = NULL; |
| 8118 | PyObject *exc = NULL; |
| 8119 | /* the following variable is used for caching string comparisons |
| 8120 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 8121 | * 3=ignore, 4=xmlcharrefreplace */ |
| 8122 | int known_errorHandler = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8123 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 8124 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8125 | return NULL; |
| 8126 | size = PyUnicode_GET_LENGTH(unicode); |
| 8127 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8128 | /* Default to Latin-1 */ |
| 8129 | if (mapping == NULL) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8130 | return unicode_encode_ucs1(unicode, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8131 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8132 | /* allocate enough for a simple encoding without |
| 8133 | replacements, if we need more, we'll resize */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8134 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8135 | if (res == NULL) |
| 8136 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 8137 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8138 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8139 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8140 | while (inpos<size) { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8141 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, inpos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8142 | /* try to encode it */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8143 | charmapencode_result x = charmapencode_output(ch, mapping, &res, &respos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8144 | if (x==enc_EXCEPTION) /* error */ |
| 8145 | goto onError; |
| 8146 | if (x==enc_FAILED) { /* unencodable character */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8147 | if (charmap_encoding_error(unicode, &inpos, mapping, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8148 | &exc, |
| 8149 | &known_errorHandler, &errorHandler, errors, |
| 8150 | &res, &respos)) { |
| 8151 | goto onError; |
| 8152 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8153 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8154 | else |
| 8155 | /* done with this character => adjust input position */ |
| 8156 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8157 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8158 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8159 | /* Resize if we allocated to much */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8160 | if (respos<PyBytes_GET_SIZE(res)) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 8161 | if (_PyBytes_Resize(&res, respos) < 0) |
| 8162 | goto onError; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8163 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8164 | Py_XDECREF(exc); |
| 8165 | Py_XDECREF(errorHandler); |
| 8166 | return res; |
| 8167 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8168 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8169 | Py_XDECREF(res); |
| 8170 | Py_XDECREF(exc); |
| 8171 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8172 | return NULL; |
| 8173 | } |
| 8174 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8175 | /* Deprecated */ |
| 8176 | PyObject * |
| 8177 | PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
| 8178 | Py_ssize_t size, |
| 8179 | PyObject *mapping, |
| 8180 | const char *errors) |
| 8181 | { |
| 8182 | PyObject *result; |
| 8183 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 8184 | if (unicode == NULL) |
| 8185 | return NULL; |
| 8186 | result = _PyUnicode_EncodeCharmap(unicode, mapping, errors); |
| 8187 | Py_DECREF(unicode); |
Victor Stinner | fc026c9 | 2011-11-04 00:24:51 +0100 | [diff] [blame] | 8188 | return result; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8189 | } |
| 8190 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8191 | PyObject * |
| 8192 | PyUnicode_AsCharmapString(PyObject *unicode, |
| 8193 | PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8194 | { |
| 8195 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8196 | PyErr_BadArgument(); |
| 8197 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8198 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8199 | return _PyUnicode_EncodeCharmap(unicode, mapping, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8200 | } |
| 8201 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8202 | /* create or adjust a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8203 | static void |
| 8204 | make_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8205 | PyObject *unicode, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8206 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8207 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8208 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8209 | if (*exceptionObject == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8210 | *exceptionObject = _PyUnicodeTranslateError_Create( |
| 8211 | unicode, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8212 | } |
| 8213 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8214 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 8215 | goto onError; |
| 8216 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 8217 | goto onError; |
| 8218 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 8219 | goto onError; |
| 8220 | return; |
| 8221 | onError: |
| 8222 | Py_DECREF(*exceptionObject); |
| 8223 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8224 | } |
| 8225 | } |
| 8226 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8227 | /* raises a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8228 | static void |
| 8229 | raise_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8230 | PyObject *unicode, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8231 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8232 | const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8233 | { |
| 8234 | make_translate_exception(exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8235 | unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8236 | if (*exceptionObject != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8237 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8238 | } |
| 8239 | |
| 8240 | /* error handling callback helper: |
| 8241 | build arguments, call the callback and check the arguments, |
| 8242 | put the result into newpos and return the replacement string, which |
| 8243 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8244 | static PyObject * |
| 8245 | unicode_translate_call_errorhandler(const char *errors, |
| 8246 | PyObject **errorHandler, |
| 8247 | const char *reason, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8248 | PyObject *unicode, PyObject **exceptionObject, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8249 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8250 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8251 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8252 | 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] | 8253 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8254 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8255 | PyObject *restuple; |
| 8256 | PyObject *resunicode; |
| 8257 | |
| 8258 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8259 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8260 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8261 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8262 | } |
| 8263 | |
| 8264 | make_translate_exception(exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8265 | unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8266 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8267 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8268 | |
| 8269 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8270 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8271 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8272 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8273 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 8274 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8275 | Py_DECREF(restuple); |
| 8276 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8277 | } |
| 8278 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8279 | &resunicode, &i_newpos)) { |
| 8280 | Py_DECREF(restuple); |
| 8281 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8282 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8283 | if (i_newpos<0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8284 | *newpos = PyUnicode_GET_LENGTH(unicode)+i_newpos; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8285 | else |
| 8286 | *newpos = i_newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8287 | if (*newpos<0 || *newpos>PyUnicode_GET_LENGTH(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8288 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 8289 | Py_DECREF(restuple); |
| 8290 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 8291 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8292 | Py_INCREF(resunicode); |
| 8293 | Py_DECREF(restuple); |
| 8294 | return resunicode; |
| 8295 | } |
| 8296 | |
| 8297 | /* Lookup the character ch in the mapping and put the result in result, |
| 8298 | which must be decrefed by the caller. |
| 8299 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8300 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8301 | charmaptranslate_lookup(Py_UCS4 c, PyObject *mapping, PyObject **result) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8302 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8303 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8304 | PyObject *x; |
| 8305 | |
| 8306 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8307 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8308 | x = PyObject_GetItem(mapping, w); |
| 8309 | Py_DECREF(w); |
| 8310 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8311 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 8312 | /* No mapping found means: use 1:1 mapping. */ |
| 8313 | PyErr_Clear(); |
| 8314 | *result = NULL; |
| 8315 | return 0; |
| 8316 | } else |
| 8317 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8318 | } |
| 8319 | else if (x == Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8320 | *result = x; |
| 8321 | return 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8322 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8323 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8324 | long value = PyLong_AS_LONG(x); |
| 8325 | long max = PyUnicode_GetMax(); |
| 8326 | if (value < 0 || value > max) { |
| 8327 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5a2f7e60 | 2007-10-24 21:13:09 +0000 | [diff] [blame] | 8328 | "character mapping must be in range(0x%x)", max+1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8329 | Py_DECREF(x); |
| 8330 | return -1; |
| 8331 | } |
| 8332 | *result = x; |
| 8333 | return 0; |
| 8334 | } |
| 8335 | else if (PyUnicode_Check(x)) { |
| 8336 | *result = x; |
| 8337 | return 0; |
| 8338 | } |
| 8339 | else { |
| 8340 | /* wrong return value */ |
| 8341 | PyErr_SetString(PyExc_TypeError, |
| 8342 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8343 | Py_DECREF(x); |
| 8344 | return -1; |
| 8345 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8346 | } |
| 8347 | /* ensure that *outobj is at least requiredsize characters long, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8348 | if not reallocate and adjust various state variables. |
| 8349 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8350 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8351 | charmaptranslate_makespace(Py_UCS4 **outobj, Py_ssize_t *psize, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8352 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8353 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8354 | Py_ssize_t oldsize = *psize; |
Kristjan Valur Jonsson | 85634d7 | 2012-05-31 09:37:31 +0000 | [diff] [blame] | 8355 | Py_UCS4 *new_outobj; |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 8356 | if (requiredsize > oldsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8357 | /* exponentially overallocate to minimize reallocations */ |
| 8358 | if (requiredsize < 2 * oldsize) |
| 8359 | requiredsize = 2 * oldsize; |
Kristjan Valur Jonsson | 85634d7 | 2012-05-31 09:37:31 +0000 | [diff] [blame] | 8360 | new_outobj = PyMem_Realloc(*outobj, requiredsize * sizeof(Py_UCS4)); |
| 8361 | if (new_outobj == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8362 | return -1; |
Kristjan Valur Jonsson | 85634d7 | 2012-05-31 09:37:31 +0000 | [diff] [blame] | 8363 | *outobj = new_outobj; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8364 | *psize = requiredsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8365 | } |
| 8366 | return 0; |
| 8367 | } |
| 8368 | /* lookup the character, put the result in the output string and adjust |
| 8369 | various state variables. Return a new reference to the object that |
| 8370 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 8371 | undefined (in which case no character was written). |
| 8372 | The called must decref result. |
| 8373 | Return 0 on success, -1 on error. */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8374 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8375 | charmaptranslate_output(PyObject *input, Py_ssize_t ipos, |
| 8376 | PyObject *mapping, Py_UCS4 **output, |
| 8377 | Py_ssize_t *osize, Py_ssize_t *opos, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8378 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8379 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8380 | Py_UCS4 curinp = PyUnicode_READ_CHAR(input, ipos); |
| 8381 | if (charmaptranslate_lookup(curinp, mapping, res)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8382 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8383 | if (*res==NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8384 | /* not found => default to 1:1 mapping */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8385 | (*output)[(*opos)++] = curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8386 | } |
| 8387 | else if (*res==Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8388 | ; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8389 | else if (PyLong_Check(*res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8390 | /* 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] | 8391 | (*output)[(*opos)++] = (Py_UCS4)PyLong_AS_LONG(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8392 | } |
| 8393 | else if (PyUnicode_Check(*res)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8394 | Py_ssize_t repsize; |
| 8395 | if (PyUnicode_READY(*res) == -1) |
| 8396 | return -1; |
| 8397 | repsize = PyUnicode_GET_LENGTH(*res); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8398 | if (repsize==1) { |
| 8399 | /* 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] | 8400 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8401 | } |
| 8402 | else if (repsize!=0) { |
| 8403 | /* more than one character */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8404 | Py_ssize_t requiredsize = *opos + |
| 8405 | (PyUnicode_GET_LENGTH(input) - ipos) + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8406 | repsize - 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8407 | Py_ssize_t i; |
| 8408 | if (charmaptranslate_makespace(output, osize, requiredsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8409 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8410 | for(i = 0; i < repsize; i++) |
| 8411 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, i); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8412 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8413 | } |
| 8414 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8415 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8416 | return 0; |
| 8417 | } |
| 8418 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8419 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8420 | _PyUnicode_TranslateCharmap(PyObject *input, |
| 8421 | PyObject *mapping, |
| 8422 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8423 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8424 | /* input object */ |
| 8425 | char *idata; |
| 8426 | Py_ssize_t size, i; |
| 8427 | int kind; |
| 8428 | /* output buffer */ |
| 8429 | Py_UCS4 *output = NULL; |
| 8430 | Py_ssize_t osize; |
| 8431 | PyObject *res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8432 | /* current output position */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8433 | Py_ssize_t opos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8434 | char *reason = "character maps to <undefined>"; |
| 8435 | PyObject *errorHandler = NULL; |
| 8436 | PyObject *exc = NULL; |
| 8437 | /* the following variable is used for caching string comparisons |
| 8438 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 8439 | * 3=ignore, 4=xmlcharrefreplace */ |
| 8440 | int known_errorHandler = -1; |
| 8441 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8442 | if (mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8443 | PyErr_BadArgument(); |
| 8444 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8445 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8446 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8447 | if (PyUnicode_READY(input) == -1) |
| 8448 | return NULL; |
| 8449 | idata = (char*)PyUnicode_DATA(input); |
| 8450 | kind = PyUnicode_KIND(input); |
| 8451 | size = PyUnicode_GET_LENGTH(input); |
| 8452 | i = 0; |
| 8453 | |
| 8454 | if (size == 0) { |
| 8455 | Py_INCREF(input); |
| 8456 | return input; |
| 8457 | } |
| 8458 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8459 | /* allocate enough for a simple 1:1 translation without |
| 8460 | replacements, if we need more, we'll resize */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8461 | osize = size; |
| 8462 | output = PyMem_Malloc(osize * sizeof(Py_UCS4)); |
| 8463 | opos = 0; |
| 8464 | if (output == NULL) { |
| 8465 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8466 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8467 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8468 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8469 | while (i<size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8470 | /* try to encode it */ |
| 8471 | PyObject *x = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8472 | if (charmaptranslate_output(input, i, mapping, |
| 8473 | &output, &osize, &opos, &x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8474 | Py_XDECREF(x); |
| 8475 | goto onError; |
| 8476 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8477 | Py_XDECREF(x); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8478 | if (x!=Py_None) /* it worked => adjust input pointer */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8479 | ++i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8480 | else { /* untranslatable character */ |
| 8481 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
| 8482 | Py_ssize_t repsize; |
| 8483 | Py_ssize_t newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8484 | Py_ssize_t uni2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8485 | /* startpos for collecting untranslatable chars */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8486 | Py_ssize_t collstart = i; |
| 8487 | Py_ssize_t collend = i+1; |
| 8488 | Py_ssize_t coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8489 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8490 | /* find all untranslatable characters */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8491 | while (collend < size) { |
| 8492 | if (charmaptranslate_lookup(PyUnicode_READ(kind,idata, collend), mapping, &x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8493 | goto onError; |
| 8494 | Py_XDECREF(x); |
| 8495 | if (x!=Py_None) |
| 8496 | break; |
| 8497 | ++collend; |
| 8498 | } |
| 8499 | /* cache callback name lookup |
| 8500 | * (if not done yet, i.e. it's the first error) */ |
| 8501 | if (known_errorHandler==-1) { |
| 8502 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 8503 | known_errorHandler = 1; |
| 8504 | else if (!strcmp(errors, "replace")) |
| 8505 | known_errorHandler = 2; |
| 8506 | else if (!strcmp(errors, "ignore")) |
| 8507 | known_errorHandler = 3; |
| 8508 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 8509 | known_errorHandler = 4; |
| 8510 | else |
| 8511 | known_errorHandler = 0; |
| 8512 | } |
| 8513 | switch (known_errorHandler) { |
| 8514 | case 1: /* strict */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8515 | raise_translate_exception(&exc, input, collstart, |
| 8516 | collend, reason); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8517 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8518 | case 2: /* replace */ |
| 8519 | /* 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] | 8520 | for (coll = collstart; coll<collend; coll++) |
| 8521 | output[opos++] = '?'; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8522 | /* fall through */ |
| 8523 | case 3: /* ignore */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8524 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8525 | break; |
| 8526 | case 4: /* xmlcharrefreplace */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8527 | /* generate replacement (temporarily (mis)uses i) */ |
| 8528 | for (i = collstart; i < collend; ++i) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8529 | char buffer[2+29+1+1]; |
| 8530 | char *cp; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8531 | sprintf(buffer, "&#%d;", PyUnicode_READ(kind, idata, i)); |
| 8532 | if (charmaptranslate_makespace(&output, &osize, |
| 8533 | opos+strlen(buffer)+(size-collend))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8534 | goto onError; |
| 8535 | for (cp = buffer; *cp; ++cp) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8536 | output[opos++] = *cp; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8537 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8538 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8539 | break; |
| 8540 | default: |
| 8541 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8542 | reason, input, &exc, |
| 8543 | collstart, collend, &newpos); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 8544 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8545 | goto onError; |
Benjamin Peterson | 9ca3ffa | 2012-01-01 16:04:29 -0600 | [diff] [blame] | 8546 | if (PyUnicode_READY(repunicode) == -1) { |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 8547 | Py_DECREF(repunicode); |
| 8548 | goto onError; |
| 8549 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8550 | /* generate replacement */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8551 | repsize = PyUnicode_GET_LENGTH(repunicode); |
| 8552 | if (charmaptranslate_makespace(&output, &osize, |
| 8553 | opos+repsize+(size-collend))) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8554 | Py_DECREF(repunicode); |
| 8555 | goto onError; |
| 8556 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8557 | for (uni2 = 0; repsize-->0; ++uni2) |
| 8558 | output[opos++] = PyUnicode_READ_CHAR(repunicode, uni2); |
| 8559 | i = newpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8560 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8561 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8562 | } |
| 8563 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8564 | res = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, output, opos); |
| 8565 | if (!res) |
| 8566 | goto onError; |
| 8567 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8568 | Py_XDECREF(exc); |
| 8569 | Py_XDECREF(errorHandler); |
| 8570 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8571 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8572 | onError: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8573 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8574 | Py_XDECREF(exc); |
| 8575 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8576 | return NULL; |
| 8577 | } |
| 8578 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8579 | /* Deprecated. Use PyUnicode_Translate instead. */ |
| 8580 | PyObject * |
| 8581 | PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
| 8582 | Py_ssize_t size, |
| 8583 | PyObject *mapping, |
| 8584 | const char *errors) |
| 8585 | { |
| 8586 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 8587 | if (!unicode) |
| 8588 | return NULL; |
| 8589 | return _PyUnicode_TranslateCharmap(unicode, mapping, errors); |
| 8590 | } |
| 8591 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8592 | PyObject * |
| 8593 | PyUnicode_Translate(PyObject *str, |
| 8594 | PyObject *mapping, |
| 8595 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8596 | { |
| 8597 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8598 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8599 | str = PyUnicode_FromObject(str); |
| 8600 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8601 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8602 | result = _PyUnicode_TranslateCharmap(str, mapping, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8603 | Py_DECREF(str); |
| 8604 | return result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8605 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8606 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8607 | Py_XDECREF(str); |
| 8608 | return NULL; |
| 8609 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8610 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8611 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8612 | fix_decimal_and_space_to_ascii(PyObject *self) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8613 | { |
| 8614 | /* No need to call PyUnicode_READY(self) because this function is only |
| 8615 | called as a callback from fixup() which does it already. */ |
| 8616 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8617 | const int kind = PyUnicode_KIND(self); |
| 8618 | void *data = PyUnicode_DATA(self); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 8619 | Py_UCS4 maxchar = 127, ch, fixed; |
Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame] | 8620 | int modified = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8621 | Py_ssize_t i; |
| 8622 | |
| 8623 | for (i = 0; i < len; ++i) { |
| 8624 | ch = PyUnicode_READ(kind, data, i); |
| 8625 | fixed = 0; |
| 8626 | if (ch > 127) { |
| 8627 | if (Py_UNICODE_ISSPACE(ch)) |
| 8628 | fixed = ' '; |
| 8629 | else { |
| 8630 | const int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8631 | if (decimal >= 0) |
| 8632 | fixed = '0' + decimal; |
| 8633 | } |
| 8634 | if (fixed != 0) { |
Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame] | 8635 | modified = 1; |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 8636 | maxchar = MAX_MAXCHAR(maxchar, fixed); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8637 | PyUnicode_WRITE(kind, data, i, fixed); |
| 8638 | } |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 8639 | else |
| 8640 | maxchar = MAX_MAXCHAR(maxchar, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8641 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8642 | } |
| 8643 | |
Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame] | 8644 | return (modified) ? maxchar : 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8645 | } |
| 8646 | |
| 8647 | PyObject * |
| 8648 | _PyUnicode_TransformDecimalAndSpaceToASCII(PyObject *unicode) |
| 8649 | { |
| 8650 | if (!PyUnicode_Check(unicode)) { |
| 8651 | PyErr_BadInternalCall(); |
| 8652 | return NULL; |
| 8653 | } |
| 8654 | if (PyUnicode_READY(unicode) == -1) |
| 8655 | return NULL; |
| 8656 | if (PyUnicode_MAX_CHAR_VALUE(unicode) <= 127) { |
| 8657 | /* If the string is already ASCII, just return the same string */ |
| 8658 | Py_INCREF(unicode); |
| 8659 | return unicode; |
| 8660 | } |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8661 | return fixup(unicode, fix_decimal_and_space_to_ascii); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8662 | } |
| 8663 | |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8664 | PyObject * |
| 8665 | PyUnicode_TransformDecimalToASCII(Py_UNICODE *s, |
| 8666 | Py_ssize_t length) |
| 8667 | { |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8668 | PyObject *decimal; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8669 | Py_ssize_t i; |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8670 | Py_UCS4 maxchar; |
| 8671 | enum PyUnicode_Kind kind; |
| 8672 | void *data; |
| 8673 | |
Victor Stinner | 99d7ad0 | 2012-02-22 13:37:39 +0100 | [diff] [blame] | 8674 | maxchar = 127; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8675 | for (i = 0; i < length; i++) { |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8676 | Py_UNICODE ch = s[i]; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8677 | if (ch > 127) { |
| 8678 | int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8679 | if (decimal >= 0) |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8680 | ch = '0' + decimal; |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 8681 | maxchar = MAX_MAXCHAR(maxchar, ch); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8682 | } |
| 8683 | } |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8684 | |
| 8685 | /* Copy to a new string */ |
| 8686 | decimal = PyUnicode_New(length, maxchar); |
| 8687 | if (decimal == NULL) |
| 8688 | return decimal; |
| 8689 | kind = PyUnicode_KIND(decimal); |
| 8690 | data = PyUnicode_DATA(decimal); |
| 8691 | /* Iterate over code points */ |
| 8692 | for (i = 0; i < length; i++) { |
| 8693 | Py_UNICODE ch = s[i]; |
| 8694 | if (ch > 127) { |
| 8695 | int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8696 | if (decimal >= 0) |
| 8697 | ch = '0' + decimal; |
| 8698 | } |
| 8699 | PyUnicode_WRITE(kind, data, i, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8700 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 8701 | return unicode_result(decimal); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8702 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8703 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 8704 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8705 | int |
| 8706 | PyUnicode_EncodeDecimal(Py_UNICODE *s, |
| 8707 | Py_ssize_t length, |
| 8708 | char *output, |
| 8709 | const char *errors) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8710 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8711 | PyObject *unicode; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8712 | Py_ssize_t i; |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8713 | enum PyUnicode_Kind kind; |
| 8714 | void *data; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8715 | |
| 8716 | if (output == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8717 | PyErr_BadArgument(); |
| 8718 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8719 | } |
| 8720 | |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8721 | unicode = PyUnicode_FromUnicode(s, length); |
| 8722 | if (unicode == NULL) |
| 8723 | return -1; |
| 8724 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 8725 | if (PyUnicode_READY(unicode) == -1) { |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8726 | Py_DECREF(unicode); |
| 8727 | return -1; |
| 8728 | } |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8729 | kind = PyUnicode_KIND(unicode); |
| 8730 | data = PyUnicode_DATA(unicode); |
| 8731 | |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8732 | for (i=0; i < length; ) { |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8733 | PyObject *exc; |
| 8734 | Py_UCS4 ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8735 | int decimal; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8736 | Py_ssize_t startpos; |
| 8737 | |
| 8738 | ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8739 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8740 | if (Py_UNICODE_ISSPACE(ch)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8741 | *output++ = ' '; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8742 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8743 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8744 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8745 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 8746 | if (decimal >= 0) { |
| 8747 | *output++ = '0' + decimal; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8748 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8749 | continue; |
| 8750 | } |
| 8751 | if (0 < ch && ch < 256) { |
| 8752 | *output++ = (char)ch; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8753 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8754 | continue; |
| 8755 | } |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8756 | |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8757 | startpos = i; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8758 | exc = NULL; |
| 8759 | raise_encode_exception(&exc, "decimal", unicode, |
| 8760 | startpos, startpos+1, |
| 8761 | "invalid decimal Unicode string"); |
| 8762 | Py_XDECREF(exc); |
| 8763 | Py_DECREF(unicode); |
| 8764 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8765 | } |
| 8766 | /* 0-terminate the output string */ |
| 8767 | *output++ = '\0'; |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8768 | Py_DECREF(unicode); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8769 | return 0; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8770 | } |
| 8771 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8772 | /* --- Helpers ------------------------------------------------------------ */ |
| 8773 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8774 | static Py_ssize_t |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 8775 | any_find_slice(int direction, PyObject* s1, PyObject* s2, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8776 | Py_ssize_t start, |
| 8777 | Py_ssize_t end) |
| 8778 | { |
| 8779 | int kind1, kind2, kind; |
| 8780 | void *buf1, *buf2; |
| 8781 | Py_ssize_t len1, len2, result; |
| 8782 | |
| 8783 | kind1 = PyUnicode_KIND(s1); |
| 8784 | kind2 = PyUnicode_KIND(s2); |
| 8785 | kind = kind1 > kind2 ? kind1 : kind2; |
| 8786 | buf1 = PyUnicode_DATA(s1); |
| 8787 | buf2 = PyUnicode_DATA(s2); |
| 8788 | if (kind1 != kind) |
| 8789 | buf1 = _PyUnicode_AsKind(s1, kind); |
| 8790 | if (!buf1) |
| 8791 | return -2; |
| 8792 | if (kind2 != kind) |
| 8793 | buf2 = _PyUnicode_AsKind(s2, kind); |
| 8794 | if (!buf2) { |
| 8795 | if (kind1 != kind) PyMem_Free(buf1); |
| 8796 | return -2; |
| 8797 | } |
| 8798 | len1 = PyUnicode_GET_LENGTH(s1); |
| 8799 | len2 = PyUnicode_GET_LENGTH(s2); |
| 8800 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 8801 | if (direction > 0) { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 8802 | switch (kind) { |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 8803 | case PyUnicode_1BYTE_KIND: |
| 8804 | if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2)) |
| 8805 | result = asciilib_find_slice(buf1, len1, buf2, len2, start, end); |
| 8806 | else |
| 8807 | result = ucs1lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 8808 | break; |
| 8809 | case PyUnicode_2BYTE_KIND: |
| 8810 | result = ucs2lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 8811 | break; |
| 8812 | case PyUnicode_4BYTE_KIND: |
| 8813 | result = ucs4lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 8814 | break; |
| 8815 | default: |
| 8816 | assert(0); result = -2; |
| 8817 | } |
| 8818 | } |
| 8819 | else { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 8820 | switch (kind) { |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 8821 | case PyUnicode_1BYTE_KIND: |
| 8822 | if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2)) |
| 8823 | result = asciilib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 8824 | else |
| 8825 | result = ucs1lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 8826 | break; |
| 8827 | case PyUnicode_2BYTE_KIND: |
| 8828 | result = ucs2lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 8829 | break; |
| 8830 | case PyUnicode_4BYTE_KIND: |
| 8831 | result = ucs4lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 8832 | break; |
| 8833 | default: |
| 8834 | assert(0); result = -2; |
| 8835 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8836 | } |
| 8837 | |
| 8838 | if (kind1 != kind) |
| 8839 | PyMem_Free(buf1); |
| 8840 | if (kind2 != kind) |
| 8841 | PyMem_Free(buf2); |
| 8842 | |
| 8843 | return result; |
| 8844 | } |
| 8845 | |
| 8846 | Py_ssize_t |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8847 | _PyUnicode_InsertThousandsGrouping( |
| 8848 | PyObject *unicode, Py_ssize_t index, |
| 8849 | Py_ssize_t n_buffer, |
| 8850 | void *digits, Py_ssize_t n_digits, |
| 8851 | Py_ssize_t min_width, |
| 8852 | const char *grouping, PyObject *thousands_sep, |
| 8853 | Py_UCS4 *maxchar) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8854 | { |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8855 | unsigned int kind, thousands_sep_kind; |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8856 | char *data, *thousands_sep_data; |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8857 | Py_ssize_t thousands_sep_len; |
| 8858 | Py_ssize_t len; |
| 8859 | |
| 8860 | if (unicode != NULL) { |
| 8861 | kind = PyUnicode_KIND(unicode); |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8862 | data = (char *) PyUnicode_DATA(unicode) + index * kind; |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8863 | } |
| 8864 | else { |
| 8865 | kind = PyUnicode_1BYTE_KIND; |
| 8866 | data = NULL; |
| 8867 | } |
| 8868 | thousands_sep_kind = PyUnicode_KIND(thousands_sep); |
| 8869 | thousands_sep_data = PyUnicode_DATA(thousands_sep); |
| 8870 | thousands_sep_len = PyUnicode_GET_LENGTH(thousands_sep); |
| 8871 | if (unicode != NULL && thousands_sep_kind != kind) { |
Victor Stinner | 90f50d4 | 2012-02-24 01:44:47 +0100 | [diff] [blame] | 8872 | if (thousands_sep_kind < kind) { |
| 8873 | thousands_sep_data = _PyUnicode_AsKind(thousands_sep, kind); |
| 8874 | if (!thousands_sep_data) |
| 8875 | return -1; |
| 8876 | } |
| 8877 | else { |
| 8878 | data = _PyUnicode_AsKind(unicode, thousands_sep_kind); |
| 8879 | if (!data) |
| 8880 | return -1; |
| 8881 | } |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8882 | } |
| 8883 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 8884 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8885 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8886 | if (unicode != NULL && PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8887 | len = asciilib_InsertThousandsGrouping( |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8888 | (Py_UCS1 *) data, n_buffer, (Py_UCS1 *) digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8889 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8890 | (Py_UCS1 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8891 | else |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8892 | len = ucs1lib_InsertThousandsGrouping( |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8893 | (Py_UCS1*)data, n_buffer, (Py_UCS1*)digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8894 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8895 | (Py_UCS1 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8896 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8897 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8898 | len = ucs2lib_InsertThousandsGrouping( |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8899 | (Py_UCS2 *) data, n_buffer, (Py_UCS2 *) digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8900 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8901 | (Py_UCS2 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8902 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8903 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8904 | len = ucs4lib_InsertThousandsGrouping( |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8905 | (Py_UCS4 *) data, n_buffer, (Py_UCS4 *) digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8906 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8907 | (Py_UCS4 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8908 | break; |
| 8909 | default: |
| 8910 | assert(0); |
| 8911 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8912 | } |
Victor Stinner | 90f50d4 | 2012-02-24 01:44:47 +0100 | [diff] [blame] | 8913 | if (unicode != NULL && thousands_sep_kind != kind) { |
| 8914 | if (thousands_sep_kind < kind) |
| 8915 | PyMem_Free(thousands_sep_data); |
| 8916 | else |
| 8917 | PyMem_Free(data); |
| 8918 | } |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8919 | if (unicode == NULL) { |
| 8920 | *maxchar = 127; |
| 8921 | if (len != n_digits) { |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 8922 | *maxchar = MAX_MAXCHAR(*maxchar, |
| 8923 | PyUnicode_MAX_CHAR_VALUE(thousands_sep)); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8924 | } |
| 8925 | } |
| 8926 | return len; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8927 | } |
| 8928 | |
| 8929 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8930 | /* helper macro to fixup start/end slice values */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 8931 | #define ADJUST_INDICES(start, end, len) \ |
| 8932 | if (end > len) \ |
| 8933 | end = len; \ |
| 8934 | else if (end < 0) { \ |
| 8935 | end += len; \ |
| 8936 | if (end < 0) \ |
| 8937 | end = 0; \ |
| 8938 | } \ |
| 8939 | if (start < 0) { \ |
| 8940 | start += len; \ |
| 8941 | if (start < 0) \ |
| 8942 | start = 0; \ |
| 8943 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8944 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8945 | Py_ssize_t |
| 8946 | PyUnicode_Count(PyObject *str, |
| 8947 | PyObject *substr, |
| 8948 | Py_ssize_t start, |
| 8949 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8950 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8951 | Py_ssize_t result; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 8952 | PyObject* str_obj; |
| 8953 | PyObject* sub_obj; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8954 | int kind1, kind2, kind; |
| 8955 | void *buf1 = NULL, *buf2 = NULL; |
| 8956 | Py_ssize_t len1, len2; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8957 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 8958 | str_obj = PyUnicode_FromObject(str); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 8959 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8960 | return -1; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 8961 | sub_obj = PyUnicode_FromObject(substr); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 8962 | if (!sub_obj) { |
| 8963 | Py_DECREF(str_obj); |
| 8964 | return -1; |
| 8965 | } |
Benjamin Peterson | 4c13a4a | 2012-01-02 09:07:38 -0600 | [diff] [blame] | 8966 | if (PyUnicode_READY(sub_obj) == -1 || PyUnicode_READY(str_obj) == -1) { |
Benjamin Peterson | 5e458f5 | 2012-01-02 10:12:13 -0600 | [diff] [blame] | 8967 | Py_DECREF(sub_obj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8968 | Py_DECREF(str_obj); |
| 8969 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8970 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8971 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8972 | kind1 = PyUnicode_KIND(str_obj); |
| 8973 | kind2 = PyUnicode_KIND(sub_obj); |
Antoine Pitrou | e45c0c5 | 2012-05-12 15:49:07 +0200 | [diff] [blame] | 8974 | kind = kind1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8975 | buf1 = PyUnicode_DATA(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8976 | buf2 = PyUnicode_DATA(sub_obj); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 8977 | if (kind2 != kind) { |
Antoine Pitrou | 758153b | 2012-05-12 15:51:51 +0200 | [diff] [blame] | 8978 | if (kind2 > kind) { |
| 8979 | Py_DECREF(sub_obj); |
| 8980 | Py_DECREF(str_obj); |
Antoine Pitrou | e45c0c5 | 2012-05-12 15:49:07 +0200 | [diff] [blame] | 8981 | return 0; |
Antoine Pitrou | 758153b | 2012-05-12 15:51:51 +0200 | [diff] [blame] | 8982 | } |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 8983 | buf2 = _PyUnicode_AsKind(sub_obj, kind); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 8984 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8985 | if (!buf2) |
| 8986 | goto onError; |
| 8987 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 8988 | len2 = PyUnicode_GET_LENGTH(sub_obj); |
| 8989 | |
| 8990 | ADJUST_INDICES(start, end, len1); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 8991 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8992 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8993 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sub_obj)) |
| 8994 | result = asciilib_count( |
| 8995 | ((Py_UCS1*)buf1) + start, end - start, |
| 8996 | buf2, len2, PY_SSIZE_T_MAX |
| 8997 | ); |
| 8998 | else |
| 8999 | result = ucs1lib_count( |
| 9000 | ((Py_UCS1*)buf1) + start, end - start, |
| 9001 | buf2, len2, PY_SSIZE_T_MAX |
| 9002 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9003 | break; |
| 9004 | case PyUnicode_2BYTE_KIND: |
| 9005 | result = ucs2lib_count( |
| 9006 | ((Py_UCS2*)buf1) + start, end - start, |
| 9007 | buf2, len2, PY_SSIZE_T_MAX |
| 9008 | ); |
| 9009 | break; |
| 9010 | case PyUnicode_4BYTE_KIND: |
| 9011 | result = ucs4lib_count( |
| 9012 | ((Py_UCS4*)buf1) + start, end - start, |
| 9013 | buf2, len2, PY_SSIZE_T_MAX |
| 9014 | ); |
| 9015 | break; |
| 9016 | default: |
| 9017 | assert(0); result = 0; |
| 9018 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9019 | |
| 9020 | Py_DECREF(sub_obj); |
| 9021 | Py_DECREF(str_obj); |
| 9022 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9023 | if (kind2 != kind) |
| 9024 | PyMem_Free(buf2); |
| 9025 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9026 | return result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9027 | onError: |
| 9028 | Py_DECREF(sub_obj); |
| 9029 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9030 | if (kind2 != kind && buf2) |
| 9031 | PyMem_Free(buf2); |
| 9032 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9033 | } |
| 9034 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9035 | Py_ssize_t |
| 9036 | PyUnicode_Find(PyObject *str, |
| 9037 | PyObject *sub, |
| 9038 | Py_ssize_t start, |
| 9039 | Py_ssize_t end, |
| 9040 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9041 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9042 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9043 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9044 | str = PyUnicode_FromObject(str); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9045 | if (!str) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9046 | return -2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9047 | sub = PyUnicode_FromObject(sub); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9048 | if (!sub) { |
| 9049 | Py_DECREF(str); |
| 9050 | return -2; |
| 9051 | } |
| 9052 | if (PyUnicode_READY(sub) == -1 || PyUnicode_READY(str) == -1) { |
| 9053 | Py_DECREF(sub); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9054 | Py_DECREF(str); |
| 9055 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9056 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9057 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9058 | result = any_find_slice(direction, |
| 9059 | str, sub, start, end |
| 9060 | ); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9061 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9062 | Py_DECREF(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9063 | Py_DECREF(sub); |
| 9064 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9065 | return result; |
| 9066 | } |
| 9067 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9068 | Py_ssize_t |
| 9069 | PyUnicode_FindChar(PyObject *str, Py_UCS4 ch, |
| 9070 | Py_ssize_t start, Py_ssize_t end, |
| 9071 | int direction) |
| 9072 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9073 | int kind; |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9074 | Py_ssize_t result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9075 | if (PyUnicode_READY(str) == -1) |
| 9076 | return -2; |
Victor Stinner | 267aa24 | 2011-10-02 01:08:37 +0200 | [diff] [blame] | 9077 | if (start < 0 || end < 0) { |
| 9078 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 9079 | return -2; |
| 9080 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9081 | if (end > PyUnicode_GET_LENGTH(str)) |
| 9082 | end = PyUnicode_GET_LENGTH(str); |
| 9083 | kind = PyUnicode_KIND(str); |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9084 | result = findchar(PyUnicode_1BYTE_DATA(str) + kind*start, |
| 9085 | kind, end-start, ch, direction); |
| 9086 | if (result == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9087 | return -1; |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9088 | else |
| 9089 | return start + result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9090 | } |
| 9091 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9092 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9093 | tailmatch(PyObject *self, |
| 9094 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9095 | Py_ssize_t start, |
| 9096 | Py_ssize_t end, |
| 9097 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9098 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9099 | int kind_self; |
| 9100 | int kind_sub; |
| 9101 | void *data_self; |
| 9102 | void *data_sub; |
| 9103 | Py_ssize_t offset; |
| 9104 | Py_ssize_t i; |
| 9105 | Py_ssize_t end_sub; |
| 9106 | |
| 9107 | if (PyUnicode_READY(self) == -1 || |
| 9108 | PyUnicode_READY(substring) == -1) |
| 9109 | return 0; |
| 9110 | |
| 9111 | if (PyUnicode_GET_LENGTH(substring) == 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9112 | return 1; |
| 9113 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9114 | ADJUST_INDICES(start, end, PyUnicode_GET_LENGTH(self)); |
| 9115 | end -= PyUnicode_GET_LENGTH(substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9116 | if (end < start) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9117 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9118 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9119 | kind_self = PyUnicode_KIND(self); |
| 9120 | data_self = PyUnicode_DATA(self); |
| 9121 | kind_sub = PyUnicode_KIND(substring); |
| 9122 | data_sub = PyUnicode_DATA(substring); |
| 9123 | end_sub = PyUnicode_GET_LENGTH(substring) - 1; |
| 9124 | |
| 9125 | if (direction > 0) |
| 9126 | offset = end; |
| 9127 | else |
| 9128 | offset = start; |
| 9129 | |
| 9130 | if (PyUnicode_READ(kind_self, data_self, offset) == |
| 9131 | PyUnicode_READ(kind_sub, data_sub, 0) && |
| 9132 | PyUnicode_READ(kind_self, data_self, offset + end_sub) == |
| 9133 | PyUnicode_READ(kind_sub, data_sub, end_sub)) { |
| 9134 | /* If both are of the same kind, memcmp is sufficient */ |
| 9135 | if (kind_self == kind_sub) { |
| 9136 | return ! memcmp((char *)data_self + |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9137 | (offset * PyUnicode_KIND(substring)), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9138 | data_sub, |
| 9139 | PyUnicode_GET_LENGTH(substring) * |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9140 | PyUnicode_KIND(substring)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9141 | } |
| 9142 | /* otherwise we have to compare each character by first accesing it */ |
| 9143 | else { |
| 9144 | /* We do not need to compare 0 and len(substring)-1 because |
| 9145 | the if statement above ensured already that they are equal |
| 9146 | when we end up here. */ |
| 9147 | // TODO: honor direction and do a forward or backwards search |
| 9148 | for (i = 1; i < end_sub; ++i) { |
| 9149 | if (PyUnicode_READ(kind_self, data_self, offset + i) != |
| 9150 | PyUnicode_READ(kind_sub, data_sub, i)) |
| 9151 | return 0; |
| 9152 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9153 | return 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9154 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9155 | } |
| 9156 | |
| 9157 | return 0; |
| 9158 | } |
| 9159 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9160 | Py_ssize_t |
| 9161 | PyUnicode_Tailmatch(PyObject *str, |
| 9162 | PyObject *substr, |
| 9163 | Py_ssize_t start, |
| 9164 | Py_ssize_t end, |
| 9165 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9166 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9167 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9168 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9169 | str = PyUnicode_FromObject(str); |
| 9170 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9171 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9172 | substr = PyUnicode_FromObject(substr); |
| 9173 | if (substr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9174 | Py_DECREF(str); |
| 9175 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9176 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9177 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9178 | result = tailmatch(str, substr, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9179 | start, end, direction); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9180 | Py_DECREF(str); |
| 9181 | Py_DECREF(substr); |
| 9182 | return result; |
| 9183 | } |
| 9184 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9185 | /* Apply fixfct filter to the Unicode object self and return a |
| 9186 | reference to the modified object */ |
| 9187 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9188 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9189 | fixup(PyObject *self, |
| 9190 | Py_UCS4 (*fixfct)(PyObject *s)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9191 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9192 | PyObject *u; |
| 9193 | Py_UCS4 maxchar_old, maxchar_new = 0; |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9194 | PyObject *v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9195 | |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 9196 | u = _PyUnicode_Copy(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9197 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9198 | return NULL; |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 9199 | maxchar_old = PyUnicode_MAX_CHAR_VALUE(u); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9200 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9201 | /* fix functions return the new maximum character in a string, |
| 9202 | if the kind of the resulting unicode object does not change, |
| 9203 | everything is fine. Otherwise we need to change the string kind |
| 9204 | and re-run the fix function. */ |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9205 | maxchar_new = fixfct(u); |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9206 | |
| 9207 | if (maxchar_new == 0) { |
| 9208 | /* no changes */; |
| 9209 | if (PyUnicode_CheckExact(self)) { |
| 9210 | Py_DECREF(u); |
| 9211 | Py_INCREF(self); |
| 9212 | return self; |
| 9213 | } |
| 9214 | else |
| 9215 | return u; |
| 9216 | } |
| 9217 | |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9218 | maxchar_new = align_maxchar(maxchar_new); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9219 | |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9220 | if (maxchar_new == maxchar_old) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9221 | return u; |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9222 | |
| 9223 | /* In case the maximum character changed, we need to |
| 9224 | convert the string to the new category. */ |
| 9225 | v = PyUnicode_New(PyUnicode_GET_LENGTH(self), maxchar_new); |
| 9226 | if (v == NULL) { |
| 9227 | Py_DECREF(u); |
| 9228 | return NULL; |
| 9229 | } |
| 9230 | if (maxchar_new > maxchar_old) { |
| 9231 | /* If the maxchar increased so that the kind changed, not all |
| 9232 | characters are representable anymore and we need to fix the |
| 9233 | string again. This only happens in very few cases. */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9234 | _PyUnicode_FastCopyCharacters(v, 0, |
| 9235 | self, 0, PyUnicode_GET_LENGTH(self)); |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9236 | maxchar_old = fixfct(v); |
| 9237 | assert(maxchar_old > 0 && maxchar_old <= maxchar_new); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9238 | } |
| 9239 | else { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9240 | _PyUnicode_FastCopyCharacters(v, 0, |
| 9241 | u, 0, PyUnicode_GET_LENGTH(self)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9242 | } |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9243 | Py_DECREF(u); |
| 9244 | assert(_PyUnicode_CheckConsistency(v, 1)); |
| 9245 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9246 | } |
| 9247 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9248 | static PyObject * |
| 9249 | ascii_upper_or_lower(PyObject *self, int lower) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9250 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9251 | Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9252 | char *resdata, *data = PyUnicode_DATA(self); |
| 9253 | PyObject *res; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9254 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9255 | res = PyUnicode_New(len, 127); |
| 9256 | if (res == NULL) |
| 9257 | return NULL; |
| 9258 | resdata = PyUnicode_DATA(res); |
| 9259 | if (lower) |
| 9260 | _Py_bytes_lower(resdata, data, len); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9261 | else |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9262 | _Py_bytes_upper(resdata, data, len); |
| 9263 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9264 | } |
| 9265 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9266 | static Py_UCS4 |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9267 | 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] | 9268 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9269 | Py_ssize_t j; |
| 9270 | int final_sigma; |
| 9271 | Py_UCS4 c; |
| 9272 | /* 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] | 9273 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9274 | \p{cased}\p{case-ignorable}*U+03A3!(\p{case-ignorable}*\p{cased}) |
| 9275 | |
| 9276 | where ! is a negation and \p{xxx} is a character with property xxx. |
| 9277 | */ |
| 9278 | for (j = i - 1; j >= 0; j--) { |
| 9279 | c = PyUnicode_READ(kind, data, j); |
| 9280 | if (!_PyUnicode_IsCaseIgnorable(c)) |
| 9281 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9282 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9283 | final_sigma = j >= 0 && _PyUnicode_IsCased(c); |
| 9284 | if (final_sigma) { |
| 9285 | for (j = i + 1; j < length; j++) { |
| 9286 | c = PyUnicode_READ(kind, data, j); |
| 9287 | if (!_PyUnicode_IsCaseIgnorable(c)) |
| 9288 | break; |
| 9289 | } |
| 9290 | final_sigma = j == length || !_PyUnicode_IsCased(c); |
| 9291 | } |
| 9292 | return (final_sigma) ? 0x3C2 : 0x3C3; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9293 | } |
| 9294 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9295 | static int |
| 9296 | lower_ucs4(int kind, void *data, Py_ssize_t length, Py_ssize_t i, |
| 9297 | Py_UCS4 c, Py_UCS4 *mapped) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9298 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9299 | /* Obscure special case. */ |
| 9300 | if (c == 0x3A3) { |
| 9301 | mapped[0] = handle_capital_sigma(kind, data, length, i); |
| 9302 | return 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9303 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9304 | return _PyUnicode_ToLowerFull(c, mapped); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9305 | } |
| 9306 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9307 | static Py_ssize_t |
| 9308 | 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] | 9309 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9310 | Py_ssize_t i, k = 0; |
| 9311 | int n_res, j; |
| 9312 | Py_UCS4 c, mapped[3]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9313 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9314 | c = PyUnicode_READ(kind, data, 0); |
| 9315 | n_res = _PyUnicode_ToUpperFull(c, mapped); |
| 9316 | for (j = 0; j < n_res; j++) { |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9317 | *maxchar = MAX_MAXCHAR(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9318 | res[k++] = mapped[j]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9319 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9320 | for (i = 1; i < length; i++) { |
| 9321 | c = PyUnicode_READ(kind, data, i); |
| 9322 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9323 | for (j = 0; j < n_res; j++) { |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9324 | *maxchar = MAX_MAXCHAR(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9325 | res[k++] = mapped[j]; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9326 | } |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9327 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9328 | return k; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9329 | } |
| 9330 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9331 | static Py_ssize_t |
| 9332 | do_swapcase(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) { |
| 9333 | Py_ssize_t i, k = 0; |
| 9334 | |
| 9335 | for (i = 0; i < length; i++) { |
| 9336 | Py_UCS4 c = PyUnicode_READ(kind, data, i), mapped[3]; |
| 9337 | int n_res, j; |
| 9338 | if (Py_UNICODE_ISUPPER(c)) { |
| 9339 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9340 | } |
| 9341 | else if (Py_UNICODE_ISLOWER(c)) { |
| 9342 | n_res = _PyUnicode_ToUpperFull(c, mapped); |
| 9343 | } |
| 9344 | else { |
| 9345 | n_res = 1; |
| 9346 | mapped[0] = c; |
| 9347 | } |
| 9348 | for (j = 0; j < n_res; j++) { |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9349 | *maxchar = MAX_MAXCHAR(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9350 | res[k++] = mapped[j]; |
| 9351 | } |
| 9352 | } |
| 9353 | return k; |
| 9354 | } |
| 9355 | |
| 9356 | static Py_ssize_t |
| 9357 | do_upper_or_lower(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, |
| 9358 | Py_UCS4 *maxchar, int lower) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9359 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9360 | Py_ssize_t i, k = 0; |
| 9361 | |
| 9362 | for (i = 0; i < length; i++) { |
| 9363 | Py_UCS4 c = PyUnicode_READ(kind, data, i), mapped[3]; |
| 9364 | int n_res, j; |
| 9365 | if (lower) |
| 9366 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9367 | else |
| 9368 | n_res = _PyUnicode_ToUpperFull(c, mapped); |
| 9369 | for (j = 0; j < n_res; j++) { |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9370 | *maxchar = MAX_MAXCHAR(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9371 | res[k++] = mapped[j]; |
| 9372 | } |
| 9373 | } |
| 9374 | return k; |
| 9375 | } |
| 9376 | |
| 9377 | static Py_ssize_t |
| 9378 | do_upper(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9379 | { |
| 9380 | return do_upper_or_lower(kind, data, length, res, maxchar, 0); |
| 9381 | } |
| 9382 | |
| 9383 | static Py_ssize_t |
| 9384 | do_lower(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9385 | { |
| 9386 | return do_upper_or_lower(kind, data, length, res, maxchar, 1); |
| 9387 | } |
| 9388 | |
Benjamin Peterson | e51757f | 2012-01-12 21:10:29 -0500 | [diff] [blame] | 9389 | static Py_ssize_t |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 9390 | do_casefold(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9391 | { |
| 9392 | Py_ssize_t i, k = 0; |
| 9393 | |
| 9394 | for (i = 0; i < length; i++) { |
| 9395 | Py_UCS4 c = PyUnicode_READ(kind, data, i); |
| 9396 | Py_UCS4 mapped[3]; |
| 9397 | int j, n_res = _PyUnicode_ToFoldedFull(c, mapped); |
| 9398 | for (j = 0; j < n_res; j++) { |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9399 | *maxchar = MAX_MAXCHAR(*maxchar, mapped[j]); |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 9400 | res[k++] = mapped[j]; |
| 9401 | } |
| 9402 | } |
| 9403 | return k; |
| 9404 | } |
| 9405 | |
| 9406 | static Py_ssize_t |
Benjamin Peterson | e51757f | 2012-01-12 21:10:29 -0500 | [diff] [blame] | 9407 | do_title(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9408 | { |
| 9409 | Py_ssize_t i, k = 0; |
| 9410 | int previous_is_cased; |
| 9411 | |
| 9412 | previous_is_cased = 0; |
| 9413 | for (i = 0; i < length; i++) { |
| 9414 | const Py_UCS4 c = PyUnicode_READ(kind, data, i); |
| 9415 | Py_UCS4 mapped[3]; |
| 9416 | int n_res, j; |
| 9417 | |
| 9418 | if (previous_is_cased) |
| 9419 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9420 | else |
| 9421 | n_res = _PyUnicode_ToTitleFull(c, mapped); |
| 9422 | |
| 9423 | for (j = 0; j < n_res; j++) { |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9424 | *maxchar = MAX_MAXCHAR(*maxchar, mapped[j]); |
Benjamin Peterson | e51757f | 2012-01-12 21:10:29 -0500 | [diff] [blame] | 9425 | res[k++] = mapped[j]; |
| 9426 | } |
| 9427 | |
| 9428 | previous_is_cased = _PyUnicode_IsCased(c); |
| 9429 | } |
| 9430 | return k; |
| 9431 | } |
| 9432 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9433 | static PyObject * |
| 9434 | case_operation(PyObject *self, |
| 9435 | Py_ssize_t (*perform)(int, void *, Py_ssize_t, Py_UCS4 *, Py_UCS4 *)) |
| 9436 | { |
| 9437 | PyObject *res = NULL; |
| 9438 | Py_ssize_t length, newlength = 0; |
| 9439 | int kind, outkind; |
| 9440 | void *data, *outdata; |
| 9441 | Py_UCS4 maxchar = 0, *tmp, *tmpend; |
| 9442 | |
Benjamin Peterson | eea4846 | 2012-01-16 14:28:50 -0500 | [diff] [blame] | 9443 | assert(PyUnicode_IS_READY(self)); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9444 | |
| 9445 | kind = PyUnicode_KIND(self); |
| 9446 | data = PyUnicode_DATA(self); |
| 9447 | length = PyUnicode_GET_LENGTH(self); |
| 9448 | tmp = PyMem_MALLOC(sizeof(Py_UCS4) * 3 * length); |
| 9449 | if (tmp == NULL) |
| 9450 | return PyErr_NoMemory(); |
| 9451 | newlength = perform(kind, data, length, tmp, &maxchar); |
| 9452 | res = PyUnicode_New(newlength, maxchar); |
| 9453 | if (res == NULL) |
| 9454 | goto leave; |
| 9455 | tmpend = tmp + newlength; |
| 9456 | outdata = PyUnicode_DATA(res); |
| 9457 | outkind = PyUnicode_KIND(res); |
| 9458 | switch (outkind) { |
| 9459 | case PyUnicode_1BYTE_KIND: |
| 9460 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS1, tmp, tmpend, outdata); |
| 9461 | break; |
| 9462 | case PyUnicode_2BYTE_KIND: |
| 9463 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS2, tmp, tmpend, outdata); |
| 9464 | break; |
| 9465 | case PyUnicode_4BYTE_KIND: |
| 9466 | memcpy(outdata, tmp, sizeof(Py_UCS4) * newlength); |
| 9467 | break; |
| 9468 | default: |
| 9469 | assert(0); |
| 9470 | break; |
| 9471 | } |
| 9472 | leave: |
| 9473 | PyMem_FREE(tmp); |
| 9474 | return res; |
| 9475 | } |
| 9476 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9477 | PyObject * |
| 9478 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9479 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9480 | PyObject *sep = NULL; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9481 | Py_ssize_t seplen; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9482 | PyObject *res = NULL; /* the result */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9483 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9484 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
| 9485 | PyObject **items; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9486 | PyObject *item; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9487 | Py_ssize_t sz, i, res_offset; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9488 | Py_UCS4 maxchar; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9489 | Py_UCS4 item_maxchar; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9490 | int use_memcpy; |
| 9491 | unsigned char *res_data = NULL, *sep_data = NULL; |
| 9492 | PyObject *last_obj; |
| 9493 | unsigned int kind = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9494 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9495 | fseq = PySequence_Fast(seq, ""); |
| 9496 | if (fseq == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9497 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9498 | } |
| 9499 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9500 | /* NOTE: the following code can't call back into Python code, |
| 9501 | * so we are sure that fseq won't be mutated. |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9502 | */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9503 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9504 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 9505 | /* If empty sequence, return u"". */ |
| 9506 | if (seqlen == 0) { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9507 | Py_DECREF(fseq); |
| 9508 | Py_INCREF(unicode_empty); |
| 9509 | res = unicode_empty; |
| 9510 | return res; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9511 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9512 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9513 | /* If singleton sequence with an exact Unicode, return that. */ |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9514 | last_obj = NULL; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9515 | items = PySequence_Fast_ITEMS(fseq); |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9516 | if (seqlen == 1) { |
| 9517 | if (PyUnicode_CheckExact(items[0])) { |
| 9518 | res = items[0]; |
| 9519 | Py_INCREF(res); |
| 9520 | Py_DECREF(fseq); |
| 9521 | return res; |
| 9522 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9523 | seplen = 0; |
Victor Stinner | c6f0df7 | 2011-10-06 15:58:54 +0200 | [diff] [blame] | 9524 | maxchar = 0; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9525 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9526 | else { |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9527 | /* Set up sep and seplen */ |
| 9528 | if (separator == NULL) { |
| 9529 | /* fall back to a blank space separator */ |
| 9530 | sep = PyUnicode_FromOrdinal(' '); |
| 9531 | if (!sep) |
| 9532 | goto onError; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9533 | seplen = 1; |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9534 | maxchar = 32; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9535 | } |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9536 | else { |
| 9537 | if (!PyUnicode_Check(separator)) { |
| 9538 | PyErr_Format(PyExc_TypeError, |
| 9539 | "separator: expected str instance," |
| 9540 | " %.80s found", |
| 9541 | Py_TYPE(separator)->tp_name); |
| 9542 | goto onError; |
| 9543 | } |
| 9544 | if (PyUnicode_READY(separator)) |
| 9545 | goto onError; |
| 9546 | sep = separator; |
| 9547 | seplen = PyUnicode_GET_LENGTH(separator); |
| 9548 | maxchar = PyUnicode_MAX_CHAR_VALUE(separator); |
| 9549 | /* inc refcount to keep this code path symmetric with the |
| 9550 | above case of a blank separator */ |
| 9551 | Py_INCREF(sep); |
| 9552 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9553 | last_obj = sep; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9554 | } |
| 9555 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9556 | /* There are at least two things to join, or else we have a subclass |
| 9557 | * of str in the sequence. |
| 9558 | * Do a pre-pass to figure out the total amount of space we'll |
| 9559 | * need (sz), and see whether all argument are strings. |
| 9560 | */ |
| 9561 | sz = 0; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9562 | #ifdef Py_DEBUG |
| 9563 | use_memcpy = 0; |
| 9564 | #else |
| 9565 | use_memcpy = 1; |
| 9566 | #endif |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9567 | for (i = 0; i < seqlen; i++) { |
| 9568 | const Py_ssize_t old_sz = sz; |
| 9569 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9570 | if (!PyUnicode_Check(item)) { |
| 9571 | PyErr_Format(PyExc_TypeError, |
| 9572 | "sequence item %zd: expected str instance," |
| 9573 | " %.80s found", |
| 9574 | i, Py_TYPE(item)->tp_name); |
| 9575 | goto onError; |
| 9576 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9577 | if (PyUnicode_READY(item) == -1) |
| 9578 | goto onError; |
| 9579 | sz += PyUnicode_GET_LENGTH(item); |
| 9580 | item_maxchar = PyUnicode_MAX_CHAR_VALUE(item); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9581 | maxchar = MAX_MAXCHAR(maxchar, item_maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9582 | if (i != 0) |
| 9583 | sz += seplen; |
| 9584 | if (sz < old_sz || sz > PY_SSIZE_T_MAX) { |
| 9585 | PyErr_SetString(PyExc_OverflowError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9586 | "join() result is too long for a Python string"); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9587 | goto onError; |
| 9588 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9589 | if (use_memcpy && last_obj != NULL) { |
| 9590 | if (PyUnicode_KIND(last_obj) != PyUnicode_KIND(item)) |
| 9591 | use_memcpy = 0; |
| 9592 | } |
| 9593 | last_obj = item; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9594 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9595 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9596 | res = PyUnicode_New(sz, maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9597 | if (res == NULL) |
| 9598 | goto onError; |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9599 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9600 | /* Catenate everything. */ |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9601 | #ifdef Py_DEBUG |
| 9602 | use_memcpy = 0; |
| 9603 | #else |
| 9604 | if (use_memcpy) { |
| 9605 | res_data = PyUnicode_1BYTE_DATA(res); |
| 9606 | kind = PyUnicode_KIND(res); |
| 9607 | if (seplen != 0) |
| 9608 | sep_data = PyUnicode_1BYTE_DATA(sep); |
| 9609 | } |
| 9610 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9611 | for (i = 0, res_offset = 0; i < seqlen; ++i) { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9612 | Py_ssize_t itemlen; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9613 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9614 | /* Copy item, and maybe the separator. */ |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9615 | if (i && seplen != 0) { |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9616 | if (use_memcpy) { |
| 9617 | Py_MEMCPY(res_data, |
| 9618 | sep_data, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9619 | kind * seplen); |
| 9620 | res_data += kind * seplen; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9621 | } |
| 9622 | else { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9623 | _PyUnicode_FastCopyCharacters(res, res_offset, sep, 0, seplen); |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9624 | res_offset += seplen; |
| 9625 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9626 | } |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9627 | itemlen = PyUnicode_GET_LENGTH(item); |
| 9628 | if (itemlen != 0) { |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9629 | if (use_memcpy) { |
| 9630 | Py_MEMCPY(res_data, |
| 9631 | PyUnicode_DATA(item), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9632 | kind * itemlen); |
| 9633 | res_data += kind * itemlen; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9634 | } |
| 9635 | else { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9636 | _PyUnicode_FastCopyCharacters(res, res_offset, item, 0, itemlen); |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9637 | res_offset += itemlen; |
| 9638 | } |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9639 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9640 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9641 | if (use_memcpy) |
| 9642 | assert(res_data == PyUnicode_1BYTE_DATA(res) |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9643 | + kind * PyUnicode_GET_LENGTH(res)); |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9644 | else |
| 9645 | assert(res_offset == PyUnicode_GET_LENGTH(res)); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9646 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9647 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9648 | Py_XDECREF(sep); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9649 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9650 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9651 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9652 | onError: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9653 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9654 | Py_XDECREF(sep); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9655 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9656 | return NULL; |
| 9657 | } |
| 9658 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9659 | #define FILL(kind, data, value, start, length) \ |
| 9660 | do { \ |
| 9661 | Py_ssize_t i_ = 0; \ |
| 9662 | assert(kind != PyUnicode_WCHAR_KIND); \ |
| 9663 | switch ((kind)) { \ |
| 9664 | case PyUnicode_1BYTE_KIND: { \ |
| 9665 | unsigned char * to_ = (unsigned char *)((data)) + (start); \ |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 9666 | memset(to_, (unsigned char)value, (length)); \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9667 | break; \ |
| 9668 | } \ |
| 9669 | case PyUnicode_2BYTE_KIND: { \ |
| 9670 | Py_UCS2 * to_ = (Py_UCS2 *)((data)) + (start); \ |
| 9671 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 9672 | break; \ |
| 9673 | } \ |
Benjamin Peterson | e157cf1 | 2012-01-01 15:56:20 -0600 | [diff] [blame] | 9674 | case PyUnicode_4BYTE_KIND: { \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9675 | Py_UCS4 * to_ = (Py_UCS4 *)((data)) + (start); \ |
| 9676 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 9677 | break; \ |
Benjamin Peterson | e157cf1 | 2012-01-01 15:56:20 -0600 | [diff] [blame] | 9678 | default: assert(0); \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9679 | } \ |
| 9680 | } \ |
| 9681 | } while (0) |
| 9682 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9683 | void |
| 9684 | _PyUnicode_FastFill(PyObject *unicode, Py_ssize_t start, Py_ssize_t length, |
| 9685 | Py_UCS4 fill_char) |
| 9686 | { |
| 9687 | const enum PyUnicode_Kind kind = PyUnicode_KIND(unicode); |
| 9688 | const void *data = PyUnicode_DATA(unicode); |
| 9689 | assert(PyUnicode_IS_READY(unicode)); |
| 9690 | assert(unicode_modifiable(unicode)); |
| 9691 | assert(fill_char <= PyUnicode_MAX_CHAR_VALUE(unicode)); |
| 9692 | assert(start >= 0); |
| 9693 | assert(start + length <= PyUnicode_GET_LENGTH(unicode)); |
| 9694 | FILL(kind, data, fill_char, start, length); |
| 9695 | } |
| 9696 | |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 9697 | Py_ssize_t |
| 9698 | PyUnicode_Fill(PyObject *unicode, Py_ssize_t start, Py_ssize_t length, |
| 9699 | Py_UCS4 fill_char) |
| 9700 | { |
| 9701 | Py_ssize_t maxlen; |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 9702 | |
| 9703 | if (!PyUnicode_Check(unicode)) { |
| 9704 | PyErr_BadInternalCall(); |
| 9705 | return -1; |
| 9706 | } |
| 9707 | if (PyUnicode_READY(unicode) == -1) |
| 9708 | return -1; |
| 9709 | if (unicode_check_modifiable(unicode)) |
| 9710 | return -1; |
| 9711 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9712 | if (start < 0) { |
| 9713 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 9714 | return -1; |
| 9715 | } |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 9716 | if (fill_char > PyUnicode_MAX_CHAR_VALUE(unicode)) { |
| 9717 | PyErr_SetString(PyExc_ValueError, |
| 9718 | "fill character is bigger than " |
| 9719 | "the string maximum character"); |
| 9720 | return -1; |
| 9721 | } |
| 9722 | |
| 9723 | maxlen = PyUnicode_GET_LENGTH(unicode) - start; |
| 9724 | length = Py_MIN(maxlen, length); |
| 9725 | if (length <= 0) |
| 9726 | return 0; |
| 9727 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9728 | _PyUnicode_FastFill(unicode, start, length, fill_char); |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 9729 | return length; |
| 9730 | } |
| 9731 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9732 | static PyObject * |
| 9733 | pad(PyObject *self, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9734 | Py_ssize_t left, |
| 9735 | Py_ssize_t right, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9736 | Py_UCS4 fill) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9737 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9738 | PyObject *u; |
| 9739 | Py_UCS4 maxchar; |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9740 | int kind; |
| 9741 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9742 | |
| 9743 | if (left < 0) |
| 9744 | left = 0; |
| 9745 | if (right < 0) |
| 9746 | right = 0; |
| 9747 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 9748 | if (left == 0 && right == 0) |
| 9749 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9750 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9751 | if (left > PY_SSIZE_T_MAX - _PyUnicode_LENGTH(self) || |
| 9752 | right > PY_SSIZE_T_MAX - (left + _PyUnicode_LENGTH(self))) { |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 9753 | PyErr_SetString(PyExc_OverflowError, "padded string is too long"); |
| 9754 | return NULL; |
| 9755 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9756 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9757 | maxchar = MAX_MAXCHAR(maxchar, fill); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9758 | u = PyUnicode_New(left + _PyUnicode_LENGTH(self) + right, maxchar); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9759 | if (!u) |
| 9760 | return NULL; |
| 9761 | |
| 9762 | kind = PyUnicode_KIND(u); |
| 9763 | data = PyUnicode_DATA(u); |
| 9764 | if (left) |
| 9765 | FILL(kind, data, fill, 0, left); |
| 9766 | if (right) |
| 9767 | FILL(kind, data, fill, left + _PyUnicode_LENGTH(self), right); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9768 | _PyUnicode_FastCopyCharacters(u, left, self, 0, _PyUnicode_LENGTH(self)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9769 | assert(_PyUnicode_CheckConsistency(u, 1)); |
| 9770 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9771 | } |
| 9772 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9773 | PyObject * |
| 9774 | PyUnicode_Splitlines(PyObject *string, int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9775 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9776 | PyObject *list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9777 | |
| 9778 | string = PyUnicode_FromObject(string); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9779 | if (string == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9780 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9781 | if (PyUnicode_READY(string) == -1) { |
| 9782 | Py_DECREF(string); |
| 9783 | return NULL; |
| 9784 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9785 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9786 | switch (PyUnicode_KIND(string)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9787 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9788 | if (PyUnicode_IS_ASCII(string)) |
| 9789 | list = asciilib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9790 | string, PyUnicode_1BYTE_DATA(string), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9791 | PyUnicode_GET_LENGTH(string), keepends); |
| 9792 | else |
| 9793 | list = ucs1lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9794 | string, PyUnicode_1BYTE_DATA(string), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9795 | PyUnicode_GET_LENGTH(string), keepends); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9796 | break; |
| 9797 | case PyUnicode_2BYTE_KIND: |
| 9798 | list = ucs2lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9799 | string, PyUnicode_2BYTE_DATA(string), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9800 | PyUnicode_GET_LENGTH(string), keepends); |
| 9801 | break; |
| 9802 | case PyUnicode_4BYTE_KIND: |
| 9803 | list = ucs4lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9804 | string, PyUnicode_4BYTE_DATA(string), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9805 | PyUnicode_GET_LENGTH(string), keepends); |
| 9806 | break; |
| 9807 | default: |
| 9808 | assert(0); |
| 9809 | list = 0; |
| 9810 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9811 | Py_DECREF(string); |
| 9812 | return list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9813 | } |
| 9814 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9815 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9816 | split(PyObject *self, |
| 9817 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9818 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9819 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9820 | int kind1, kind2, kind; |
| 9821 | void *buf1, *buf2; |
| 9822 | Py_ssize_t len1, len2; |
| 9823 | PyObject* out; |
| 9824 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9825 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9826 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9827 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9828 | if (PyUnicode_READY(self) == -1) |
| 9829 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9830 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9831 | if (substring == NULL) |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9832 | switch (PyUnicode_KIND(self)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9833 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9834 | if (PyUnicode_IS_ASCII(self)) |
| 9835 | return asciilib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9836 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9837 | PyUnicode_GET_LENGTH(self), maxcount |
| 9838 | ); |
| 9839 | else |
| 9840 | return ucs1lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9841 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9842 | PyUnicode_GET_LENGTH(self), maxcount |
| 9843 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9844 | case PyUnicode_2BYTE_KIND: |
| 9845 | return ucs2lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9846 | self, PyUnicode_2BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9847 | PyUnicode_GET_LENGTH(self), maxcount |
| 9848 | ); |
| 9849 | case PyUnicode_4BYTE_KIND: |
| 9850 | return ucs4lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9851 | self, PyUnicode_4BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9852 | PyUnicode_GET_LENGTH(self), maxcount |
| 9853 | ); |
| 9854 | default: |
| 9855 | assert(0); |
| 9856 | return NULL; |
| 9857 | } |
| 9858 | |
| 9859 | if (PyUnicode_READY(substring) == -1) |
| 9860 | return NULL; |
| 9861 | |
| 9862 | kind1 = PyUnicode_KIND(self); |
| 9863 | kind2 = PyUnicode_KIND(substring); |
| 9864 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9865 | buf1 = PyUnicode_DATA(self); |
| 9866 | buf2 = PyUnicode_DATA(substring); |
| 9867 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9868 | buf1 = _PyUnicode_AsKind(self, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9869 | if (!buf1) |
| 9870 | return NULL; |
| 9871 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9872 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9873 | if (!buf2) { |
| 9874 | if (kind1 != kind) PyMem_Free(buf1); |
| 9875 | return NULL; |
| 9876 | } |
| 9877 | len1 = PyUnicode_GET_LENGTH(self); |
| 9878 | len2 = PyUnicode_GET_LENGTH(substring); |
| 9879 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9880 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9881 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9882 | if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) |
| 9883 | out = asciilib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9884 | self, buf1, len1, buf2, len2, maxcount); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9885 | else |
| 9886 | out = ucs1lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9887 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9888 | break; |
| 9889 | case PyUnicode_2BYTE_KIND: |
| 9890 | out = ucs2lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9891 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9892 | break; |
| 9893 | case PyUnicode_4BYTE_KIND: |
| 9894 | out = ucs4lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9895 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9896 | break; |
| 9897 | default: |
| 9898 | out = NULL; |
| 9899 | } |
| 9900 | if (kind1 != kind) |
| 9901 | PyMem_Free(buf1); |
| 9902 | if (kind2 != kind) |
| 9903 | PyMem_Free(buf2); |
| 9904 | return out; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9905 | } |
| 9906 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9907 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9908 | rsplit(PyObject *self, |
| 9909 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9910 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9911 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9912 | int kind1, kind2, kind; |
| 9913 | void *buf1, *buf2; |
| 9914 | Py_ssize_t len1, len2; |
| 9915 | PyObject* out; |
| 9916 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9917 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9918 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9919 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9920 | if (PyUnicode_READY(self) == -1) |
| 9921 | return NULL; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9922 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9923 | if (substring == NULL) |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9924 | switch (PyUnicode_KIND(self)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9925 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9926 | if (PyUnicode_IS_ASCII(self)) |
| 9927 | return asciilib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9928 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9929 | PyUnicode_GET_LENGTH(self), maxcount |
| 9930 | ); |
| 9931 | else |
| 9932 | return ucs1lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9933 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9934 | PyUnicode_GET_LENGTH(self), maxcount |
| 9935 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9936 | case PyUnicode_2BYTE_KIND: |
| 9937 | return ucs2lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9938 | self, PyUnicode_2BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9939 | PyUnicode_GET_LENGTH(self), maxcount |
| 9940 | ); |
| 9941 | case PyUnicode_4BYTE_KIND: |
| 9942 | return ucs4lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9943 | self, PyUnicode_4BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9944 | PyUnicode_GET_LENGTH(self), maxcount |
| 9945 | ); |
| 9946 | default: |
| 9947 | assert(0); |
| 9948 | return NULL; |
| 9949 | } |
| 9950 | |
| 9951 | if (PyUnicode_READY(substring) == -1) |
| 9952 | return NULL; |
| 9953 | |
| 9954 | kind1 = PyUnicode_KIND(self); |
| 9955 | kind2 = PyUnicode_KIND(substring); |
| 9956 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9957 | buf1 = PyUnicode_DATA(self); |
| 9958 | buf2 = PyUnicode_DATA(substring); |
| 9959 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9960 | buf1 = _PyUnicode_AsKind(self, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9961 | if (!buf1) |
| 9962 | return NULL; |
| 9963 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9964 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9965 | if (!buf2) { |
| 9966 | if (kind1 != kind) PyMem_Free(buf1); |
| 9967 | return NULL; |
| 9968 | } |
| 9969 | len1 = PyUnicode_GET_LENGTH(self); |
| 9970 | len2 = PyUnicode_GET_LENGTH(substring); |
| 9971 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9972 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9973 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9974 | if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) |
| 9975 | out = asciilib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9976 | self, buf1, len1, buf2, len2, maxcount); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9977 | else |
| 9978 | out = ucs1lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9979 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9980 | break; |
| 9981 | case PyUnicode_2BYTE_KIND: |
| 9982 | out = ucs2lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9983 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9984 | break; |
| 9985 | case PyUnicode_4BYTE_KIND: |
| 9986 | out = ucs4lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9987 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9988 | break; |
| 9989 | default: |
| 9990 | out = NULL; |
| 9991 | } |
| 9992 | if (kind1 != kind) |
| 9993 | PyMem_Free(buf1); |
| 9994 | if (kind2 != kind) |
| 9995 | PyMem_Free(buf2); |
| 9996 | return out; |
| 9997 | } |
| 9998 | |
| 9999 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10000 | anylib_find(int kind, PyObject *str1, void *buf1, Py_ssize_t len1, |
| 10001 | 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] | 10002 | { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10003 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10004 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10005 | if (PyUnicode_IS_ASCII(str1) && PyUnicode_IS_ASCII(str2)) |
| 10006 | return asciilib_find(buf1, len1, buf2, len2, offset); |
| 10007 | else |
| 10008 | return ucs1lib_find(buf1, len1, buf2, len2, offset); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10009 | case PyUnicode_2BYTE_KIND: |
| 10010 | return ucs2lib_find(buf1, len1, buf2, len2, offset); |
| 10011 | case PyUnicode_4BYTE_KIND: |
| 10012 | return ucs4lib_find(buf1, len1, buf2, len2, offset); |
| 10013 | } |
| 10014 | assert(0); |
| 10015 | return -1; |
| 10016 | } |
| 10017 | |
| 10018 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10019 | anylib_count(int kind, PyObject *sstr, void* sbuf, Py_ssize_t slen, |
| 10020 | 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] | 10021 | { |
Benjamin Peterson | c0b95d1 | 2011-12-20 17:24:05 -0600 | [diff] [blame] | 10022 | switch (kind) { |
| 10023 | case PyUnicode_1BYTE_KIND: |
| 10024 | if (PyUnicode_IS_ASCII(sstr) && PyUnicode_IS_ASCII(str1)) |
| 10025 | return asciilib_count(sbuf, slen, buf1, len1, maxcount); |
| 10026 | else |
| 10027 | return ucs1lib_count(sbuf, slen, buf1, len1, maxcount); |
| 10028 | case PyUnicode_2BYTE_KIND: |
| 10029 | return ucs2lib_count(sbuf, slen, buf1, len1, maxcount); |
| 10030 | case PyUnicode_4BYTE_KIND: |
| 10031 | return ucs4lib_count(sbuf, slen, buf1, len1, maxcount); |
| 10032 | } |
| 10033 | assert(0); |
| 10034 | return 0; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10035 | } |
| 10036 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10037 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10038 | replace(PyObject *self, PyObject *str1, |
| 10039 | PyObject *str2, Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10040 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10041 | PyObject *u; |
| 10042 | char *sbuf = PyUnicode_DATA(self); |
| 10043 | char *buf1 = PyUnicode_DATA(str1); |
| 10044 | char *buf2 = PyUnicode_DATA(str2); |
| 10045 | int srelease = 0, release1 = 0, release2 = 0; |
| 10046 | int skind = PyUnicode_KIND(self); |
| 10047 | int kind1 = PyUnicode_KIND(str1); |
| 10048 | int kind2 = PyUnicode_KIND(str2); |
| 10049 | Py_ssize_t slen = PyUnicode_GET_LENGTH(self); |
| 10050 | Py_ssize_t len1 = PyUnicode_GET_LENGTH(str1); |
| 10051 | Py_ssize_t len2 = PyUnicode_GET_LENGTH(str2); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10052 | int mayshrink; |
| 10053 | Py_UCS4 maxchar, maxchar_str2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10054 | |
| 10055 | if (maxcount < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10056 | maxcount = PY_SSIZE_T_MAX; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10057 | else if (maxcount == 0 || slen == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10058 | goto nothing; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10059 | |
Victor Stinner | 59de0ee | 2011-10-07 10:01:28 +0200 | [diff] [blame] | 10060 | if (str1 == str2) |
| 10061 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10062 | if (skind < kind1) |
| 10063 | /* substring too wide to be present */ |
| 10064 | goto nothing; |
| 10065 | |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10066 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
| 10067 | maxchar_str2 = PyUnicode_MAX_CHAR_VALUE(str2); |
| 10068 | /* Replacing str1 with str2 may cause a maxchar reduction in the |
| 10069 | result string. */ |
| 10070 | mayshrink = (maxchar_str2 < maxchar); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 10071 | maxchar = MAX_MAXCHAR(maxchar, maxchar_str2); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10072 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10073 | if (len1 == len2) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10074 | /* same length */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10075 | if (len1 == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10076 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10077 | if (len1 == 1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10078 | /* replace characters */ |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10079 | Py_UCS4 u1, u2; |
| 10080 | int rkind; |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10081 | Py_ssize_t index, pos; |
| 10082 | char *src; |
| 10083 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10084 | u1 = PyUnicode_READ_CHAR(str1, 0); |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10085 | pos = findchar(sbuf, PyUnicode_KIND(self), slen, u1, 1); |
| 10086 | if (pos < 0) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10087 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10088 | u2 = PyUnicode_READ_CHAR(str2, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10089 | u = PyUnicode_New(slen, maxchar); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10090 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10091 | goto error; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 10092 | _PyUnicode_FastCopyCharacters(u, 0, self, 0, slen); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10093 | rkind = PyUnicode_KIND(u); |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10094 | |
| 10095 | PyUnicode_WRITE(rkind, PyUnicode_DATA(u), pos, u2); |
| 10096 | index = 0; |
| 10097 | src = sbuf; |
| 10098 | while (--maxcount) |
| 10099 | { |
| 10100 | pos++; |
| 10101 | src += pos * PyUnicode_KIND(self); |
| 10102 | slen -= pos; |
| 10103 | index += pos; |
| 10104 | pos = findchar(src, PyUnicode_KIND(self), slen, u1, 1); |
| 10105 | if (pos < 0) |
| 10106 | break; |
| 10107 | PyUnicode_WRITE(rkind, PyUnicode_DATA(u), index + pos, u2); |
| 10108 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10109 | } |
| 10110 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10111 | int rkind = skind; |
| 10112 | char *res; |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10113 | Py_ssize_t i; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10114 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10115 | if (kind1 < rkind) { |
| 10116 | /* widen substring */ |
| 10117 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10118 | if (!buf1) goto error; |
| 10119 | release1 = 1; |
| 10120 | } |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10121 | i = anylib_find(rkind, self, sbuf, slen, str1, buf1, len1, 0); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10122 | if (i < 0) |
| 10123 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10124 | if (rkind > kind2) { |
| 10125 | /* widen replacement */ |
| 10126 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 10127 | if (!buf2) goto error; |
| 10128 | release2 = 1; |
| 10129 | } |
| 10130 | else if (rkind < kind2) { |
| 10131 | /* widen self and buf1 */ |
| 10132 | rkind = kind2; |
| 10133 | if (release1) PyMem_Free(buf1); |
| 10134 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 10135 | if (!sbuf) goto error; |
| 10136 | srelease = 1; |
| 10137 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10138 | if (!buf1) goto error; |
| 10139 | release1 = 1; |
| 10140 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10141 | u = PyUnicode_New(slen, maxchar); |
| 10142 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10143 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10144 | assert(PyUnicode_KIND(u) == rkind); |
| 10145 | res = PyUnicode_DATA(u); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10146 | |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10147 | memcpy(res, sbuf, rkind * slen); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10148 | /* change everything in-place, starting with this one */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10149 | memcpy(res + rkind * i, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10150 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10151 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10152 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10153 | |
| 10154 | while ( --maxcount > 0) { |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10155 | i = anylib_find(rkind, self, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10156 | sbuf+rkind*i, slen-i, |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10157 | str1, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10158 | if (i == -1) |
| 10159 | break; |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10160 | memcpy(res + rkind * i, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10161 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10162 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10163 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10164 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10165 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10166 | } |
| 10167 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10168 | Py_ssize_t n, i, j, ires; |
| 10169 | Py_ssize_t product, new_size; |
| 10170 | int rkind = skind; |
| 10171 | char *res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10172 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10173 | if (kind1 < rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10174 | /* widen substring */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10175 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10176 | if (!buf1) goto error; |
| 10177 | release1 = 1; |
| 10178 | } |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10179 | n = anylib_count(rkind, self, sbuf, slen, str1, buf1, len1, maxcount); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10180 | if (n == 0) |
| 10181 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10182 | if (kind2 < rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10183 | /* widen replacement */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10184 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 10185 | if (!buf2) goto error; |
| 10186 | release2 = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10187 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10188 | else if (kind2 > rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10189 | /* widen self and buf1 */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10190 | rkind = kind2; |
| 10191 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 10192 | if (!sbuf) goto error; |
| 10193 | srelease = 1; |
| 10194 | if (release1) PyMem_Free(buf1); |
| 10195 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10196 | if (!buf1) goto error; |
| 10197 | release1 = 1; |
| 10198 | } |
| 10199 | /* new_size = PyUnicode_GET_LENGTH(self) + n * (PyUnicode_GET_LENGTH(str2) - |
| 10200 | PyUnicode_GET_LENGTH(str1))); */ |
| 10201 | product = n * (len2-len1); |
| 10202 | if ((product / (len2-len1)) != n) { |
| 10203 | PyErr_SetString(PyExc_OverflowError, |
| 10204 | "replace string is too long"); |
| 10205 | goto error; |
| 10206 | } |
| 10207 | new_size = slen + product; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10208 | if (new_size == 0) { |
| 10209 | Py_INCREF(unicode_empty); |
| 10210 | u = unicode_empty; |
| 10211 | goto done; |
| 10212 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10213 | if (new_size < 0 || new_size > (PY_SSIZE_T_MAX >> (rkind-1))) { |
| 10214 | PyErr_SetString(PyExc_OverflowError, |
| 10215 | "replace string is too long"); |
| 10216 | goto error; |
| 10217 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10218 | u = PyUnicode_New(new_size, maxchar); |
| 10219 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10220 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10221 | assert(PyUnicode_KIND(u) == rkind); |
| 10222 | res = PyUnicode_DATA(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10223 | ires = i = 0; |
| 10224 | if (len1 > 0) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10225 | while (n-- > 0) { |
| 10226 | /* look for next match */ |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10227 | j = anylib_find(rkind, self, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10228 | sbuf + rkind * i, slen-i, |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10229 | str1, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10230 | if (j == -1) |
| 10231 | break; |
| 10232 | else if (j > i) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10233 | /* copy unchanged part [i:j] */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10234 | memcpy(res + rkind * ires, |
| 10235 | sbuf + rkind * i, |
| 10236 | rkind * (j-i)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10237 | ires += j - i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10238 | } |
| 10239 | /* copy substitution string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10240 | if (len2 > 0) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10241 | memcpy(res + rkind * ires, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10242 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10243 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10244 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10245 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10246 | i = j + len1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10247 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10248 | if (i < slen) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10249 | /* copy tail [i:] */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10250 | memcpy(res + rkind * ires, |
| 10251 | sbuf + rkind * i, |
| 10252 | rkind * (slen-i)); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10253 | } |
| 10254 | else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10255 | /* interleave */ |
| 10256 | while (n > 0) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10257 | memcpy(res + rkind * ires, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10258 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10259 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10260 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10261 | if (--n <= 0) |
| 10262 | break; |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10263 | memcpy(res + rkind * ires, |
| 10264 | sbuf + rkind * i, |
| 10265 | rkind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10266 | ires++; |
| 10267 | i++; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10268 | } |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10269 | memcpy(res + rkind * ires, |
| 10270 | sbuf + rkind * i, |
| 10271 | rkind * (slen-i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10272 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10273 | } |
| 10274 | |
| 10275 | if (mayshrink) { |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10276 | unicode_adjust_maxchar(&u); |
| 10277 | if (u == NULL) |
| 10278 | goto error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10279 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10280 | |
| 10281 | done: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10282 | if (srelease) |
| 10283 | PyMem_FREE(sbuf); |
| 10284 | if (release1) |
| 10285 | PyMem_FREE(buf1); |
| 10286 | if (release2) |
| 10287 | PyMem_FREE(buf2); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10288 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10289 | return u; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10290 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10291 | nothing: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10292 | /* nothing to replace; return original string (when possible) */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10293 | if (srelease) |
| 10294 | PyMem_FREE(sbuf); |
| 10295 | if (release1) |
| 10296 | PyMem_FREE(buf1); |
| 10297 | if (release2) |
| 10298 | PyMem_FREE(buf2); |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10299 | return unicode_result_unchanged(self); |
| 10300 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10301 | error: |
| 10302 | if (srelease && sbuf) |
| 10303 | PyMem_FREE(sbuf); |
| 10304 | if (release1 && buf1) |
| 10305 | PyMem_FREE(buf1); |
| 10306 | if (release2 && buf2) |
| 10307 | PyMem_FREE(buf2); |
| 10308 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10309 | } |
| 10310 | |
| 10311 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 10312 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10313 | PyDoc_STRVAR(title__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10314 | "S.title() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10315 | \n\ |
| 10316 | 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] | 10317 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10318 | |
| 10319 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10320 | unicode_title(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10321 | { |
Benjamin Peterson | eea4846 | 2012-01-16 14:28:50 -0500 | [diff] [blame] | 10322 | if (PyUnicode_READY(self) == -1) |
| 10323 | return NULL; |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 10324 | return case_operation(self, do_title); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10325 | } |
| 10326 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10327 | PyDoc_STRVAR(capitalize__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10328 | "S.capitalize() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10329 | \n\ |
| 10330 | 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] | 10331 | have upper case and the rest lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10332 | |
| 10333 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10334 | unicode_capitalize(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10335 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 10336 | if (PyUnicode_READY(self) == -1) |
| 10337 | return NULL; |
| 10338 | if (PyUnicode_GET_LENGTH(self) == 0) |
| 10339 | return unicode_result_unchanged(self); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 10340 | return case_operation(self, do_capitalize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10341 | } |
| 10342 | |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 10343 | PyDoc_STRVAR(casefold__doc__, |
| 10344 | "S.casefold() -> str\n\ |
| 10345 | \n\ |
| 10346 | Return a version of S suitable for caseless comparisons."); |
| 10347 | |
| 10348 | static PyObject * |
| 10349 | unicode_casefold(PyObject *self) |
| 10350 | { |
| 10351 | if (PyUnicode_READY(self) == -1) |
| 10352 | return NULL; |
| 10353 | if (PyUnicode_IS_ASCII(self)) |
| 10354 | return ascii_upper_or_lower(self, 1); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 10355 | return case_operation(self, do_casefold); |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 10356 | } |
| 10357 | |
| 10358 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10359 | /* Argument converter. Coerces to a single unicode character */ |
| 10360 | |
| 10361 | static int |
| 10362 | convert_uc(PyObject *obj, void *addr) |
| 10363 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10364 | Py_UCS4 *fillcharloc = (Py_UCS4 *)addr; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10365 | PyObject *uniobj; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10366 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10367 | uniobj = PyUnicode_FromObject(obj); |
| 10368 | if (uniobj == NULL) { |
| 10369 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10370 | "The fill character cannot be converted to Unicode"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10371 | return 0; |
| 10372 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10373 | if (PyUnicode_GET_LENGTH(uniobj) != 1) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10374 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10375 | "The fill character must be exactly one character long"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10376 | Py_DECREF(uniobj); |
| 10377 | return 0; |
| 10378 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10379 | *fillcharloc = PyUnicode_READ_CHAR(uniobj, 0); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10380 | Py_DECREF(uniobj); |
| 10381 | return 1; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10382 | } |
| 10383 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10384 | PyDoc_STRVAR(center__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10385 | "S.center(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10386 | \n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10387 | Return S centered in a string of length width. Padding is\n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10388 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10389 | |
| 10390 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10391 | unicode_center(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10392 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10393 | Py_ssize_t marg, left; |
| 10394 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10395 | Py_UCS4 fillchar = ' '; |
| 10396 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 10397 | 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] | 10398 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10399 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 10400 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10401 | return NULL; |
| 10402 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10403 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 10404 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10405 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10406 | marg = width - PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10407 | left = marg / 2 + (marg & width & 1); |
| 10408 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10409 | return pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10410 | } |
| 10411 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10412 | /* This function assumes that str1 and str2 are readied by the caller. */ |
| 10413 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10414 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10415 | unicode_compare(PyObject *str1, PyObject *str2) |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10416 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10417 | int kind1, kind2; |
| 10418 | void *data1, *data2; |
| 10419 | Py_ssize_t len1, len2, i; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10420 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10421 | kind1 = PyUnicode_KIND(str1); |
| 10422 | kind2 = PyUnicode_KIND(str2); |
| 10423 | data1 = PyUnicode_DATA(str1); |
| 10424 | data2 = PyUnicode_DATA(str2); |
| 10425 | len1 = PyUnicode_GET_LENGTH(str1); |
| 10426 | len2 = PyUnicode_GET_LENGTH(str2); |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10427 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10428 | for (i = 0; i < len1 && i < len2; ++i) { |
| 10429 | Py_UCS4 c1, c2; |
| 10430 | c1 = PyUnicode_READ(kind1, data1, i); |
| 10431 | c2 = PyUnicode_READ(kind2, data2, i); |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 10432 | |
| 10433 | if (c1 != c2) |
| 10434 | return (c1 < c2) ? -1 : 1; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10435 | } |
| 10436 | |
| 10437 | return (len1 < len2) ? -1 : (len1 != len2); |
| 10438 | } |
| 10439 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10440 | int |
| 10441 | PyUnicode_Compare(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10442 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10443 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 10444 | if (PyUnicode_READY(left) == -1 || |
| 10445 | PyUnicode_READY(right) == -1) |
| 10446 | return -1; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10447 | return unicode_compare(left, right); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10448 | } |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 10449 | PyErr_Format(PyExc_TypeError, |
| 10450 | "Can't compare %.100s and %.100s", |
| 10451 | left->ob_type->tp_name, |
| 10452 | right->ob_type->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10453 | return -1; |
| 10454 | } |
| 10455 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10456 | int |
| 10457 | PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str) |
| 10458 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10459 | Py_ssize_t i; |
| 10460 | int kind; |
| 10461 | void *data; |
| 10462 | Py_UCS4 chr; |
| 10463 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 10464 | assert(_PyUnicode_CHECK(uni)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10465 | if (PyUnicode_READY(uni) == -1) |
| 10466 | return -1; |
| 10467 | kind = PyUnicode_KIND(uni); |
| 10468 | data = PyUnicode_DATA(uni); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10469 | /* Compare Unicode string and source character set string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10470 | for (i = 0; (chr = PyUnicode_READ(kind, data, i)) && str[i]; i++) |
| 10471 | if (chr != str[i]) |
| 10472 | return (chr < (unsigned char)(str[i])) ? -1 : 1; |
Benjamin Peterson | 8667a9b | 2010-01-09 21:45:28 +0000 | [diff] [blame] | 10473 | /* This check keeps Python strings that end in '\0' from comparing equal |
| 10474 | to C strings identical up to that point. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10475 | if (PyUnicode_GET_LENGTH(uni) != i || chr) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10476 | return 1; /* uni is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10477 | if (str[i]) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10478 | return -1; /* str is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10479 | return 0; |
| 10480 | } |
| 10481 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10482 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10483 | #define TEST_COND(cond) \ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10484 | ((cond) ? Py_True : Py_False) |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10485 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10486 | PyObject * |
| 10487 | PyUnicode_RichCompare(PyObject *left, PyObject *right, int op) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10488 | { |
| 10489 | int result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10490 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10491 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 10492 | PyObject *v; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10493 | if (PyUnicode_READY(left) == -1 || |
| 10494 | PyUnicode_READY(right) == -1) |
| 10495 | return NULL; |
| 10496 | if (PyUnicode_GET_LENGTH(left) != PyUnicode_GET_LENGTH(right) || |
| 10497 | PyUnicode_KIND(left) != PyUnicode_KIND(right)) { |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10498 | if (op == Py_EQ) { |
| 10499 | Py_INCREF(Py_False); |
| 10500 | return Py_False; |
| 10501 | } |
| 10502 | if (op == Py_NE) { |
| 10503 | Py_INCREF(Py_True); |
| 10504 | return Py_True; |
| 10505 | } |
| 10506 | } |
| 10507 | if (left == right) |
| 10508 | result = 0; |
| 10509 | else |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10510 | result = unicode_compare(left, right); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10511 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10512 | /* Convert the return value to a Boolean */ |
| 10513 | switch (op) { |
| 10514 | case Py_EQ: |
| 10515 | v = TEST_COND(result == 0); |
| 10516 | break; |
| 10517 | case Py_NE: |
| 10518 | v = TEST_COND(result != 0); |
| 10519 | break; |
| 10520 | case Py_LE: |
| 10521 | v = TEST_COND(result <= 0); |
| 10522 | break; |
| 10523 | case Py_GE: |
| 10524 | v = TEST_COND(result >= 0); |
| 10525 | break; |
| 10526 | case Py_LT: |
| 10527 | v = TEST_COND(result == -1); |
| 10528 | break; |
| 10529 | case Py_GT: |
| 10530 | v = TEST_COND(result == 1); |
| 10531 | break; |
| 10532 | default: |
| 10533 | PyErr_BadArgument(); |
| 10534 | return NULL; |
| 10535 | } |
| 10536 | Py_INCREF(v); |
| 10537 | return v; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10538 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10539 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 10540 | Py_RETURN_NOTIMPLEMENTED; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10541 | } |
| 10542 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10543 | int |
| 10544 | PyUnicode_Contains(PyObject *container, PyObject *element) |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10545 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10546 | PyObject *str, *sub; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10547 | int kind1, kind2, kind; |
| 10548 | void *buf1, *buf2; |
| 10549 | Py_ssize_t len1, len2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10550 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10551 | |
| 10552 | /* Coerce the two arguments */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10553 | sub = PyUnicode_FromObject(element); |
| 10554 | if (!sub) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10555 | PyErr_Format(PyExc_TypeError, |
| 10556 | "'in <string>' requires string as left operand, not %s", |
| 10557 | element->ob_type->tp_name); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10558 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10559 | } |
| 10560 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10561 | str = PyUnicode_FromObject(container); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 10562 | if (!str) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10563 | Py_DECREF(sub); |
| 10564 | return -1; |
| 10565 | } |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 10566 | if (PyUnicode_READY(sub) == -1 || PyUnicode_READY(str) == -1) { |
| 10567 | Py_DECREF(sub); |
| 10568 | Py_DECREF(str); |
| 10569 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10570 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10571 | kind1 = PyUnicode_KIND(str); |
| 10572 | kind2 = PyUnicode_KIND(sub); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 10573 | kind = kind1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10574 | buf1 = PyUnicode_DATA(str); |
| 10575 | buf2 = PyUnicode_DATA(sub); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 10576 | if (kind2 != kind) { |
Antoine Pitrou | 758153b | 2012-05-12 15:51:51 +0200 | [diff] [blame] | 10577 | if (kind2 > kind) { |
| 10578 | Py_DECREF(sub); |
| 10579 | Py_DECREF(str); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 10580 | return 0; |
Antoine Pitrou | 758153b | 2012-05-12 15:51:51 +0200 | [diff] [blame] | 10581 | } |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10582 | buf2 = _PyUnicode_AsKind(sub, kind); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 10583 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10584 | if (!buf2) { |
| 10585 | Py_DECREF(sub); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 10586 | Py_DECREF(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10587 | return -1; |
| 10588 | } |
| 10589 | len1 = PyUnicode_GET_LENGTH(str); |
| 10590 | len2 = PyUnicode_GET_LENGTH(sub); |
| 10591 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10592 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10593 | case PyUnicode_1BYTE_KIND: |
| 10594 | result = ucs1lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10595 | break; |
| 10596 | case PyUnicode_2BYTE_KIND: |
| 10597 | result = ucs2lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10598 | break; |
| 10599 | case PyUnicode_4BYTE_KIND: |
| 10600 | result = ucs4lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10601 | break; |
| 10602 | default: |
| 10603 | result = -1; |
| 10604 | assert(0); |
| 10605 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10606 | |
| 10607 | Py_DECREF(str); |
| 10608 | Py_DECREF(sub); |
| 10609 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10610 | if (kind2 != kind) |
| 10611 | PyMem_Free(buf2); |
| 10612 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10613 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10614 | } |
| 10615 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10616 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 10617 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10618 | PyObject * |
| 10619 | PyUnicode_Concat(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10620 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10621 | PyObject *u = NULL, *v = NULL, *w; |
Victor Stinner | 127226b | 2011-10-13 01:12:34 +0200 | [diff] [blame] | 10622 | Py_UCS4 maxchar, maxchar2; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10623 | Py_ssize_t u_len, v_len, new_len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10624 | |
| 10625 | /* Coerce the two arguments */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10626 | u = PyUnicode_FromObject(left); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10627 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10628 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10629 | v = PyUnicode_FromObject(right); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10630 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10631 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10632 | |
| 10633 | /* Shortcuts */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 10634 | if (v == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10635 | Py_DECREF(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10636 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10637 | } |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 10638 | if (u == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10639 | Py_DECREF(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10640 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10641 | } |
| 10642 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10643 | u_len = PyUnicode_GET_LENGTH(u); |
| 10644 | v_len = PyUnicode_GET_LENGTH(v); |
| 10645 | if (u_len > PY_SSIZE_T_MAX - v_len) { |
| 10646 | PyErr_SetString(PyExc_OverflowError, |
| 10647 | "strings are too large to concat"); |
| 10648 | goto onError; |
| 10649 | } |
| 10650 | new_len = u_len + v_len; |
| 10651 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10652 | maxchar = PyUnicode_MAX_CHAR_VALUE(u); |
Victor Stinner | 127226b | 2011-10-13 01:12:34 +0200 | [diff] [blame] | 10653 | maxchar2 = PyUnicode_MAX_CHAR_VALUE(v); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 10654 | maxchar = MAX_MAXCHAR(maxchar, maxchar2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10655 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10656 | /* Concat the two Unicode strings */ |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10657 | w = PyUnicode_New(new_len, maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10658 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10659 | goto onError; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 10660 | _PyUnicode_FastCopyCharacters(w, 0, u, 0, u_len); |
| 10661 | _PyUnicode_FastCopyCharacters(w, u_len, v, 0, v_len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10662 | Py_DECREF(u); |
| 10663 | Py_DECREF(v); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10664 | assert(_PyUnicode_CheckConsistency(w, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10665 | return w; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10666 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10667 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10668 | Py_XDECREF(u); |
| 10669 | Py_XDECREF(v); |
| 10670 | return NULL; |
| 10671 | } |
| 10672 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10673 | void |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10674 | PyUnicode_Append(PyObject **p_left, PyObject *right) |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10675 | { |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10676 | PyObject *left, *res; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10677 | Py_UCS4 maxchar, maxchar2; |
| 10678 | Py_ssize_t left_len, right_len, new_len; |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10679 | |
| 10680 | if (p_left == NULL) { |
| 10681 | if (!PyErr_Occurred()) |
| 10682 | PyErr_BadInternalCall(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10683 | return; |
| 10684 | } |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10685 | left = *p_left; |
| 10686 | if (right == NULL || !PyUnicode_Check(left)) { |
| 10687 | if (!PyErr_Occurred()) |
| 10688 | PyErr_BadInternalCall(); |
| 10689 | goto error; |
| 10690 | } |
| 10691 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 10692 | if (PyUnicode_READY(left) == -1) |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 10693 | goto error; |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 10694 | if (PyUnicode_READY(right) == -1) |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 10695 | goto error; |
| 10696 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10697 | /* Shortcuts */ |
| 10698 | if (left == unicode_empty) { |
| 10699 | Py_DECREF(left); |
| 10700 | Py_INCREF(right); |
| 10701 | *p_left = right; |
| 10702 | return; |
| 10703 | } |
| 10704 | if (right == unicode_empty) |
| 10705 | return; |
| 10706 | |
| 10707 | left_len = PyUnicode_GET_LENGTH(left); |
| 10708 | right_len = PyUnicode_GET_LENGTH(right); |
| 10709 | if (left_len > PY_SSIZE_T_MAX - right_len) { |
| 10710 | PyErr_SetString(PyExc_OverflowError, |
| 10711 | "strings are too large to concat"); |
| 10712 | goto error; |
| 10713 | } |
| 10714 | new_len = left_len + right_len; |
| 10715 | |
| 10716 | if (unicode_modifiable(left) |
| 10717 | && PyUnicode_CheckExact(right) |
| 10718 | && PyUnicode_KIND(right) <= PyUnicode_KIND(left) |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10719 | /* Don't resize for ascii += latin1. Convert ascii to latin1 requires |
| 10720 | to change the structure size, but characters are stored just after |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 10721 | the structure, and so it requires to move all characters which is |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10722 | not so different than duplicating the string. */ |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10723 | && !(PyUnicode_IS_ASCII(left) && !PyUnicode_IS_ASCII(right))) |
| 10724 | { |
| 10725 | /* append inplace */ |
| 10726 | if (unicode_resize(p_left, new_len) != 0) { |
| 10727 | /* XXX if _PyUnicode_Resize() fails, 'left' has been |
| 10728 | * deallocated so it cannot be put back into |
| 10729 | * 'variable'. The MemoryError is raised when there |
| 10730 | * is no value in 'variable', which might (very |
| 10731 | * remotely) be a cause of incompatibilities. |
| 10732 | */ |
| 10733 | goto error; |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10734 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10735 | /* copy 'right' into the newly allocated area of 'left' */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 10736 | _PyUnicode_FastCopyCharacters(*p_left, left_len, right, 0, right_len); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10737 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10738 | else { |
| 10739 | maxchar = PyUnicode_MAX_CHAR_VALUE(left); |
| 10740 | maxchar2 = PyUnicode_MAX_CHAR_VALUE(right); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 10741 | maxchar = MAX_MAXCHAR(maxchar, maxchar2); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10742 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10743 | /* Concat the two Unicode strings */ |
| 10744 | res = PyUnicode_New(new_len, maxchar); |
| 10745 | if (res == NULL) |
| 10746 | goto error; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 10747 | _PyUnicode_FastCopyCharacters(res, 0, left, 0, left_len); |
| 10748 | _PyUnicode_FastCopyCharacters(res, left_len, right, 0, right_len); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10749 | Py_DECREF(left); |
| 10750 | *p_left = res; |
| 10751 | } |
| 10752 | assert(_PyUnicode_CheckConsistency(*p_left, 1)); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10753 | return; |
| 10754 | |
| 10755 | error: |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10756 | Py_CLEAR(*p_left); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10757 | } |
| 10758 | |
| 10759 | void |
| 10760 | PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right) |
| 10761 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10762 | PyUnicode_Append(pleft, right); |
| 10763 | Py_XDECREF(right); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10764 | } |
| 10765 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10766 | PyDoc_STRVAR(count__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10767 | "S.count(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10768 | \n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10769 | Return the number of non-overlapping occurrences of substring sub in\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10770 | 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] | 10771 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10772 | |
| 10773 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10774 | unicode_count(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10775 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10776 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10777 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10778 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10779 | PyObject *result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10780 | int kind1, kind2, kind; |
| 10781 | void *buf1, *buf2; |
| 10782 | Py_ssize_t len1, len2, iresult; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10783 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10784 | if (!stringlib_parse_args_finds_unicode("count", args, &substring, |
| 10785 | &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10786 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10787 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10788 | kind1 = PyUnicode_KIND(self); |
| 10789 | kind2 = PyUnicode_KIND(substring); |
Benjamin Peterson | b63f49f | 2012-05-03 18:31:07 -0400 | [diff] [blame] | 10790 | if (kind2 > kind1) |
| 10791 | return PyLong_FromLong(0); |
| 10792 | kind = kind1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10793 | buf1 = PyUnicode_DATA(self); |
| 10794 | buf2 = PyUnicode_DATA(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10795 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10796 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10797 | if (!buf2) { |
| 10798 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10799 | return NULL; |
| 10800 | } |
| 10801 | len1 = PyUnicode_GET_LENGTH(self); |
| 10802 | len2 = PyUnicode_GET_LENGTH(substring); |
| 10803 | |
| 10804 | ADJUST_INDICES(start, end, len1); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10805 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10806 | case PyUnicode_1BYTE_KIND: |
| 10807 | iresult = ucs1lib_count( |
| 10808 | ((Py_UCS1*)buf1) + start, end - start, |
| 10809 | buf2, len2, PY_SSIZE_T_MAX |
| 10810 | ); |
| 10811 | break; |
| 10812 | case PyUnicode_2BYTE_KIND: |
| 10813 | iresult = ucs2lib_count( |
| 10814 | ((Py_UCS2*)buf1) + start, end - start, |
| 10815 | buf2, len2, PY_SSIZE_T_MAX |
| 10816 | ); |
| 10817 | break; |
| 10818 | case PyUnicode_4BYTE_KIND: |
| 10819 | iresult = ucs4lib_count( |
| 10820 | ((Py_UCS4*)buf1) + start, end - start, |
| 10821 | buf2, len2, PY_SSIZE_T_MAX |
| 10822 | ); |
| 10823 | break; |
| 10824 | default: |
| 10825 | assert(0); iresult = 0; |
| 10826 | } |
| 10827 | |
| 10828 | result = PyLong_FromSsize_t(iresult); |
| 10829 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10830 | if (kind2 != kind) |
| 10831 | PyMem_Free(buf2); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10832 | |
| 10833 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10834 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10835 | return result; |
| 10836 | } |
| 10837 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10838 | PyDoc_STRVAR(encode__doc__, |
Victor Stinner | c911bbf | 2010-11-07 19:04:46 +0000 | [diff] [blame] | 10839 | "S.encode(encoding='utf-8', errors='strict') -> bytes\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10840 | \n\ |
Victor Stinner | e14e212 | 2010-11-07 18:41:46 +0000 | [diff] [blame] | 10841 | Encode S using the codec registered for encoding. Default encoding\n\ |
| 10842 | 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] | 10843 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 10844 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 10845 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 10846 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10847 | |
| 10848 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10849 | unicode_encode(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10850 | { |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 10851 | static char *kwlist[] = {"encoding", "errors", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10852 | char *encoding = NULL; |
| 10853 | char *errors = NULL; |
Guido van Rossum | 35d9428 | 2007-08-27 18:20:11 +0000 | [diff] [blame] | 10854 | |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 10855 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:encode", |
| 10856 | kwlist, &encoding, &errors)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10857 | return NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10858 | return PyUnicode_AsEncodedString(self, encoding, errors); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 10859 | } |
| 10860 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10861 | PyDoc_STRVAR(expandtabs__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10862 | "S.expandtabs([tabsize]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10863 | \n\ |
| 10864 | 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] | 10865 | 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] | 10866 | |
| 10867 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10868 | unicode_expandtabs(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10869 | { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10870 | Py_ssize_t i, j, line_pos, src_len, incr; |
| 10871 | Py_UCS4 ch; |
| 10872 | PyObject *u; |
| 10873 | void *src_data, *dest_data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10874 | int tabsize = 8; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10875 | int kind; |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10876 | int found; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10877 | |
| 10878 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10879 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10880 | |
Antoine Pitrou | 2242522 | 2011-10-04 19:10:51 +0200 | [diff] [blame] | 10881 | if (PyUnicode_READY(self) == -1) |
| 10882 | return NULL; |
| 10883 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 10884 | /* First pass: determine size of output string */ |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10885 | src_len = PyUnicode_GET_LENGTH(self); |
| 10886 | i = j = line_pos = 0; |
| 10887 | kind = PyUnicode_KIND(self); |
| 10888 | src_data = PyUnicode_DATA(self); |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10889 | found = 0; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10890 | for (; i < src_len; i++) { |
| 10891 | ch = PyUnicode_READ(kind, src_data, i); |
| 10892 | if (ch == '\t') { |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10893 | found = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10894 | if (tabsize > 0) { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10895 | incr = tabsize - (line_pos % tabsize); /* cannot overflow */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10896 | if (j > PY_SSIZE_T_MAX - incr) |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10897 | goto overflow; |
| 10898 | line_pos += incr; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10899 | j += incr; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10900 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10901 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10902 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10903 | if (j > PY_SSIZE_T_MAX - 1) |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10904 | goto overflow; |
| 10905 | line_pos++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10906 | j++; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10907 | if (ch == '\n' || ch == '\r') |
| 10908 | line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10909 | } |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10910 | } |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10911 | if (!found) |
| 10912 | return unicode_result_unchanged(self); |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 10913 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10914 | /* Second pass: create output string and fill it */ |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10915 | u = PyUnicode_New(j, PyUnicode_MAX_CHAR_VALUE(self)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10916 | if (!u) |
| 10917 | return NULL; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10918 | dest_data = PyUnicode_DATA(u); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10919 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10920 | i = j = line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10921 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10922 | for (; i < src_len; i++) { |
| 10923 | ch = PyUnicode_READ(kind, src_data, i); |
| 10924 | if (ch == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10925 | if (tabsize > 0) { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10926 | incr = tabsize - (line_pos % tabsize); |
| 10927 | line_pos += incr; |
Victor Stinner | da79e63 | 2012-02-22 13:37:04 +0100 | [diff] [blame] | 10928 | FILL(kind, dest_data, ' ', j, incr); |
| 10929 | j += incr; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10930 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10931 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10932 | else { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10933 | line_pos++; |
| 10934 | PyUnicode_WRITE(kind, dest_data, j, ch); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10935 | j++; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10936 | if (ch == '\n' || ch == '\r') |
| 10937 | line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10938 | } |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10939 | } |
| 10940 | assert (j == PyUnicode_GET_LENGTH(u)); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 10941 | return unicode_result(u); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10942 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10943 | overflow: |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10944 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 10945 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10946 | } |
| 10947 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10948 | PyDoc_STRVAR(find__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10949 | "S.find(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10950 | \n\ |
| 10951 | Return the lowest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 10952 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10953 | arguments start and end are interpreted as in slice notation.\n\ |
| 10954 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10955 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10956 | |
| 10957 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10958 | unicode_find(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10959 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10960 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 10961 | Py_ssize_t start; |
| 10962 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10963 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10964 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10965 | if (!stringlib_parse_args_finds_unicode("find", args, &substring, |
| 10966 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10967 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10968 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10969 | if (PyUnicode_READY(self) == -1) |
| 10970 | return NULL; |
| 10971 | if (PyUnicode_READY(substring) == -1) |
| 10972 | return NULL; |
| 10973 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10974 | result = any_find_slice(1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10975 | |
| 10976 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10977 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10978 | if (result == -2) |
| 10979 | return NULL; |
| 10980 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 10981 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10982 | } |
| 10983 | |
| 10984 | static PyObject * |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 10985 | unicode_getitem(PyObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10986 | { |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 10987 | void *data; |
| 10988 | enum PyUnicode_Kind kind; |
| 10989 | Py_UCS4 ch; |
| 10990 | PyObject *res; |
| 10991 | |
| 10992 | if (!PyUnicode_Check(self) || PyUnicode_READY(self) == -1) { |
| 10993 | PyErr_BadArgument(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10994 | return NULL; |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 10995 | } |
| 10996 | if (index < 0 || index >= PyUnicode_GET_LENGTH(self)) { |
| 10997 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 10998 | return NULL; |
| 10999 | } |
| 11000 | kind = PyUnicode_KIND(self); |
| 11001 | data = PyUnicode_DATA(self); |
| 11002 | ch = PyUnicode_READ(kind, data, index); |
| 11003 | if (ch < 256) |
| 11004 | return get_latin1_char(ch); |
| 11005 | |
| 11006 | res = PyUnicode_New(1, ch); |
| 11007 | if (res == NULL) |
| 11008 | return NULL; |
| 11009 | kind = PyUnicode_KIND(res); |
| 11010 | data = PyUnicode_DATA(res); |
| 11011 | PyUnicode_WRITE(kind, data, 0, ch); |
| 11012 | assert(_PyUnicode_CheckConsistency(res, 1)); |
| 11013 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11014 | } |
| 11015 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11016 | /* Believe it or not, this produces the same value for ASCII strings |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 11017 | as bytes_hash(). */ |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 11018 | static Py_hash_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11019 | unicode_hash(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11020 | { |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11021 | Py_ssize_t len; |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 11022 | Py_uhash_t x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11023 | |
Benjamin Peterson | f6622c8 | 2012-04-09 14:53:07 -0400 | [diff] [blame] | 11024 | #ifdef Py_DEBUG |
Benjamin Peterson | 69e9727 | 2012-02-21 11:08:50 -0500 | [diff] [blame] | 11025 | assert(_Py_HashSecret_Initialized); |
Benjamin Peterson | f6622c8 | 2012-04-09 14:53:07 -0400 | [diff] [blame] | 11026 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11027 | if (_PyUnicode_HASH(self) != -1) |
| 11028 | return _PyUnicode_HASH(self); |
| 11029 | if (PyUnicode_READY(self) == -1) |
| 11030 | return -1; |
| 11031 | len = PyUnicode_GET_LENGTH(self); |
Georg Brandl | 16fa2a1 | 2012-02-21 00:50:13 +0100 | [diff] [blame] | 11032 | /* |
| 11033 | We make the hash of the empty string be 0, rather than using |
| 11034 | (prefix ^ suffix), since this slightly obfuscates the hash secret |
| 11035 | */ |
| 11036 | if (len == 0) { |
| 11037 | _PyUnicode_HASH(self) = 0; |
| 11038 | return 0; |
| 11039 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11040 | |
| 11041 | /* The hash function as a macro, gets expanded three times below. */ |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 11042 | #define HASH(P) \ |
| 11043 | x ^= (Py_uhash_t) *P << 7; \ |
| 11044 | while (--len >= 0) \ |
| 11045 | x = (_PyHASH_MULTIPLIER * x) ^ (Py_uhash_t) *P++; \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11046 | |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 11047 | x = (Py_uhash_t) _Py_HashSecret.prefix; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11048 | switch (PyUnicode_KIND(self)) { |
| 11049 | case PyUnicode_1BYTE_KIND: { |
| 11050 | const unsigned char *c = PyUnicode_1BYTE_DATA(self); |
| 11051 | HASH(c); |
| 11052 | break; |
| 11053 | } |
| 11054 | case PyUnicode_2BYTE_KIND: { |
| 11055 | const Py_UCS2 *s = PyUnicode_2BYTE_DATA(self); |
| 11056 | HASH(s); |
| 11057 | break; |
| 11058 | } |
| 11059 | default: { |
| 11060 | Py_UCS4 *l; |
| 11061 | assert(PyUnicode_KIND(self) == PyUnicode_4BYTE_KIND && |
| 11062 | "Impossible switch case in unicode_hash"); |
| 11063 | l = PyUnicode_4BYTE_DATA(self); |
| 11064 | HASH(l); |
| 11065 | break; |
| 11066 | } |
| 11067 | } |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 11068 | x ^= (Py_uhash_t) PyUnicode_GET_LENGTH(self); |
| 11069 | x ^= (Py_uhash_t) _Py_HashSecret.suffix; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11070 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11071 | if (x == -1) |
| 11072 | x = -2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11073 | _PyUnicode_HASH(self) = x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11074 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11075 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11076 | #undef HASH |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11077 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11078 | PyDoc_STRVAR(index__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11079 | "S.index(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11080 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11081 | 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] | 11082 | |
| 11083 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11084 | unicode_index(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11085 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11086 | Py_ssize_t result; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11087 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 11088 | Py_ssize_t start; |
| 11089 | Py_ssize_t end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11090 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11091 | if (!stringlib_parse_args_finds_unicode("index", args, &substring, |
| 11092 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11093 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11094 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11095 | if (PyUnicode_READY(self) == -1) |
| 11096 | return NULL; |
| 11097 | if (PyUnicode_READY(substring) == -1) |
| 11098 | return NULL; |
| 11099 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11100 | result = any_find_slice(1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11101 | |
| 11102 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11103 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11104 | if (result == -2) |
| 11105 | return NULL; |
| 11106 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11107 | if (result < 0) { |
| 11108 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 11109 | return NULL; |
| 11110 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11111 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11112 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11113 | } |
| 11114 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11115 | PyDoc_STRVAR(islower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11116 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11117 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11118 | 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] | 11119 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11120 | |
| 11121 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11122 | unicode_islower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11123 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11124 | Py_ssize_t i, length; |
| 11125 | int kind; |
| 11126 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11127 | int cased; |
| 11128 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11129 | if (PyUnicode_READY(self) == -1) |
| 11130 | return NULL; |
| 11131 | length = PyUnicode_GET_LENGTH(self); |
| 11132 | kind = PyUnicode_KIND(self); |
| 11133 | data = PyUnicode_DATA(self); |
| 11134 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11135 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11136 | if (length == 1) |
| 11137 | return PyBool_FromLong( |
| 11138 | Py_UNICODE_ISLOWER(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11139 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11140 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11141 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11142 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11143 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11144 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11145 | for (i = 0; i < length; i++) { |
| 11146 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11147 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11148 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 11149 | return PyBool_FromLong(0); |
| 11150 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 11151 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11152 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11153 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11154 | } |
| 11155 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11156 | PyDoc_STRVAR(isupper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11157 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11158 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11159 | 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] | 11160 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11161 | |
| 11162 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11163 | unicode_isupper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11164 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11165 | Py_ssize_t i, length; |
| 11166 | int kind; |
| 11167 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11168 | int cased; |
| 11169 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11170 | if (PyUnicode_READY(self) == -1) |
| 11171 | return NULL; |
| 11172 | length = PyUnicode_GET_LENGTH(self); |
| 11173 | kind = PyUnicode_KIND(self); |
| 11174 | data = PyUnicode_DATA(self); |
| 11175 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11176 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11177 | if (length == 1) |
| 11178 | return PyBool_FromLong( |
| 11179 | Py_UNICODE_ISUPPER(PyUnicode_READ(kind, data, 0)) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11180 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11181 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11182 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11183 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11184 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11185 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11186 | for (i = 0; i < length; i++) { |
| 11187 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11188 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11189 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 11190 | return PyBool_FromLong(0); |
| 11191 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 11192 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11193 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11194 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11195 | } |
| 11196 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11197 | PyDoc_STRVAR(istitle__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11198 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11199 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11200 | Return True if S is a titlecased string and there is at least one\n\ |
| 11201 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 11202 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 11203 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11204 | |
| 11205 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11206 | unicode_istitle(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11207 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11208 | Py_ssize_t i, length; |
| 11209 | int kind; |
| 11210 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11211 | int cased, previous_is_cased; |
| 11212 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11213 | if (PyUnicode_READY(self) == -1) |
| 11214 | return NULL; |
| 11215 | length = PyUnicode_GET_LENGTH(self); |
| 11216 | kind = PyUnicode_KIND(self); |
| 11217 | data = PyUnicode_DATA(self); |
| 11218 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11219 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11220 | if (length == 1) { |
| 11221 | Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11222 | return PyBool_FromLong((Py_UNICODE_ISTITLE(ch) != 0) || |
| 11223 | (Py_UNICODE_ISUPPER(ch) != 0)); |
| 11224 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11225 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11226 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11227 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11228 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11229 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11230 | cased = 0; |
| 11231 | previous_is_cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11232 | for (i = 0; i < length; i++) { |
| 11233 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11234 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11235 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 11236 | if (previous_is_cased) |
| 11237 | return PyBool_FromLong(0); |
| 11238 | previous_is_cased = 1; |
| 11239 | cased = 1; |
| 11240 | } |
| 11241 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 11242 | if (!previous_is_cased) |
| 11243 | return PyBool_FromLong(0); |
| 11244 | previous_is_cased = 1; |
| 11245 | cased = 1; |
| 11246 | } |
| 11247 | else |
| 11248 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11249 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11250 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11251 | } |
| 11252 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11253 | PyDoc_STRVAR(isspace__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11254 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11255 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11256 | Return True if all characters in S are whitespace\n\ |
| 11257 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11258 | |
| 11259 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11260 | unicode_isspace(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11261 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11262 | Py_ssize_t i, length; |
| 11263 | int kind; |
| 11264 | void *data; |
| 11265 | |
| 11266 | if (PyUnicode_READY(self) == -1) |
| 11267 | return NULL; |
| 11268 | length = PyUnicode_GET_LENGTH(self); |
| 11269 | kind = PyUnicode_KIND(self); |
| 11270 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11271 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11272 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11273 | if (length == 1) |
| 11274 | return PyBool_FromLong( |
| 11275 | Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11276 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11277 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11278 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11279 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11280 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11281 | for (i = 0; i < length; i++) { |
| 11282 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11283 | if (!Py_UNICODE_ISSPACE(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11284 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11285 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11286 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11287 | } |
| 11288 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11289 | PyDoc_STRVAR(isalpha__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11290 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11291 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11292 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11293 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11294 | |
| 11295 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11296 | unicode_isalpha(PyObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11297 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11298 | Py_ssize_t i, length; |
| 11299 | int kind; |
| 11300 | void *data; |
| 11301 | |
| 11302 | if (PyUnicode_READY(self) == -1) |
| 11303 | return NULL; |
| 11304 | length = PyUnicode_GET_LENGTH(self); |
| 11305 | kind = PyUnicode_KIND(self); |
| 11306 | data = PyUnicode_DATA(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11307 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11308 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11309 | if (length == 1) |
| 11310 | return PyBool_FromLong( |
| 11311 | Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, 0))); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11312 | |
| 11313 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11314 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11315 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11316 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11317 | for (i = 0; i < length; i++) { |
| 11318 | if (!Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11319 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11320 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11321 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11322 | } |
| 11323 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11324 | PyDoc_STRVAR(isalnum__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11325 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11326 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11327 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11328 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11329 | |
| 11330 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11331 | unicode_isalnum(PyObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11332 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11333 | int kind; |
| 11334 | void *data; |
| 11335 | Py_ssize_t len, i; |
| 11336 | |
| 11337 | if (PyUnicode_READY(self) == -1) |
| 11338 | return NULL; |
| 11339 | |
| 11340 | kind = PyUnicode_KIND(self); |
| 11341 | data = PyUnicode_DATA(self); |
| 11342 | len = PyUnicode_GET_LENGTH(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11343 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11344 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11345 | if (len == 1) { |
| 11346 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11347 | return PyBool_FromLong(Py_UNICODE_ISALNUM(ch)); |
| 11348 | } |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11349 | |
| 11350 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11351 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11352 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11353 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11354 | for (i = 0; i < len; i++) { |
| 11355 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11356 | if (!Py_UNICODE_ISALNUM(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11357 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11358 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11359 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11360 | } |
| 11361 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11362 | PyDoc_STRVAR(isdecimal__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11363 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11364 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11365 | 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] | 11366 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11367 | |
| 11368 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11369 | unicode_isdecimal(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11370 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11371 | Py_ssize_t i, length; |
| 11372 | int kind; |
| 11373 | void *data; |
| 11374 | |
| 11375 | if (PyUnicode_READY(self) == -1) |
| 11376 | return NULL; |
| 11377 | length = PyUnicode_GET_LENGTH(self); |
| 11378 | kind = PyUnicode_KIND(self); |
| 11379 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11380 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11381 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11382 | if (length == 1) |
| 11383 | return PyBool_FromLong( |
| 11384 | Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11385 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11386 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11387 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11388 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11389 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11390 | for (i = 0; i < length; i++) { |
| 11391 | if (!Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11392 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11393 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11394 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11395 | } |
| 11396 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11397 | PyDoc_STRVAR(isdigit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11398 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11399 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11400 | Return True if all characters in S are digits\n\ |
| 11401 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11402 | |
| 11403 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11404 | unicode_isdigit(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11405 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11406 | Py_ssize_t i, length; |
| 11407 | int kind; |
| 11408 | void *data; |
| 11409 | |
| 11410 | if (PyUnicode_READY(self) == -1) |
| 11411 | return NULL; |
| 11412 | length = PyUnicode_GET_LENGTH(self); |
| 11413 | kind = PyUnicode_KIND(self); |
| 11414 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11415 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11416 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11417 | if (length == 1) { |
| 11418 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11419 | return PyBool_FromLong(Py_UNICODE_ISDIGIT(ch)); |
| 11420 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11421 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11422 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11423 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11424 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11425 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11426 | for (i = 0; i < length; i++) { |
| 11427 | if (!Py_UNICODE_ISDIGIT(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11428 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11429 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11430 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11431 | } |
| 11432 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11433 | PyDoc_STRVAR(isnumeric__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11434 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11435 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11436 | 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] | 11437 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11438 | |
| 11439 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11440 | unicode_isnumeric(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11441 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11442 | Py_ssize_t i, length; |
| 11443 | int kind; |
| 11444 | void *data; |
| 11445 | |
| 11446 | if (PyUnicode_READY(self) == -1) |
| 11447 | return NULL; |
| 11448 | length = PyUnicode_GET_LENGTH(self); |
| 11449 | kind = PyUnicode_KIND(self); |
| 11450 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11451 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11452 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11453 | if (length == 1) |
| 11454 | return PyBool_FromLong( |
| 11455 | Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11456 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11457 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11458 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11459 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11460 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11461 | for (i = 0; i < length; i++) { |
| 11462 | if (!Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11463 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11464 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11465 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11466 | } |
| 11467 | |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11468 | int |
| 11469 | PyUnicode_IsIdentifier(PyObject *self) |
| 11470 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11471 | int kind; |
| 11472 | void *data; |
| 11473 | Py_ssize_t i; |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11474 | Py_UCS4 first; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11475 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11476 | if (PyUnicode_READY(self) == -1) { |
| 11477 | Py_FatalError("identifier not ready"); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11478 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11479 | } |
| 11480 | |
| 11481 | /* Special case for empty strings */ |
| 11482 | if (PyUnicode_GET_LENGTH(self) == 0) |
| 11483 | return 0; |
| 11484 | kind = PyUnicode_KIND(self); |
| 11485 | data = PyUnicode_DATA(self); |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11486 | |
| 11487 | /* PEP 3131 says that the first character must be in |
| 11488 | XID_Start and subsequent characters in XID_Continue, |
| 11489 | and for the ASCII range, the 2.x rules apply (i.e |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11490 | start with letters and underscore, continue with |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11491 | letters, digits, underscore). However, given the current |
| 11492 | definition of XID_Start and XID_Continue, it is sufficient |
| 11493 | to check just for these, except that _ must be allowed |
| 11494 | as starting an identifier. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11495 | first = PyUnicode_READ(kind, data, 0); |
Benjamin Peterson | f413b80 | 2011-08-12 22:17:18 -0500 | [diff] [blame] | 11496 | if (!_PyUnicode_IsXidStart(first) && first != 0x5F /* LOW LINE */) |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11497 | return 0; |
| 11498 | |
Benjamin Peterson | 9c6e6a0 | 2011-09-28 08:09:05 -0400 | [diff] [blame] | 11499 | for (i = 1; i < PyUnicode_GET_LENGTH(self); i++) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11500 | if (!_PyUnicode_IsXidContinue(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11501 | return 0; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11502 | return 1; |
| 11503 | } |
| 11504 | |
| 11505 | PyDoc_STRVAR(isidentifier__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11506 | "S.isidentifier() -> bool\n\ |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11507 | \n\ |
| 11508 | Return True if S is a valid identifier according\n\ |
| 11509 | to the language definition."); |
| 11510 | |
| 11511 | static PyObject* |
| 11512 | unicode_isidentifier(PyObject *self) |
| 11513 | { |
| 11514 | return PyBool_FromLong(PyUnicode_IsIdentifier(self)); |
| 11515 | } |
| 11516 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11517 | PyDoc_STRVAR(isprintable__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11518 | "S.isprintable() -> bool\n\ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11519 | \n\ |
| 11520 | Return True if all characters in S are considered\n\ |
| 11521 | printable in repr() or S is empty, False otherwise."); |
| 11522 | |
| 11523 | static PyObject* |
| 11524 | unicode_isprintable(PyObject *self) |
| 11525 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11526 | Py_ssize_t i, length; |
| 11527 | int kind; |
| 11528 | void *data; |
| 11529 | |
| 11530 | if (PyUnicode_READY(self) == -1) |
| 11531 | return NULL; |
| 11532 | length = PyUnicode_GET_LENGTH(self); |
| 11533 | kind = PyUnicode_KIND(self); |
| 11534 | data = PyUnicode_DATA(self); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11535 | |
| 11536 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11537 | if (length == 1) |
| 11538 | return PyBool_FromLong( |
| 11539 | Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, 0))); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11540 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11541 | for (i = 0; i < length; i++) { |
| 11542 | if (!Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, i))) { |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11543 | Py_RETURN_FALSE; |
| 11544 | } |
| 11545 | } |
| 11546 | Py_RETURN_TRUE; |
| 11547 | } |
| 11548 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11549 | PyDoc_STRVAR(join__doc__, |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 11550 | "S.join(iterable) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11551 | \n\ |
| 11552 | 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] | 11553 | iterable. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11554 | |
| 11555 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11556 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11557 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11558 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11559 | } |
| 11560 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11561 | static Py_ssize_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11562 | unicode_length(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11563 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11564 | if (PyUnicode_READY(self) == -1) |
| 11565 | return -1; |
| 11566 | return PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11567 | } |
| 11568 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11569 | PyDoc_STRVAR(ljust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11570 | "S.ljust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11571 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 11572 | 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] | 11573 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11574 | |
| 11575 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11576 | unicode_ljust(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11577 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11578 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11579 | Py_UCS4 fillchar = ' '; |
| 11580 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11581 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11582 | return NULL; |
| 11583 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 11584 | if (PyUnicode_READY(self) == -1) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11585 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11586 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11587 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 11588 | return unicode_result_unchanged(self); |
| 11589 | |
| 11590 | return pad(self, 0, width - PyUnicode_GET_LENGTH(self), fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11591 | } |
| 11592 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11593 | PyDoc_STRVAR(lower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11594 | "S.lower() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11595 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11596 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11597 | |
| 11598 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11599 | unicode_lower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11600 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 11601 | if (PyUnicode_READY(self) == -1) |
| 11602 | return NULL; |
| 11603 | if (PyUnicode_IS_ASCII(self)) |
| 11604 | return ascii_upper_or_lower(self, 1); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 11605 | return case_operation(self, do_lower); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11606 | } |
| 11607 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11608 | #define LEFTSTRIP 0 |
| 11609 | #define RIGHTSTRIP 1 |
| 11610 | #define BOTHSTRIP 2 |
| 11611 | |
| 11612 | /* Arrays indexed by above */ |
| 11613 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 11614 | |
| 11615 | #define STRIPNAME(i) (stripformat[i]+3) |
| 11616 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11617 | /* externally visible for str.strip(unicode) */ |
| 11618 | PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11619 | _PyUnicode_XStrip(PyObject *self, int striptype, PyObject *sepobj) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11620 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11621 | void *data; |
| 11622 | int kind; |
| 11623 | Py_ssize_t i, j, len; |
| 11624 | BLOOM_MASK sepmask; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11625 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11626 | if (PyUnicode_READY(self) == -1 || PyUnicode_READY(sepobj) == -1) |
| 11627 | return NULL; |
| 11628 | |
| 11629 | kind = PyUnicode_KIND(self); |
| 11630 | data = PyUnicode_DATA(self); |
| 11631 | len = PyUnicode_GET_LENGTH(self); |
| 11632 | sepmask = make_bloom_mask(PyUnicode_KIND(sepobj), |
| 11633 | PyUnicode_DATA(sepobj), |
| 11634 | PyUnicode_GET_LENGTH(sepobj)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11635 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11636 | i = 0; |
| 11637 | if (striptype != RIGHTSTRIP) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11638 | while (i < len && |
| 11639 | BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, i), sepobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11640 | i++; |
| 11641 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11642 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11643 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11644 | j = len; |
| 11645 | if (striptype != LEFTSTRIP) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11646 | do { |
| 11647 | j--; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11648 | } while (j >= i && |
| 11649 | BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, j), sepobj)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11650 | j++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11651 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11652 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11653 | return PyUnicode_Substring(self, i, j); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11654 | } |
| 11655 | |
| 11656 | PyObject* |
| 11657 | PyUnicode_Substring(PyObject *self, Py_ssize_t start, Py_ssize_t end) |
| 11658 | { |
| 11659 | unsigned char *data; |
| 11660 | int kind; |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11661 | Py_ssize_t length; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11662 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 11663 | if (PyUnicode_READY(self) == -1) |
| 11664 | return NULL; |
| 11665 | |
Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 11666 | length = PyUnicode_GET_LENGTH(self); |
| 11667 | end = Py_MIN(end, length); |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 11668 | |
Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 11669 | if (start == 0 && end == length) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11670 | return unicode_result_unchanged(self); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11671 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 11672 | if (start < 0 || end < 0) { |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11673 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 11674 | return NULL; |
| 11675 | } |
Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 11676 | if (start >= length || end < start) { |
Victor Stinner | 3a7f7977 | 2012-05-03 03:36:40 +0200 | [diff] [blame] | 11677 | Py_INCREF(unicode_empty); |
| 11678 | return unicode_empty; |
Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 11679 | } |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11680 | |
Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 11681 | length = end - start; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11682 | if (PyUnicode_IS_ASCII(self)) { |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11683 | data = PyUnicode_1BYTE_DATA(self); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 11684 | return _PyUnicode_FromASCII((char*)(data + start), length); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11685 | } |
| 11686 | else { |
| 11687 | kind = PyUnicode_KIND(self); |
| 11688 | data = PyUnicode_1BYTE_DATA(self); |
| 11689 | return PyUnicode_FromKindAndData(kind, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 11690 | data + kind * start, |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11691 | length); |
| 11692 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11693 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11694 | |
| 11695 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11696 | do_strip(PyObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11697 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11698 | int kind; |
| 11699 | void *data; |
| 11700 | Py_ssize_t len, i, j; |
| 11701 | |
| 11702 | if (PyUnicode_READY(self) == -1) |
| 11703 | return NULL; |
| 11704 | |
| 11705 | kind = PyUnicode_KIND(self); |
| 11706 | data = PyUnicode_DATA(self); |
| 11707 | len = PyUnicode_GET_LENGTH(self); |
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 | i = 0; |
| 11710 | if (striptype != RIGHTSTRIP) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11711 | while (i < len && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, i))) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11712 | i++; |
| 11713 | } |
| 11714 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11715 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11716 | j = len; |
| 11717 | if (striptype != LEFTSTRIP) { |
| 11718 | do { |
| 11719 | j--; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11720 | } while (j >= i && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, j))); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11721 | j++; |
| 11722 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11723 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11724 | return PyUnicode_Substring(self, i, j); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11725 | } |
| 11726 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11727 | |
| 11728 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11729 | do_argstrip(PyObject *self, int striptype, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11730 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11731 | PyObject *sep = NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11732 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11733 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
| 11734 | return NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11735 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11736 | if (sep != NULL && sep != Py_None) { |
| 11737 | if (PyUnicode_Check(sep)) |
| 11738 | return _PyUnicode_XStrip(self, striptype, sep); |
| 11739 | else { |
| 11740 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11741 | "%s arg must be None or str", |
| 11742 | STRIPNAME(striptype)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11743 | return NULL; |
| 11744 | } |
| 11745 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11746 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11747 | return do_strip(self, striptype); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11748 | } |
| 11749 | |
| 11750 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11751 | PyDoc_STRVAR(strip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11752 | "S.strip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11753 | \n\ |
| 11754 | Return a copy of the string S with leading and trailing\n\ |
| 11755 | whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11756 | 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] | 11757 | |
| 11758 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11759 | unicode_strip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11760 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11761 | if (PyTuple_GET_SIZE(args) == 0) |
| 11762 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 11763 | else |
| 11764 | return do_argstrip(self, BOTHSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11765 | } |
| 11766 | |
| 11767 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11768 | PyDoc_STRVAR(lstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11769 | "S.lstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11770 | \n\ |
| 11771 | Return a copy of the string S with leading whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11772 | 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] | 11773 | |
| 11774 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11775 | unicode_lstrip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11776 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11777 | if (PyTuple_GET_SIZE(args) == 0) |
| 11778 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 11779 | else |
| 11780 | return do_argstrip(self, LEFTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11781 | } |
| 11782 | |
| 11783 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11784 | PyDoc_STRVAR(rstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11785 | "S.rstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11786 | \n\ |
| 11787 | Return a copy of the string S with trailing whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11788 | 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] | 11789 | |
| 11790 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11791 | unicode_rstrip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11792 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11793 | if (PyTuple_GET_SIZE(args) == 0) |
| 11794 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 11795 | else |
| 11796 | return do_argstrip(self, RIGHTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11797 | } |
| 11798 | |
| 11799 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11800 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11801 | unicode_repeat(PyObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11802 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11803 | PyObject *u; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11804 | Py_ssize_t nchars, n; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11805 | |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 11806 | if (len < 1) { |
| 11807 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 11808 | return unicode_empty; |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 11809 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11810 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11811 | /* no repeat, return original string */ |
| 11812 | if (len == 1) |
| 11813 | return unicode_result_unchanged(str); |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 11814 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 11815 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11816 | return NULL; |
| 11817 | |
Victor Stinner | c759f3e | 2011-10-01 03:09:58 +0200 | [diff] [blame] | 11818 | if (PyUnicode_GET_LENGTH(str) > PY_SSIZE_T_MAX / len) { |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11819 | PyErr_SetString(PyExc_OverflowError, |
| 11820 | "repeated string is too long"); |
| 11821 | return NULL; |
| 11822 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11823 | nchars = len * PyUnicode_GET_LENGTH(str); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11824 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11825 | u = PyUnicode_New(nchars, PyUnicode_MAX_CHAR_VALUE(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11826 | if (!u) |
| 11827 | return NULL; |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11828 | assert(PyUnicode_KIND(u) == PyUnicode_KIND(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11829 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11830 | if (PyUnicode_GET_LENGTH(str) == 1) { |
| 11831 | const int kind = PyUnicode_KIND(str); |
| 11832 | const Py_UCS4 fill_char = PyUnicode_READ(kind, PyUnicode_DATA(str), 0); |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 11833 | if (kind == PyUnicode_1BYTE_KIND) { |
| 11834 | void *to = PyUnicode_DATA(u); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11835 | memset(to, (unsigned char)fill_char, len); |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 11836 | } |
| 11837 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 11838 | Py_UCS2 *ucs2 = PyUnicode_2BYTE_DATA(u); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11839 | for (n = 0; n < len; ++n) |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 11840 | ucs2[n] = fill_char; |
| 11841 | } else { |
| 11842 | Py_UCS4 *ucs4 = PyUnicode_4BYTE_DATA(u); |
| 11843 | assert(kind == PyUnicode_4BYTE_KIND); |
| 11844 | for (n = 0; n < len; ++n) |
| 11845 | ucs4[n] = fill_char; |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11846 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11847 | } |
| 11848 | else { |
| 11849 | /* number of characters copied this far */ |
| 11850 | Py_ssize_t done = PyUnicode_GET_LENGTH(str); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 11851 | const Py_ssize_t char_size = PyUnicode_KIND(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11852 | char *to = (char *) PyUnicode_DATA(u); |
| 11853 | Py_MEMCPY(to, PyUnicode_DATA(str), |
| 11854 | PyUnicode_GET_LENGTH(str) * char_size); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11855 | while (done < nchars) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11856 | n = (done <= nchars-done) ? done : nchars-done; |
| 11857 | Py_MEMCPY(to + (done * char_size), to, n * char_size); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11858 | done += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11859 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11860 | } |
| 11861 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 11862 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11863 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11864 | } |
| 11865 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11866 | PyObject * |
| 11867 | PyUnicode_Replace(PyObject *obj, |
| 11868 | PyObject *subobj, |
| 11869 | PyObject *replobj, |
| 11870 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11871 | { |
| 11872 | PyObject *self; |
| 11873 | PyObject *str1; |
| 11874 | PyObject *str2; |
| 11875 | PyObject *result; |
| 11876 | |
| 11877 | self = PyUnicode_FromObject(obj); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 11878 | if (self == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11879 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11880 | str1 = PyUnicode_FromObject(subobj); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 11881 | if (str1 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11882 | Py_DECREF(self); |
| 11883 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11884 | } |
| 11885 | str2 = PyUnicode_FromObject(replobj); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 11886 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11887 | Py_DECREF(self); |
| 11888 | Py_DECREF(str1); |
| 11889 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11890 | } |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 11891 | if (PyUnicode_READY(self) == -1 || |
| 11892 | PyUnicode_READY(str1) == -1 || |
| 11893 | PyUnicode_READY(str2) == -1) |
| 11894 | result = NULL; |
| 11895 | else |
| 11896 | result = replace(self, str1, str2, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11897 | Py_DECREF(self); |
| 11898 | Py_DECREF(str1); |
| 11899 | Py_DECREF(str2); |
| 11900 | return result; |
| 11901 | } |
| 11902 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11903 | PyDoc_STRVAR(replace__doc__, |
Ezio Melotti | c1897e7 | 2010-06-26 18:50:39 +0000 | [diff] [blame] | 11904 | "S.replace(old, new[, count]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11905 | \n\ |
| 11906 | Return a copy of S with all occurrences of substring\n\ |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 11907 | old replaced by new. If the optional argument count is\n\ |
| 11908 | given, only the first count occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11909 | |
| 11910 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11911 | unicode_replace(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11912 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11913 | PyObject *str1; |
| 11914 | PyObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11915 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11916 | PyObject *result; |
| 11917 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11918 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11919 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 11920 | if (PyUnicode_READY(self) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11921 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11922 | str1 = PyUnicode_FromObject(str1); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 11923 | if (str1 == NULL) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11924 | return NULL; |
| 11925 | str2 = PyUnicode_FromObject(str2); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 11926 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11927 | Py_DECREF(str1); |
| 11928 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 11929 | } |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 11930 | if (PyUnicode_READY(str1) == -1 || PyUnicode_READY(str2) == -1) |
| 11931 | result = NULL; |
| 11932 | else |
| 11933 | result = replace(self, str1, str2, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11934 | |
| 11935 | Py_DECREF(str1); |
| 11936 | Py_DECREF(str2); |
| 11937 | return result; |
| 11938 | } |
| 11939 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11940 | static PyObject * |
| 11941 | unicode_repr(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11942 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11943 | PyObject *repr; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11944 | Py_ssize_t isize; |
| 11945 | Py_ssize_t osize, squote, dquote, i, o; |
| 11946 | Py_UCS4 max, quote; |
| 11947 | int ikind, okind; |
| 11948 | void *idata, *odata; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11949 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11950 | if (PyUnicode_READY(unicode) == -1) |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11951 | return NULL; |
| 11952 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11953 | isize = PyUnicode_GET_LENGTH(unicode); |
| 11954 | idata = PyUnicode_DATA(unicode); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11955 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11956 | /* Compute length of output, quote characters, and |
| 11957 | maximum character */ |
| 11958 | osize = 2; /* quotes */ |
| 11959 | max = 127; |
| 11960 | squote = dquote = 0; |
| 11961 | ikind = PyUnicode_KIND(unicode); |
| 11962 | for (i = 0; i < isize; i++) { |
| 11963 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
| 11964 | switch (ch) { |
| 11965 | case '\'': squote++; osize++; break; |
| 11966 | case '"': dquote++; osize++; break; |
| 11967 | case '\\': case '\t': case '\r': case '\n': |
| 11968 | osize += 2; break; |
| 11969 | default: |
| 11970 | /* Fast-path ASCII */ |
| 11971 | if (ch < ' ' || ch == 0x7f) |
| 11972 | osize += 4; /* \xHH */ |
| 11973 | else if (ch < 0x7f) |
| 11974 | osize++; |
| 11975 | else if (Py_UNICODE_ISPRINTABLE(ch)) { |
| 11976 | osize++; |
| 11977 | max = ch > max ? ch : max; |
| 11978 | } |
| 11979 | else if (ch < 0x100) |
| 11980 | osize += 4; /* \xHH */ |
| 11981 | else if (ch < 0x10000) |
| 11982 | osize += 6; /* \uHHHH */ |
| 11983 | else |
| 11984 | osize += 10; /* \uHHHHHHHH */ |
| 11985 | } |
| 11986 | } |
| 11987 | |
| 11988 | quote = '\''; |
| 11989 | if (squote) { |
| 11990 | if (dquote) |
| 11991 | /* Both squote and dquote present. Use squote, |
| 11992 | and escape them */ |
| 11993 | osize += squote; |
| 11994 | else |
| 11995 | quote = '"'; |
| 11996 | } |
| 11997 | |
| 11998 | repr = PyUnicode_New(osize, max); |
| 11999 | if (repr == NULL) |
| 12000 | return NULL; |
| 12001 | okind = PyUnicode_KIND(repr); |
| 12002 | odata = PyUnicode_DATA(repr); |
| 12003 | |
| 12004 | PyUnicode_WRITE(okind, odata, 0, quote); |
| 12005 | PyUnicode_WRITE(okind, odata, osize-1, quote); |
| 12006 | |
| 12007 | for (i = 0, o = 1; i < isize; i++) { |
| 12008 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12009 | |
| 12010 | /* Escape quotes and backslashes */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12011 | if ((ch == quote) || (ch == '\\')) { |
| 12012 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12013 | PyUnicode_WRITE(okind, odata, o++, ch); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12014 | continue; |
| 12015 | } |
| 12016 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12017 | /* Map special whitespace to '\t', \n', '\r' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12018 | if (ch == '\t') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12019 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12020 | PyUnicode_WRITE(okind, odata, o++, 't'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12021 | } |
| 12022 | else if (ch == '\n') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12023 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12024 | PyUnicode_WRITE(okind, odata, o++, 'n'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12025 | } |
| 12026 | else if (ch == '\r') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12027 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12028 | PyUnicode_WRITE(okind, odata, o++, 'r'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12029 | } |
| 12030 | |
| 12031 | /* Map non-printable US ASCII to '\xhh' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12032 | else if (ch < ' ' || ch == 0x7F) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12033 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12034 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12035 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0x000F]); |
| 12036 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0x000F]); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12037 | } |
| 12038 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12039 | /* Copy ASCII characters as-is */ |
| 12040 | else if (ch < 0x7F) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12041 | PyUnicode_WRITE(okind, odata, o++, ch); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12042 | } |
| 12043 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12044 | /* Non-ASCII characters */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12045 | else { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12046 | /* Map Unicode whitespace and control characters |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12047 | (categories Z* and C* except ASCII space) |
| 12048 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12049 | if (!Py_UNICODE_ISPRINTABLE(ch)) { |
Kristján Valur Jónsson | 55e5dc8 | 2012-06-06 21:58:08 +0000 | [diff] [blame] | 12050 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12051 | /* Map 8-bit characters to '\xhh' */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12052 | if (ch <= 0xff) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12053 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12054 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0x000F]); |
| 12055 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0x000F]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12056 | } |
Kristján Valur Jónsson | 55e5dc8 | 2012-06-06 21:58:08 +0000 | [diff] [blame] | 12057 | /* Map 16-bit characters to '\uxxxx' */ |
| 12058 | else if (ch <= 0xffff) { |
| 12059 | PyUnicode_WRITE(okind, odata, o++, 'u'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12060 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 12) & 0xF]); |
| 12061 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 8) & 0xF]); |
| 12062 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0xF]); |
| 12063 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0xF]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12064 | } |
Kristján Valur Jónsson | 55e5dc8 | 2012-06-06 21:58:08 +0000 | [diff] [blame] | 12065 | /* Map 21-bit characters to '\U00xxxxxx' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12066 | else { |
Kristján Valur Jónsson | 55e5dc8 | 2012-06-06 21:58:08 +0000 | [diff] [blame] | 12067 | PyUnicode_WRITE(okind, odata, o++, 'U'); |
| 12068 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 28) & 0xF]); |
| 12069 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 24) & 0xF]); |
| 12070 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 20) & 0xF]); |
| 12071 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 16) & 0xF]); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12072 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 12) & 0xF]); |
| 12073 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 8) & 0xF]); |
| 12074 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0xF]); |
| 12075 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0xF]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12076 | } |
| 12077 | } |
| 12078 | /* Copy characters as-is */ |
| 12079 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12080 | PyUnicode_WRITE(okind, odata, o++, ch); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12081 | } |
| 12082 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12083 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12084 | /* Closing quote already added at the beginning */ |
Victor Stinner | 05d1189 | 2011-10-06 01:13:58 +0200 | [diff] [blame] | 12085 | assert(_PyUnicode_CheckConsistency(repr, 1)); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12086 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12087 | } |
| 12088 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12089 | PyDoc_STRVAR(rfind__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12090 | "S.rfind(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12091 | \n\ |
| 12092 | Return the highest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 12093 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12094 | arguments start and end are interpreted as in slice notation.\n\ |
| 12095 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12096 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12097 | |
| 12098 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12099 | unicode_rfind(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12100 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12101 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 12102 | Py_ssize_t start; |
| 12103 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12104 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12105 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12106 | if (!stringlib_parse_args_finds_unicode("rfind", args, &substring, |
| 12107 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12108 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12109 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12110 | if (PyUnicode_READY(self) == -1) |
| 12111 | return NULL; |
| 12112 | if (PyUnicode_READY(substring) == -1) |
| 12113 | return NULL; |
| 12114 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12115 | result = any_find_slice(-1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12116 | |
| 12117 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12118 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12119 | if (result == -2) |
| 12120 | return NULL; |
| 12121 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12122 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12123 | } |
| 12124 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12125 | PyDoc_STRVAR(rindex__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12126 | "S.rindex(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12127 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12128 | 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] | 12129 | |
| 12130 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12131 | unicode_rindex(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12132 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12133 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 12134 | Py_ssize_t start; |
| 12135 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12136 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12137 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12138 | if (!stringlib_parse_args_finds_unicode("rindex", args, &substring, |
| 12139 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12140 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12141 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12142 | if (PyUnicode_READY(self) == -1) |
| 12143 | return NULL; |
| 12144 | if (PyUnicode_READY(substring) == -1) |
| 12145 | return NULL; |
| 12146 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12147 | result = any_find_slice(-1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12148 | |
| 12149 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12150 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12151 | if (result == -2) |
| 12152 | return NULL; |
| 12153 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12154 | if (result < 0) { |
| 12155 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 12156 | return NULL; |
| 12157 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12158 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12159 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12160 | } |
| 12161 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12162 | PyDoc_STRVAR(rjust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12163 | "S.rjust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12164 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 12165 | 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] | 12166 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12167 | |
| 12168 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12169 | unicode_rjust(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12170 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12171 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12172 | Py_UCS4 fillchar = ' '; |
| 12173 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 12174 | 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] | 12175 | return NULL; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 12176 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 12177 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12178 | return NULL; |
| 12179 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12180 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 12181 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12182 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12183 | return pad(self, width - PyUnicode_GET_LENGTH(self), 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12184 | } |
| 12185 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12186 | PyObject * |
| 12187 | PyUnicode_Split(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12188 | { |
| 12189 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 12190 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12191 | s = PyUnicode_FromObject(s); |
| 12192 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12193 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12194 | if (sep != NULL) { |
| 12195 | sep = PyUnicode_FromObject(sep); |
| 12196 | if (sep == NULL) { |
| 12197 | Py_DECREF(s); |
| 12198 | return NULL; |
| 12199 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12200 | } |
| 12201 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12202 | result = split(s, sep, maxsplit); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12203 | |
| 12204 | Py_DECREF(s); |
| 12205 | Py_XDECREF(sep); |
| 12206 | return result; |
| 12207 | } |
| 12208 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12209 | PyDoc_STRVAR(split__doc__, |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12210 | "S.split(sep=None, maxsplit=-1) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12211 | \n\ |
| 12212 | Return a list of the words in S, using sep as the\n\ |
| 12213 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 12214 | 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] | 12215 | whitespace string is a separator and empty strings are\n\ |
| 12216 | removed from the result."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12217 | |
| 12218 | static PyObject* |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12219 | unicode_split(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12220 | { |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12221 | static char *kwlist[] = {"sep", "maxsplit", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12222 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12223 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12224 | |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12225 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|On:split", |
| 12226 | kwlist, &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12227 | return NULL; |
| 12228 | |
| 12229 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12230 | return split(self, NULL, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12231 | else if (PyUnicode_Check(substring)) |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12232 | return split(self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12233 | else |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12234 | return PyUnicode_Split(self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12235 | } |
| 12236 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12237 | PyObject * |
| 12238 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 12239 | { |
| 12240 | PyObject* str_obj; |
| 12241 | PyObject* sep_obj; |
| 12242 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12243 | int kind1, kind2, kind; |
| 12244 | void *buf1 = NULL, *buf2 = NULL; |
| 12245 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12246 | |
| 12247 | str_obj = PyUnicode_FromObject(str_in); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12248 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12249 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12250 | sep_obj = PyUnicode_FromObject(sep_in); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12251 | if (!sep_obj) { |
| 12252 | Py_DECREF(str_obj); |
| 12253 | return NULL; |
| 12254 | } |
| 12255 | if (PyUnicode_READY(sep_obj) == -1 || PyUnicode_READY(str_obj) == -1) { |
| 12256 | Py_DECREF(sep_obj); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12257 | Py_DECREF(str_obj); |
| 12258 | return NULL; |
| 12259 | } |
| 12260 | |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12261 | kind1 = PyUnicode_KIND(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12262 | kind2 = PyUnicode_KIND(sep_obj); |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12263 | kind = Py_MAX(kind1, kind2); |
| 12264 | buf1 = PyUnicode_DATA(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12265 | if (kind1 != kind) |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12266 | buf1 = _PyUnicode_AsKind(str_obj, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12267 | if (!buf1) |
| 12268 | goto onError; |
| 12269 | buf2 = PyUnicode_DATA(sep_obj); |
| 12270 | if (kind2 != kind) |
| 12271 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 12272 | if (!buf2) |
| 12273 | goto onError; |
| 12274 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 12275 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 12276 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 12277 | switch (PyUnicode_KIND(str_obj)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12278 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 12279 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) |
| 12280 | out = asciilib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12281 | else |
| 12282 | 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] | 12283 | break; |
| 12284 | case PyUnicode_2BYTE_KIND: |
| 12285 | out = ucs2lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12286 | break; |
| 12287 | case PyUnicode_4BYTE_KIND: |
| 12288 | out = ucs4lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12289 | break; |
| 12290 | default: |
| 12291 | assert(0); |
| 12292 | out = 0; |
| 12293 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12294 | |
| 12295 | Py_DECREF(sep_obj); |
| 12296 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12297 | if (kind1 != kind) |
| 12298 | PyMem_Free(buf1); |
| 12299 | if (kind2 != kind) |
| 12300 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12301 | |
| 12302 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12303 | onError: |
| 12304 | Py_DECREF(sep_obj); |
| 12305 | Py_DECREF(str_obj); |
| 12306 | if (kind1 != kind && buf1) |
| 12307 | PyMem_Free(buf1); |
| 12308 | if (kind2 != kind && buf2) |
| 12309 | PyMem_Free(buf2); |
| 12310 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12311 | } |
| 12312 | |
| 12313 | |
| 12314 | PyObject * |
| 12315 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 12316 | { |
| 12317 | PyObject* str_obj; |
| 12318 | PyObject* sep_obj; |
| 12319 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12320 | int kind1, kind2, kind; |
| 12321 | void *buf1 = NULL, *buf2 = NULL; |
| 12322 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12323 | |
| 12324 | str_obj = PyUnicode_FromObject(str_in); |
| 12325 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12326 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12327 | sep_obj = PyUnicode_FromObject(sep_in); |
| 12328 | if (!sep_obj) { |
| 12329 | Py_DECREF(str_obj); |
| 12330 | return NULL; |
| 12331 | } |
| 12332 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12333 | kind1 = PyUnicode_KIND(str_in); |
| 12334 | kind2 = PyUnicode_KIND(sep_obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 12335 | kind = Py_MAX(kind1, kind2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12336 | buf1 = PyUnicode_DATA(str_in); |
| 12337 | if (kind1 != kind) |
| 12338 | buf1 = _PyUnicode_AsKind(str_in, kind); |
| 12339 | if (!buf1) |
| 12340 | goto onError; |
| 12341 | buf2 = PyUnicode_DATA(sep_obj); |
| 12342 | if (kind2 != kind) |
| 12343 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 12344 | if (!buf2) |
| 12345 | goto onError; |
| 12346 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 12347 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 12348 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 12349 | switch (PyUnicode_KIND(str_in)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12350 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 12351 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) |
| 12352 | out = asciilib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12353 | else |
| 12354 | 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] | 12355 | break; |
| 12356 | case PyUnicode_2BYTE_KIND: |
| 12357 | out = ucs2lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12358 | break; |
| 12359 | case PyUnicode_4BYTE_KIND: |
| 12360 | out = ucs4lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12361 | break; |
| 12362 | default: |
| 12363 | assert(0); |
| 12364 | out = 0; |
| 12365 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12366 | |
| 12367 | Py_DECREF(sep_obj); |
| 12368 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12369 | if (kind1 != kind) |
| 12370 | PyMem_Free(buf1); |
| 12371 | if (kind2 != kind) |
| 12372 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12373 | |
| 12374 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12375 | onError: |
| 12376 | Py_DECREF(sep_obj); |
| 12377 | Py_DECREF(str_obj); |
| 12378 | if (kind1 != kind && buf1) |
| 12379 | PyMem_Free(buf1); |
| 12380 | if (kind2 != kind && buf2) |
| 12381 | PyMem_Free(buf2); |
| 12382 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12383 | } |
| 12384 | |
| 12385 | PyDoc_STRVAR(partition__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12386 | "S.partition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12387 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 12388 | 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] | 12389 | 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] | 12390 | found, return S and two empty strings."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12391 | |
| 12392 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12393 | unicode_partition(PyObject *self, PyObject *separator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12394 | { |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12395 | return PyUnicode_Partition(self, separator); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12396 | } |
| 12397 | |
| 12398 | PyDoc_STRVAR(rpartition__doc__, |
Ezio Melotti | 5b2b242 | 2010-01-25 11:58:28 +0000 | [diff] [blame] | 12399 | "S.rpartition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12400 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 12401 | 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] | 12402 | 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] | 12403 | separator is not found, return two empty strings and S."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12404 | |
| 12405 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12406 | unicode_rpartition(PyObject *self, PyObject *separator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12407 | { |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12408 | return PyUnicode_RPartition(self, separator); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12409 | } |
| 12410 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12411 | PyObject * |
| 12412 | PyUnicode_RSplit(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12413 | { |
| 12414 | PyObject *result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12415 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12416 | s = PyUnicode_FromObject(s); |
| 12417 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12418 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12419 | if (sep != NULL) { |
| 12420 | sep = PyUnicode_FromObject(sep); |
| 12421 | if (sep == NULL) { |
| 12422 | Py_DECREF(s); |
| 12423 | return NULL; |
| 12424 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12425 | } |
| 12426 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12427 | result = rsplit(s, sep, maxsplit); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12428 | |
| 12429 | Py_DECREF(s); |
| 12430 | Py_XDECREF(sep); |
| 12431 | return result; |
| 12432 | } |
| 12433 | |
| 12434 | PyDoc_STRVAR(rsplit__doc__, |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12435 | "S.rsplit(sep=None, maxsplit=-1) -> list of strings\n\ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12436 | \n\ |
| 12437 | Return a list of the words in S, using sep as the\n\ |
| 12438 | delimiter string, starting at the end of the string and\n\ |
| 12439 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 12440 | splits are done. If sep is not specified, any whitespace string\n\ |
| 12441 | is a separator."); |
| 12442 | |
| 12443 | static PyObject* |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12444 | unicode_rsplit(PyObject *self, PyObject *args, PyObject *kwds) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12445 | { |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12446 | static char *kwlist[] = {"sep", "maxsplit", 0}; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12447 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12448 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12449 | |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12450 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|On:rsplit", |
| 12451 | kwlist, &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12452 | return NULL; |
| 12453 | |
| 12454 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12455 | return rsplit(self, NULL, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12456 | else if (PyUnicode_Check(substring)) |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12457 | return rsplit(self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12458 | else |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12459 | return PyUnicode_RSplit(self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12460 | } |
| 12461 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12462 | PyDoc_STRVAR(splitlines__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12463 | "S.splitlines([keepends]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12464 | \n\ |
| 12465 | 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] | 12466 | 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] | 12467 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12468 | |
| 12469 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12470 | unicode_splitlines(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12471 | { |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12472 | static char *kwlist[] = {"keepends", 0}; |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 12473 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12474 | |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12475 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:splitlines", |
| 12476 | kwlist, &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12477 | return NULL; |
| 12478 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12479 | return PyUnicode_Splitlines(self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12480 | } |
| 12481 | |
| 12482 | static |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 12483 | PyObject *unicode_str(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12484 | { |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12485 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12486 | } |
| 12487 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12488 | PyDoc_STRVAR(swapcase__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12489 | "S.swapcase() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12490 | \n\ |
| 12491 | 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] | 12492 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12493 | |
| 12494 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12495 | unicode_swapcase(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12496 | { |
Benjamin Peterson | eea4846 | 2012-01-16 14:28:50 -0500 | [diff] [blame] | 12497 | if (PyUnicode_READY(self) == -1) |
| 12498 | return NULL; |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 12499 | return case_operation(self, do_swapcase); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12500 | } |
| 12501 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12502 | PyDoc_STRVAR(maketrans__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12503 | "str.maketrans(x[, y[, z]]) -> dict (static method)\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12504 | \n\ |
| 12505 | Return a translation table usable for str.translate().\n\ |
| 12506 | If there is only one argument, it must be a dictionary mapping Unicode\n\ |
| 12507 | ordinals (integers) or characters to Unicode ordinals, strings or None.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12508 | Character keys will be then converted to ordinals.\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12509 | If there are two arguments, they must be strings of equal length, and\n\ |
| 12510 | in the resulting dictionary, each character in x will be mapped to the\n\ |
| 12511 | character at the same position in y. If there is a third argument, it\n\ |
| 12512 | must be a string, whose characters will be mapped to None in the result."); |
| 12513 | |
| 12514 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12515 | unicode_maketrans(PyObject *null, PyObject *args) |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12516 | { |
| 12517 | PyObject *x, *y = NULL, *z = NULL; |
| 12518 | PyObject *new = NULL, *key, *value; |
| 12519 | Py_ssize_t i = 0; |
| 12520 | int res; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12521 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12522 | if (!PyArg_ParseTuple(args, "O|UU:maketrans", &x, &y, &z)) |
| 12523 | return NULL; |
| 12524 | new = PyDict_New(); |
| 12525 | if (!new) |
| 12526 | return NULL; |
| 12527 | if (y != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12528 | int x_kind, y_kind, z_kind; |
| 12529 | void *x_data, *y_data, *z_data; |
| 12530 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12531 | /* x must be a string too, of equal length */ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12532 | if (!PyUnicode_Check(x)) { |
| 12533 | PyErr_SetString(PyExc_TypeError, "first maketrans argument must " |
| 12534 | "be a string if there is a second argument"); |
| 12535 | goto err; |
| 12536 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12537 | if (PyUnicode_GET_LENGTH(x) != PyUnicode_GET_LENGTH(y)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12538 | PyErr_SetString(PyExc_ValueError, "the first two maketrans " |
| 12539 | "arguments must have equal length"); |
| 12540 | goto err; |
| 12541 | } |
| 12542 | /* 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] | 12543 | x_kind = PyUnicode_KIND(x); |
| 12544 | y_kind = PyUnicode_KIND(y); |
| 12545 | x_data = PyUnicode_DATA(x); |
| 12546 | y_data = PyUnicode_DATA(y); |
| 12547 | for (i = 0; i < PyUnicode_GET_LENGTH(x); i++) { |
| 12548 | key = PyLong_FromLong(PyUnicode_READ(x_kind, x_data, i)); |
Benjamin Peterson | 53aa1d7 | 2011-12-20 13:29:45 -0600 | [diff] [blame] | 12549 | if (!key) |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12550 | goto err; |
Benjamin Peterson | 822c790 | 2011-12-20 13:32:50 -0600 | [diff] [blame] | 12551 | value = PyLong_FromLong(PyUnicode_READ(y_kind, y_data, i)); |
Benjamin Peterson | 53aa1d7 | 2011-12-20 13:29:45 -0600 | [diff] [blame] | 12552 | if (!value) { |
| 12553 | Py_DECREF(key); |
| 12554 | goto err; |
| 12555 | } |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12556 | res = PyDict_SetItem(new, key, value); |
| 12557 | Py_DECREF(key); |
| 12558 | Py_DECREF(value); |
| 12559 | if (res < 0) |
| 12560 | goto err; |
| 12561 | } |
| 12562 | /* create entries for deleting chars in z */ |
| 12563 | if (z != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12564 | z_kind = PyUnicode_KIND(z); |
| 12565 | z_data = PyUnicode_DATA(z); |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 12566 | for (i = 0; i < PyUnicode_GET_LENGTH(z); i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12567 | key = PyLong_FromLong(PyUnicode_READ(z_kind, z_data, i)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12568 | if (!key) |
| 12569 | goto err; |
| 12570 | res = PyDict_SetItem(new, key, Py_None); |
| 12571 | Py_DECREF(key); |
| 12572 | if (res < 0) |
| 12573 | goto err; |
| 12574 | } |
| 12575 | } |
| 12576 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12577 | int kind; |
| 12578 | void *data; |
| 12579 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12580 | /* x must be a dict */ |
Raymond Hettinger | 3ad0576 | 2009-05-29 22:11:22 +0000 | [diff] [blame] | 12581 | if (!PyDict_CheckExact(x)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12582 | PyErr_SetString(PyExc_TypeError, "if you give only one argument " |
| 12583 | "to maketrans it must be a dict"); |
| 12584 | goto err; |
| 12585 | } |
| 12586 | /* copy entries into the new dict, converting string keys to int keys */ |
| 12587 | while (PyDict_Next(x, &i, &key, &value)) { |
| 12588 | if (PyUnicode_Check(key)) { |
| 12589 | /* convert string keys to integer keys */ |
| 12590 | PyObject *newkey; |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 12591 | if (PyUnicode_GET_LENGTH(key) != 1) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12592 | PyErr_SetString(PyExc_ValueError, "string keys in translate " |
| 12593 | "table must be of length 1"); |
| 12594 | goto err; |
| 12595 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12596 | kind = PyUnicode_KIND(key); |
| 12597 | data = PyUnicode_DATA(key); |
| 12598 | newkey = PyLong_FromLong(PyUnicode_READ(kind, data, 0)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12599 | if (!newkey) |
| 12600 | goto err; |
| 12601 | res = PyDict_SetItem(new, newkey, value); |
| 12602 | Py_DECREF(newkey); |
| 12603 | if (res < 0) |
| 12604 | goto err; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12605 | } else if (PyLong_Check(key)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12606 | /* just keep integer keys */ |
| 12607 | if (PyDict_SetItem(new, key, value) < 0) |
| 12608 | goto err; |
| 12609 | } else { |
| 12610 | PyErr_SetString(PyExc_TypeError, "keys in translate table must " |
| 12611 | "be strings or integers"); |
| 12612 | goto err; |
| 12613 | } |
| 12614 | } |
| 12615 | } |
| 12616 | return new; |
| 12617 | err: |
| 12618 | Py_DECREF(new); |
| 12619 | return NULL; |
| 12620 | } |
| 12621 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12622 | PyDoc_STRVAR(translate__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12623 | "S.translate(table) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12624 | \n\ |
| 12625 | Return a copy of the string S, where all characters have been mapped\n\ |
| 12626 | through the given translation table, which must be a mapping of\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12627 | Unicode ordinals to Unicode ordinals, strings, or None.\n\ |
Walter Dörwald | 5c1ee17 | 2002-09-04 20:31:32 +0000 | [diff] [blame] | 12628 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 12629 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12630 | |
| 12631 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12632 | unicode_translate(PyObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12633 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12634 | return _PyUnicode_TranslateCharmap(self, table, "ignore"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12635 | } |
| 12636 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12637 | PyDoc_STRVAR(upper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12638 | "S.upper() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12639 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12640 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12641 | |
| 12642 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12643 | unicode_upper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12644 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 12645 | if (PyUnicode_READY(self) == -1) |
| 12646 | return NULL; |
| 12647 | if (PyUnicode_IS_ASCII(self)) |
| 12648 | return ascii_upper_or_lower(self, 0); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 12649 | return case_operation(self, do_upper); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12650 | } |
| 12651 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12652 | PyDoc_STRVAR(zfill__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12653 | "S.zfill(width) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12654 | \n\ |
Benjamin Peterson | 9aa4299 | 2008-09-10 21:57:34 +0000 | [diff] [blame] | 12655 | Pad a numeric string S with zeros on the left, to fill a field\n\ |
| 12656 | of the specified width. The string S is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12657 | |
| 12658 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12659 | unicode_zfill(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12660 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12661 | Py_ssize_t fill; |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12662 | PyObject *u; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12663 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12664 | int kind; |
| 12665 | void *data; |
| 12666 | Py_UCS4 chr; |
| 12667 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12668 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12669 | return NULL; |
| 12670 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 12671 | if (PyUnicode_READY(self) == -1) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12672 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12673 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12674 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 12675 | return unicode_result_unchanged(self); |
| 12676 | |
| 12677 | fill = width - PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12678 | |
| 12679 | u = pad(self, fill, 0, '0'); |
| 12680 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 12681 | if (u == NULL) |
| 12682 | return NULL; |
| 12683 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12684 | kind = PyUnicode_KIND(u); |
| 12685 | data = PyUnicode_DATA(u); |
| 12686 | chr = PyUnicode_READ(kind, data, fill); |
| 12687 | |
| 12688 | if (chr == '+' || chr == '-') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12689 | /* move sign to beginning of string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12690 | PyUnicode_WRITE(kind, data, 0, chr); |
| 12691 | PyUnicode_WRITE(kind, data, fill, '0'); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12692 | } |
| 12693 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12694 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12695 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12696 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12697 | |
| 12698 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12699 | static PyObject * |
| 12700 | unicode__decimal2ascii(PyObject *self) |
| 12701 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12702 | return PyUnicode_TransformDecimalAndSpaceToASCII(self); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12703 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12704 | #endif |
| 12705 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12706 | PyDoc_STRVAR(startswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12707 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12708 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 12709 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 12710 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12711 | With optional end, stop comparing S at that position.\n\ |
| 12712 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12713 | |
| 12714 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12715 | unicode_startswith(PyObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12716 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12717 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12718 | PyObject *subobj; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12719 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12720 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12721 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12722 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12723 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12724 | if (!stringlib_parse_args_finds("startswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12725 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12726 | if (PyTuple_Check(subobj)) { |
| 12727 | Py_ssize_t i; |
| 12728 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12729 | substring = PyUnicode_FromObject(PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12730 | if (substring == NULL) |
| 12731 | return NULL; |
| 12732 | result = tailmatch(self, substring, start, end, -1); |
| 12733 | Py_DECREF(substring); |
| 12734 | if (result) { |
| 12735 | Py_RETURN_TRUE; |
| 12736 | } |
| 12737 | } |
| 12738 | /* nothing matched */ |
| 12739 | Py_RETURN_FALSE; |
| 12740 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12741 | substring = PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12742 | if (substring == NULL) { |
| 12743 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 12744 | PyErr_Format(PyExc_TypeError, "startswith first arg must be str or " |
| 12745 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12746 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12747 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12748 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12749 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12750 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12751 | } |
| 12752 | |
| 12753 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12754 | PyDoc_STRVAR(endswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12755 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12756 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 12757 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 12758 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12759 | With optional end, stop comparing S at that position.\n\ |
| 12760 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12761 | |
| 12762 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12763 | unicode_endswith(PyObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12764 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12765 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12766 | PyObject *subobj; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12767 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12768 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12769 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12770 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12771 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12772 | if (!stringlib_parse_args_finds("endswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12773 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12774 | if (PyTuple_Check(subobj)) { |
| 12775 | Py_ssize_t i; |
| 12776 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12777 | substring = PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12778 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12779 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12780 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12781 | result = tailmatch(self, substring, start, end, +1); |
| 12782 | Py_DECREF(substring); |
| 12783 | if (result) { |
| 12784 | Py_RETURN_TRUE; |
| 12785 | } |
| 12786 | } |
| 12787 | Py_RETURN_FALSE; |
| 12788 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12789 | substring = PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12790 | if (substring == NULL) { |
| 12791 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 12792 | PyErr_Format(PyExc_TypeError, "endswith first arg must be str or " |
| 12793 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12794 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12795 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12796 | result = tailmatch(self, substring, start, end, +1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12797 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12798 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12799 | } |
| 12800 | |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12801 | Py_LOCAL_INLINE(void) |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 12802 | _PyUnicodeWriter_Update(_PyUnicodeWriter *writer) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12803 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12804 | writer->size = PyUnicode_GET_LENGTH(writer->buffer); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12805 | writer->maxchar = PyUnicode_MAX_CHAR_VALUE(writer->buffer); |
| 12806 | writer->data = PyUnicode_DATA(writer->buffer); |
| 12807 | writer->kind = PyUnicode_KIND(writer->buffer); |
| 12808 | } |
| 12809 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12810 | void |
| 12811 | _PyUnicodeWriter_Init(_PyUnicodeWriter *writer, Py_ssize_t min_length) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12812 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12813 | memset(writer, 0, sizeof(*writer)); |
| 12814 | #ifdef Py_DEBUG |
| 12815 | writer->kind = 5; /* invalid kind */ |
| 12816 | #endif |
| 12817 | writer->min_length = Py_MAX(min_length, 100); |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 12818 | writer->overallocate = (min_length > 0); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12819 | } |
| 12820 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12821 | int |
| 12822 | _PyUnicodeWriter_PrepareInternal(_PyUnicodeWriter *writer, |
| 12823 | Py_ssize_t length, Py_UCS4 maxchar) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12824 | { |
| 12825 | Py_ssize_t newlen; |
| 12826 | PyObject *newbuffer; |
| 12827 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12828 | assert(length > 0); |
| 12829 | |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12830 | if (length > PY_SSIZE_T_MAX - writer->pos) { |
| 12831 | PyErr_NoMemory(); |
| 12832 | return -1; |
| 12833 | } |
| 12834 | newlen = writer->pos + length; |
| 12835 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12836 | if (writer->buffer == NULL) { |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 12837 | if (writer->overallocate) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12838 | /* overallocate 25% to limit the number of resize */ |
| 12839 | if (newlen <= (PY_SSIZE_T_MAX - newlen / 4)) |
| 12840 | newlen += newlen / 4; |
| 12841 | if (newlen < writer->min_length) |
| 12842 | newlen = writer->min_length; |
| 12843 | } |
| 12844 | writer->buffer = PyUnicode_New(newlen, maxchar); |
| 12845 | if (writer->buffer == NULL) |
| 12846 | return -1; |
| 12847 | _PyUnicodeWriter_Update(writer); |
| 12848 | return 0; |
| 12849 | } |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12850 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12851 | if (newlen > writer->size) { |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 12852 | if (writer->overallocate) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12853 | /* overallocate 25% to limit the number of resize */ |
| 12854 | if (newlen <= (PY_SSIZE_T_MAX - newlen / 4)) |
| 12855 | newlen += newlen / 4; |
| 12856 | if (newlen < writer->min_length) |
| 12857 | newlen = writer->min_length; |
| 12858 | } |
| 12859 | |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 12860 | if (maxchar > writer->maxchar || writer->readonly) { |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12861 | /* resize + widen */ |
| 12862 | newbuffer = PyUnicode_New(newlen, maxchar); |
| 12863 | if (newbuffer == NULL) |
| 12864 | return -1; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12865 | _PyUnicode_FastCopyCharacters(newbuffer, 0, |
| 12866 | writer->buffer, 0, writer->pos); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12867 | Py_DECREF(writer->buffer); |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 12868 | writer->readonly = 0; |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12869 | } |
| 12870 | else { |
| 12871 | newbuffer = resize_compact(writer->buffer, newlen); |
| 12872 | if (newbuffer == NULL) |
| 12873 | return -1; |
| 12874 | } |
| 12875 | writer->buffer = newbuffer; |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 12876 | _PyUnicodeWriter_Update(writer); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12877 | } |
| 12878 | else if (maxchar > writer->maxchar) { |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 12879 | assert(!writer->readonly); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12880 | newbuffer = PyUnicode_New(writer->size, maxchar); |
| 12881 | if (newbuffer == NULL) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12882 | return -1; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12883 | _PyUnicode_FastCopyCharacters(newbuffer, 0, |
| 12884 | writer->buffer, 0, writer->pos); |
| 12885 | Py_DECREF(writer->buffer); |
| 12886 | writer->buffer = newbuffer; |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 12887 | _PyUnicodeWriter_Update(writer); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12888 | } |
| 12889 | return 0; |
| 12890 | } |
| 12891 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12892 | int |
| 12893 | _PyUnicodeWriter_WriteStr(_PyUnicodeWriter *writer, PyObject *str) |
| 12894 | { |
| 12895 | Py_UCS4 maxchar; |
| 12896 | Py_ssize_t len; |
| 12897 | |
| 12898 | if (PyUnicode_READY(str) == -1) |
| 12899 | return -1; |
| 12900 | len = PyUnicode_GET_LENGTH(str); |
| 12901 | if (len == 0) |
| 12902 | return 0; |
| 12903 | maxchar = PyUnicode_MAX_CHAR_VALUE(str); |
| 12904 | if (maxchar > writer->maxchar || len > writer->size - writer->pos) { |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 12905 | if (writer->buffer == NULL && !writer->overallocate) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12906 | Py_INCREF(str); |
| 12907 | writer->buffer = str; |
| 12908 | _PyUnicodeWriter_Update(writer); |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 12909 | writer->readonly = 1; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12910 | writer->size = 0; |
| 12911 | writer->pos += len; |
| 12912 | return 0; |
| 12913 | } |
| 12914 | if (_PyUnicodeWriter_PrepareInternal(writer, len, maxchar) == -1) |
| 12915 | return -1; |
| 12916 | } |
| 12917 | _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos, |
| 12918 | str, 0, len); |
| 12919 | writer->pos += len; |
| 12920 | return 0; |
| 12921 | } |
| 12922 | |
| 12923 | PyObject * |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 12924 | _PyUnicodeWriter_Finish(_PyUnicodeWriter *writer) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12925 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12926 | if (writer->pos == 0) { |
| 12927 | Py_XDECREF(writer->buffer); |
| 12928 | Py_INCREF(unicode_empty); |
| 12929 | return unicode_empty; |
| 12930 | } |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 12931 | if (writer->readonly) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12932 | assert(PyUnicode_GET_LENGTH(writer->buffer) == writer->pos); |
| 12933 | return writer->buffer; |
| 12934 | } |
| 12935 | if (PyUnicode_GET_LENGTH(writer->buffer) != writer->pos) { |
| 12936 | PyObject *newbuffer; |
| 12937 | newbuffer = resize_compact(writer->buffer, writer->pos); |
| 12938 | if (newbuffer == NULL) { |
| 12939 | Py_DECREF(writer->buffer); |
| 12940 | return NULL; |
| 12941 | } |
| 12942 | writer->buffer = newbuffer; |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12943 | } |
Victor Stinner | f59c28c | 2012-05-09 03:24:14 +0200 | [diff] [blame] | 12944 | assert(_PyUnicode_CheckConsistency(writer->buffer, 1)); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12945 | return writer->buffer; |
| 12946 | } |
| 12947 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12948 | void |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 12949 | _PyUnicodeWriter_Dealloc(_PyUnicodeWriter *writer) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12950 | { |
| 12951 | Py_CLEAR(writer->buffer); |
| 12952 | } |
| 12953 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12954 | #include "stringlib/unicode_format.h" |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12955 | |
| 12956 | PyDoc_STRVAR(format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12957 | "S.format(*args, **kwargs) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12958 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 12959 | Return a formatted version of S, using substitutions from args and kwargs.\n\ |
| 12960 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12961 | |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 12962 | PyDoc_STRVAR(format_map__doc__, |
| 12963 | "S.format_map(mapping) -> str\n\ |
| 12964 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 12965 | Return a formatted version of S, using substitutions from mapping.\n\ |
| 12966 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 12967 | |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12968 | static PyObject * |
| 12969 | unicode__format__(PyObject* self, PyObject* args) |
| 12970 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12971 | PyObject *format_spec; |
| 12972 | _PyUnicodeWriter writer; |
| 12973 | int ret; |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12974 | |
| 12975 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
| 12976 | return NULL; |
| 12977 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12978 | if (PyUnicode_READY(self) == -1) |
| 12979 | return NULL; |
| 12980 | _PyUnicodeWriter_Init(&writer, 0); |
| 12981 | ret = _PyUnicode_FormatAdvancedWriter(&writer, |
| 12982 | self, format_spec, 0, |
| 12983 | PyUnicode_GET_LENGTH(format_spec)); |
| 12984 | if (ret == -1) { |
| 12985 | _PyUnicodeWriter_Dealloc(&writer); |
| 12986 | return NULL; |
| 12987 | } |
| 12988 | return _PyUnicodeWriter_Finish(&writer); |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12989 | } |
| 12990 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12991 | PyDoc_STRVAR(p_format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12992 | "S.__format__(format_spec) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12993 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 12994 | Return a formatted version of S as described by format_spec."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12995 | |
| 12996 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12997 | unicode__sizeof__(PyObject *v) |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12998 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12999 | Py_ssize_t size; |
| 13000 | |
| 13001 | /* If it's a compact object, account for base structure + |
| 13002 | character data. */ |
| 13003 | if (PyUnicode_IS_COMPACT_ASCII(v)) |
| 13004 | size = sizeof(PyASCIIObject) + PyUnicode_GET_LENGTH(v) + 1; |
| 13005 | else if (PyUnicode_IS_COMPACT(v)) |
| 13006 | size = sizeof(PyCompactUnicodeObject) + |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 13007 | (PyUnicode_GET_LENGTH(v) + 1) * PyUnicode_KIND(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13008 | else { |
| 13009 | /* If it is a two-block object, account for base object, and |
| 13010 | for character block if present. */ |
| 13011 | size = sizeof(PyUnicodeObject); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 13012 | if (_PyUnicode_DATA_ANY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13013 | size += (PyUnicode_GET_LENGTH(v) + 1) * |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 13014 | PyUnicode_KIND(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13015 | } |
| 13016 | /* 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] | 13017 | with the data pointer. Check if the data is not shared. */ |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 13018 | if (_PyUnicode_HAS_WSTR_MEMORY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13019 | size += (PyUnicode_WSTR_LENGTH(v) + 1) * sizeof(wchar_t); |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 13020 | if (_PyUnicode_HAS_UTF8_MEMORY(v)) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 13021 | size += PyUnicode_UTF8_LENGTH(v) + 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13022 | |
| 13023 | return PyLong_FromSsize_t(size); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13024 | } |
| 13025 | |
| 13026 | PyDoc_STRVAR(sizeof__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13027 | "S.__sizeof__() -> size of S in memory, in bytes"); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13028 | |
| 13029 | static PyObject * |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 13030 | unicode_getnewargs(PyObject *v) |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 13031 | { |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 13032 | PyObject *copy = _PyUnicode_Copy(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13033 | if (!copy) |
| 13034 | return NULL; |
| 13035 | return Py_BuildValue("(N)", copy); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 13036 | } |
| 13037 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13038 | static PyMethodDef unicode_methods[] = { |
Benjamin Peterson | 28a4dce | 2010-12-12 01:33:04 +0000 | [diff] [blame] | 13039 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS | METH_KEYWORDS, encode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13040 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 13041 | {"split", (PyCFunction) unicode_split, METH_VARARGS | METH_KEYWORDS, split__doc__}, |
| 13042 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS | METH_KEYWORDS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13043 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 13044 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 13045 | {"casefold", (PyCFunction) unicode_casefold, METH_NOARGS, casefold__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13046 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 13047 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 13048 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 13049 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 13050 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13051 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13052 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 13053 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 13054 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 13055 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13056 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 13057 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 13058 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 13059 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13060 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 13061 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS | METH_KEYWORDS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 13062 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13063 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 13064 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 13065 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 13066 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 13067 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 13068 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 13069 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 13070 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 13071 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 13072 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 13073 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 13074 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 13075 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 13076 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 13077 | {"isidentifier", (PyCFunction) unicode_isidentifier, METH_NOARGS, isidentifier__doc__}, |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 13078 | {"isprintable", (PyCFunction) unicode_isprintable, METH_NOARGS, isprintable__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13079 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Eric Smith | 9cd1e09 | 2007-08-31 18:39:38 +0000 | [diff] [blame] | 13080 | {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 13081 | {"format_map", (PyCFunction) do_string_format_map, METH_O, format_map__doc__}, |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13082 | {"__format__", (PyCFunction) unicode__format__, METH_VARARGS, p_format__doc__}, |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 13083 | {"maketrans", (PyCFunction) unicode_maketrans, |
| 13084 | METH_VARARGS | METH_STATIC, maketrans__doc__}, |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13085 | {"__sizeof__", (PyCFunction) unicode__sizeof__, METH_NOARGS, sizeof__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 13086 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 13087 | /* These methods are just used for debugging the implementation. */ |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 13088 | {"_decimal2ascii", (PyCFunction) unicode__decimal2ascii, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13089 | #endif |
| 13090 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13091 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13092 | {NULL, NULL} |
| 13093 | }; |
| 13094 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13095 | static PyObject * |
| 13096 | unicode_mod(PyObject *v, PyObject *w) |
| 13097 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 13098 | if (!PyUnicode_Check(v)) |
| 13099 | Py_RETURN_NOTIMPLEMENTED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13100 | return PyUnicode_Format(v, w); |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13101 | } |
| 13102 | |
| 13103 | static PyNumberMethods unicode_as_number = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13104 | 0, /*nb_add*/ |
| 13105 | 0, /*nb_subtract*/ |
| 13106 | 0, /*nb_multiply*/ |
| 13107 | unicode_mod, /*nb_remainder*/ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13108 | }; |
| 13109 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13110 | static PySequenceMethods unicode_as_sequence = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13111 | (lenfunc) unicode_length, /* sq_length */ |
| 13112 | PyUnicode_Concat, /* sq_concat */ |
| 13113 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 13114 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
| 13115 | 0, /* sq_slice */ |
| 13116 | 0, /* sq_ass_item */ |
| 13117 | 0, /* sq_ass_slice */ |
| 13118 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13119 | }; |
| 13120 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13121 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13122 | unicode_subscript(PyObject* self, PyObject* item) |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13123 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13124 | if (PyUnicode_READY(self) == -1) |
| 13125 | return NULL; |
| 13126 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 13127 | if (PyIndex_Check(item)) { |
| 13128 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13129 | if (i == -1 && PyErr_Occurred()) |
| 13130 | return NULL; |
| 13131 | if (i < 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13132 | i += PyUnicode_GET_LENGTH(self); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13133 | return unicode_getitem(self, i); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13134 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13135 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13136 | PyObject *result; |
| 13137 | void *src_data, *dest_data; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13138 | int src_kind, dest_kind; |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 13139 | Py_UCS4 ch, max_char, kind_limit; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13140 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13141 | if (PySlice_GetIndicesEx(item, PyUnicode_GET_LENGTH(self), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13142 | &start, &stop, &step, &slicelength) < 0) { |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13143 | return NULL; |
| 13144 | } |
| 13145 | |
| 13146 | if (slicelength <= 0) { |
Victor Stinner | 382955f | 2011-12-11 21:44:00 +0100 | [diff] [blame] | 13147 | Py_INCREF(unicode_empty); |
| 13148 | return unicode_empty; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13149 | } else if (start == 0 && step == 1 && |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 13150 | slicelength == PyUnicode_GET_LENGTH(self)) { |
| 13151 | return unicode_result_unchanged(self); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 13152 | } else if (step == 1) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13153 | return PyUnicode_Substring(self, |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 13154 | start, start + slicelength); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13155 | } |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13156 | /* General case */ |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13157 | src_kind = PyUnicode_KIND(self); |
| 13158 | src_data = PyUnicode_DATA(self); |
Victor Stinner | 55c9911 | 2011-10-13 01:17:06 +0200 | [diff] [blame] | 13159 | if (!PyUnicode_IS_ASCII(self)) { |
| 13160 | kind_limit = kind_maxchar_limit(src_kind); |
| 13161 | max_char = 0; |
| 13162 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 13163 | ch = PyUnicode_READ(src_kind, src_data, cur); |
| 13164 | if (ch > max_char) { |
| 13165 | max_char = ch; |
| 13166 | if (max_char >= kind_limit) |
| 13167 | break; |
| 13168 | } |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 13169 | } |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13170 | } |
Victor Stinner | 55c9911 | 2011-10-13 01:17:06 +0200 | [diff] [blame] | 13171 | else |
| 13172 | max_char = 127; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13173 | result = PyUnicode_New(slicelength, max_char); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13174 | if (result == NULL) |
| 13175 | return NULL; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13176 | dest_kind = PyUnicode_KIND(result); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13177 | dest_data = PyUnicode_DATA(result); |
| 13178 | |
| 13179 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13180 | Py_UCS4 ch = PyUnicode_READ(src_kind, src_data, cur); |
| 13181 | PyUnicode_WRITE(dest_kind, dest_data, i, ch); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13182 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13183 | assert(_PyUnicode_CheckConsistency(result, 1)); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13184 | return result; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13185 | } else { |
| 13186 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 13187 | return NULL; |
| 13188 | } |
| 13189 | } |
| 13190 | |
| 13191 | static PyMappingMethods unicode_as_mapping = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13192 | (lenfunc)unicode_length, /* mp_length */ |
| 13193 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 13194 | (objobjargproc)0, /* mp_ass_subscript */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13195 | }; |
| 13196 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13197 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13198 | /* Helpers for PyUnicode_Format() */ |
| 13199 | |
| 13200 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13201 | 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] | 13202 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13203 | Py_ssize_t argidx = *p_argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13204 | if (argidx < arglen) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13205 | (*p_argidx)++; |
| 13206 | if (arglen < 0) |
| 13207 | return args; |
| 13208 | else |
| 13209 | return PyTuple_GetItem(args, argidx); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13210 | } |
| 13211 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13212 | "not enough arguments for format string"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13213 | return NULL; |
| 13214 | } |
| 13215 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13216 | /* 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] | 13217 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13218 | static int |
| 13219 | formatfloat(PyObject *v, int flags, int prec, int type, |
| 13220 | PyObject **p_output, _PyUnicodeWriter *writer) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13221 | { |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13222 | char *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13223 | double x; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13224 | Py_ssize_t len; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 13225 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13226 | x = PyFloat_AsDouble(v); |
| 13227 | if (x == -1.0 && PyErr_Occurred()) |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13228 | return -1; |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13229 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13230 | if (prec < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13231 | prec = 6; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13232 | |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13233 | p = PyOS_double_to_string(x, type, prec, |
| 13234 | (flags & F_ALT) ? Py_DTSF_ALT : 0, NULL); |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13235 | if (p == NULL) |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13236 | return -1; |
| 13237 | len = strlen(p); |
| 13238 | if (writer) { |
| 13239 | if (_PyUnicodeWriter_Prepare(writer, len, 127) == -1) |
| 13240 | return -1; |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 13241 | unicode_write_cstr(writer->buffer, writer->pos, p, len); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13242 | writer->pos += len; |
| 13243 | } |
| 13244 | else |
| 13245 | *p_output = _PyUnicode_FromASCII(p, len); |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13246 | PyMem_Free(p); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13247 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13248 | } |
| 13249 | |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13250 | /* formatlong() emulates the format codes d, u, o, x and X, and |
| 13251 | * the F_ALT flag, for Python's long (unbounded) ints. It's not used for |
| 13252 | * Python's regular ints. |
| 13253 | * Return value: a new PyUnicodeObject*, or NULL if error. |
| 13254 | * The output string is of the form |
| 13255 | * "-"? ("0x" | "0X")? digit+ |
| 13256 | * "0x"/"0X" are present only for x and X conversions, with F_ALT |
| 13257 | * set in flags. The case of hex digits will be correct, |
| 13258 | * There will be at least prec digits, zero-filled on the left if |
| 13259 | * necessary to get that many. |
| 13260 | * val object to be converted |
| 13261 | * flags bitmask of format flags; only F_ALT is looked at |
| 13262 | * prec minimum number of digits; 0-fill on left if needed |
| 13263 | * type a character in [duoxX]; u acts the same as d |
| 13264 | * |
| 13265 | * CAUTION: o, x and X conversions on regular ints can never |
| 13266 | * produce a '-' sign, but can for Python's unbounded ints. |
| 13267 | */ |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13268 | static PyObject* |
| 13269 | formatlong(PyObject *val, int flags, int prec, int type) |
| 13270 | { |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13271 | PyObject *result = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13272 | char *buf; |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13273 | Py_ssize_t i; |
| 13274 | int sign; /* 1 if '-', else 0 */ |
| 13275 | int len; /* number of characters */ |
| 13276 | Py_ssize_t llen; |
| 13277 | int numdigits; /* len == numnondigits + numdigits */ |
| 13278 | int numnondigits = 0; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13279 | |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13280 | /* Avoid exceeding SSIZE_T_MAX */ |
| 13281 | if (prec > INT_MAX-3) { |
| 13282 | PyErr_SetString(PyExc_OverflowError, |
| 13283 | "precision too large"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13284 | return NULL; |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13285 | } |
| 13286 | |
| 13287 | assert(PyLong_Check(val)); |
| 13288 | |
| 13289 | switch (type) { |
| 13290 | case 'd': |
| 13291 | case 'u': |
| 13292 | /* Special-case boolean: we want 0/1 */ |
Victor Stinner | b11d91d | 2012-04-28 00:25:34 +0200 | [diff] [blame] | 13293 | if (PyBool_Check(val)) |
| 13294 | result = PyNumber_ToBase(val, 10); |
| 13295 | else |
| 13296 | result = Py_TYPE(val)->tp_str(val); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13297 | break; |
| 13298 | case 'o': |
| 13299 | numnondigits = 2; |
| 13300 | result = PyNumber_ToBase(val, 8); |
| 13301 | break; |
| 13302 | case 'x': |
| 13303 | case 'X': |
| 13304 | numnondigits = 2; |
| 13305 | result = PyNumber_ToBase(val, 16); |
| 13306 | break; |
| 13307 | default: |
| 13308 | assert(!"'type' not in [duoxX]"); |
| 13309 | } |
| 13310 | if (!result) |
| 13311 | return NULL; |
| 13312 | |
| 13313 | assert(unicode_modifiable(result)); |
| 13314 | assert(PyUnicode_IS_READY(result)); |
| 13315 | assert(PyUnicode_IS_ASCII(result)); |
| 13316 | |
| 13317 | /* To modify the string in-place, there can only be one reference. */ |
| 13318 | if (Py_REFCNT(result) != 1) { |
| 13319 | PyErr_BadInternalCall(); |
| 13320 | return NULL; |
| 13321 | } |
| 13322 | buf = PyUnicode_DATA(result); |
| 13323 | llen = PyUnicode_GET_LENGTH(result); |
| 13324 | if (llen > INT_MAX) { |
| 13325 | PyErr_SetString(PyExc_ValueError, |
| 13326 | "string too large in _PyBytes_FormatLong"); |
| 13327 | return NULL; |
| 13328 | } |
| 13329 | len = (int)llen; |
| 13330 | sign = buf[0] == '-'; |
| 13331 | numnondigits += sign; |
| 13332 | numdigits = len - numnondigits; |
| 13333 | assert(numdigits > 0); |
| 13334 | |
| 13335 | /* Get rid of base marker unless F_ALT */ |
| 13336 | if (((flags & F_ALT) == 0 && |
| 13337 | (type == 'o' || type == 'x' || type == 'X'))) { |
| 13338 | assert(buf[sign] == '0'); |
| 13339 | assert(buf[sign+1] == 'x' || buf[sign+1] == 'X' || |
| 13340 | buf[sign+1] == 'o'); |
| 13341 | numnondigits -= 2; |
| 13342 | buf += 2; |
| 13343 | len -= 2; |
| 13344 | if (sign) |
| 13345 | buf[0] = '-'; |
| 13346 | assert(len == numnondigits + numdigits); |
| 13347 | assert(numdigits > 0); |
| 13348 | } |
| 13349 | |
| 13350 | /* Fill with leading zeroes to meet minimum width. */ |
| 13351 | if (prec > numdigits) { |
| 13352 | PyObject *r1 = PyBytes_FromStringAndSize(NULL, |
| 13353 | numnondigits + prec); |
| 13354 | char *b1; |
| 13355 | if (!r1) { |
| 13356 | Py_DECREF(result); |
| 13357 | return NULL; |
| 13358 | } |
| 13359 | b1 = PyBytes_AS_STRING(r1); |
| 13360 | for (i = 0; i < numnondigits; ++i) |
| 13361 | *b1++ = *buf++; |
| 13362 | for (i = 0; i < prec - numdigits; i++) |
| 13363 | *b1++ = '0'; |
| 13364 | for (i = 0; i < numdigits; i++) |
| 13365 | *b1++ = *buf++; |
| 13366 | *b1 = '\0'; |
| 13367 | Py_DECREF(result); |
| 13368 | result = r1; |
| 13369 | buf = PyBytes_AS_STRING(result); |
| 13370 | len = numnondigits + prec; |
| 13371 | } |
| 13372 | |
| 13373 | /* Fix up case for hex conversions. */ |
| 13374 | if (type == 'X') { |
| 13375 | /* Need to convert all lower case letters to upper case. |
| 13376 | and need to convert 0x to 0X (and -0x to -0X). */ |
| 13377 | for (i = 0; i < len; i++) |
| 13378 | if (buf[i] >= 'a' && buf[i] <= 'x') |
| 13379 | buf[i] -= 'a'-'A'; |
| 13380 | } |
| 13381 | if (!PyUnicode_Check(result) || len != PyUnicode_GET_LENGTH(result)) { |
| 13382 | PyObject *unicode; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13383 | unicode = _PyUnicode_FromASCII(buf, len); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13384 | Py_DECREF(result); |
| 13385 | result = unicode; |
| 13386 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13387 | return result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13388 | } |
| 13389 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13390 | static Py_UCS4 |
| 13391 | formatchar(PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13392 | { |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 13393 | /* presume that the buffer is at least 3 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 13394 | if (PyUnicode_Check(v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13395 | if (PyUnicode_GET_LENGTH(v) == 1) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13396 | return PyUnicode_READ_CHAR(v, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13397 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13398 | goto onError; |
| 13399 | } |
| 13400 | else { |
| 13401 | /* Integer input truncated to a character */ |
| 13402 | long x; |
| 13403 | x = PyLong_AsLong(v); |
| 13404 | if (x == -1 && PyErr_Occurred()) |
| 13405 | goto onError; |
| 13406 | |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 13407 | if (x < 0 || x > MAX_UNICODE) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13408 | PyErr_SetString(PyExc_OverflowError, |
| 13409 | "%c arg not in range(0x110000)"); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13410 | return (Py_UCS4) -1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13411 | } |
| 13412 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13413 | return (Py_UCS4) x; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13414 | } |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 13415 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13416 | onError: |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 13417 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13418 | "%c requires int or char"); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13419 | return (Py_UCS4) -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13420 | } |
| 13421 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 13422 | PyObject * |
| 13423 | PyUnicode_Format(PyObject *format, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13424 | { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13425 | Py_ssize_t fmtcnt, fmtpos, arglen, argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13426 | int args_owned = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13427 | PyObject *dict = NULL; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13428 | PyObject *temp = NULL; |
| 13429 | PyObject *second = NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13430 | PyObject *uformat; |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13431 | void *fmt; |
| 13432 | enum PyUnicode_Kind kind, fmtkind; |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 13433 | _PyUnicodeWriter writer; |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 13434 | Py_ssize_t sublen; |
| 13435 | Py_UCS4 maxchar; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 13436 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13437 | if (format == NULL || args == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13438 | PyErr_BadInternalCall(); |
| 13439 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13440 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13441 | uformat = PyUnicode_FromObject(format); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 13442 | if (uformat == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13443 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 13444 | if (PyUnicode_READY(uformat) == -1) |
| 13445 | Py_DECREF(uformat); |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13446 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13447 | fmt = PyUnicode_DATA(uformat); |
| 13448 | fmtkind = PyUnicode_KIND(uformat); |
| 13449 | fmtcnt = PyUnicode_GET_LENGTH(uformat); |
| 13450 | fmtpos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13451 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13452 | _PyUnicodeWriter_Init(&writer, fmtcnt + 100); |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13453 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13454 | if (PyTuple_Check(args)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13455 | arglen = PyTuple_Size(args); |
| 13456 | argidx = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13457 | } |
| 13458 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13459 | arglen = -1; |
| 13460 | argidx = -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13461 | } |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 13462 | if (Py_TYPE(args)->tp_as_mapping && !PyTuple_Check(args) && |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 13463 | !PyUnicode_Check(args)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13464 | dict = args; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13465 | |
| 13466 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13467 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) != '%') { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13468 | Py_ssize_t nonfmtpos; |
| 13469 | nonfmtpos = fmtpos++; |
| 13470 | while (fmtcnt >= 0 && |
| 13471 | PyUnicode_READ(fmtkind, fmt, fmtpos) != '%') { |
| 13472 | fmtpos++; |
| 13473 | fmtcnt--; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13474 | } |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13475 | if (fmtcnt < 0) |
| 13476 | fmtpos--; |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 13477 | sublen = fmtpos - nonfmtpos; |
| 13478 | maxchar = _PyUnicode_FindMaxChar(uformat, |
| 13479 | nonfmtpos, nonfmtpos + sublen); |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 13480 | if (_PyUnicodeWriter_Prepare(&writer, sublen, maxchar) == -1) |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13481 | goto onError; |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 13482 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13483 | _PyUnicode_FastCopyCharacters(writer.buffer, writer.pos, |
| 13484 | uformat, nonfmtpos, sublen); |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 13485 | writer.pos += sublen; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13486 | } |
| 13487 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13488 | /* Got a format specifier */ |
| 13489 | int flags = 0; |
| 13490 | Py_ssize_t width = -1; |
| 13491 | int prec = -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13492 | Py_UCS4 c = '\0'; |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13493 | Py_UCS4 fill; |
| 13494 | int sign; |
| 13495 | Py_UCS4 signchar; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13496 | int isnumok; |
| 13497 | PyObject *v = NULL; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13498 | void *pbuf = NULL; |
| 13499 | Py_ssize_t pindex, len; |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 13500 | Py_UCS4 bufmaxchar; |
| 13501 | Py_ssize_t buflen; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13502 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13503 | fmtpos++; |
Victor Stinner | 438106b | 2012-05-02 00:41:57 +0200 | [diff] [blame] | 13504 | c = PyUnicode_READ(fmtkind, fmt, fmtpos); |
| 13505 | if (c == '(') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13506 | Py_ssize_t keystart; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13507 | Py_ssize_t keylen; |
| 13508 | PyObject *key; |
| 13509 | int pcount = 1; |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 13510 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13511 | if (dict == NULL) { |
| 13512 | PyErr_SetString(PyExc_TypeError, |
| 13513 | "format requires a mapping"); |
| 13514 | goto onError; |
| 13515 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13516 | ++fmtpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13517 | --fmtcnt; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13518 | keystart = fmtpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13519 | /* Skip over balanced parentheses */ |
| 13520 | while (pcount > 0 && --fmtcnt >= 0) { |
Victor Stinner | bff7c96 | 2012-05-03 01:44:59 +0200 | [diff] [blame] | 13521 | c = PyUnicode_READ(fmtkind, fmt, fmtpos); |
| 13522 | if (c == ')') |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13523 | --pcount; |
Victor Stinner | bff7c96 | 2012-05-03 01:44:59 +0200 | [diff] [blame] | 13524 | else if (c == '(') |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13525 | ++pcount; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13526 | fmtpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13527 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13528 | keylen = fmtpos - keystart - 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13529 | if (fmtcnt < 0 || pcount > 0) { |
| 13530 | PyErr_SetString(PyExc_ValueError, |
| 13531 | "incomplete format key"); |
| 13532 | goto onError; |
| 13533 | } |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 13534 | key = PyUnicode_Substring(uformat, |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 13535 | keystart, keystart + keylen); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13536 | if (key == NULL) |
| 13537 | goto onError; |
| 13538 | if (args_owned) { |
| 13539 | Py_DECREF(args); |
| 13540 | args_owned = 0; |
| 13541 | } |
| 13542 | args = PyObject_GetItem(dict, key); |
| 13543 | Py_DECREF(key); |
| 13544 | if (args == NULL) { |
| 13545 | goto onError; |
| 13546 | } |
| 13547 | args_owned = 1; |
| 13548 | arglen = -1; |
| 13549 | argidx = -2; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13550 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13551 | while (--fmtcnt >= 0) { |
Victor Stinner | 438106b | 2012-05-02 00:41:57 +0200 | [diff] [blame] | 13552 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
| 13553 | switch (c) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13554 | case '-': flags |= F_LJUST; continue; |
| 13555 | case '+': flags |= F_SIGN; continue; |
| 13556 | case ' ': flags |= F_BLANK; continue; |
| 13557 | case '#': flags |= F_ALT; continue; |
| 13558 | case '0': flags |= F_ZERO; continue; |
| 13559 | } |
| 13560 | break; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13561 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13562 | if (c == '*') { |
| 13563 | v = getnextarg(args, arglen, &argidx); |
| 13564 | if (v == NULL) |
| 13565 | goto onError; |
| 13566 | if (!PyLong_Check(v)) { |
| 13567 | PyErr_SetString(PyExc_TypeError, |
| 13568 | "* wants int"); |
| 13569 | goto onError; |
| 13570 | } |
| 13571 | width = PyLong_AsLong(v); |
| 13572 | if (width == -1 && PyErr_Occurred()) |
| 13573 | goto onError; |
| 13574 | if (width < 0) { |
| 13575 | flags |= F_LJUST; |
| 13576 | width = -width; |
| 13577 | } |
| 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 | } |
| 13581 | else if (c >= '0' && c <= '9') { |
| 13582 | width = c - '0'; |
| 13583 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13584 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13585 | if (c < '0' || c > '9') |
| 13586 | break; |
Martin v. Löwis | b05c073 | 2012-05-15 13:45:49 +0200 | [diff] [blame] | 13587 | /* Since c is unsigned, the RHS would end up as unsigned, |
| 13588 | mixing signed and unsigned comparison. Since c is between |
| 13589 | '0' and '9', casting to int is safe. */ |
| 13590 | if (width > (PY_SSIZE_T_MAX - ((int)c - '0')) / 10) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13591 | PyErr_SetString(PyExc_ValueError, |
| 13592 | "width too big"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13593 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13594 | } |
| 13595 | width = width*10 + (c - '0'); |
| 13596 | } |
| 13597 | } |
| 13598 | if (c == '.') { |
| 13599 | prec = 0; |
| 13600 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13601 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13602 | if (c == '*') { |
| 13603 | v = getnextarg(args, arglen, &argidx); |
| 13604 | if (v == NULL) |
| 13605 | goto onError; |
| 13606 | if (!PyLong_Check(v)) { |
| 13607 | PyErr_SetString(PyExc_TypeError, |
| 13608 | "* wants int"); |
| 13609 | goto onError; |
| 13610 | } |
| 13611 | prec = PyLong_AsLong(v); |
| 13612 | if (prec == -1 && PyErr_Occurred()) |
| 13613 | goto onError; |
| 13614 | if (prec < 0) |
| 13615 | prec = 0; |
| 13616 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13617 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13618 | } |
| 13619 | else if (c >= '0' && c <= '9') { |
| 13620 | prec = c - '0'; |
| 13621 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13622 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13623 | if (c < '0' || c > '9') |
| 13624 | break; |
Martin v. Löwis | b05c073 | 2012-05-15 13:45:49 +0200 | [diff] [blame] | 13625 | if (prec > (INT_MAX - ((int)c - '0')) / 10) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13626 | PyErr_SetString(PyExc_ValueError, |
| 13627 | "prec too big"); |
| 13628 | goto onError; |
| 13629 | } |
| 13630 | prec = prec*10 + (c - '0'); |
| 13631 | } |
| 13632 | } |
| 13633 | } /* prec */ |
| 13634 | if (fmtcnt >= 0) { |
| 13635 | if (c == 'h' || c == 'l' || c == 'L') { |
| 13636 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13637 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13638 | } |
| 13639 | } |
| 13640 | if (fmtcnt < 0) { |
| 13641 | PyErr_SetString(PyExc_ValueError, |
| 13642 | "incomplete format"); |
| 13643 | goto onError; |
| 13644 | } |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13645 | if (fmtcnt == 0) |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 13646 | writer.overallocate = 0; |
Victor Stinner | aff3cc6 | 2012-04-30 05:19:21 +0200 | [diff] [blame] | 13647 | |
| 13648 | if (c == '%') { |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 13649 | if (_PyUnicodeWriter_Prepare(&writer, 1, '%') == -1) |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13650 | goto onError; |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 13651 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, '%'); |
| 13652 | writer.pos += 1; |
Victor Stinner | aff3cc6 | 2012-04-30 05:19:21 +0200 | [diff] [blame] | 13653 | continue; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13654 | } |
Victor Stinner | aff3cc6 | 2012-04-30 05:19:21 +0200 | [diff] [blame] | 13655 | |
Victor Stinner | aff3cc6 | 2012-04-30 05:19:21 +0200 | [diff] [blame] | 13656 | v = getnextarg(args, arglen, &argidx); |
| 13657 | if (v == NULL) |
| 13658 | goto onError; |
| 13659 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13660 | sign = 0; |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13661 | signchar = '\0'; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13662 | fill = ' '; |
| 13663 | switch (c) { |
| 13664 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13665 | case 's': |
| 13666 | case 'r': |
| 13667 | case 'a': |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13668 | if (PyLong_CheckExact(v) && width == -1 && prec == -1) { |
| 13669 | /* Fast path */ |
| 13670 | if (_PyLong_FormatWriter(&writer, v, 10, flags & F_ALT) == -1) |
| 13671 | goto onError; |
| 13672 | goto nextarg; |
| 13673 | } |
| 13674 | |
Victor Stinner | 808fc0a | 2010-03-22 12:50:40 +0000 | [diff] [blame] | 13675 | if (PyUnicode_CheckExact(v) && c == 's') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13676 | temp = v; |
| 13677 | Py_INCREF(temp); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13678 | } |
| 13679 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13680 | if (c == 's') |
| 13681 | temp = PyObject_Str(v); |
| 13682 | else if (c == 'r') |
| 13683 | temp = PyObject_Repr(v); |
| 13684 | else |
| 13685 | temp = PyObject_ASCII(v); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13686 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13687 | break; |
| 13688 | |
| 13689 | case 'i': |
| 13690 | case 'd': |
| 13691 | case 'u': |
| 13692 | case 'o': |
| 13693 | case 'x': |
| 13694 | case 'X': |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13695 | if (PyLong_CheckExact(v) |
| 13696 | && width == -1 && prec == -1 |
| 13697 | && !(flags & (F_SIGN | F_BLANK))) |
| 13698 | { |
| 13699 | /* Fast path */ |
| 13700 | switch(c) |
| 13701 | { |
| 13702 | case 'd': |
| 13703 | case 'i': |
| 13704 | case 'u': |
| 13705 | if (_PyLong_FormatWriter(&writer, v, 10, flags & F_ALT) == -1) |
| 13706 | goto onError; |
| 13707 | goto nextarg; |
| 13708 | case 'x': |
| 13709 | if (_PyLong_FormatWriter(&writer, v, 16, flags & F_ALT) == -1) |
| 13710 | goto onError; |
| 13711 | goto nextarg; |
| 13712 | case 'o': |
| 13713 | if (_PyLong_FormatWriter(&writer, v, 8, flags & F_ALT) == -1) |
| 13714 | goto onError; |
| 13715 | goto nextarg; |
| 13716 | default: |
| 13717 | break; |
| 13718 | } |
| 13719 | } |
| 13720 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13721 | isnumok = 0; |
| 13722 | if (PyNumber_Check(v)) { |
| 13723 | PyObject *iobj=NULL; |
| 13724 | |
| 13725 | if (PyLong_Check(v)) { |
| 13726 | iobj = v; |
| 13727 | Py_INCREF(iobj); |
| 13728 | } |
| 13729 | else { |
| 13730 | iobj = PyNumber_Long(v); |
| 13731 | } |
| 13732 | if (iobj!=NULL) { |
| 13733 | if (PyLong_Check(iobj)) { |
| 13734 | isnumok = 1; |
Victor Stinner | aff3cc6 | 2012-04-30 05:19:21 +0200 | [diff] [blame] | 13735 | sign = 1; |
Senthil Kumaran | 9ebe08d | 2011-07-03 21:03:16 -0700 | [diff] [blame] | 13736 | temp = formatlong(iobj, flags, prec, (c == 'i'? 'd': c)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13737 | Py_DECREF(iobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13738 | } |
| 13739 | else { |
| 13740 | Py_DECREF(iobj); |
| 13741 | } |
| 13742 | } |
| 13743 | } |
| 13744 | if (!isnumok) { |
| 13745 | PyErr_Format(PyExc_TypeError, |
| 13746 | "%%%c format: a number is required, " |
| 13747 | "not %.200s", (char)c, Py_TYPE(v)->tp_name); |
| 13748 | goto onError; |
| 13749 | } |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13750 | if (flags & F_ZERO) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13751 | fill = '0'; |
| 13752 | break; |
| 13753 | |
| 13754 | case 'e': |
| 13755 | case 'E': |
| 13756 | case 'f': |
| 13757 | case 'F': |
| 13758 | case 'g': |
| 13759 | case 'G': |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13760 | if (width == -1 && prec == -1 |
| 13761 | && !(flags & (F_SIGN | F_BLANK))) |
| 13762 | { |
| 13763 | /* Fast path */ |
| 13764 | if (formatfloat(v, flags, prec, c, NULL, &writer) == -1) |
| 13765 | goto onError; |
| 13766 | goto nextarg; |
| 13767 | } |
| 13768 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13769 | sign = 1; |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13770 | if (flags & F_ZERO) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13771 | fill = '0'; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13772 | if (formatfloat(v, flags, prec, c, &temp, NULL) == -1) |
| 13773 | temp = NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13774 | break; |
| 13775 | |
| 13776 | case 'c': |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13777 | { |
| 13778 | Py_UCS4 ch = formatchar(v); |
| 13779 | if (ch == (Py_UCS4) -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13780 | goto onError; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13781 | if (width == -1 && prec == -1) { |
| 13782 | /* Fast path */ |
| 13783 | if (_PyUnicodeWriter_Prepare(&writer, 1, ch) == -1) |
| 13784 | goto onError; |
| 13785 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, ch); |
| 13786 | writer.pos += 1; |
| 13787 | goto nextarg; |
| 13788 | } |
Victor Stinner | b5c3ea3 | 2012-05-02 00:29:36 +0200 | [diff] [blame] | 13789 | temp = PyUnicode_FromOrdinal(ch); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13790 | break; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13791 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13792 | |
| 13793 | default: |
| 13794 | PyErr_Format(PyExc_ValueError, |
| 13795 | "unsupported format character '%c' (0x%x) " |
| 13796 | "at index %zd", |
| 13797 | (31<=c && c<=126) ? (char)c : '?', |
| 13798 | (int)c, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13799 | fmtpos - 1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13800 | goto onError; |
| 13801 | } |
Victor Stinner | aff3cc6 | 2012-04-30 05:19:21 +0200 | [diff] [blame] | 13802 | if (temp == NULL) |
| 13803 | goto onError; |
| 13804 | assert (PyUnicode_Check(temp)); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13805 | |
| 13806 | if (width == -1 && prec == -1 |
| 13807 | && !(flags & (F_SIGN | F_BLANK))) |
| 13808 | { |
| 13809 | /* Fast path */ |
| 13810 | if (_PyUnicodeWriter_WriteStr(&writer, temp) == -1) |
| 13811 | goto onError; |
| 13812 | goto nextarg; |
| 13813 | } |
| 13814 | |
Victor Stinner | aff3cc6 | 2012-04-30 05:19:21 +0200 | [diff] [blame] | 13815 | if (PyUnicode_READY(temp) == -1) { |
| 13816 | Py_CLEAR(temp); |
| 13817 | goto onError; |
| 13818 | } |
| 13819 | kind = PyUnicode_KIND(temp); |
| 13820 | pbuf = PyUnicode_DATA(temp); |
| 13821 | len = PyUnicode_GET_LENGTH(temp); |
| 13822 | |
| 13823 | if (c == 's' || c == 'r' || c == 'a') { |
| 13824 | if (prec >= 0 && len > prec) |
| 13825 | len = prec; |
| 13826 | } |
| 13827 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13828 | /* pbuf is initialized here. */ |
| 13829 | pindex = 0; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13830 | if (sign) { |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13831 | Py_UCS4 ch = PyUnicode_READ(kind, pbuf, pindex); |
| 13832 | if (ch == '-' || ch == '+') { |
| 13833 | signchar = ch; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13834 | len--; |
| 13835 | pindex++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13836 | } |
| 13837 | else if (flags & F_SIGN) |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13838 | signchar = '+'; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13839 | else if (flags & F_BLANK) |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13840 | signchar = ' '; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13841 | else |
| 13842 | sign = 0; |
| 13843 | } |
| 13844 | if (width < len) |
| 13845 | width = len; |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 13846 | |
| 13847 | /* Compute the length and maximum character of the |
| 13848 | written characters */ |
| 13849 | bufmaxchar = 127; |
| 13850 | if (!(flags & F_LJUST)) { |
| 13851 | if (sign) { |
| 13852 | if ((width-1) > len) |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13853 | bufmaxchar = MAX_MAXCHAR(bufmaxchar, fill); |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 13854 | } |
| 13855 | else { |
| 13856 | if (width > len) |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13857 | bufmaxchar = MAX_MAXCHAR(bufmaxchar, fill); |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 13858 | } |
| 13859 | } |
| 13860 | maxchar = _PyUnicode_FindMaxChar(temp, 0, pindex+len); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13861 | bufmaxchar = MAX_MAXCHAR(bufmaxchar, maxchar); |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 13862 | |
| 13863 | buflen = width; |
| 13864 | if (sign && len == width) |
| 13865 | buflen++; |
| 13866 | |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 13867 | if (_PyUnicodeWriter_Prepare(&writer, buflen, bufmaxchar) == -1) |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 13868 | goto onError; |
| 13869 | |
| 13870 | /* Write characters */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13871 | if (sign) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13872 | if (fill != ' ') { |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 13873 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, signchar); |
| 13874 | writer.pos += 1; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13875 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13876 | if (width > len) |
| 13877 | width--; |
| 13878 | } |
| 13879 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13880 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13881 | assert(PyUnicode_READ(kind, pbuf, pindex + 1) == c); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13882 | if (fill != ' ') { |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13883 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, '0'); |
| 13884 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos+1, c); |
| 13885 | writer.pos += 2; |
| 13886 | pindex += 2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13887 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13888 | width -= 2; |
| 13889 | if (width < 0) |
| 13890 | width = 0; |
| 13891 | len -= 2; |
| 13892 | } |
| 13893 | if (width > len && !(flags & F_LJUST)) { |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13894 | sublen = width - len; |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13895 | FILL(writer.kind, writer.data, fill, writer.pos, sublen); |
| 13896 | writer.pos += sublen; |
Antoine Pitrou | 978b9d2 | 2011-10-07 12:35:48 +0200 | [diff] [blame] | 13897 | width = len; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13898 | } |
| 13899 | if (fill == ' ') { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13900 | if (sign) { |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 13901 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, signchar); |
| 13902 | writer.pos += 1; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13903 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13904 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13905 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
| 13906 | assert(PyUnicode_READ(kind, pbuf, pindex+1) == c); |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13907 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, '0'); |
| 13908 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos+1, c); |
| 13909 | writer.pos += 2; |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13910 | pindex += 2; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13911 | } |
| 13912 | } |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13913 | |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 13914 | if (len) { |
| 13915 | _PyUnicode_FastCopyCharacters(writer.buffer, writer.pos, |
| 13916 | temp, pindex, len); |
| 13917 | writer.pos += len; |
| 13918 | } |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13919 | if (width > len) { |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 13920 | sublen = width - len; |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13921 | FILL(writer.kind, writer.data, ' ', writer.pos, sublen); |
| 13922 | writer.pos += sublen; |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13923 | } |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 13924 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13925 | nextarg: |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13926 | if (dict && (argidx < arglen) && c != '%') { |
| 13927 | PyErr_SetString(PyExc_TypeError, |
| 13928 | "not all arguments converted during string formatting"); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13929 | goto onError; |
| 13930 | } |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13931 | Py_CLEAR(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13932 | } /* '%' */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13933 | } /* until end */ |
| 13934 | if (argidx < arglen && !dict) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13935 | PyErr_SetString(PyExc_TypeError, |
| 13936 | "not all arguments converted during string formatting"); |
| 13937 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13938 | } |
| 13939 | |
| 13940 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13941 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13942 | } |
| 13943 | Py_DECREF(uformat); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13944 | Py_XDECREF(temp); |
| 13945 | Py_XDECREF(second); |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 13946 | return _PyUnicodeWriter_Finish(&writer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13947 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13948 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13949 | Py_DECREF(uformat); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13950 | Py_XDECREF(temp); |
| 13951 | Py_XDECREF(second); |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 13952 | _PyUnicodeWriter_Dealloc(&writer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13953 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13954 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13955 | } |
| 13956 | return NULL; |
| 13957 | } |
| 13958 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 13959 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 13960 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 13961 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13962 | static PyObject * |
| 13963 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 13964 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13965 | PyObject *x = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13966 | static char *kwlist[] = {"object", "encoding", "errors", 0}; |
| 13967 | char *encoding = NULL; |
| 13968 | char *errors = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13969 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13970 | if (type != &PyUnicode_Type) |
| 13971 | return unicode_subtype_new(type, args, kwds); |
| 13972 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:str", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13973 | kwlist, &x, &encoding, &errors)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13974 | return NULL; |
Victor Stinner | 382955f | 2011-12-11 21:44:00 +0100 | [diff] [blame] | 13975 | if (x == NULL) { |
| 13976 | Py_INCREF(unicode_empty); |
| 13977 | return unicode_empty; |
| 13978 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13979 | if (encoding == NULL && errors == NULL) |
| 13980 | return PyObject_Str(x); |
| 13981 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13982 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13983 | } |
| 13984 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 13985 | static PyObject * |
| 13986 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 13987 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13988 | PyObject *unicode, *self; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13989 | Py_ssize_t length, char_size; |
| 13990 | int share_wstr, share_utf8; |
| 13991 | unsigned int kind; |
| 13992 | void *data; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 13993 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13994 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13995 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13996 | unicode = unicode_new(&PyUnicode_Type, args, kwds); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 13997 | if (unicode == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13998 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 13999 | assert(_PyUnicode_CHECK(unicode)); |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 14000 | if (PyUnicode_READY(unicode) == -1) { |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 14001 | Py_DECREF(unicode); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14002 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 14003 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14004 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14005 | self = type->tp_alloc(type, 0); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14006 | if (self == NULL) { |
| 14007 | Py_DECREF(unicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14008 | return NULL; |
| 14009 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14010 | kind = PyUnicode_KIND(unicode); |
| 14011 | length = PyUnicode_GET_LENGTH(unicode); |
| 14012 | |
| 14013 | _PyUnicode_LENGTH(self) = length; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 14014 | #ifdef Py_DEBUG |
| 14015 | _PyUnicode_HASH(self) = -1; |
| 14016 | #else |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14017 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 14018 | #endif |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14019 | _PyUnicode_STATE(self).interned = 0; |
| 14020 | _PyUnicode_STATE(self).kind = kind; |
| 14021 | _PyUnicode_STATE(self).compact = 0; |
Victor Stinner | 3cf4637 | 2011-10-03 14:42:15 +0200 | [diff] [blame] | 14022 | _PyUnicode_STATE(self).ascii = _PyUnicode_STATE(unicode).ascii; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14023 | _PyUnicode_STATE(self).ready = 1; |
| 14024 | _PyUnicode_WSTR(self) = NULL; |
| 14025 | _PyUnicode_UTF8_LENGTH(self) = 0; |
| 14026 | _PyUnicode_UTF8(self) = NULL; |
| 14027 | _PyUnicode_WSTR_LENGTH(self) = 0; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 14028 | _PyUnicode_DATA_ANY(self) = NULL; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14029 | |
| 14030 | share_utf8 = 0; |
| 14031 | share_wstr = 0; |
| 14032 | if (kind == PyUnicode_1BYTE_KIND) { |
| 14033 | char_size = 1; |
| 14034 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) |
| 14035 | share_utf8 = 1; |
| 14036 | } |
| 14037 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 14038 | char_size = 2; |
| 14039 | if (sizeof(wchar_t) == 2) |
| 14040 | share_wstr = 1; |
| 14041 | } |
| 14042 | else { |
| 14043 | assert(kind == PyUnicode_4BYTE_KIND); |
| 14044 | char_size = 4; |
| 14045 | if (sizeof(wchar_t) == 4) |
| 14046 | share_wstr = 1; |
| 14047 | } |
| 14048 | |
| 14049 | /* Ensure we won't overflow the length. */ |
| 14050 | if (length > (PY_SSIZE_T_MAX / char_size - 1)) { |
| 14051 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14052 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14053 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14054 | data = PyObject_MALLOC((length + 1) * char_size); |
| 14055 | if (data == NULL) { |
| 14056 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14057 | goto onError; |
| 14058 | } |
| 14059 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 14060 | _PyUnicode_DATA_ANY(self) = data; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14061 | if (share_utf8) { |
| 14062 | _PyUnicode_UTF8_LENGTH(self) = length; |
| 14063 | _PyUnicode_UTF8(self) = data; |
| 14064 | } |
| 14065 | if (share_wstr) { |
| 14066 | _PyUnicode_WSTR_LENGTH(self) = length; |
| 14067 | _PyUnicode_WSTR(self) = (wchar_t *)data; |
| 14068 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14069 | |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14070 | Py_MEMCPY(data, PyUnicode_DATA(unicode), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 14071 | kind * (length + 1)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 14072 | assert(_PyUnicode_CheckConsistency(self, 1)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 14073 | #ifdef Py_DEBUG |
| 14074 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
| 14075 | #endif |
Victor Stinner | dd18d3a | 2011-10-22 11:08:10 +0200 | [diff] [blame] | 14076 | Py_DECREF(unicode); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 14077 | return self; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14078 | |
| 14079 | onError: |
| 14080 | Py_DECREF(unicode); |
| 14081 | Py_DECREF(self); |
| 14082 | return NULL; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14083 | } |
| 14084 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 14085 | PyDoc_STRVAR(unicode_doc, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14086 | "str(string[, encoding[, errors]]) -> str\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14087 | \n\ |
Collin Winter | d474ce8 | 2007-08-07 19:42:11 +0000 | [diff] [blame] | 14088 | Create a new string object from the given encoded string.\n\ |
Skip Montanaro | 35b37a5 | 2002-07-26 16:22:46 +0000 | [diff] [blame] | 14089 | encoding defaults to the current default string encoding.\n\ |
| 14090 | errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14091 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14092 | static PyObject *unicode_iter(PyObject *seq); |
| 14093 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14094 | PyTypeObject PyUnicode_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 14095 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14096 | "str", /* tp_name */ |
| 14097 | sizeof(PyUnicodeObject), /* tp_size */ |
| 14098 | 0, /* tp_itemsize */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14099 | /* Slots */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14100 | (destructor)unicode_dealloc, /* tp_dealloc */ |
| 14101 | 0, /* tp_print */ |
| 14102 | 0, /* tp_getattr */ |
| 14103 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 14104 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14105 | unicode_repr, /* tp_repr */ |
| 14106 | &unicode_as_number, /* tp_as_number */ |
| 14107 | &unicode_as_sequence, /* tp_as_sequence */ |
| 14108 | &unicode_as_mapping, /* tp_as_mapping */ |
| 14109 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 14110 | 0, /* tp_call*/ |
| 14111 | (reprfunc) unicode_str, /* tp_str */ |
| 14112 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 14113 | 0, /* tp_setattro */ |
| 14114 | 0, /* tp_as_buffer */ |
| 14115 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14116 | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14117 | unicode_doc, /* tp_doc */ |
| 14118 | 0, /* tp_traverse */ |
| 14119 | 0, /* tp_clear */ |
| 14120 | PyUnicode_RichCompare, /* tp_richcompare */ |
| 14121 | 0, /* tp_weaklistoffset */ |
| 14122 | unicode_iter, /* tp_iter */ |
| 14123 | 0, /* tp_iternext */ |
| 14124 | unicode_methods, /* tp_methods */ |
| 14125 | 0, /* tp_members */ |
| 14126 | 0, /* tp_getset */ |
| 14127 | &PyBaseObject_Type, /* tp_base */ |
| 14128 | 0, /* tp_dict */ |
| 14129 | 0, /* tp_descr_get */ |
| 14130 | 0, /* tp_descr_set */ |
| 14131 | 0, /* tp_dictoffset */ |
| 14132 | 0, /* tp_init */ |
| 14133 | 0, /* tp_alloc */ |
| 14134 | unicode_new, /* tp_new */ |
| 14135 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14136 | }; |
| 14137 | |
| 14138 | /* Initialize the Unicode implementation */ |
| 14139 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 14140 | int _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14141 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 14142 | int i; |
| 14143 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14144 | /* XXX - move this array to unicodectype.c ? */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14145 | Py_UCS2 linebreak[] = { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14146 | 0x000A, /* LINE FEED */ |
| 14147 | 0x000D, /* CARRIAGE RETURN */ |
| 14148 | 0x001C, /* FILE SEPARATOR */ |
| 14149 | 0x001D, /* GROUP SEPARATOR */ |
| 14150 | 0x001E, /* RECORD SEPARATOR */ |
| 14151 | 0x0085, /* NEXT LINE */ |
| 14152 | 0x2028, /* LINE SEPARATOR */ |
| 14153 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 14154 | }; |
| 14155 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 14156 | /* Init the implementation */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 14157 | unicode_empty = PyUnicode_New(0, 0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 14158 | if (!unicode_empty) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14159 | Py_FatalError("Can't create empty string"); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 14160 | assert(_PyUnicode_CheckConsistency(unicode_empty, 1)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 14161 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 14162 | for (i = 0; i < 256; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14163 | unicode_latin1[i] = NULL; |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 14164 | if (PyType_Ready(&PyUnicode_Type) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14165 | Py_FatalError("Can't initialize 'unicode'"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14166 | |
| 14167 | /* initialize the linebreak bloom filter */ |
| 14168 | bloom_linebreak = make_bloom_mask( |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14169 | PyUnicode_2BYTE_KIND, linebreak, |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 14170 | Py_ARRAY_LENGTH(linebreak)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 14171 | |
| 14172 | PyType_Ready(&EncodingMapType); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 14173 | |
| 14174 | #ifdef HAVE_MBCS |
| 14175 | winver.dwOSVersionInfoSize = sizeof(winver); |
| 14176 | if (!GetVersionEx((OSVERSIONINFO*)&winver)) { |
| 14177 | PyErr_SetFromWindowsErr(0); |
| 14178 | return -1; |
| 14179 | } |
| 14180 | #endif |
| 14181 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14182 | } |
| 14183 | |
| 14184 | /* Finalize the Unicode implementation */ |
| 14185 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 14186 | int |
| 14187 | PyUnicode_ClearFreeList(void) |
| 14188 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14189 | return 0; |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 14190 | } |
| 14191 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14192 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 14193 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14194 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 14195 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14196 | |
Guido van Rossum | 4ae8ef8 | 2000-10-03 18:09:04 +0000 | [diff] [blame] | 14197 | Py_XDECREF(unicode_empty); |
| 14198 | unicode_empty = NULL; |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 14199 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 14200 | for (i = 0; i < 256; i++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14201 | if (unicode_latin1[i]) { |
| 14202 | Py_DECREF(unicode_latin1[i]); |
| 14203 | unicode_latin1[i] = NULL; |
| 14204 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 14205 | } |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 14206 | _PyUnicode_ClearStaticStrings(); |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 14207 | (void)PyUnicode_ClearFreeList(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14208 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 14209 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14210 | void |
| 14211 | PyUnicode_InternInPlace(PyObject **p) |
| 14212 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14213 | register PyObject *s = *p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14214 | PyObject *t; |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 14215 | #ifdef Py_DEBUG |
| 14216 | assert(s != NULL); |
| 14217 | assert(_PyUnicode_CHECK(s)); |
| 14218 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14219 | if (s == NULL || !PyUnicode_Check(s)) |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 14220 | return; |
| 14221 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14222 | /* If it's a subclass, we don't really know what putting |
| 14223 | it in the interned dict might do. */ |
| 14224 | if (!PyUnicode_CheckExact(s)) |
| 14225 | return; |
| 14226 | if (PyUnicode_CHECK_INTERNED(s)) |
| 14227 | return; |
| 14228 | if (interned == NULL) { |
| 14229 | interned = PyDict_New(); |
| 14230 | if (interned == NULL) { |
| 14231 | PyErr_Clear(); /* Don't leave an exception */ |
| 14232 | return; |
| 14233 | } |
| 14234 | } |
| 14235 | /* It might be that the GetItem call fails even |
| 14236 | though the key is present in the dictionary, |
| 14237 | namely when this happens during a stack overflow. */ |
| 14238 | Py_ALLOW_RECURSION |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 14239 | t = PyDict_GetItem(interned, s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14240 | Py_END_ALLOW_RECURSION |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 14241 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14242 | if (t) { |
| 14243 | Py_INCREF(t); |
| 14244 | Py_DECREF(*p); |
| 14245 | *p = t; |
| 14246 | return; |
| 14247 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14248 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14249 | PyThreadState_GET()->recursion_critical = 1; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 14250 | if (PyDict_SetItem(interned, s, s) < 0) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14251 | PyErr_Clear(); |
| 14252 | PyThreadState_GET()->recursion_critical = 0; |
| 14253 | return; |
| 14254 | } |
| 14255 | PyThreadState_GET()->recursion_critical = 0; |
| 14256 | /* The two references in interned are not counted by refcnt. |
| 14257 | The deallocator will take care of this */ |
| 14258 | Py_REFCNT(s) -= 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14259 | _PyUnicode_STATE(s).interned = SSTATE_INTERNED_MORTAL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14260 | } |
| 14261 | |
| 14262 | void |
| 14263 | PyUnicode_InternImmortal(PyObject **p) |
| 14264 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14265 | PyUnicode_InternInPlace(p); |
| 14266 | if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { |
Victor Stinner | af9e4b8 | 2011-10-23 20:07:00 +0200 | [diff] [blame] | 14267 | _PyUnicode_STATE(*p).interned = SSTATE_INTERNED_IMMORTAL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14268 | Py_INCREF(*p); |
| 14269 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14270 | } |
| 14271 | |
| 14272 | PyObject * |
| 14273 | PyUnicode_InternFromString(const char *cp) |
| 14274 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14275 | PyObject *s = PyUnicode_FromString(cp); |
| 14276 | if (s == NULL) |
| 14277 | return NULL; |
| 14278 | PyUnicode_InternInPlace(&s); |
| 14279 | return s; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14280 | } |
| 14281 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 14282 | void |
| 14283 | _Py_ReleaseInternedUnicodeStrings(void) |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14284 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14285 | PyObject *keys; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14286 | PyObject *s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14287 | Py_ssize_t i, n; |
| 14288 | Py_ssize_t immortal_size = 0, mortal_size = 0; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14289 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14290 | if (interned == NULL || !PyDict_Check(interned)) |
| 14291 | return; |
| 14292 | keys = PyDict_Keys(interned); |
| 14293 | if (keys == NULL || !PyList_Check(keys)) { |
| 14294 | PyErr_Clear(); |
| 14295 | return; |
| 14296 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14297 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14298 | /* Since _Py_ReleaseInternedUnicodeStrings() is intended to help a leak |
| 14299 | detector, interned unicode strings are not forcibly deallocated; |
| 14300 | rather, we give them their stolen references back, and then clear |
| 14301 | and DECREF the interned dict. */ |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14302 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14303 | n = PyList_GET_SIZE(keys); |
| 14304 | fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14305 | n); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14306 | for (i = 0; i < n; i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14307 | s = PyList_GET_ITEM(keys, i); |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 14308 | if (PyUnicode_READY(s) == -1) { |
| 14309 | assert(0 && "could not ready string"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14310 | fprintf(stderr, "could not ready string\n"); |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 14311 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14312 | switch (PyUnicode_CHECK_INTERNED(s)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14313 | case SSTATE_NOT_INTERNED: |
| 14314 | /* XXX Shouldn't happen */ |
| 14315 | break; |
| 14316 | case SSTATE_INTERNED_IMMORTAL: |
| 14317 | Py_REFCNT(s) += 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14318 | immortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14319 | break; |
| 14320 | case SSTATE_INTERNED_MORTAL: |
| 14321 | Py_REFCNT(s) += 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14322 | mortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14323 | break; |
| 14324 | default: |
| 14325 | Py_FatalError("Inconsistent interned string state."); |
| 14326 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14327 | _PyUnicode_STATE(s).interned = SSTATE_NOT_INTERNED; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14328 | } |
| 14329 | fprintf(stderr, "total size of all interned strings: " |
| 14330 | "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " |
| 14331 | "mortal/immortal\n", mortal_size, immortal_size); |
| 14332 | Py_DECREF(keys); |
| 14333 | PyDict_Clear(interned); |
| 14334 | Py_DECREF(interned); |
| 14335 | interned = NULL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14336 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14337 | |
| 14338 | |
| 14339 | /********************* Unicode Iterator **************************/ |
| 14340 | |
| 14341 | typedef struct { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14342 | PyObject_HEAD |
| 14343 | Py_ssize_t it_index; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14344 | PyObject *it_seq; /* Set to NULL when iterator is exhausted */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14345 | } unicodeiterobject; |
| 14346 | |
| 14347 | static void |
| 14348 | unicodeiter_dealloc(unicodeiterobject *it) |
| 14349 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14350 | _PyObject_GC_UNTRACK(it); |
| 14351 | Py_XDECREF(it->it_seq); |
| 14352 | PyObject_GC_Del(it); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14353 | } |
| 14354 | |
| 14355 | static int |
| 14356 | unicodeiter_traverse(unicodeiterobject *it, visitproc visit, void *arg) |
| 14357 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14358 | Py_VISIT(it->it_seq); |
| 14359 | return 0; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14360 | } |
| 14361 | |
| 14362 | static PyObject * |
| 14363 | unicodeiter_next(unicodeiterobject *it) |
| 14364 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14365 | PyObject *seq, *item; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14366 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14367 | assert(it != NULL); |
| 14368 | seq = it->it_seq; |
| 14369 | if (seq == NULL) |
| 14370 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 14371 | assert(_PyUnicode_CHECK(seq)); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14372 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14373 | if (it->it_index < PyUnicode_GET_LENGTH(seq)) { |
| 14374 | int kind = PyUnicode_KIND(seq); |
| 14375 | void *data = PyUnicode_DATA(seq); |
| 14376 | Py_UCS4 chr = PyUnicode_READ(kind, data, it->it_index); |
| 14377 | item = PyUnicode_FromOrdinal(chr); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14378 | if (item != NULL) |
| 14379 | ++it->it_index; |
| 14380 | return item; |
| 14381 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14382 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14383 | Py_DECREF(seq); |
| 14384 | it->it_seq = NULL; |
| 14385 | return NULL; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14386 | } |
| 14387 | |
| 14388 | static PyObject * |
| 14389 | unicodeiter_len(unicodeiterobject *it) |
| 14390 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14391 | Py_ssize_t len = 0; |
| 14392 | if (it->it_seq) |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 14393 | len = PyUnicode_GET_LENGTH(it->it_seq) - it->it_index; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14394 | return PyLong_FromSsize_t(len); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14395 | } |
| 14396 | |
| 14397 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
| 14398 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 14399 | static PyObject * |
| 14400 | unicodeiter_reduce(unicodeiterobject *it) |
| 14401 | { |
| 14402 | if (it->it_seq != NULL) { |
Antoine Pitrou | a701388 | 2012-04-05 00:04:20 +0200 | [diff] [blame] | 14403 | return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("iter"), |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 14404 | it->it_seq, it->it_index); |
| 14405 | } else { |
| 14406 | PyObject *u = PyUnicode_FromUnicode(NULL, 0); |
| 14407 | if (u == NULL) |
| 14408 | return NULL; |
Antoine Pitrou | a701388 | 2012-04-05 00:04:20 +0200 | [diff] [blame] | 14409 | return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), u); |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 14410 | } |
| 14411 | } |
| 14412 | |
| 14413 | PyDoc_STRVAR(reduce_doc, "Return state information for pickling."); |
| 14414 | |
| 14415 | static PyObject * |
| 14416 | unicodeiter_setstate(unicodeiterobject *it, PyObject *state) |
| 14417 | { |
| 14418 | Py_ssize_t index = PyLong_AsSsize_t(state); |
| 14419 | if (index == -1 && PyErr_Occurred()) |
| 14420 | return NULL; |
| 14421 | if (index < 0) |
| 14422 | index = 0; |
| 14423 | it->it_index = index; |
| 14424 | Py_RETURN_NONE; |
| 14425 | } |
| 14426 | |
| 14427 | PyDoc_STRVAR(setstate_doc, "Set state information for unpickling."); |
| 14428 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14429 | static PyMethodDef unicodeiter_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14430 | {"__length_hint__", (PyCFunction)unicodeiter_len, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14431 | length_hint_doc}, |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 14432 | {"__reduce__", (PyCFunction)unicodeiter_reduce, METH_NOARGS, |
| 14433 | reduce_doc}, |
| 14434 | {"__setstate__", (PyCFunction)unicodeiter_setstate, METH_O, |
| 14435 | setstate_doc}, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14436 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14437 | }; |
| 14438 | |
| 14439 | PyTypeObject PyUnicodeIter_Type = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14440 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 14441 | "str_iterator", /* tp_name */ |
| 14442 | sizeof(unicodeiterobject), /* tp_basicsize */ |
| 14443 | 0, /* tp_itemsize */ |
| 14444 | /* methods */ |
| 14445 | (destructor)unicodeiter_dealloc, /* tp_dealloc */ |
| 14446 | 0, /* tp_print */ |
| 14447 | 0, /* tp_getattr */ |
| 14448 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 14449 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14450 | 0, /* tp_repr */ |
| 14451 | 0, /* tp_as_number */ |
| 14452 | 0, /* tp_as_sequence */ |
| 14453 | 0, /* tp_as_mapping */ |
| 14454 | 0, /* tp_hash */ |
| 14455 | 0, /* tp_call */ |
| 14456 | 0, /* tp_str */ |
| 14457 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 14458 | 0, /* tp_setattro */ |
| 14459 | 0, /* tp_as_buffer */ |
| 14460 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 14461 | 0, /* tp_doc */ |
| 14462 | (traverseproc)unicodeiter_traverse, /* tp_traverse */ |
| 14463 | 0, /* tp_clear */ |
| 14464 | 0, /* tp_richcompare */ |
| 14465 | 0, /* tp_weaklistoffset */ |
| 14466 | PyObject_SelfIter, /* tp_iter */ |
| 14467 | (iternextfunc)unicodeiter_next, /* tp_iternext */ |
| 14468 | unicodeiter_methods, /* tp_methods */ |
| 14469 | 0, |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14470 | }; |
| 14471 | |
| 14472 | static PyObject * |
| 14473 | unicode_iter(PyObject *seq) |
| 14474 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14475 | unicodeiterobject *it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14476 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14477 | if (!PyUnicode_Check(seq)) { |
| 14478 | PyErr_BadInternalCall(); |
| 14479 | return NULL; |
| 14480 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14481 | if (PyUnicode_READY(seq) == -1) |
| 14482 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14483 | it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type); |
| 14484 | if (it == NULL) |
| 14485 | return NULL; |
| 14486 | it->it_index = 0; |
| 14487 | Py_INCREF(seq); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14488 | it->it_seq = seq; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14489 | _PyObject_GC_TRACK(it); |
| 14490 | return (PyObject *)it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14491 | } |
| 14492 | |
Martin v. Löwis | 0d3072e | 2011-10-31 08:40:56 +0100 | [diff] [blame] | 14493 | |
| 14494 | size_t |
| 14495 | Py_UNICODE_strlen(const Py_UNICODE *u) |
| 14496 | { |
| 14497 | int res = 0; |
| 14498 | while(*u++) |
| 14499 | res++; |
| 14500 | return res; |
| 14501 | } |
| 14502 | |
| 14503 | Py_UNICODE* |
| 14504 | Py_UNICODE_strcpy(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 14505 | { |
| 14506 | Py_UNICODE *u = s1; |
| 14507 | while ((*u++ = *s2++)); |
| 14508 | return s1; |
| 14509 | } |
| 14510 | |
| 14511 | Py_UNICODE* |
| 14512 | Py_UNICODE_strncpy(Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 14513 | { |
| 14514 | Py_UNICODE *u = s1; |
| 14515 | while ((*u++ = *s2++)) |
| 14516 | if (n-- == 0) |
| 14517 | break; |
| 14518 | return s1; |
| 14519 | } |
| 14520 | |
| 14521 | Py_UNICODE* |
| 14522 | Py_UNICODE_strcat(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 14523 | { |
| 14524 | Py_UNICODE *u1 = s1; |
| 14525 | u1 += Py_UNICODE_strlen(u1); |
| 14526 | Py_UNICODE_strcpy(u1, s2); |
| 14527 | return s1; |
| 14528 | } |
| 14529 | |
| 14530 | int |
| 14531 | Py_UNICODE_strcmp(const Py_UNICODE *s1, const Py_UNICODE *s2) |
| 14532 | { |
| 14533 | while (*s1 && *s2 && *s1 == *s2) |
| 14534 | s1++, s2++; |
| 14535 | if (*s1 && *s2) |
| 14536 | return (*s1 < *s2) ? -1 : +1; |
| 14537 | if (*s1) |
| 14538 | return 1; |
| 14539 | if (*s2) |
| 14540 | return -1; |
| 14541 | return 0; |
| 14542 | } |
| 14543 | |
| 14544 | int |
| 14545 | Py_UNICODE_strncmp(const Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 14546 | { |
| 14547 | register Py_UNICODE u1, u2; |
| 14548 | for (; n != 0; n--) { |
| 14549 | u1 = *s1; |
| 14550 | u2 = *s2; |
| 14551 | if (u1 != u2) |
| 14552 | return (u1 < u2) ? -1 : +1; |
| 14553 | if (u1 == '\0') |
| 14554 | return 0; |
| 14555 | s1++; |
| 14556 | s2++; |
| 14557 | } |
| 14558 | return 0; |
| 14559 | } |
| 14560 | |
| 14561 | Py_UNICODE* |
| 14562 | Py_UNICODE_strchr(const Py_UNICODE *s, Py_UNICODE c) |
| 14563 | { |
| 14564 | const Py_UNICODE *p; |
| 14565 | for (p = s; *p; p++) |
| 14566 | if (*p == c) |
| 14567 | return (Py_UNICODE*)p; |
| 14568 | return NULL; |
| 14569 | } |
| 14570 | |
| 14571 | Py_UNICODE* |
| 14572 | Py_UNICODE_strrchr(const Py_UNICODE *s, Py_UNICODE c) |
| 14573 | { |
| 14574 | const Py_UNICODE *p; |
| 14575 | p = s + Py_UNICODE_strlen(s); |
| 14576 | while (p != s) { |
| 14577 | p--; |
| 14578 | if (*p == c) |
| 14579 | return (Py_UNICODE*)p; |
| 14580 | } |
| 14581 | return NULL; |
| 14582 | } |
Victor Stinner | 331ea92 | 2010-08-10 16:37:20 +0000 | [diff] [blame] | 14583 | |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14584 | Py_UNICODE* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14585 | PyUnicode_AsUnicodeCopy(PyObject *unicode) |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14586 | { |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 14587 | Py_UNICODE *u, *copy; |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14588 | Py_ssize_t len, size; |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14589 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14590 | if (!PyUnicode_Check(unicode)) { |
| 14591 | PyErr_BadArgument(); |
| 14592 | return NULL; |
| 14593 | } |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14594 | u = PyUnicode_AsUnicodeAndSize(unicode, &len); |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 14595 | if (u == NULL) |
| 14596 | return NULL; |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14597 | /* Ensure we won't overflow the size. */ |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14598 | if (len > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14599 | PyErr_NoMemory(); |
| 14600 | return NULL; |
| 14601 | } |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14602 | size = len + 1; /* copy the null character */ |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14603 | size *= sizeof(Py_UNICODE); |
| 14604 | copy = PyMem_Malloc(size); |
| 14605 | if (copy == NULL) { |
| 14606 | PyErr_NoMemory(); |
| 14607 | return NULL; |
| 14608 | } |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 14609 | memcpy(copy, u, size); |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14610 | return copy; |
| 14611 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 14612 | |
Georg Brandl | 66c221e | 2010-10-14 07:04:07 +0000 | [diff] [blame] | 14613 | /* A _string module, to export formatter_parser and formatter_field_name_split |
| 14614 | to the string.Formatter class implemented in Python. */ |
| 14615 | |
| 14616 | static PyMethodDef _string_methods[] = { |
| 14617 | {"formatter_field_name_split", (PyCFunction) formatter_field_name_split, |
| 14618 | METH_O, PyDoc_STR("split the argument as a field name")}, |
| 14619 | {"formatter_parser", (PyCFunction) formatter_parser, |
| 14620 | METH_O, PyDoc_STR("parse the argument as a format string")}, |
| 14621 | {NULL, NULL} |
| 14622 | }; |
| 14623 | |
| 14624 | static struct PyModuleDef _string_module = { |
| 14625 | PyModuleDef_HEAD_INIT, |
| 14626 | "_string", |
| 14627 | PyDoc_STR("string helper module"), |
| 14628 | 0, |
| 14629 | _string_methods, |
| 14630 | NULL, |
| 14631 | NULL, |
| 14632 | NULL, |
| 14633 | NULL |
| 14634 | }; |
| 14635 | |
| 14636 | PyMODINIT_FUNC |
| 14637 | PyInit__string(void) |
| 14638 | { |
| 14639 | return PyModule_Create(&_string_module); |
| 14640 | } |
| 14641 | |
| 14642 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 14643 | #ifdef __cplusplus |
| 14644 | } |
| 14645 | #endif |